One of the
things you can add to columns when you CREATE or ALTER a table are constraints.
There are many different types of constraints:
·
Not
null
·
Unique
·
Default
·
Check
·
Primary
key
·
Foreign
key
A not null
constraint is self-explanatory. It prevents that column from holding a NULL
value. A unique constraint prevents duplicate values from being put into a
column. You can have a NULL value in a column with a unique constraint. But
there can be only one row with a NULL. Trying to add a second NULL to a column
with a unique constraint causes a violation and error.
Default
constraints will let you specify a value that the column gets when you insert a
record without a value for that column. Normally the column would get a NULL in
this scenario. But with this constraint, it gets another value you specify for
the column.
The check
constraint will enforce criteria you specify. This is done for new values you
insert, as well as existing data at the time when you add the constraint. The
primary key constraint is like the unique constraint. However, a column with a
primary key constraint cannot be NULL. By default, the primary key constraint
will cause the table to be clustered (put in order using values from the
column).
Foreign key constraints establish references to
primary keys in another table. You can only put values in a foreign key
constraint column that exist in the primary key of the other table. The only
exception to this is that you can put a NULL in the foreign key constraint
column.