作者:qzq9037091 | 来源:互联网 | 2023-10-11 13:23
试试这个:
SELECT
*
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
WHERE CONSTRAINT_NAME ='FK_ChannelPlayerskins_Channels'
当我最初回答这个问题时,我在想“外键”,因为最初的问题是关于查找“FK_ChannelPlayerskins_Channels”。从那时起,许多人评论说要找到其他“约束”,这里有一些其他查询:
--Returns one row for each CHECK, UNIQUE, PRIMARY KEY, and/or FOREIGN KEY
SELECT *
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE CONSTRAINT_NAME='XYZ'
--Returns one row for each FOREIGN KEY constrain
SELECT *
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
WHERE CONSTRAINT_NAME='XYZ'
--Returns one row for each CHECK constraint
SELECT *
FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS
WHERE CONSTRAINT_NAME='XYZ'
这是另一种方法
--Returns 1 row for each CHECK, UNIQUE, PRIMARY KEY, FOREIGN KEY, and/or DEFAULT
SELECT
OBJECT_NAME(OBJECT_ID) AS NameofConstraint
,SCHEMA_NAME(schema_id) AS SchemaName
,OBJECT_NAME(parent_object_id) AS TableName
,type_desc AS ConstraintType
FROM sys.objects
WHERE type_desc LIKE '%CONSTRAINT'
AND OBJECT_NAME(OBJECT_ID)='XYZ'
如果您需要更多约束信息,请查看系统存储过程内部master.sys.sp_helpconstraint
以了解如何获取某些信息。要使用 SQL
Server Management Studio
查看源代码,请进入“对象资源管理器”。从那里展开“Master”数据库,然后展开“Programmability”,然后展开“Stored
Procedures”,然后展开“System Stored
Procedures”。然后您可以找到“sys.sp_helpconstraint”并右键单击它并选择“修改”。请注意不要保存对它的任何更改。此外,您可以在任何表上使用此系统存储过程,例如EXEC
sp_helpconstraint YourTableNameHere
.