0

我在下面创建了一个表,“CREATE TABLE UNIQ(NUM INT UNIQUE, NAMES VARCHAR(10))”

现在我想删除/禁用在 NUM 列上设置的 UNIQUE 约束。如何使用 Query.. 做到这一点?

我用谷歌搜索并知道,如果我在创建表时给出约束名称,那么使用该名称我可以轻松地删除约束。

例如:创建表UNIQ(NUM INT CONSTRAINT CONTR_NAME UNIQUE,NAMES VARCHAR(10))

对于删除约束,ALTER TABLE UNIQ DROP CONSTRAINT CONTR_NAME

提前致谢.. :)

谢谢

马赫什

4

2 回答 2

0
ALTER TABLE UNIQ
DROP CONSTRAINT [name of constraint]

The below query should help you find the name of the constraint

SELECT
    o.name AS ConstraintName,
    c.name AS ColumnName
FROM sys.objects o
    JOIN sys.objects p
        ON o.parent_object_id = p.object_id
    JOIN sys.columns c
        ON c.object_id = p.object_id
WHERE o.type_desc LIKE '%CONSTRAINT'
AND p.Name = 'UNIQ'
于 2013-09-19T06:25:52.253 回答
0

尝试这个:

DECLARE @ConstraintName VARCHAR(256)
SET @ConstraintName = (
     SELECT             obj.name
     FROM               sys.columns col 

     LEFT OUTER JOIN    sys.objects obj 
     ON                 obj.object_id = col.default_object_id 
     AND                obj.type = 'UQ' 

     WHERE              col.object_id = OBJECT_ID('TableName') 
     AND                obj.name IS NOT NULL
     AND                col.name = 'ColunmName'
)   

IF(@ConstraintName IS NOT NULL)
BEGIN
    EXEC ('ALTER TABLE [TableName] DROP CONSTRAINT ['+@ConstraintName+']')
END
于 2013-09-19T06:34:31.797 回答