我有这个sql:
ALTER TABLE dbo.ChannelPlayerSkins
DROP CONSTRAINT FK_ChannelPlayerSkins_Channels
但显然,在我们使用的其他一些数据库中,约束有不同的名称。我如何检查是否有名称FK_ChannelPlayerSkins_Channels的约束。
我有这个sql:
ALTER TABLE dbo.ChannelPlayerSkins
DROP CONSTRAINT FK_ChannelPlayerSkins_Channels
但显然,在我们使用的其他一些数据库中,约束有不同的名称。我如何检查是否有名称FK_ChannelPlayerSkins_Channels的约束。
当前回答
IF (OBJECT_ID('FK_ChannelPlayerSkins_Channels') IS NOT NULL)
其他回答
试试这个:
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.
检查约束是否存在(然后做一些事情,比如如果存在就删除它)最简单的方法是使用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 .),则需要括号。
IF EXISTS(SELECT TOP 1 1 FROM sys.default_constraints WHERE parent_object_id = OBJECT_ID(N'[dbo].[ChannelPlayerSkins]') AND name = 'FK_ChannelPlayerSkins_Channels')
BEGIN
DROP CONSTRAINT FK_ChannelPlayerSkins_Channels
END
GO
你可以使用上面的方法,但有一个警告:
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 =[约束名],因为一个表可能有多个外键,但仍然没有外键被检查
你在看类似这样的东西吗,下面是在SQL Server 2005中测试的
SELECT * FROM sys.check_constraints WHERE
object_id = OBJECT_ID(N'[dbo].[CK_accounts]') AND
parent_object_id = OBJECT_ID(N'[dbo]. [accounts]')