How can I add primary key and foreign key constraints after export data from Azure SQL

Viewed 165

I'm using SQL Server Management Studio 19 to migrate data from source database to target database.
I select SQL Server Native Client 11.0 as the Data Source.

For Destination I also use "SQL Server Native Client 11.0" and choose target database as destination.

The data was exported successfully but primary key and foreign key constraints aren't there. What do I missed?

Any help or any suggestions are appreciated. Thank you so much!

1 Answers

There are two ways to export the PK and FK.

  1. Using SSMS to generate the sql script. We just need to select the tables. It will generate a script.sql in your local PC.

enter image description here

We also can write some scripts to export the PK and FK of the User tables manually.
I've created a sql script to export PK and FK from system tables and views.

2.1 We can use the following script to export PK.

select  case when colNo = 1 then concat('alter table ',concat(concat(res.schemaName,'.'),res.tableName)) else '' end headerOne,
        case when colNo = 1 then concat(concat('add constraint ' , res.PKName),' primary key( ') else '' end headerTwo,
        case when colNo = 1 then colName else concat(',',colName) end headerThree,
        case when colNo = s2.maxRow then ');' else '' end as headerFour
from (
    select s.name as schemaName,i.name as PKName,ov.name as tableName,c.name as colName,k.colid as colNo,k.keyno as indexNO 
        from
            sysindexes i  
            join sysindexkeys k on i.id = k.id and i.indid = k.indid  
            join sysobjects o on i.id = o.id  
            join sys.objects ov on o.id = ov.object_id
            join sys.schemas s ON ov.schema_id = s.schema_id
            join syscolumns c on i.id=c.id and k.colid = c.colid  
        where o.xtype = 'U' and exists(select 1 from sysobjects where xtype = 'PK' and name = i.name)  
        ) res
left join 
    (select schemaName,PKName,tableName,max(rono) as maxRow
         from 
         (
            select s.name as schemaName,i.name as PKName,ov.name as tableName,c.name as colName, ROW_NUMBER() OVER (PARTITION BY s.name,i.name,ov.name  ORDER BY o.name,k.colid) AS rono
                from
                    sysindexes i  
                    join sysindexkeys k on i.id = k.id and i.indid = k.indid  
                    join sysobjects o on i.id = o.id  
                    join sys.objects ov on o.id = ov.object_id
                    join sys.schemas s ON ov.schema_id = s.schema_id
                    join syscolumns c on i.id=c.id and k.colid = c.colid  
                where o.xtype = 'U' and exists(select 1 from sysobjects where xtype = 'PK' and name = i.name)  
 
         ) s1
         group by schemaName,PKName,tableName 
    ) s2 on res.schemaName = s2.schemaName and res.PKName=s2.PKName and res.tableName=s2.tableName

2.2 Then we can copy the script from SSMS. enter image description here

2.3 Then we paste the script to query window of the Staging database to execute the script.

2.4 After created PK, in the same way, we can export FK and create them.

select
   concat(concat('alter table ',c.CONSTRAINT_SCHEMA),concat('.',fk.TABLE_NAME)),
   concat(' add constraint  ', c.CONSTRAINT_NAME),  --cu.COLUMN_NAME
   concat(' foreign key( ',cu.COLUMN_NAME),
   concat(concat(') references ',c.CONSTRAINT_SCHEMA),concat('.',pk.TABLE_NAME)),
   concat(concat('(',pt.COLUMN_NAME),');')
from
    INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS c
inner join INFORMATION_SCHEMA.TABLE_CONSTRAINTS fk
    on c.CONSTRAINT_NAME = FK.CONSTRAINT_NAME
inner join INFORMATION_SCHEMA.TABLE_CONSTRAINTS pk
    on c.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME
inner join INFORMATION_SCHEMA.KEY_COLUMN_USAGE cu
    on c.CONSTRAINT_NAME = CU.CONSTRAINT_NAME
inner join  (
            select
                i1.TABLE_NAME,
                i2.COLUMN_NAME
            from
                INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1
            inner join INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2
                on i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME
            where
                i1.CONSTRAINT_TYPE = 'PRIMARY KEY'
           ) PT
    on pt.TABLE_NAME = pk.TABLE_NAME
Related