我在下面创建了一张表, “CREATE TABLE UNIQ(NUM INT UNIQUE,NAMES VARCHAR(10))”
现在我想删除/禁用NUM列上设置的UNIQUE约束。 如何使用Query ..?
执行此操作我用Google搜索并了解到,如果我在创建表格时给出约束名称,那么使用该名称我可以轻松删除约束。
EX: CREATE TABLE UNIQ(NUM INT CONSTRAINT CONTR_NAME UNIQUE,NAMES VARCHAR(10))
用于删除约束。, ALTER TABLE UNIQ DROP CONSTRAINT CONTR_NAME
提前致谢.. :))
由于
马赫什
答案 0 :(得分:0)
ALTER TABLE UNIQ
DROP CONSTRAINT [name of 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'
答案 1 :(得分: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