How can foreign key constraints be temporarily disabled using T-SQL?

后端 未结 16 1900
Happy的楠姐
Happy的楠姐 2020-11-22 04:57

Are disabling and enabling foreign key constraints supported in SQL Server? Or is my only option to drop and then re-create

16条回答
  •  旧巷少年郎
    2020-11-22 05:45

       --Drop and Recreate Foreign Key Constraints
    
    SET NOCOUNT ON
    
    DECLARE @table TABLE(
       RowId INT PRIMARY KEY IDENTITY(1, 1),
       ForeignKeyConstraintName NVARCHAR(200),
       ForeignKeyConstraintTableSchema NVARCHAR(200),
       ForeignKeyConstraintTableName NVARCHAR(200),
       ForeignKeyConstraintColumnName NVARCHAR(200),
       PrimaryKeyConstraintName NVARCHAR(200),
       PrimaryKeyConstraintTableSchema NVARCHAR(200),
       PrimaryKeyConstraintTableName NVARCHAR(200),
       PrimaryKeyConstraintColumnName NVARCHAR(200)    
    )
    
    INSERT INTO @table(ForeignKeyConstraintName, ForeignKeyConstraintTableSchema, ForeignKeyConstraintTableName, ForeignKeyConstraintColumnName)
    SELECT 
       U.CONSTRAINT_NAME, 
       U.TABLE_SCHEMA, 
       U.TABLE_NAME, 
       U.COLUMN_NAME 
    FROM 
       INFORMATION_SCHEMA.KEY_COLUMN_USAGE U
          INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS C
             ON U.CONSTRAINT_NAME = C.CONSTRAINT_NAME
    WHERE
       C.CONSTRAINT_TYPE = 'FOREIGN KEY'
    
    UPDATE @table SET
       PrimaryKeyConstraintName = UNIQUE_CONSTRAINT_NAME
    FROM 
       @table T
          INNER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS R
             ON T.ForeignKeyConstraintName = R.CONSTRAINT_NAME
    
    UPDATE @table SET
       PrimaryKeyConstraintTableSchema  = TABLE_SCHEMA,
       PrimaryKeyConstraintTableName  = TABLE_NAME
    FROM @table T
       INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS C
          ON T.PrimaryKeyConstraintName = C.CONSTRAINT_NAME
    
    UPDATE @table SET
       PrimaryKeyConstraintColumnName = COLUMN_NAME
    FROM @table T
       INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE U
          ON T.PrimaryKeyConstraintName = U.CONSTRAINT_NAME
    
    --SELECT * FROM @table
    
    --DROP CONSTRAINT:
    SELECT
       '
       ALTER TABLE [' + ForeignKeyConstraintTableSchema + '].[' + ForeignKeyConstraintTableName + '] 
       DROP CONSTRAINT ' + ForeignKeyConstraintName + '
    
       GO'
    FROM
       @table
    
    --ADD CONSTRAINT:
    SELECT
       '
       ALTER TABLE [' + ForeignKeyConstraintTableSchema + '].[' + ForeignKeyConstraintTableName + '] 
       ADD CONSTRAINT ' + ForeignKeyConstraintName + ' FOREIGN KEY(' + ForeignKeyConstraintColumnName + ') REFERENCES [' + PrimaryKeyConstraintTableSchema + '].[' + PrimaryKeyConstraintTableName + '](' + PrimaryKeyConstraintColumnName + ')
    
       GO'
    FROM
       @table
    
    GO
    

    I do agree with you, Hamlin. When you are transfer data using SSIS or when want to replicate data, it seems quite necessary to temporarily disable or drop foreign key constraints and then re-enable or recreate them. In these cases, referential integrity is not an issue, because it is already maintained in the source database. Therefore, you can rest assured regarding this matter.

提交回复
热议问题