当我们的名字为exactly = "ID"
我的目标是检索重构的所有这些键。我尝试过information_schema
表,但我自己无法实现目标。
最喜欢的输出应该是:
SCHEMA_NAME | TABLE_NAME | PRIMARY_KEY_NAME
--------------------------------------------------------
... | ... | ID
... | ... | ID
... | ... | ID
... | ... | ID
... | ... | ID
... | ... | ID
... | ... | ID
答案 0 :(得分:1)
使用INFORMATION_SCHEMA.TABLE_CONSTRAINTS
SELECT SCHEMA_NAME = TABLE_SCHEMA,
TABLE_NAME,
PRIMARY_KEY_NAME = CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'PRIMARY KEY'
AND CONSTRAINT_NAME = 'ID'
或使用sys.key_constraints
SELECT SCHEMA_NAME= s.NAME,
TABLE_NAME = Object_name(kc.parent_object_id),
PRIMARY_KEY_NAME = kc.NAME
FROM sys.key_constraints kc
JOIN sys.schemas s
ON kc.schema_id = s.schema_id
WHERE type = 'pk'
AND kc.NAME = 'ID'
答案 1 :(得分:0)
SELECT Col.Column_Name, col.CONSTRAINT_NAME from
INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab,
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col
WHERE
Col.Constraint_Name = Tab.Constraint_Name
AND Col.Table_Name = Tab.Table_Name
AND Constraint_Type = 'PRIMARY KEY'
AND col.CONSTRAINT_NAME like '%adhoc%'