我怎样才能找出哪些FOREIGN KEY约束引用SQL Server中的表?
我试图删除一个表,但得到以下消息:
3726信息,16级,1状态,3号线
无法删除对象'dbo.UserProfile',因为它是由FOREIGN KEY约束引用的。
消息2714,级别16,状态6,2号线
数据库中已经有一个名为“UserProfile”的对象。
我用SQL Server Management Studio环顾四周,但我无法find约束。 我怎样才能找出外键约束?
这里是:
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'
这样,您将获得引用表和列名称。
按照评论build议编辑使用sys.tables而不是通用的sys.objects。 谢谢,marc_s
尝试这个
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')
另一种方法是检查结果
sp_help 'TableName'
(或者只是突出显示引用的TableName并按下Alt + F1)
随着时间的推移,我只是决定改进我的答案。 以下是sp_help
提供的结果的屏幕截图。 A在这个例子中使用了AdventureWorksDW2012 DB。 那里有很多很好的信息,我们正在寻找的是最后的 – 绿色突出显示:
我发现这个答案很简单,做了我所需要的技巧: https : //stackoverflow.com/a/12956348/652519
从链接摘要,使用此查询:
EXEC sp_fkeys 'TableName'
快速简单。 我能够很快find所有的外键表,外键列和外键名。
正如@mdisibio在下面指出的那样,下面是一个指向文档的链接,详细介绍可以使用的不同参数: https : //docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp- fkeys-的Transact-SQL
如果你想通过对象资源pipe理器窗口上的SSMS去,右键点击你想要删除的对象,查看依赖关系。
我正在使用此脚本查找与外键相关的所有详细信息。 我正在使用INFORMATION.SCHEMA。 下面是一个SQL脚本:
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
这里是找出所有数据库中的外键关系的最好方法。
exec sp_helpconstraint 'Table Name'
还有一个方法
select * from INFORMATION_SCHEMA.KEY_COLUMN_USAGE where TABLE_NAME='Table Name' --and left(CONSTRAINT_NAME,2)='FK'(If you want single key)
– 以下可能会给你更多你正在寻找的东西:
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
您还可以通过调整@LittleSweetSeas来返回有关Foreign Keys
的所有信息:
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
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;
尝试以下查询。
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和将取决于约束的表将在那里。
您可以使用此查询来显示Foreign key
约束:
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'
获取表的Primary Key
和Foreign Key
最简单的方法是:
/* 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'
在SQL Server Management Studio中,您可以右键单击对象资源pipe理器中的表并select“查看依赖关系”。 这会给你一个很好的起点。 它显示引用表的表,视图和过程。