Home > Database > Mysql Tutorial > How can I retrieve foreign key references using SQL Server's INFORMATION_SCHEMA?

How can I retrieve foreign key references using SQL Server's INFORMATION_SCHEMA?

Linda Hamilton
Release: 2025-01-01 01:17:09
Original
253 people have browsed it

How can I retrieve foreign key references using SQL Server's INFORMATION_SCHEMA?

Getting Foreign Key References from Information_Schema in SQL Server

In SQL Server, foreign keys play a crucial role in maintaining referential integrity within a database. To retrieve information about foreign key references, the information_schema can be a valuable resource.

Query to Retrieve Foreign Key References:

The following query retrieves the foreign key constraint name, referenced table, and column for a given foreign key column:

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 
    ,KCU2.TABLE_SCHEMA AS REFERENCED_TABLE_SCHEMA 
    ,KCU2.TABLE_NAME AS REFERENCED_TABLE_NAME 
    ,KCU2.COLUMN_NAME AS REFERENCED_COLUMN_NAME 
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

Note: Information_schema does not contain indices. To find foreign keys based on unique indices, refer to the proprietary tables of SQL Server.

Example of Foreign Key Reference Retrieval:

Consider the following table structure:

CREATE TABLE [dbo].[T_ALV_Ref_FilterDisplay] (
    [FA_MDT_ID] [varchar](20) NOT NULL,
    [FA_NAME] [varchar](255) NOT NULL
)

CREATE TABLE [dbo].[T_AP_Ref_Customer] (
    [MDT_ID] [varchar](20) NOT NULL,
    [CUST_NAME] [varchar](255) NOT NULL
)

ALTER TABLE [dbo].[T_ALV_Ref_FilterDisplay]  WITH CHECK ADD  CONSTRAINT [FK_T_ALV_Ref_FilterDisplay_T_AP_Ref_Customer] FOREIGN KEY([FA_MDT_ID])
REFERENCES [dbo].[T_AP_Ref_Customer] ([MDT_ID])
Copy after login

To retrieve the referenced table and column for the foreign key [FA_MDT_ID], execute the following query:

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

The result will display:

FK_TABLE_NAME | REFERENCED_TABLE_NAME | REFERENCED_COLUMN_NAME
---------------|----------------------|-------------------------
T_ALV_Ref_FilterDisplay | T_AP_Ref_Customer    | MDT_ID
Copy after login

This indicates that the foreign key [FA_MDT_ID] in [T_ALV_Ref_FilterDisplay] refers to the [MDT_ID] column in the [T_AP_Ref_Customer] table.

The above is the detailed content of How can I retrieve foreign key references using SQL Server's 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