May 20th, 2009

How do Constraints Work?, CHECK (Part 1 of 2)

Posted by admin in P. Table Creation

A CHECK constraint makes sure that a column or columns have limited values.

> A table can have zero, one, or many CHECK constraints. It uses the reserved word CHECK in the CREATE TABLE statement.

> A simple constraint (one column) can be defined as a column constraint or a table constraint.
As a simple column constraint, CHECK is placed in the column definition.

> A composite constraint (multiple columns) can only be defines as a table constraint.
As a table constraint, the CHECK definition has the column names specified.

> A CHECK column constraint works just like a WHERE clause with BETWEEN, IN, IS NULL, or LIKE.
It can allow only specific values, minimum or maximum values, or a range of values.
If you apply a CHECK constraint after the table has been populated with rows, you must ensure that the values in the table fall within the CHECK constraint values.
If the CHECK constraint is in place before the table is populated, the constraint will ensure proper values.

> General practice is to use a CONSTRAINT clause to explicitly name all constraints.
This gives a constraint name to make maintenance easier.

Comments are closed.

Sorry, the comment form is closed at this time.