在Microsoft SQL Server檢視資料表的外鍵(FK, Foreign Key)的語法如下。
SELECT
f.name AS foreign_key_name
,OBJECT_NAME(f.parent_object_id) AS table_name
,COL_NAME(fc.parent_object_id, fc.parent_column_id) AS constraint_column_name
,OBJECT_NAME (f.referenced_object_id) AS referenced_object
,COL_NAME(fc.referenced_object_id, fc.referenced_column_id) AS referenced_column_name
,is_disabled
,delete_referential_action_desc
,update_referential_action_desc
FROM sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc
ON f.object_id = fc.constraint_object_id
WHERE f.parent_object_id = OBJECT_ID('[TABLE_NAME]');
[TABLE_NAME]
是要查詢的資料表名稱,例如EMPLOYEE
。
- foreign_key_name:外鍵名稱
- table_name:查詢資料表名稱
- constraint_column_name:限制欄位名稱
- referenced_object:外鍵所在資料表名稱
- referenced_column_name:外鍵所在的欄位名稱
- is_disabled:是否停用
- delete_referential_action_desc:串聯刪除設定
- delete_referential_action_desc:串聯更新設定
或在SSMS的物件總管中右鍵點選資料表 -> 設計 -> 在設計視窗上右鍵開啟功能選單 -> 關聯性...
即可看到所選資料表現有的關聯性設定。
參考:
沒有留言:
張貼留言