我有这个sql:

ALTER TABLE dbo.ChannelPlayerSkins
    DROP CONSTRAINT FK_ChannelPlayerSkins_Channels

但显然,在我们使用的其他一些数据库中,约束有不同的名称。我如何检查是否有名称FK_ChannelPlayerSkins_Channels的约束。


当前回答

试试这个:

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'

If you need even more constraint information, look inside the system stored procedure master.sys.sp_helpconstraint to see how to get certain information. To view the source code using SQL Server Management Studio get into the "Object Explorer". From there you expand the "Master" database, then expand "Programmability", then "Stored Procedures", then "System Stored Procedures". You can then find "sys.sp_helpconstraint" and right click it and select "modify". Just be careful to not save any changes to it. Also, you can just use this system stored procedure on any table by using it like EXEC sp_helpconstraint YourTableNameHere.

其他回答

如果你正在寻找其他类型的约束,例如默认值,你应该使用不同的查询 如何使用INFORMATION_SCHEMA找到默认约束?由devio回答)。使用:

SELECT * FROM sys.objects WHERE type = 'D' AND name = @name

按名称查找默认约束。

我已经把不同的“IF不存在”检查在我的帖子“DDL”IF不存在“条件使SQL脚本可重新运行”

你可以使用上面的方法,但有一个警告:

IF EXISTS(
    SELECT 1 FROM sys.foreign_keys 
    WHERE parent_object_id = OBJECT_ID(N'dbo.TableName') 
        AND name = 'CONSTRAINTNAME'
)
BEGIN 
    ALTER TABLE TableName DROP CONSTRAINT CONSTRAINTNAME 
END 

需要使用name =[约束名],因为一个表可能有多个外键,但仍然没有外键被检查

INFORMATION_SCHEMA是你的朋友。它有各种显示各种模式信息的视图。检查系统视图。您将发现有三个视图处理约束,其中一个是CHECK_CONSTRAINTS。

检查约束是否存在(然后做一些事情,比如如果存在就删除它)最简单的方法是使用OBJECT_ID()函数…

IF OBJECT_ID('dbo.[CK_ConstraintName]', 'C') IS NOT NULL 
    ALTER TABLE dbo.[tablename] DROP CONSTRAINT CK_ConstraintName

OBJECT_ID可以在没有第二个参数的情况下使用('C'仅用于检查约束),这也可以工作,但如果您的约束名称与数据库中其他对象的名称匹配,则可能会得到意想不到的结果。

IF OBJECT_ID('dbo.[CK_ConstraintName]') IS NOT NULL 
    ALTER TABLE dbo.[tablename] DROP CONSTRAINT CK_ConstraintName

OBJECT_ID还可以与其他“约束”一起使用,如外键约束或主键约束等。为获得最佳结果,始终将适当的对象类型作为OBJECT_ID函数的第二个参数:

约束对象类型:

C = CHECK约束 D = DEFAULT(约束或独立) F =外键约束 PK =主键约束 R =规则(老式的,独立的) UQ = UNIQUE约束

还要注意,模式通常是必需的。约束的模式通常采用父表的模式。

在使用此方法时,如果没有将约束(或您正在检查的任何内容)放在括号中,也可能会导致误判——如果您的对象使用不寻常的字符(例如a .),则需要括号。

试试这个:

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'

If you need even more constraint information, look inside the system stored procedure master.sys.sp_helpconstraint to see how to get certain information. To view the source code using SQL Server Management Studio get into the "Object Explorer". From there you expand the "Master" database, then expand "Programmability", then "Stored Procedures", then "System Stored Procedures". You can then find "sys.sp_helpconstraint" and right click it and select "modify". Just be careful to not save any changes to it. Also, you can just use this system stored procedure on any table by using it like EXEC sp_helpconstraint YourTableNameHere.