简体   繁体   中英

How can I recognize 1-1,1-M and M-N relationship in SQL Server by query?

I have a query like this

SELECT 
    RC.CONSTRAINT_NAME FKName, 
    KF.TABLE_SCHEMA FKSchema,
    KF.TABLE_NAME FKTable, 
    KF.COLUMN_NAME FKColumn,
    RC.UNIQUE_CONSTRAINT_NAME PKName,
    KP.TABLE_SCHEMA PKSchema,
    KP.TABLE_NAME PKTable, 
    KP.COLUMN_NAME PKColumn, 
    RC.MATCH_OPTION MatchOption, 
    RC.UPDATE_RULE UpdateRule,
    RC.DELETE_RULE DeleteRule
FROM
    [INFORMATION_SCHEMA].[REFERENTIAL_CONSTRAINTS] RC
JOIN 
    [INFORMATION_SCHEMA].[KEY_COLUMN_USAGE] KF ON RC.CONSTRAINT_NAME = KF.CONSTRAINT_NAME
JOIN 
    [INFORMATION_SCHEMA].[KEY_COLUMN_USAGE] KP ON RC.UNIQUE_CONSTRAINT_NAME = KP.CONSTRAINT_NAME

and it have a result like :

FK_Person_Address   dbo Person  Id  PK_Address  dbo Address Id  SIMPLE  NO ACTION   NO ACTION --[1-1] or [1-M] or [M-N] I need this!

I want to recognize what is the type of relationship in each row 1-1 or 1-M as column with relationship status.

[1-1] or [1-M] or [MN] I need this!

Can any one guide me ?

EDIT :

Why I asked this question ? so I see some visual studio extensions like Reverse poco Generator and Entity Framework Power Tools Beta 4 can reverse table to C# pocos so they must know relationships status 1-1 or 1-M or MN for generating Navigation property as single class or list of classes and also create EntityTypeConfiguration for mapping so How they get these info from SQL server tables ? If we dont know any about relationships of tables so we can not reverse them to C# class !

For 1-1 relationship : There is unique keyword with foreign key constraints in referenced table.

For M-1 relationship : There is no unique keyword with foreign key constraints in referenced table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM