由于我们数据库的重构,我希望重命名数据库中的一些外键。我们想要应用的外键名称的格式根据关系的基数而不同(目前我们只有1-n和1-1)。
我想确定所有1-1外键关系,以便我对这些关键字应用不同的命名模板。我不知道如何实现这一目标。到目前为止,我只能列出与此查询的所有外键关系:
SELECT RC.CONSTRAINT_NAME FK_Name
, KP.TABLE_NAME PK_Table
, KF.TABLE_NAME FK_Table
, KP.COLUMN_NAME PK_Column
, KF.COLUMN_NAME FK_Column
, RC.UNIQUE_CONSTRAINT_NAME PK_Name
, KP.TABLE_SCHEMA PK_Schema
, 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
SSMS显然可以提取这些数据,因为在图表查看器中它显示两种类型的关系(在连接线的两端都有一个键)。
感谢任何帮助。
答案 0 :(得分:0)
我相信我在sqlmetal用于生成映射的查询的帮助下找到了它:
select RC.CONSTRAINT_NAME FK_Name
, KP.TABLE_NAME PK_Table
, KF.TABLE_NAME FK_Table
, KP.COLUMN_NAME PK_Column
, KF.COLUMN_NAME FK_Column
, RC.UNIQUE_CONSTRAINT_NAME PK_Name
, KP.TABLE_SCHEMA PK_Schema
, 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
where KF.TABLE_NAME + '.' + KF.COLUMN_NAME in
(
select t.name + '.' + c.name
from sys.indexes as x,
sys.index_columns as ic,
sys.columns as c,
sys.tables as t,
sys.schemas as s
where x.object_id = ic.object_id and
x.index_id = ic.index_id and
x.object_id = c.object_id and
ic.column_id = c.column_id and
c.object_id = t.object_id and
t.schema_id = s.schema_id and
ISNULL(OBJECTPROPERTY(t.object_id, 'IsMSShipped'), 0) = 0 and
x.is_unique = 1 and
(
select count(*)
from sys.index_columns ic2
where ic2.object_id = ic.object_id and
ic2.index_id = ic.index_id and
ISNULL(OBJECTPROPERTY(ic2.object_id, 'IsMSShipped'), 0) = 0
) = 1
)