Constraints

Constraints offer a way to have Microsoft® SQL Server™ enforce the integrity of a database automatically. Constraints define rules regarding the values allowed in columns and are the standard mechanism for enforcing integrity, preferred over triggers, rules, and defaults. They are also used by the query optimizer to improve performance in selectivity estimation, cost calculations, and query rewriting.

There are five classes of constraints.

Constraints can be column constraints or table constraints:

Table constraints must be used when more than one column must be included in a constraint.

For example, if a table has two or more columns in the primary key, you must use a table constraint to include both columns in the primary key. Consider a table that records events happening in a machine in a factory. Assume that events of several types can happen at the same time, but that no two events happening at the same time can be of the same type. This can be enforced in the table by including both the type and time columns in a two-column primary key.

CREATE TABLE factory_process

    (event_type    int,

    event_time    datetime,

    event_site    char(50),

    event_desc    char(1024),

CONSTRAINT event_key PRIMARY KEY (event_type, event_time) )

  

  


(c) 1988-98 Microsoft Corporation. All Rights Reserved.