Berikut ini pertanyaan sederhana untuk mencocokkan kunci asing dengan tabel / kolom yang dirujuk:
SELECT
o1.name AS FK_table,
c1.name AS FK_column,
fk.name AS FK_name,
o2.name AS PK_table,
c2.name AS PK_column,
pk.name AS PK_name,
fk.delete_referential_action_desc AS Delete_Action,
fk.update_referential_action_desc AS Update_Action
FROM sys.objects o1
INNER JOIN sys.foreign_keys fk
ON o1.object_id = fk.parent_object_id
INNER JOIN sys.foreign_key_columns fkc
ON fk.object_id = fkc.constraint_object_id
INNER JOIN sys.columns c1
ON fkc.parent_object_id = c1.object_id
AND fkc.parent_column_id = c1.column_id
INNER JOIN sys.columns c2
ON fkc.referenced_object_id = c2.object_id
AND fkc.referenced_column_id = c2.column_id
INNER JOIN sys.objects o2
ON fk.referenced_object_id = o2.object_id
INNER JOIN sys.key_constraints pk
ON fk.referenced_object_id = pk.parent_object_id
AND fk.key_index_id = pk.unique_index_id
ORDER BY o1.name, o2.name, fkc.constraint_column_id
Output memiliki delapan kolom: tabel dan nama kolom untuk kunci asing (FK_table, FK_column), nama batasan kunci asing (FK_name), PK yang dirujuk atau tabel indeks unik dan nama kolom (PK_table, PK_column), yang nama PK yang dirujuk atau indeks unik (PK_name), dan tindakan pembaruan / hapus kaskade (Delete_Action, Update_Action).
(Diedit untuk menambahkan beberapa kolom output lagi.)
EDIT: Saya kembali 6 tahun kemudian dengan versi perbaikan ini. Saya menyadari bahwa kueri asli tidak benar-benar menangani kunci asing multi-kolom dengan baik, dan saya juga ingin dapat dengan cepat mengidentifikasi kunci asing yang dinonaktifkan, tidak dipercaya, atau tidak indeks. Jadi, inilah versi baru yang memperbaiki semua itu.
Kunci multi-kolom ditampilkan sebagai daftar yang dipisahkan koma di FK_columns
dan PK_columns
, menggunakan tradisional FOR XML
/ STUFF
penyalahgunaan. The FK_indexes
kolom menunjukkan nama-nama dari setiap indeks di atas meja asing-kunci yang berpotensi dapat digunakan untuk memuaskan berusaha menggunakan kolom kunci asing (terutama untuk mengoptimalkan menghapus atau update ke meja primary key). Jika ya NULL
, maka Anda memiliki kunci asing yang tidak tereksplorasi. Anda dapat mengubah ORDER BY
, atau menambahkan WHERE
klausa (dikomentari di bawah) jika Anda ingin mengurutkan berdasarkan nama tabel PK, memfilter untuk tabel PK / FK tertentu, dll.
SELECT
fk.is_disabled,
fk.is_not_trusted,
OBJECT_SCHEMA_NAME(o1.object_id) AS FK_schema,
o1.name AS FK_table,
--Generate list of columns in referring side of foreign key
STUFF(
(
SELECT ', ' + c1.name AS [text()]
FROM sys.columns c1 INNER
JOIN sys.foreign_key_columns fkc
ON c1.object_id = fkc.parent_object_id
AND c1.column_id = fkc.parent_column_id
WHERE fkc.constraint_object_id = fk.object_id
FOR XML PATH('')
), 1, 2, '') AS FK_columns,
--Look for any indexes that will fully satisfy the foreign key columns
STUFF(
(
SELECT ', ' + i.name AS [text()]
FROM sys.indexes i
WHERE i.object_id = o1.object_id
AND NOT EXISTS ( --Find foreign key columns that don't match the index key columns
SELECT fkc.constraint_column_id, fkc.parent_column_id
FROM sys.foreign_key_columns fkc
WHERE fkc.constraint_object_id = fk.object_id
EXCEPT
SELECT ic.key_ordinal, ic.column_id
FROM sys.index_columns ic
WHERE ic.object_id = i.object_id AND ic.index_id = i.index_id
)
FOR XML PATH('')
), 1, 2, '') AS FK_indexes,
fk.name AS FK_name,
OBJECT_SCHEMA_NAME(o2.object_id) AS PK_schema,
o2.name AS PK_table,
--Generate list of columns in referenced (i.e. PK) side of foreign key
STUFF(
(
SELECT ', ' + c2.name AS [text()]
FROM sys.columns c2
INNER JOIN sys.foreign_key_columns fkc
ON c2.object_id = fkc.referenced_object_id
AND c2.column_id = fkc.referenced_column_id
WHERE fkc.constraint_object_id = fk.object_id
FOR XML PATH('')
), 1, 2, '') AS PK_columns,
pk.name AS PK_name,
fk.delete_referential_action_desc AS Delete_Action,
fk.update_referential_action_desc AS Update_Action
FROM sys.objects o1
INNER JOIN sys.foreign_keys fk
ON o1.object_id = fk.parent_object_id
INNER JOIN sys.objects o2
ON fk.referenced_object_id = o2.object_id
INNER JOIN sys.key_constraints pk
ON fk.referenced_object_id = pk.parent_object_id
AND fk.key_index_id = pk.unique_index_id
--WHERE o2.name = 'Company_Address'
ORDER BY o1.name, o2.name