How can I find out what FOREIGN KEY constraint references a table in SQL Server?
我想删除一个表但收到以下消息:
Msg 3726, Level 16, State 1, Line 3
Could not drop object 'dbo.UserProfile' because it is referenced by a FOREIGN KEY constraint.
Msg 2714, Level 16, State 6, Line 2
There is already an object named 'UserProfile' in the database.
我浏览了SQL Server Management Studio但我无法找到约束。 如何找出外键约束?
这里是:
1 2 3 4 5 6 7 8 9 10 11 12 13 | SELECT OBJECT_NAME(f.parent_object_id) TableName, COL_NAME(fc.parent_object_id,fc.parent_column_id) ColName FROM sys.foreign_keys AS f INNER JOIN sys.foreign_key_columns AS fc ON f.OBJECT_ID = fc.constraint_object_id INNER JOIN sys.tables t ON t.OBJECT_ID = fc.referenced_object_id WHERE OBJECT_NAME (f.referenced_object_id) = 'YourTableName' |
这样,您将获得引用表和列名称。
根据评论建议编辑使用sys.tables而不是通用sys.objects。
谢谢,marc_s
另一种方法是检查结果
1 | sp_help 'TableName' |
(或只是突出显示引用的TableName和pres ALT + F1)
随着时间的推移,我决定改进我的答案。下面是
试试这个
1 2 3 4 5 6 | SELECT object_name(parent_object_id) ParentTableName, object_name(referenced_object_id) RefTableName, name FROM sys.foreign_keys WHERE parent_object_id = object_id('Tablename') |
我发现这个答案非常简单,并根据我的需要做了诀窍:https://stackoverflow.com/a/12956348/652519
链接摘要,使用此查询:
1 | EXEC sp_fkeys 'TableName' |
快速而简单。我能够很快找到15个表的所有外键表,各自的列和外键名。
正如@mdisibio在下面提到的,这里是文档的链接,详细说明了可以使用的不同参数:https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp- fkeys-的Transact-SQL
我正在使用此脚本查找与外键相关的所有详细信息。
我正在使用INFORMATION.SCHEMA。
下面是一个SQL脚本:
1 2 3 4 5 6 7 8 9 10 11 12 | SELECT ccu.table_name AS SourceTable ,ccu.constraint_name AS SourceConstraint ,ccu.column_name AS SourceColumn ,kcu.table_name AS TargetTable ,kcu.column_name AS TargetColumn FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu INNER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc ON ccu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu ON kcu.CONSTRAINT_NAME = rc.UNIQUE_CONSTRAINT_NAME ORDER BY ccu.table_name |
这是在所有数据库中找出外键关系的最佳方法。
1 | EXEC sp_helpconstraint 'Table Name' |
还有一种方式
1 2 | SELECT * FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME='Table Name' --and left(CONSTRAINT_NAME,2)='FK'(If you want single key) |
如果要在对象资源管理器窗口中通过SSMS,请右键单击要删除的对象,然后查看依赖项。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | SELECT obj.name AS FK_NAME, sch.name AS [schema_name], tab1.name AS [TABLE], col1.name AS [COLUMN], tab2.name AS [referenced_table], col2.name AS [referenced_column] FROM sys.foreign_key_columns fkc INNER JOIN sys.objects obj ON obj.object_id = fkc.constraint_object_id INNER JOIN sys.tables tab1 ON tab1.object_id = fkc.parent_object_id INNER JOIN sys.schemas sch ON tab1.schema_id = sch.schema_id INNER JOIN sys.columns col1 ON col1.column_id = parent_column_id AND col1.object_id = tab1.object_id INNER JOIN sys.tables tab2 ON tab2.object_id = fkc.referenced_object_id INNER JOIN sys.columns col2 ON col2.column_id = referenced_column_id AND col2.object_id = tab2.object_id; |
在SQL Server Management Studio中,您只需右键单击中的表即可
对象资源管理器并选择"查看依赖项"。这会给你一个
良好的起点。它显示了引用的表,视图和过程
桌子。
您还可以通过调整@LittleSweetSeas答案来返回有关
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | SELECT OBJECT_NAME(f.parent_object_id) ConsTable, OBJECT_NAME (f.referenced_object_id) refTable, COL_NAME(fc.parent_object_id,fc.parent_column_id) ColName FROM sys.foreign_keys AS f INNER JOIN sys.foreign_key_columns AS fc ON f.OBJECT_ID = fc.constraint_object_id INNER JOIN sys.tables t ON t.OBJECT_ID = fc.referenced_object_id ORDER BY ConsTable |
- 以下内容可能会为您提供更多您想要的内容:
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 37 38 39 40 41 42 43 44 45 | CREATE PROCEDURE spShowRelationShips ( @TABLE VARCHAR(250) = NULL, @RelatedTable VARCHAR(250) = NULL ) AS BEGIN IF @TABLE IS NULL AND @RelatedTable IS NULL SELECT object_name(k.constraint_object_id) ForeginKeyName, object_name(k.Parent_Object_id) TableName, object_name(k.referenced_object_id) RelatedTable, c.Name RelatedColumnName, object_name(rc.object_id) + '.' + rc.name RelatedKeyField FROM sys.foreign_key_columns k LEFT JOIN sys.columns c ON object_name(c.object_id) = object_name(k.Parent_Object_id) AND c.column_id = k.parent_column_id LEFT JOIN sys.columns rc ON object_name(rc.object_id) = object_name(k.referenced_object_id) AND rc.column_id = k.referenced_column_id ORDER BY 2,3 IF @TABLE IS NOT NULL AND @RelatedTable IS NULL SELECT object_name(k.constraint_object_id) ForeginKeyName, object_name(k.Parent_Object_id) TableName, object_name(k.referenced_object_id) RelatedTable, c.Name RelatedColumnName, object_name(rc.object_id) + '.' + rc.name RelatedKeyField FROM sys.foreign_key_columns k LEFT JOIN sys.columns c ON object_name(c.object_id) = object_name(k.Parent_Object_id) AND c.column_id = k.parent_column_id LEFT JOIN sys.columns rc ON object_name(rc.object_id) = object_name(k.referenced_object_id) AND rc.column_id = k.referenced_column_id WHERE object_name(k.Parent_Object_id) =@TABLE ORDER BY 2,3 IF @TABLE IS NULL AND @RelatedTable IS NOT NULL SELECT object_name(k.constraint_object_id) ForeginKeyName, object_name(k.Parent_Object_id) TableName, object_name(k.referenced_object_id) RelatedTable, c.Name RelatedColumnName, object_name(rc.object_id) + '.' + rc.name RelatedKeyField FROM sys.foreign_key_columns k LEFT JOIN sys.columns c ON object_name(c.object_id) = object_name(k.Parent_Object_id) AND c.column_id = k.parent_column_id LEFT JOIN sys.columns rc ON object_name(rc.object_id) = object_name(k.referenced_object_id) AND rc.column_id = k.referenced_column_id WHERE object_name(k.referenced_object_id) =@RelatedTable ORDER BY 2,3 END |
获取表的
1 2 3 4 5 6 7 8 9 10 | /* Get primary key and foreign key for a table */ USE DatabaseName; SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE CONSTRAINT_NAME LIKE 'PK%' AND TABLE_NAME = 'TableName' SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE CONSTRAINT_NAME LIKE 'FK%' AND TABLE_NAME = 'TableName' |
您可以使用此查询来显示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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 ---- optional: ORDER BY 1,2,3,4 WHERE PK.TABLE_NAME='YourTable' |
取自http://blog.sqlauthority.com/2006/11/01/sql-server-query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-在数据库/
尝试以下查询。
1 2 3 4 5 6 7 8 9 | SELECT object_name(sfc.constraint_object_id) AS constraint_name, OBJECT_Name(parent_object_id) AS TABLE_NAME , ac1.name AS table_column_name, OBJECT_name(referenced_object_id) AS reference_table_name, ac2.name AS reference_column_name FROM sys.foreign_key_columns sfc JOIN sys.all_columns ac1 ON (ac1.object_id=sfc.parent_object_id AND ac1.column_id=sfc.parent_column_id) JOIN sys.all_columns ac2 ON (ac2.object_id=sfc.referenced_object_id AND ac2.column_id=sfc.referenced_column_id) WHERE sfc.parent_object_id=OBJECT_ID(<main TABLE name>); |
这将给出将要引用的constraint_name,column_names以及将依赖于约束的表。