Constraints:¶
SQL constraints are used to specify rules for data in a table
Types of constraints:¶
1.Primary key :¶
The PRIMARY KEY constraint uniquely identifies each record in a table.
Primary keys must contain UNIQUE values, and cannot contain NULL values.
A table can have only ONE primary key; and in the table, this primary key can consist of single or multiple columns (fields).
The following SQL creates a PRIMARY KEY on the "ID" column when the "Persons" table is created:
1 2 3 4 5 6 | |
for defining a PRIMARY KEY constraint on multiple columns, use the following SQL syntax:
1 2 3 4 5 6 7 8 | |
How to add primary key constraint for existing table
``` Alter table Persons add constrait pk_Persons_id primary key(id) );
1 2 3 4 5 6 7 8 9 10 | |
CREATE TABLE Persons ( ID int NOT NULL UNIQUE, LastName varchar(255) NOT NULL, FirstName varchar(255), Age int );
1 | |
CREATE TABLE Persons ( ID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Age int, CONSTRAINT UC_Person UNIQUE (ID,LastName) );
1 2 3 4 5 6 7 | |
NOT NULL:¶
The NOT NULL constraint enforces a column to NOT accept NULL values.
This enforces a field to always contain a value, which means that you cannot insert a new record, or update a record without adding a value to this field.
***The following SQL ensures that the "ID", "LastName", and "FirstName" columns will NOT accept NULL values when the "Persons" table is created:
1 2 3 4 5 6 | |
4.CHECK constraint¶
The CHECK constraint is used to limit the value range that can be placed in a column.
If you define a CHECK constraint on a column it will allow only certain values for this column.
If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row.
***CHECK constraint on the "Age" column when the "Persons" table is created. The CHECK constraint ensures that the age of a person must be 18, or older:
1 2 3 4 5 6 | |
How to add CHECK constraint on existing table
1 2 3 | |
5. DEFAULT constraint¶
The DEFAULT constraint is used to set a default value for a column.
The default value will be added to all new records, if no other value is specified.
***The following SQL sets a DEFAULT value for the "City" column when the "Persons" table is created:
1 2 3 4 5 6 7 | |
How to add DEFAULT constraint on existing table
1 2 3 | |
5.FAREIGN KEY constraint:¶
The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables.
A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY KEY in another table.
The table with the foreign key is called the child table, and the table with the primary key is called the referenced or parent table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
***The following SQL creates a FOREIGN KEY on the "PersonID" column when the "Orders" table is created:
1 2 3 4 5 | |
***To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns, use the following SQL syntax:
1 2 3 4 5 6 7 8 | |
***How to create FAREIGN KEY on existing table
1 2 3 | |