List all default constraints in SQL Server database

Article for: Azure SQL Database MySQL MariaDB

Query below lists default constraints defined in the database ordered by constraint name.

Check this query to see them organized by table.

Query

select con.[name] as constraint_name,
    schema_name(t.schema_id) + '.' + t.[name]  as [table],
    col.[name] as column_name,
    con.[definition]
from sys.default_constraints con
    left outer join sys.objects t
        on con.parent_object_id = t.object_id
    left outer join sys.all_columns col
        on con.parent_column_id = col.column_id
        and con.parent_object_id = col.object_id
order by con.name

Columns

  • constraint_name - name of the constraint in the database
  • table - schema and table name constraint is defined for
  • column_name - name of the column
  • definition - SQL expression that defines this default constraint

Rows

  • One row represents one default constraint
  • Scope of rows: query returns all default constraints in the database (all columns with default constraints)
  • Ordered by constraint name

Sample results

List of default constraints in AdventureWorks database.

Comments are only visible when the visitor has consented to statistics cookies. To see and add comments please accept statistics cookies.
0
There are no comments. Click here to write the first comment.