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

后端 未结 16 2237
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:46

    Your best option is to DROP and CREATE foreign key constraints.

    I didn't find examples in this post that would work for me "as-is", one would not work if foreign keys reference different schemas, the other would not work if foreign key references multiple columns. This script considers both, multiple schemas and multiple columns per foreign key.

    Here is the script that generates "ADD CONSTRAINT" statements, for multiple columns it will separate them by comma (be sure to save this output before executing DROP statements):

    PRINT N'-- CREATE FOREIGN KEY CONSTRAINTS --';
    
    SET NOCOUNT ON;
    SELECT '
    PRINT N''Creating '+ const.const_name +'...''
    GO
    ALTER TABLE ' + const.parent_obj + '
        ADD CONSTRAINT ' + const.const_name + ' FOREIGN KEY (
                ' + const.parent_col_csv + '
                ) REFERENCES ' + const.ref_obj + '(' + const.ref_col_csv + ')
    GO'
    FROM (
        SELECT QUOTENAME(fk.NAME) AS [const_name]
            ,QUOTENAME(schParent.NAME) + '.' + QUOTENAME(OBJECT_name(fkc.parent_object_id)) AS [parent_obj]
            ,STUFF((
                    SELECT ',' + QUOTENAME(COL_NAME(fcP.parent_object_id, fcp.parent_column_id))
                    FROM sys.foreign_key_columns AS fcP
                    WHERE fcp.constraint_object_id = fk.object_id
                    FOR XML path('')
                    ), 1, 1, '') AS [parent_col_csv]
            ,QUOTENAME(schRef.NAME) + '.' + QUOTENAME(OBJECT_NAME(fkc.referenced_object_id)) AS [ref_obj]
            ,STUFF((
                    SELECT ',' + QUOTENAME(COL_NAME(fcR.referenced_object_id, fcR.referenced_column_id))
                    FROM sys.foreign_key_columns AS fcR
                    WHERE fcR.constraint_object_id = fk.object_id
                    FOR XML path('')
                    ), 1, 1, '') AS [ref_col_csv]
        FROM sys.foreign_key_columns AS fkc
        INNER JOIN sys.foreign_keys AS fk ON fk.object_id = fkc.constraint_object_id
        INNER JOIN sys.objects AS oParent ON oParent.object_id = fkc.parent_object_id
        INNER JOIN sys.schemas AS schParent ON schParent.schema_id = oParent.schema_id
        INNER JOIN sys.objects AS oRef ON oRef.object_id = fkc.referenced_object_id
        INNER JOIN sys.schemas AS schRef ON schRef.schema_id = oRef.schema_id
        GROUP BY fkc.parent_object_id
            ,fkc.referenced_object_id
            ,fk.NAME
            ,fk.object_id
            ,schParent.NAME
            ,schRef.NAME
        ) AS const
    ORDER BY const.const_name
    

    Here is the script that generates "DROP CONSTRAINT" statements:

    PRINT N'-- DROP FOREIGN KEY CONSTRAINTS --';
    
    SET NOCOUNT ON;
    
    SELECT '
    PRINT N''Dropping ' + fk.NAME + '...''
    GO
    ALTER TABLE [' + sch.NAME + '].[' + OBJECT_NAME(fk.parent_object_id) + ']' + ' DROP  CONSTRAINT ' + '[' + fk.NAME + ']
    GO'
    FROM sys.foreign_keys AS fk
    INNER JOIN sys.schemas AS sch ON sch.schema_id = fk.schema_id
    ORDER BY fk.NAME
    

提交回复
热议问题