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:
Editor tab Specifies scroll bars in the Check Constraint area. Your choices are:
Vertical – unselected by default
Horizontal – selected by default
Tabs tab Specifies the amount of space for tabs and indents. You can also set up auto-indenting, including indenting code brackets.
Format tab Allows you to change the font size and colors of the syntax that appears in the text area. The elements you can change are:
Text selections
Texts
Keywords
Comments
Formal Comments
Numbers
Strings
Open strings
Operators
Punctuation
Break point margins
HTML element names
HTML attribute names
HTML attribute values
HTML comments
HTML tag delimiters
HTML operators
Print tab Specifies the font size that the text area’s syntax prints at. You can also specify what information should appear in the header and footer.
Creating a table check constraint
In the table, select the Check Constraints folder.
Select File | New | Check Constraint.
The New Check Constraint Properties dialog box opens.
Enter a name for the constraint.
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.
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.
Click Apply.
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:
A list of constant expressions introduced with in.
A range of constant expressions introduced with between.
A set of conditions introduced with like, which may contain wildcard characters.
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.