How to select all automatically named constrains in tables, sql server

时间:2017-10-12 10:22:06

标签: sql sql-server tsql

I have a database, with some tables in which, some fields have constraints. I have named some of these constraints myself, but some others, the names are generated automatically.

I want to select all these constrains. I am using

SELECT * FROM sys.objects
WHERE type_desc LIKE '%CONSTRAINT'

to get all the constraints. However there are a lot, and I cannot find them manually. These names are unpredictable with alphanumeric characters like PK__getParen__50EEF97F6BC33CCA How do I separate constraints I have named myself and automatically named?

3 个答案:

答案 0 :(得分:2)

sys.check_constraintssys.key_constraintssys.default_constraintssys.foreign_keys视图包含一列is_system_named,可以告诉您是否自动生成了约束的名称。

答案 1 :(得分:0)

在对象资源管理器的“数据库”文件夹的“系统数据库”文件夹的“主”数据库文件夹的“视图”文件夹中的“系统视图”文件夹中有许多与CONSTRAINT相关的视图。

即:除非已经打开,否则请从Object Explorer标签打开View

Databases > System Databases > master > Views > System Views

只需将它们作为(例如)SELECT * FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS运行,您就应该找到所需的内容。

答案 2 :(得分:0)

尝试类似:

 SELECT * 
    FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
    WHERE CONSTRAINT_NAME NOT LIKE 'PK_%'