sql server如何查询一个表中的一个列是否存在唯一约束 比如查询 AA表的 BB列
示例:
CREATE TABLE AA (
BB INT UNIQUE
);
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 = 'AA'
结果:
表名 约束名称 约束列名
AA UQ__AA__3214B5C34460231C BB