Creating check constraints

This topic describes creating check constraints on a table.

Select the Properties option for help check constraint. In its General tab, you see the Check Constraint text area. You can customize the formatting of this area by right-clicking to invoke the Options Editor. The settings are universal; once you set them in one text area, all text areas appear the same way. The tabs in the Options Editor include:

StepsCreating a table check constraint

  1. In the table, select the Check Constraints folder.

  2. Select File | New | Check Constraint.

  3. The New Check Constraint Properties dialog box opens.

  4. Enter a name for the constraint.

  5. In the Check Constraint box, enter the text of the constraint. Check constraint text specifies a search condition that any value must pass before it is inserted into the table.

  6. You can also select the columns which are to be included in the check constraint text by selecting the columns button to bring up the column list dialog.

  7. Click Apply.

Shortcuts

Select the Check Constraints folder. In the right pane, double-click the Add Check Constraint icon.

This dialog lists columns can be used in the definition of check constraints.

You can declare a check constraint to limit the values users insert into a column in a table. Check constraints are useful for applications that check a limited, specific range of values. A check constraint specifies a search_condition that any value must pass before it is inserted into the table. A search_condition can include:

An expression can include arithmetic operations and Transact-SQL built-in functions. The search_condition cannot contain subqueries, a set function specification, or a target specification.

For example, this statement ensures that only certain values can be entered for the pub_id column:

create table my_new_publishers
(pub_id      char(4)
        check (pub_id in ("1389", "0736", "0877",
             "1622", "1756")
        or pub_id like "99[0-9][0-9]"),
pub_name     varchar(40),
city         varchar(20),
state        char(2)) 

Column-level check constraints can reference only the column on which the constraint is defined; they cannot reference other columns in the table. Table-level check constraints can reference any columns in the table. create table allows multiple check constraints in a column definition.

Because check constraints do not override column definitions, you cannot use a check constraint to prohibit null values if the column definition permits them. If you declare a check constraint on a column that allows null values, you can insert NULL into the column, implicitly or explicitly, even though NULL is not included in the search_condition. For example, suppose you define the following check constraint on a table column that allows null values:

check (pub_id in ("1389", "0736", "0877", "1622", "1756")) 

You can still insert NULL into that column. The column definition overrides the check constraint because the following expression always evaluates to true:

col_name != null  

The dialog displays is the list of columns included in the table. You can select the columns which need to be included in the check contraint definition.