SQL Constraints
1
Constraints
A property that can be set on a column or set of columns
in a table is called Constraints.
A database program uses constraints to enforce limitation
on the data entered in tables. If a constrained is violated,
the command that cause the violation is terminated.
The constraints can be defined at the time of creation of a
table or later.
The constraints can be applied at the following levels:
Constraints cont..
The constraints can be applied at the following levels:
Column Level Constraint: A constraint is called
column-level constraint if it is defined with column
definition.
Table level Constraint: A constraint is called table- level
constraint if it is not defined with a specific column
definition. This constraint is applied to the whole table.
Types of Constraints
Different constraints in SQL is as follows:
NULL/NOT NULL
UNIQUE
PRIMARY KEY
FOREGIN KEY
CHECK Constraint
NULL/NOT NULL
It determine whether a column can be blank or not.
The word NOT NULL means that a column cannot have
a null value. User has to supply some value in the column.
A NULL value is not same as 0, blank, zero-length string
or empty string.
NULL means that no entry has been made. It can only be
applied at column level.
Example
CREATE TABLE EMP
(EMPNO NUMBER(4) NOT NULL,
ENAME VARCHAR2(30) NOT NULL,
JOB VARCHAR2(15),
MGR NUMBER(4),
SAL NUMBER(7,2),
DEPTNO NUMBER(4) NOT NULL);
UNIQUE
A UNIQUE constraint specifies that all values in a given
column must be unique. The column must have different
value in every row of the table.
It can be used to ensure that duplicate values are not
entered in a column. It can be applied at column level or
table level.
Example 1 (Column - Level)
CREATE TABLE DEPT
(DEPTNO NUMBER(5) CONSTRAINT enum_ukey
UNIQUE,
DNAME VARCHAR2(14),
LOC VARCHAR2(13));
Example 2 (Table - Level)
CREATE TABLE DEPT
(DEPTNO NUMBER(5),
DNAME VARCHAR2(14),
LOC VARCHAR2(13), CONSTRAINT enum_ukey
UNIQUE (DEPTNO));
Primary Key
It is used to define a column or set of columns as primary
key.
It restricts duplication of rows and dose not allow NULL
values.
A primary key that consist of two or more columns is
called composite key.
A table can have only one primary key.
Primary key constraint can be defined at table level or the
column level.
Example 1(Column - Level)
The following example creates a primary key at column
level.
CREATE TABLE DEPT
(DEPTNO NUMBER(5) PRIMARY KEY,
DNAME VARCHAR2(14) CONSTRAINT enum_ukey
UNIQUE,
LOC VARCHAR2(13));
Example 2 (Table - Level)
The following example creates a primary key at table level.
CREATE TABLE DEPT
(DEPTNO NUMBER(5),
DNAME VARCHAR2(14),
LOC VARCHAR2(13),
CONSTRAINT dept_uk UNIQUE (DNAME),
CONSTRAINT enum_pkey PRIMARY KEY (DEPTNO));
Foreign Key
It is used to define a column or set of columns as foreign
key.
Foreign key is used to create a link between the data in
two table.
The link created between two tables is based upon
common filed.
This filed is a PRIMARY key in the parent table and
FOREGIN key in child table.
A table can have multiple foreign keys.
Foreign Key
It is not necessary to link a FOREIGN key constraint only to a
primary key in another table. It may also refer to any column
with UNIQUE constraint in the table.
FOREIGN KEY: is used to define a foreign key in the child
table.
REFERENCES: identifies the table and column in parent table.
ON DELETE CASCADE: specifies that when the row in parent
table is deleted, the corresponding row in the child table will
also be deleted. If this option is not used, the rows in the parent
table cannot be deleted if it referenced in the child table.
Example 1
CREATE TABLE EMP
( EMPNO NUMBER(4) NOT NULL,
ENAME VARCHAR2(30) NOT NULL,
JOB VARCHAR2(15),
HIREDATE DATE,
MGR NUMBER(4),
SAL NUMBER(7,2),
COMM NUMBER(7,2),
DEPTNO NUMBER(4) REFERENCES DEPT );
Example 1
CREATE TABLE EMP
( EMPNO NUMBER(4),
ENAME VARCHAR2(30),
JOB VARCHAR2(15),
HIREDATE DATE,
MGR NUMBER(4),
SAL NUMBER(7,2),
COMM NUMBER(7,2),
DEPTNO NUMBER(4), CONSTRAINT EMP_DEP_FK
FOREIGN KEY (DEPTNO) REFERENCES DEPT(DEPTNO));
The above example shows two ways of creating foreign key. In
both ways, DEPTNO column of EMP table references DEPTNO
column of DEPT table.
CHECK Constraint
The CHECK constraint is used to apply a test on data
values in a column. It enforces domain integrity by
limiting the values stored in column.
Example 1
CREATE TABLE EMP
( EMPNO NUMBER(10),
ENAME CHAR(20),
DEPTNO NUMBER(2),
SALARY NUMBER(4) CHECK (SALARY<100000) );
Adding and Dropping a
Constraint
SQL also provides the facility to add, drop, enable or
disable an existing constraint. But it is not possible to
modify an existing constraint. The syntax is as follow:
ALTER TABLE table_name ADD [constarint name] type
(column);
Example 1
The following example creates a foreign key constraint on
EMP table. The constraint ensures that manager exist as
valid employee in EMP table.
ALTER TABLE EMP ADD CONSTRAINT EMP_FK FOREIGN
KEY
(MGR) REFERENECES EMP(EMPNO);
Example 2
The following example removes the primary key constraint
on DEPT table and drops the associated FOREIGN KEY
constraints on EMP.DEPT column.
ALTER TABLE DEPT DROP PRIMARY KEY CASCADE;
Example 3
The following example deletes the manager constraint from
EMP table.
ALTER TABLE EMP DROP CONSTRAINT EMP_FK;
Practical
Practice of constraint queries using Microsoft
SQL Server.