Search

SQL Constraints

•
SQL constraints are used to specify rules for the data in a table.
•
The following constraints are commonly used in SQL
Not Null
Ensures that a column cannot have a NULL value
UNIQUE
Ensures that all values in a column are different
PRIMARY KEY
A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table
FOREIGN KEY
Uniquely identifies a row/record in another table
CHECK
Ensures that all values in a column satisfies a specific condition
DEFAULT
Sets a default value for a column when no value is specified
INDEX
Use to create and retrieve data from the database very quickly

Constraints and Indexes

A foreign key checks that a referenced value exists; several rows can reference the same parent. It does not make the referencing column unique.
An index is an access structure, rather than a general SQL integrity constraint. PRIMARY KEY and UNIQUE constraints can use indexes to enforce their rules, but an ordinary index need not enforce uniqueness. DEFAULT supplies a value when one is omitted; it does not by itself validate every value a caller provides.

Reference