SQL Server:如何從資訊架構中擷取外鍵引用
在SQL Server 中,擷取有關外鍵所引用的資訊對於了解資料關係並維護資料庫完整性。透過利用資訊模式,您可以存取有關外鍵的有價值的元數據,包括它們引用的表和列。
下面顯示的SQL 查詢提供了從資訊模式中提取外鍵引用的全面解決方案:
`選擇
KCU1.CONSTRAINT_SCHEMA AS FK_CONSTRAINT_SCHEMA, KCU1.CONSTRAINT_NAME AS FK_CONSTRAINT_NAME, KCU1.TABLE_SCHEMA AS FK_TABLE_SCHEMA, KCU1.TABLE_NAME AS FK_TABLE_NAME, KCU1.COLUMN_NAME AS FK_COLUMN_NAME, KCU1.ORDINAL_POSITION AS FK_ORDINAL_POSITION, KCU2.CONSTRAINT_SCHEMA AS REFERENCED_CONSTRAINT_SCHEMA, KCU2.CONSTRAINT_NAME AS REFERENCED_CONSTRAINT_NAME, KCU2.TABLE_SCHEMA AS REFERENCED_TABLE_SCHEMA, KCU2.TABLE_NAME AS REFERENCED_TABLE_NAME, KCU2.COLUMN_NAME AS REFERENCED_COLUMN_NAME, KCU2.ORDINAL_POSITION AS REFERENCED_ORDINAL_POSITION
FROM INFORMATION_SCHEMA.REFERENTL_CONSTRAINTS ASL_CONSTRAINTS 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
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KCU2
ON KCU2.CONSTRAINT_CATALOG = RC.UNIQUE_CONSTRAINT_CATALOG AND KCU2.CONSTRAINT_SCHEMA = RC.UNIQUE_CONSTRAINT_SCHEMA AND KCU2.CONSTRAINT_NAME = RC.UNIQUE_CONSTRAINT_NAME AND KCU2.ORDINAL_POSITION = KCU1.ORDINAL_POSITION`
表示🎜> REFERENTIAL_CONSTRAINTS 表,其中包含所有外鍵約束的資訊。
以上是如何使用資訊架構檢索 SQL Server 中的外鍵參考?的詳細內容。更多資訊請關注PHP中文網其他相關文章!