除明显的主要/外键之外,是否有快速获取数据库中所有约束的列表?
答案 0 :(得分:8)
假设SQL Server 2005+(总是用于指定SQL Server的版本!):
SELECT
[constraint] = name,
[object] = QUOTENAME(OBJECT_SCHEMA_NAME(parent_object_id))
+ '.' + QUOTENAME(OBJECT_NAME(parent_object_id)),
[definition]
FROM sys.check_constraints
ORDER BY [object], name;
SELECT
[constraint] = name,
[object] = QUOTENAME(OBJECT_SCHEMA_NAME(parent_object_id))
+ '.' + QUOTENAME(OBJECT_NAME(parent_object_id)),
[definition]
FROM sys.default_constraints
ORDER BY [object], name;
如果要将它们组合成单个结果集:
SELECT
[constraint] = name,
type_desc,
[object] = QUOTENAME(OBJECT_SCHEMA_NAME(parent_object_id))
+ '.' + QUOTENAME(OBJECT_NAME(parent_object_id)),
[definition]
FROM sys.check_constraints
UNION ALL
SELECT
[constraint] = name,
type_desc,
[object] = QUOTENAME(OBJECT_SCHEMA_NAME(parent_object_id))
+ '.' + QUOTENAME(OBJECT_NAME(parent_object_id)),
[definition]
FROM sys.default_constraints
ORDER BY [object], name;