DROP Constraint 不知道名字

DROP Constraint without knowing the name

我知道有很多关于我要提到的内容的参考资料。

我宁愿分享一些我认为应该非常有用的东西,并集中在一个地方 - Q&A-style。由于不同时间的不同限制,我之前一直在努力寻找这个。

常用约束如下:

如果您知道名称,则很容易删除约束 -

ALTER TABLE {table_name} DROP CONSTRAINT {constraint_name};

但大多数时候我们在创建表时定义约束并且没有名称。一段时间后,如果我们决定删除任何约束,就不是那么简单了,我们确实需要编写一些相对复杂的查询。

那么,我们该怎么做呢?

-- Table T1
IF OBJECT_ID('dbo.T1') IS NOT NULL
    DROP TABLE T1

CREATE TABLE T1 (COL1 INT PRIMARY KEY)

-- Table T2
IF OBJECT_ID('dbo.T2') IS NOT NULL
    DROP TABLE T2

CREATE TABLE T2 (
    COL1 INT FOREIGN KEY REFERENCES T1(COL1)
    ,COL2 VARCHAR(2) UNIQUE
    ,COL3 INT NOT NULL DEFAULT(0)
    ,CHECK (
        COL3 IN (
            0
            ,1
            ,2
            )
        )
    )
GO

-- *** Foreign key constraint ***

DECLARE @Table NVARCHAR(256) = N'T2'
DECLARE @Column NVARCHAR(256) = N'COL1'
DECLARE @Command NVARCHAR(1000)

SELECT @Command = 'ALTER TABLE ' + '[' + @Table + '] DROP CONSTRAINT ' + CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_NAME = @Table
    AND COLUMN_NAME = @Column

--PRINT (@Command)  -- ALTER TABLE [T2] DROP CONSTRAINT FK__T2__COL1__058EC7FB
EXECUTE (@Command)

GO

-- *** Primary key constraint ***

DECLARE @Table NVARCHAR(256) = N'T1'
DECLARE @Command NVARCHAR(1000)

SELECT @Command = 'ALTER TABLE ' + '[' + @Table + '] DROP CONSTRAINT ' + [name]
FROM sys.key_constraints
WHERE [type] = 'PK'
    AND [parent_object_id] = OBJECT_ID(@table);

--PRINT (@Command)  -- ALTER TABLE [T1] DROP CONSTRAINT PK__T1__AA1D004EBAE57D94
EXECUTE (@Command)

GO

-- *** Unique key constraint ***

DECLARE @Table NVARCHAR(256) = N'T2'
DECLARE @Column NVARCHAR(256) = N'COL2'
DECLARE @Command NVARCHAR(1000)

SELECT @Command = 'ALTER TABLE [' + @Table + '] DROP CONSTRAINT ' + d.name
FROM sys.tables t
JOIN sys.indexes d ON d.object_id = t.object_id
    AND d.type = 2
    AND d.is_unique = 1
JOIN sys.index_columns ic ON d.index_id = ic.index_id
    AND ic.object_id = t.object_id
JOIN sys.columns c ON ic.column_id = c.column_id
    AND c.object_id = t.object_id
WHERE t.name = @Table
    AND c.name = @Column

--PRINT (@Command)  -- ALTER TABLE [T2] DROP CONSTRAINT UQ__T2__AA1D0040A435D4E0
EXECUTE (@Command)

GO

-- *** Not Null constraint ***

-- ALTER TABLE T2 ALTER COLUMN COL3 INT NULL

-- GO

DECLARE @Table NVARCHAR(256) = N'T2'
DECLARE @Column NVARCHAR(256) = N'COL3'
DECLARE @Command NVARCHAR(1000)

SELECT @Command = 'ALTER TABLE ' + '[' + @Table + '] DROP CONSTRAINT ' + d.name
FROM sys.tables t
JOIN sys.default_constraints d ON d.parent_object_id = t.object_id
JOIN sys.columns c ON c.object_id = t.object_id
    AND c.column_id = d.parent_column_id
WHERE t.name = @Table
    AND c.name = @Column

--PRINT (@Command)  -- ALTER TABLE [T2] DROP CONSTRAINT DF__T2__COL3__0682EC34
EXECUTE (@Command)

GO

-- *** Check constraint ***

DECLARE @Table NVARCHAR(256) = N'T2'
DECLARE @Column NVARCHAR(256) = N'COL3'
DECLARE @Command NVARCHAR(1000)

SELECT @Command = 'ALTER TABLE ' + '[' + @Table + '] DROP CONSTRAINT ' + d.name
FROM sys.tables t
JOIN sys.check_constraints d ON d.parent_object_id = t.object_id
JOIN sys.columns c ON c.object_id = t.object_id
    AND c.column_id = d.parent_column_id
WHERE t.name = @Table
    AND c.name = @Column

--PRINT (@Command)  -- ALTER TABLE [T2] DROP CONSTRAINT CK__T2__COL3__0777106D
EXECUTE (@Command)

有些查询来自我的本地存储库,有些是从不同来源引用的:

Finding a Primary Key Constraint on the fly in SQL Server 2005

Drop Foreign Key without knowing the name of the constraint?

How to drop a unique constraint from table column?