SQL constraints (Constraints)
SQL constraint for specifying data in the table rules.
If the data violates the constraint behavior exists, the behavior will be constrained terminated.
Constraints can be specified when creating a table (via the CREATE TABLE statement), or after a predetermined table is created (via ALTER TABLE statement).
SQL CREATE TABLE + CONSTRAINT syntax
column_name1 data_type ( size ) constraint_name ,
column_name2 data_type ( size ) constraint_name ,
column_name3 data_type ( size ) constraint_name ,
In SQL, we have the following constraints:
- NOT NULL - which indicates that a column can not store a NULL value.
- UNIQUE - to ensure that every row of a column must have a unique value.
- PRIMARY KEY - the combination of NOT NULL and UNIQUE. To ensure that a column (or two columns combine multiple columns) with a unique identifier, help make it easier and more quickly find a particular record in the table.
- FOREIGN KEY - ensure that the data in a table referential integrity match values in another table.
- CHECK - guaranteed value in the column that meet the specified conditions.
- DEFAULT - there is no provision to the default value of the column on assignment.
In the following sections, we will explain in detail each constraint.