So I’ve been working on an automation project that makes frequent use of foreign key metadata. I find myself writing queries for this data but I discovered that there’s no super-easy out-of-the-box view of foreign keys for me to use. Here are the ones I considered.
INFORMATION_SCHEMA views that give me foreign key information include
- INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE for the foreign key info
- INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS for the referring columns,
- INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE for the referenced columns
The first view here gives a list of foreign keys and you have to join to the other two tables in order to find the column names. But it’s a crummy solution. First of all, if the foreign key has multiple columns, there’s no real way to match the referring columns to the referenced columns.
The second thing is that we don’t see foreign keys that point to unique Keys (as pointed out by Aaron Bertrand in his post The case against INFORMATION_SCHEMA views.
So that’s out. What else have we got in?
Microsoft SQL Server system views
These views include
- with support from sys.columns and sys.tables
These are the views I deserve, but not the views I need right now. The joins are just too annoying to remember and type each time.
Besides, the word “parent” used here changes with context. The parent table in a foreign key relationship owns the foreign key and does the pointing. But say I’m modeling a hierarchy. In the context of the data model, children records point to their parent records. The mental effort needed to keep these straight is not difficult, but it’s annoying.
My Own Views
So I’ve created my own, the goal is to simplify typing and minimize joins. I skip the word “parent” all together and use “referrer” and “referrenced”. Feel free to use and build on these.
CREATE VIEW dbo.FOREIGN_KEYS AS SELECT SCHEMA_NAME(fk.schema_id) AS FKSchema , fk.name AS FK , SCHEMA_NAME(p.schema_id) AS ReferrerSchema , p.name AS Referrer , STUFF(CAST(( SELECT ',' + c.name FROM sys.foreign_key_columns fkc JOIN sys.columns c ON fkc.parent_object_id = c.object_id AND fkc.parent_column_id = c.column_id WHERE fkc.constraint_object_id = fk.object_id ORDER BY fkc.constraint_column_id ASC FOR XML PATH('') , TYPE ) AS NVARCHAR(MAX)), 1, 1, '') AS ReferrerColumns , SCHEMA_NAME(r.schema_id) AS ReferencedSchema , r.name AS Referenced , STUFF(CAST(( SELECT ',' + c.name FROM sys.foreign_key_columns fkc JOIN sys.columns c ON fkc.referenced_object_id = c.object_id AND fkc.referenced_column_id = c.column_id WHERE fkc.constraint_object_id = fk.object_id ORDER BY fkc.constraint_column_id ASC FOR XML PATH('') , TYPE ) AS NVARCHAR(MAX)), 1, 1, '') AS ReferencedColumns , fk.delete_referential_action_desc AS deleteAction , fk.update_referential_action_desc AS updateAction , fk.object_id AS FKId , p.object_id AS ReferrerId , r.object_id AS ReferencedId FROM sys.foreign_keys fk JOIN sys.tables p ON p.object_id = fk.parent_object_id JOIN sys.tables r ON r.object_id = fk.referenced_object_id GO
CREATE VIEW dbo.FOREIGN_KEY_COLUMNS AS SELECT SCHEMA_NAME(fk.schema_id) AS FKSchema , fk.name AS FK , SCHEMA_NAME(p.schema_id) AS ReferrerSchema , p.name AS Referrer , pc.name AS ReferrerColumn , SCHEMA_NAME(r.schema_id) AS ReferencedSchema , r.name AS Referenced, rc.name AS ReferencedColumn , fk.object_id AS FKId , fkc.constraint_column_id AS FKColumnId , p.object_id AS ReferrerId , r.object_id AS ReferencedId FROM sys.foreign_keys fk JOIN sys.foreign_key_columns fkc ON fkc.constraint_object_id = fk.object_id JOIN sys.tables p ON p.object_id = fk.parent_object_id JOIN sys.columns pc ON fkc.parent_object_id = pc.object_id AND fkc.parent_column_id = pc.column_id JOIN sys.tables r ON r.object_id = fk.referenced_object_id JOIN sys.columns rc ON fkc.referenced_object_id = rc.object_id AND fkc.referenced_column_id = rc.column_id GO