Created
December 3, 2019 16:30
-
-
Save wbhinton/805a84b58b938fb6f93a4c240024efba to your computer and use it in GitHub Desktop.
List tables and foreign keys
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Foreign Keys | |
SELECT @@Servername AS ServerName , | |
DB_NAME() AS DB_Name , | |
parent.name AS 'TableName' , | |
o.name AS 'ForeignKey' , | |
o.[Type] , | |
o.Create_date | |
FROM sys.objects o | |
INNER JOIN sys.objects parent ON o.parent_object_id = parent.object_id | |
WHERE o.[Type] = 'F' | |
-- Foreign Keys | |
ORDER BY parent.name , | |
o.name | |
--OR | |
SELECT f.name AS ForeignKey , | |
SCHEMA_NAME(f.SCHEMA_ID) AS SchemaName , | |
OBJECT_NAME(f.parent_object_id) AS TableName , | |
COL_NAME(fc.parent_object_id, fc.parent_column_id) AS ColumnName , | |
SCHEMA_NAME(o.SCHEMA_ID) ReferenceSchemaName , | |
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 | |
INNER JOIN sys.objects AS o ON o.OBJECT_ID = fc.referenced_object_id | |
ORDER BY TableName , | |
ReferenceTableName; | |
GO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment