简体   繁体   English

如何通过查询识别SQL Server中的1-1,1-M和MN关系?

[英]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或1-M的关系类型识别为具有关系状态的列。

[1-1] or [1-M] or [MN] I need this! 我需要这个[1-1]或[1-M]或[MN]!

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 ? 所以我看到一些Visual Studio扩展,例如Reverse poco GeneratorEntity Framework Power Tools Beta 4可以将表反向转换为C#pocos,因此他们必须知道关系状态1-1或1-M或MN,以便将导航属性生成为单个类或类列表并且还创建EntityTypeConfiguration进行映射,以便它们如何从SQL Server表中获取这些信息? If we dont know any about relationships of tables so we can not reverse them to C# class ! 如果我们对表的关系一无所知,就不能将它们反向转换为C#类!

For 1-1 relationship : There is unique keyword with foreign key constraints in referenced table. 对于1-1关系:引用表中有一个具有外键约束的唯一关键字。

For M-1 relationship : There is no unique keyword with foreign key constraints in referenced table. 对于M-1关系:在参考表中没有具有外键约束的唯一关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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