SQL Constraints Page 1 By HSP
SQL Constraints
Constraints are used to limit the type of data that can go into a table.
Constraints are used to prevent the entry of incorrect information.
Constraint is a column or a set of columns in a table to that prevents certain types of
inconsistent data values from being placed in a columns.
Constraints are used to enforce the data integrity
The data integrity means correctness and consistency of the data.
Integrity Constraints
Integrity Constraints ensure that changes made to the database by authorized user , do not
result in a loss of data ( consistency) .
Types of Integrity Constraints
Entity Constraints Domain Constraints Referential
(Primary Key , (Check , Not Null , Constraints
Unique key , Not Default ) ( Foreign Key )
Null )
Entity Constraints
The entity Integrity is a constraint on primary key values , it states that any
attribute of primary key cannot contain Null values.
If a primary key contain null value , then it is not possible to uniquely identify
record in a table.
It ensures that there are no duplicate rows in a table.
Page 1
SQL Constraints Page 2 By HSP
Domain Constraints
It enforce valid entries for a given column by restricting the type , the format or
the range of possible value.
Domain constraint specifies that the value of an attribute must be from the
domain.
A domain is a set of atomic values , e.g The domain for the attribute AGE for
EMPLOYEE Table will be the set of all possible positive numbers between 18
and 65.
The attribute cannot hold a value other than those specified in the domain.
Referential Constraints
It ensures that value appears in one relation for an attribute also appears for an
attribute of another relation.
The referential constraint is a foreign key constraint.
Foreign key points to the primary key of another table.
Constraints can be specified when a table is created (with the CREATE TABLE
statement) or after the table is created (with the ALTER TABLE statement).
We will focus on the following constraints:
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
DEFAULT
NOT NULL
By default, a table column can hold NULL values.
SQL NOT NULL Constraint
The NOT NULL constraint enforces a column to NOT accept NULL values.
The NOT NULL constraint enforces a field to always contain a value. This means that
you cannot insert a new record, or update a record without adding a value to this field.
Page 2
SQL Constraints Page 3 By HSP
The following SQL enforces the "P_Id" column and the "LastName" column to not
accept NULL values:
CREATE TABLE Persons
(
P_Id numberNOT NULL,
LastName text(255) NOT NULL,
FirstName text(255),
Address text(255),
City text(255)
)
SQL UNIQUE Constraint
The UNIQUE constraint uniquely identifies each record in a database table.
The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness
for a column or set of columns.
A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it.
Note that you can have many UNIQUE constraints per table, but only one PRIMARY
KEY constraint per table.
SQL UNIQUE Constraint on CREATE TABLE
The following SQL creates a UNIQUE constraint on the "P_Id" column when the
"Persons" table is created:
MySQL:
CREATE TABLE Persons
(
P_Id numberNOT NULL,
LastName text(255) NOT NULL,
FirstName text(255),
Address text(255),
City text(255),
UNIQUE (P_Id)
)
Page 3
SQL Constraints Page 4 By HSP
SQL Server / Oracle / MS Access:
CREATE TABLE Persons
(
P_Id numberNOT NULL UNIQUE,
LastName text(255) NOT NULL,
FirstName text(255),
Address text(255),
City text(255)
)
To allow naming of a UNIQUE constraint, and for defining a UNIQUE constraint on
multiple columns, use the following SQL syntax:
MySQL / SQL Server / Oracle / MS Access:
CREATE TABLE Persons
(
P_Id numberNOT NULL,
LastName text(255) NOT NULL,
FirstName text(255),
Address text(255),
City text(255),
CONSTRAINT uc_PersonID UNIQUE (P_Id,LastName)
)
SQL UNIQUE Constraint on ALTER TABLE
To create a UNIQUE constrainton the "P_Id" column when the table is already created,
use the following SQL:
MySQL / SQL Server / Oracle / MS Access:
ALTER TABLE Persons
ADD UNIQUE (P_Id)
To allow naming of a UNIQUE constraint, and for defining a UNIQUE constrainton
multiple columns, use the following SQL syntax:
MySQL / SQL Server / Oracle / MS Access:
ALTER TABLE Persons
ADD CONSTRAINT uc_PersonID UNIQUE (P_Id,LastName)
Page 4
SQL Constraints Page 5 By HSP
To DROP a UNIQUE Constraint
To drop a UNIQUE constraint, use the following SQL:
MySQL:
ALTER TABLE Persons
DROP INDEX uc_PersonID
SQL Server / Oracle / MS Access:
ALTER TABLE Persons
DROP CONSTRAINT uc_PersonID
SQL PRIMARY KEY Constraint
The PRIMARY KEY constraint uniquely identifies each record in a database table.
Primary keys must contain unique values.
A primary key column cannot contain NULL values.
Each table should have a primary key, and each table can have only ONE primary key.
SQL PRIMARY KEY Constraint on CREATE TABLE
The following SQL creates a PRIMARY KEY on the "P_Id" column when the "Persons"
table is created:
MySQL:
CREATE TABLE Persons
(
P_Id numberNOT NULL,
LastName text(255) NOT NULL,
FirstName text(255),
Address text(255),
City text(255),
PRIMARY KEY (P_Id)
)
Page 5
SQL Constraints Page 6 By HSP
SQL Server / Oracle / MS Access:
CREATE TABLE Persons
(
P_Id numberNOT NULL PRIMARY KEY,
LastName text(255) NOT NULL,
FirstName text(255),
Address text(255),
City text(255)
)
To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY
constrainton multiple columns, use the following SQL syntax:
MySQL / SQL Server / Oracle / MS Access:
CREATE TABLE Persons
(
P_Id number NOT NULL,
LastName text(255) NOT NULL,
FirstName text(255),
Address text(255),
City text(255),
CONSTRAINT pk_PersonID PRIMARY KEY (P_Id , LastName)
)
SQL PRIMARY KEY Constraint on ALTER TABLE
To create a PRIMARY KEY constrainton the "P_Id" column when the table is already
created, use the following SQL:
MySQL / SQL Server / Oracle / MS Access:
ALTER TABLE Persons
ADD PRIMARY KEY (P_Id)
To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY
constraint on multiple columns, use the following SQL syntax:
MySQL / SQL Server / Oracle / MS Access:
ALTER TABLE Persons
ADD CONSTRAINT pk_PersonID PRIMARY KEY (P_Id,LastName)
Page 6
SQL Constraints Page 7 By HSP
Note: If you use the ALTER TABLE statement to add a primary key, the primary key
column(s) must already have been declared to not contain NULL values (when the table
was first created).
To DROP a PRIMARY KEY Constraint
To drop a PRIMARY KEY constraint, use the following SQL:
MySQL:
ALTER TABLE Persons
DROP PRIMARY KEY
SQL Server / Oracle / MS Access:
ALTER TABLE Persons
DROP CONSTRAINT pk_PersonID
SQL FOREIGN KEY Constraint
A FOREIGN KEY in one table points to a PRIMARY KEY in another table.
Let's illustrate the foreign key with an example. Look at the following two tables:
The "Persons" table:
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger
The "Orders" table:
O_Id OrderNo P_Id
1 77895 3
2 44678 3
3 22456 2
4 24562 1
Note that the "P_Id" column in the "Orders" table points to the "P_Id" column in the
"Persons" table.
Page 7
SQL Constraints Page 8 By HSP
The "P_Id" column in the "Persons" table is the PRIMARY KEY in the "Persons" table.
The "P_Id" column in the "Orders" table is a FOREIGN KEY in the "Orders" table.
The FOREIGN KEY constraint is used to prevent actions that would destroy links
between tables.
The FOREIGN KEY constraint also prevents that invalid data form being inserted into
the foreign key column, because it has to be one of the values contained in the table it
points to.
SQL FOREIGN KEY Constraint on CREATE TABLE
The following SQL creates a FOREIGN KEY on the "P_Id" column when the "Orders"
table is created:
MySQL:
CREATE TABLE Orders
(
O_Id number NOT NULL,
OrderNo number NOT NULL,
P_Id number,
PRIMARY KEY (O_Id),
FOREIGN KEY (P_Id) REFERENCES Persons(P_Id)
)
SQL Server / Oracle / MS Access:
CREATE TABLE Orders
(
O_Id number NOT NULL PRIMARY KEY,
OrderNo number NOT NULL,
P_Id number FOREIGN KEY REFERENCES Persons(P_Id)
)
To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY
constraint on multiple columns, use the following SQL syntax:
MySQL / SQL Server / Oracle / MS Access:
CREATE TABLE Orders
(
O_Id number NOT NULL,
OrderNo number NOT NULL,
P_Id number,
Page 8
SQL Constraints Page 9 By HSP
PRIMARY KEY (O_Id),
CONSTRAINT fk_PerOrders FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)
)
SQL FOREIGN KEY Constraint on ALTER TABLE
To create a FOREIGN KEY constrainton the "P_Id" column when the "Orders" table is
already created, use the following SQL:
MySQL / SQL Server / Oracle / MS Access:
ALTER TABLE Orders
ADD FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)
To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY
constrainton multiple columns, use the following SQL syntax:
MySQL / SQL Server / Oracle / MS Access:
ALTER TABLE Orders
ADD CONSTRAINT fk_PerOrders
FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)
To DROP a FOREIGN KEY Constraint
To drop a FOREIGN KEY constraint, use the following SQL:
MySQL:
ALTER TABLE Orders
DROP FOREIGN KEY fk_PerOrders
SQL Server / Oracle / MS Access:
ALTER TABLE Orders
DROP CONSTRAINT fk_PerOrders
SQL CHECK Constraint
Page 9
SQL Constraints Page 10 By HSP
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 single column it allows 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.
SQL CHECK Constraint on CREATE TABLE
The following SQL creates a CHECK constraint on the "P_Id" column when the
"Persons" table is created. The CHECK constraint specifies that the column "P_Id" must
only include integers greater than 0.
My SQL:
CREATE TABLE Persons
(
P_Id number NOT NULL,
LastName text(255) NOT NULL,
FirstName text(255),
Address text(255),
City text(255),
CHECK (P_Id>0)
)
SQL Server / Oracle / MS Access:
CREATE TABLE Persons
(
P_Id number NOT NULL CHECK (P_Id>0),
LastName text(255) NOT NULL,
FirstName text(255),
Address text(255),
City text(255)
)
To allow naming of a CHECK constraint, and for defining a CHECK constraint on
multiple columns, use the following SQL syntax:
MySQL / SQL Server / Oracle / MS Access:
CREATE TABLE Persons
Page 10
SQL Constraints Page 11 By HSP
(
P_Id number NOT NULL,
LastName text(255) NOT NULL,
FirstName text(255),
Address text(255),
City text(255),
CONSTRAINT chk_Person CHECK (P_Id>0 AND City='Sandnes')
)
SQL CHECK Constraint on ALTER TABLE
To create a CHECK constraint on the "P_Id" column when the table is already created,
use the following SQL:
MySQL / SQL Server / Oracle / MS Access:
ALTER TABLE Persons
ADD CHECK (P_Id>0)
To allow naming of a CHECK constraint, and for defining a CHECK constraint on
multiple columns, use the following SQL syntax:
MySQL / SQL Server / Oracle / MS Access:
ALTER TABLE Persons
ADD CONSTRAINT chk_Person CHECK (P_Id>0 AND City='Sandnes')
To DROP a CHECK Constraint
To drop a CHECK constraint, use the following SQL:
SQL Server / Oracle :
ALTER TABLE Persons
DROP CONSTRAINT chk_Person
SQL DEFAULT Constraint
The DEFAULT constraint is used to insert a default value into a column.
The default value will be added to all new records, if no other value is specified.
Page 11
SQL Constraints Page 12 By HSP
SQL DEFAULT Constraint on CREATE TABLE
The following SQL creates a DEFAULT constraint on the "City" column when the
"Persons" table is created:
My SQL / SQL Server / Oracle :
CREATE TABLE Persons
(
P_Id number NOT NULL,
LastName text(255) NOT NULL,
FirstName text(255),
Address text(255),
City text(255) DEFAULT 'Sandnes'
)
The DEFAULT constraint can also be used to insert system values, by using functions
like GETDATE():
CREATE TABLE Orders
(
O_Id number NOT NULL,
OrderNo number NOT NULL,
P_Id number,
OrderDate date DEFAULT GETDATE( )
)
SQL DEFAULT Constraint on ALTER TABLE
To create a DEFAULT constraint on the "City" column when the table is already created,
use the following SQL:
MySQL:
ALTER TABLE Persons
ALTER City SET DEFAULT 'SANDNES'
Page 12
SQL Constraints Page 13 By HSP
SQL Server / Oracle / MS Access:
ALTER TABLE Persons
ALTER COLUMN City SET DEFAULT 'SANDNES'
To DROP a DEFAULT Constraint
To drop a DEFAULT constraint, use the following SQL:
MySQL:
ALTER TABLE Persons
ALTER City DROP DEFAULT
SQL Server / Oracle :
ALTER TABLE Persons
ALTER COLUMN City DROP DEFAULT
Page 13