sqlserver查找、新增、删除约束

sqlserver查找、新增、删除约束

--添加唯一约束
alter table 表名 add constraint 约束名称 UNIQUE(字段1,字段2)

alter table TAcceptanceForm add constraint AcceptanceNumber_Unique unique(AcceptanceNumber)
--查找唯一约束
SELECT
tab.name AS [表名],
idx.name AS [约束名称],
col.name AS [约束列名]
FROM
sys.indexes idx
JOIN sys.index_columns idxCol
ON (idx.object_id = idxCol.object_id
AND idx.index_id = idxCol.index_id
AND idx.is_unique_constraint = 1)
JOIN sys.tables tab
ON (idx.object_id = tab.object_id)
JOIN sys.columns col
ON (idx.object_id = col.object_id
AND idxCol.column_id = col.column_id)
where tab.name='TAcceptanceForm'
--删除唯一约束
alter table 表名 DROP constraint 约束名

alter table TAcceptanceForm DROP constraint AcceptanceNumber_Unique

 

 

posted on 2022-08-10 10:24  Jankie1122  阅读(292)  评论(0编辑  收藏  举报