我使用以下语句在SQL Server数据库中创建了一个唯一约束:
ALTER TABLE mytable ADD CONSTRAINT mytable_unique UNIQUE (uid)
如何使用C#SQLConnection对象以编程方式获取所有唯一约束名称?
答案 0 :(得分:3)
查询information_schema.constraint_column_usage
SELECT TC.Constraint_Name ,
CC.Column_Name
FROM information_schema.table_constraints TC
INNER JOIN information_schema.constraint_column_usage CC
ON TC.Constraint_Name = CC.Constraint_Name
WHERE TC.constraint_type = 'Unique'
ORDER BY TC.Constraint_Name
答案 1 :(得分:3)
您也可以从sys.key_constraints获取这些内容:
select name from sys.key_constraints where type = 'UQ'