要列出SQL Server中给定表的所有外键,您可以使用以下查询:
SELECT f.name AS ForeignKey,
OBJECT_NAME(f.parent_object_id) AS TableName,
COL_NAME(fc.parent_object_id, fc.parent_column_id) AS ColumnName,
OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName,
COL_NAME(fc.referenced_object_id, fc.referenced_column_id) AS ReferenceColumnName
FROM sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc
ON f.OBJECT_ID = fc.constraint_object_id;
这个查询将返回一个包含以下列的表:
这个查询将显示给定数据库中所有表的所有外键。如果您只想查看特定表的外键,您可以在查询的 WHERE 子句中添加一个条件,例如:
WHERE OBJECT_NAME(f.parent_object_id) = 'YourTableName'
将 'YourTableName' 替换为您要查询的表的名称。
领取专属 10元无门槛券
手把手带您无忧上云