How can I list all foreign keys referencing a given table in SQL Server?

Source: Internet
Author: User

1. EXEC sp_fkeys ' TableName '

2. SELECT Obj.name as Fk_name,
Sch.name as [schema_name],
Tab1.name as [table],
Col1.name as [column],
Tab2.name as [referenced_table],
Col2.name as [Referenced_column]
From Sys.foreign_key_columns FKC
INNER JOIN sys.objects obj
On obj.object_id = fkc.constraint_object_id
INNER JOIN sys.tables Tab1
On tab1.object_id = fkc.parent_object_id
INNER JOIN Sys.schemas Sch
On tab1.schema_id = sch.schema_id
INNER JOIN sys.columns col1
On col1.column_id = parent_column_id and col1.object_id = tab1.object_id
INNER JOIN sys.tables tab2
On tab2.object_id = fkc.referenced_object_id
INNER JOIN sys.columns col2
On col2.column_id = referenced_column_id and col2.object_id = tab2.object_id

How can I list all foreign keys referencing a given table in SQL Server?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.