How can I find out what FOREIGN KEY constraint references a table, How can I list all foreign keys referencing a given table in SQL Server, How to find whether a Table is referenced by the Foreign Key, Identify all of your foreign keys in a SQL Server database, SQL SERVER - 2005 - Find Tables With Foreign Key Constraint, SQL SERVER - Query to Display Foreign Key Relationships, SQL Server – List all Foreign Key Constraints of Database or Table, View Foreign Key Properties - MSDN - Microsoft, Viewing all foreign key constraints in SQL Server | Jimmy Bogard's Blog
SQL Puzzle | List of foreign keys with their tables in SQL Server
Write a T-SQL function List of foreign keys in tables in SQL Server.
- The challenge is to do it with T-SQL statements only.
- Add your solution(s) in the comments section or send you solution(s) to email@example.com
SOLUTION – 1
-- SELECT OBJECT_NAME(fk.parent_object_id) tabName , COL_NAME(fkc.parent_object_id,fkc.parent_column_id) colName FROM sys.foreign_keys fk CROSS APPLY ( SELECT fkc.parent_object_id,fkc.parent_column_id FROM sys.foreign_key_columns fkc WHERE fk.OBJECT_ID = fkc.constraint_object_id )k --
Add a comment if you have a solution in mind. I would love to learn it. We all need to learn.
Enjoy !!! Keep Learning