Home > Database > Mysql Tutorial > How to Find Foreign Key References in SQL Server Using Information_Schema?

How to Find Foreign Key References in SQL Server Using Information_Schema?

Linda Hamilton
Release: 2025-01-02 17:20:40
Original
817 people have browsed it

How to Find Foreign Key References in SQL Server Using Information_Schema?

Getting Foreign Key References with SQL Server's Information Schema

In SQL Server, identifying the table and column a foreign key references can be valuable information for understanding data relationships. To obtain this information, we can leverage the information_schema views.

SELECT 
     KCU1.CONSTRAINT_SCHEMA AS FK_CONSTRAINT_SCHEMA 
    ,KCU1.CONSTRAINT_NAME AS FK_CONSTRAINT_NAME 
    ,KCU1.TABLE_SCHEMA AS FK_TABLE_SCHEMA 
    ,KCU1.TABLE_NAME AS FK_TABLE_NAME 
    ,KCU1.COLUMN_NAME AS FK_COLUMN_NAME 
    ,KCU1.ORDINAL_POSITION AS FK_ORDINAL_POSITION 
    ,KCU2.CONSTRAINT_SCHEMA AS REFERENCED_CONSTRAINT_SCHEMA 
    ,KCU2.CONSTRAINT_NAME AS REFERENCED_CONSTRAINT_NAME 
    ,KCU2.TABLE_SCHEMA AS REFERENCED_TABLE_SCHEMA 
    ,KCU2.TABLE_NAME AS REFERENCED_TABLE_NAME 
    ,KCU2.COLUMN_NAME AS REFERENCED_COLUMN_NAME 
    ,KCU2.ORDINAL_POSITION AS REFERENCED_ORDINAL_POSITION 
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS RC 

INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KCU1 
    ON KCU1.CONSTRAINT_CATALOG = RC.CONSTRAINT_CATALOG  
    AND KCU1.CONSTRAINT_SCHEMA = RC.CONSTRAINT_SCHEMA 
    AND KCU1.CONSTRAINT_NAME = RC.CONSTRAINT_NAME 

INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KCU2 
    ON KCU2.CONSTRAINT_CATALOG = RC.UNIQUE_CONSTRAINT_CATALOG  
    AND KCU2.CONSTRAINT_SCHEMA = RC.UNIQUE_CONSTRAINT_SCHEMA 
    AND KCU2.CONSTRAINT_NAME = RC.UNIQUE_CONSTRAINT_NAME 
    AND KCU2.ORDINAL_POSITION = KCU1.ORDINAL_POSITION 
Copy after login

Example:

Suppose we have tables T_ALV_Ref_FilterDisplay and T_AP_Ref_Customer, and T_ALV_Ref_FilterDisplay has a foreign key constraint referencing T_AP_Ref_Customer.MDT_ID. To find the referenced table and column, we can execute the query:

SELECT 
    REFERENCED_TABLE_SCHEMA, 
    REFERENCED_TABLE_NAME, 
    REFERENCED_COLUMN_NAME 
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS 
WHERE 
    FK_TABLE_SCHEMA = 'dbo' 
    AND FK_TABLE_NAME = 'T_ALV_Ref_FilterDisplay' 
    AND FK_COLUMN_NAME = 'FA_MDT_ID';
Copy after login

This would return the following result:

REFERENCED_TABLE_SCHEMA | REFERENCED_TABLE_NAME | REFERENCED_COLUMN_NAME
------------------------ | ---------------------- | ----------------------
dbo                      | T_AP_Ref_Customer      | MDT_ID
Copy after login

Limitations:

Note that the information_schema doesn't include information about indices. If the foreign key reference is based on a unique index, you'll need to use Microsoft proprietary tables instead.

The above is the detailed content of How to Find Foreign Key References in SQL Server Using Information_Schema?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template