How to drop all Foreign Key constraints in all tables?
我想编写sql命令来删除所有表中的所有约束。 我在互联网上搜索,发现如果数据库很小而且不复杂,以下哪个工作正常。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | DECLARE @name VARCHAR(128) DECLARE @CONSTRAINT VARCHAR(254) DECLARE @SQL VARCHAR(254) DECLARE @schema VARCHAR(128) SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' ORDER BY TABLE_NAME) SELECT @schema = (SELECT TOP 1 schema_name(schema_id) FROM sys.objects WHERE [name] = @name) WHILE @name IS NOT NULL BEGIN SELECT @CONSTRAINT = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME) WHILE @CONSTRAINT IS NOT NULL BEGIN SELECT @SQL = 'ALTER TABLE ' + @schema + '.[' + RTRIM(@name) +'] DROP CONSTRAINT [' + RTRIM(@CONSTRAINT) +']' EXEC (@SQL) PRINT 'Dropped FK Constraint: ' + @CONSTRAINT + ' on ' + @name SELECT @CONSTRAINT = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' AND CONSTRAINT_NAME <> @CONSTRAINT AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME) END SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' ORDER BY TABLE_NAME) SELECT @schema = (SELECT TOP 1 schema_name(schema_id) FROM sys.objects WHERE [name] = @name) END GO |
如果我使用更复杂的数据库甚至AdventureWork运行它,它就不起作用。 它显示了下面的一些错误。
1 2 3 4 5 6 7 8 | Msg 3728, Level 16, State 1, Line 1 'FK_ap_invoice_modification_type_id' IS NOT a CONSTRAINT. Msg 3727, Level 16, State 0, Line 1 Could NOT DROP CONSTRAINT. See previous errors. Msg 3725, Level 16, State 0, Line 1 The CONSTRAINT 'PK_ap_invoice' IS being referenced BY TABLE '_drop_now_ap_invoice_detail', FOREIGN KEY CONSTRAINT 'FK_ap_invoice_detail_ap_invoice'. Msg 3727, Level 16, State 0, Line 1 Could NOT DROP CONSTRAINT. See previous errors. |
原因是因为某些FK被其他表引用。 我必须运行这个脚本几次,直到数据库干净。
我想知道如何清除数据库中的所有FK。
周围有很多关于这个主题的信息。请查看@AaronBertrand的详细答案。它讨论临时禁用外键但是阅读所有内容并随意修改你将有一个很好的脚本可以玩并实现很多。
从我这边我可以提出2个不同的脚本来获取所有外键。在这两种情况下,取消注释
第一个使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | DECLARE @SQL VARCHAR(MAX)='' SELECT @SQL = @SQL + 'ALTER TABLE ' + QUOTENAME(FK.TABLE_SCHEMA) + '.' + QUOTENAME(FK.TABLE_NAME) + ' DROP CONSTRAINT [' + RTRIM(C.CONSTRAINT_NAME) +'];' + CHAR(13) --SELECT K_Table = FK.TABLE_NAME, FK_Column = CU.COLUMN_NAME, PK_Table = PK.TABLE_NAME, PK_Column = PT.COLUMN_NAME, Constraint_Name = C.CONSTRAINT_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 --EXEC (@SQL) PRINT @SQL |
这一个使用不同的系统视图和一个CTE表。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | DECLARE @SQL VARCHAR(4000)='' ;WITH ReferencingFK AS ( SELECT fk.Name AS 'FKName', OBJECT_NAME(fk.parent_object_id) 'ParentTable', cpa.name 'ParentColumnName', OBJECT_NAME(fk.referenced_object_id) 'ReferencedTable', cref.name 'ReferencedColumnName' FROM sys.foreign_keys fk INNER JOIN sys.foreign_key_columns fkc ON fkc.constraint_object_id = fk.object_id INNER JOIN sys.columns cpa ON fkc.parent_object_id = cpa.object_id AND fkc.parent_column_id = cpa.column_id INNER JOIN sys.columns cref ON fkc.referenced_object_id = cref.object_id AND fkc.referenced_column_id = cref.column_id ) SELECT @SQL = @SQL + 'ALTER TABLE ' + ParentTable + ' DROP CONSTRAINT [' + RTRIM(FKName) +'];' + CHAR(13) --SELECT FKName, ParentTable, ParentColumnName, ReferencedTable, ReferencedColumnName FROM ReferencingFK WHERE ReferencedTable = 'Employee' ORDER BY ParentTable, ReferencedTable, FKName --EXEC (@SQL) PRINT @SQL |
这是我使用的一个简短而可爱的脚本(在SQL Server 2008及更高版本上),用于删除所有考虑了对象架构的外键:
1 2 3 4 5 6 7 8 9 | DECLARE @SQL VARCHAR(MAX) = ( SELECT 'alter table ' + quotename(schema_name(schema_id)) + '.' + quotename(object_name(parent_object_id)) + ' drop constraint '+quotename(name) + ';' FROM sys.foreign_keys FOR xml path('') ); EXEC sp_executesql @SQL; |
我已经改进了@Yaroslav提供的第一个脚本和@Quandary提供的脚本,所以它们现在适用于那些逐个删除所有外键的SQL查询超过为
更改的脚本每次迭代都会删除
@Yaroslav的第一个剧本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | DECLARE @SQL VARCHAR(4000) IterationStart: SET @SQL='' SELECT TOP 5 @SQL = @SQL + 'ALTER TABLE ' + FK.TABLE_NAME + ' DROP CONSTRAINT [' + RTRIM(C.CONSTRAINT_NAME) +'];' + CHAR(13) 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 IF @SQL <> '' BEGIN EXEC(@SQL) GOTO IterationStart END |
@Quandary的脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | DECLARE @SQL nvarchar(MAX) IterationStart: SET @SQL = '' SELECT TOP 5 @SQL = @SQL + 'ALTER TABLE ' + QUOTENAME(RC.CONSTRAINT_SCHEMA) + '.' + QUOTENAME(KCU1.TABLE_NAME) + ' DROP CONSTRAINT ' + QUOTENAME(rc.CONSTRAINT_NAME) + '; ' FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS RC INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KCU1 ON KCU1.CONSTRAINT_CATALOG = RC.CONSTRAINT_CATALOG AND KCU1.CONSTRAINT_SCHEMA = RC.CONSTRAINT_SCHEMA AND KCU1.CONSTRAINT_NAME = RC.CONSTRAINT_NAME IF @SQL <> '' BEGIN EXEC(@SQL) GOTO IterationStart END |
最简单的变种:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | DECLARE @SQL nvarchar(MAX) SET @SQL = N'' SELECT @SQL = @SQL + N'ALTER TABLE ' + QUOTENAME(KCU1.TABLE_SCHEMA) + N'.' + QUOTENAME(KCU1.TABLE_NAME) + N' DROP CONSTRAINT ' -- + QUOTENAME(rc.CONSTRAINT_SCHEMA) + N'.' -- not in MS-SQL + QUOTENAME(rc.CONSTRAINT_NAME) + N'; ' + CHAR(13) + CHAR(10) FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS RC INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KCU1 ON KCU1.CONSTRAINT_CATALOG = RC.CONSTRAINT_CATALOG AND KCU1.CONSTRAINT_SCHEMA = RC.CONSTRAINT_SCHEMA AND KCU1.CONSTRAINT_NAME = RC.CONSTRAINT_NAME -- PRINT @sql EXECUTE(@SQL) |
我使用了@Yaroslav提到的INFORMATION_SCHEMA解决方案,但在我的数据库中有太多的外键常量使它们全部适合varchar(MAX)。所以我不得不修改脚本以使用临时表和游标。
另外,我在表名周围添加了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | DECLARE @SQL TABLE (Command VARCHAR(MAX)) INSERT @SQL SELECT 'ALTER TABLE [' + FK.TABLE_NAME + '] DROP CONSTRAINT [' + RTRIM(C.CONSTRAINT_NAME) +'];' + CHAR(13) 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 DECLARE cmdCursor CURSOR FOR SELECT Command FROM @SQL OPEN cmdCursor DECLARE @Command VARCHAR(MAX) FETCH NEXT FROM cmdCursor INTO @Command WHILE @@FETCH_STATUS = 0 BEGIN PRINT @Command EXEC (@Command) FETCH NEXT FROM cmdCursor INTO @Command END CLOSE cmdCursor; DEALLOCATE cmdCursor; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | CREATE TABLE #Commands (Command VARCHAR(MAX)) INSERT #Commands SELECT 'ALTER TABLE ' + QUOTENAME(RC.CONSTRAINT_SCHEMA) + '.' + QUOTENAME(KCU1.TABLE_NAME) + ' DROP CONSTRAINT ' + QUOTENAME(rc.CONSTRAINT_NAME) + '; ' FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS RC INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KCU1 ON KCU1.CONSTRAINT_CATALOG = RC.CONSTRAINT_CATALOG AND KCU1.CONSTRAINT_SCHEMA = RC.CONSTRAINT_SCHEMA AND KCU1.CONSTRAINT_NAME = RC.CONSTRAINT_NAME WHERE ORDINAL_POSITION=1 --SELECT * FROM #Commands DECLARE @Command VARCHAR(MAX) DECLARE curCommand CURSOR FOR SELECT Command FROM #Commands OPEN curCommand FETCH NEXT FROM curCommand INTO @Command WHILE @@FETCH_STATUS =0 BEGIN EXEC(@Command) FETCH NEXT FROM curCommand INTO @Command END CLOSE curCommand DEALLOCATE curCommand DROP TABLE #Commands |