【发布时间】:2021-01-08 16:59:06
【问题描述】:
我使用来自Know relationships between all the tables of database in SQL Server stackoverflow question 的这个选择来查找我的数据库中存在的所有关系。
SELECT
fk.name 'FK Name',
tp.name 'Parent table',
cp.name, cp.column_id,
tr.name 'Refrenced table',
cr.name, cr.column_id
FROM
sys.foreign_keys fk
INNER JOIN
sys.tables tp ON fk.parent_object_id = tp.object_id
INNER JOIN
sys.tables tr ON fk.referenced_object_id = tr.object_id
INNER JOIN
sys.foreign_key_columns fkc ON fkc.constraint_object_id = fk.object_id
INNER JOIN
sys.columns cp ON fkc.parent_column_id = cp.column_id AND fkc.parent_object_id = cp.object_id
INNER JOIN
sys.columns cr ON fkc.referenced_column_id = cr.column_id AND fkc.referenced_object_id = cr.object_id
ORDER BY
tp.name, cp.column_id
但是如何将M:MM:1、1:1 等关系类型添加到结果中?哪个 sql 属性或列这么说?
【问题讨论】:
-
在任何 2 个表之间不可能有 M:M 关系。要支持这种类型的关系,您必须有第三个表(通常称为交集表)。
标签: sql sql-server