Experiment No.
AIM: To implement Domain and entity integrity constraints and referential integrity
constraints on a database.
THEORY :
The types of the integrity constraints are,
[A] Domain Integrity Constraints
It is used to maintain value according to the user specification.
There are basically two types of domain integrity constraints
i. Not Null Constraint
ii. Check Constraint
Not Null Constraint
By default all columns allows null values . When a not null constraint is enforced
on a column or a set of columns in a table , it will not allow null values.
Note:- Constraints can be created at a time of table creation or after the table has
been created.
At a time of table creation :-
Syntax Example
Create table <table_name> CREATE TABLE customer_details
(column_name1 datatype(size), (
column_name2 datatype(size), customer_id character varying(255) NOT
: NULL,
: customer_name character varying(255)
,column_name n datatype(size)); NOT NULL,
quantity integer NOT NULL,
date_purchased date
);
INSERT INTO customer_details(
customer_id, customer_name, quantity,
date_purchased)
VALUES ('US1002','Kabir Khan',NULL,
2019-12-31);
After the table creation :-
Syntax Example
Alter table <table_name> Modify ALTER TABLE Persons MODIFY
<column_name> not null ; COLUMN customer_name not null;
Constraint Guidelines :
1. Constraints are easy to reference , if you give them a meaningful name.
2. If you do not name your constraint , Oracle generates a name with the format
Sys_Cn where n is integer to create unique constraint name.
3. Zero and Null are not equivalent.
Restrictions :
1. “Not Null” integrity constraint can’t be defined using alter table command when a
table contains Null values.
Check Constraints
The check constraint defines a condition that each row must satisfy. A single column can
have multiple check constraints that reference the column in it’s definition.
Syntax Example
At the time of table creation :-
Create table <table_name> CREATE TABLE Persons (
(column_name1 datatype(size) ID int NOT NULL,
constraint <constraint_name> LastName varchar(255) NOT NULL,
check <condition>, FirstName varchar(255),
column_name2 datatype(size), Age int,
CHECK (Age>=18)
:
);
:
,column_namen datatype(size));
After table creation :-
alter table <table_name>add constraint ALTER TABLE Persons ADD
<constraint_ name> check<condition> ; CONSTRAINT CHK_PersonAge
CHECK (Age>=18 AND City='Sandnes');
Table level and column level constraint
ColumnReferences a single column and is defined within a specification .It can be
imposed only on the column on which it is defined ; can define any type of integrity
constraint. TableReferences one or more columns and is defined separately from the
definition of the column in the table . It can impose rules on any columns in the table can
define any constraints except Not Null.
[B] Entity Integrity Constraints
An entity is an data recorded in an database .Each database represents a table and each
row of a table represents an instance of that entity.
There are two types of entity integrity constraints.
i. Unique constraints
ii. Primary key constraints
Unique Constraints
It is used to prevent the duplication of values within the row of a specified column or a
set of columns in a table. Columns defined with this constraint can also allow null
values.
Syntax Example
Create table <table_name> column level constraint
(column_name1 datatype(size)
constraint <constraint_name> create table dept ( deptno number(5)
unique , constraint dept_deptno_uk unique,
column_name2 datatype(size), dname varchar2(20),
loc varchar2(20));
:
:
Table level constraint
,column_namen datatype(size));
create table dept (deptno number(5),
dname varchar2(20),loc
varchar2(20),constraint dept_dname_uk
unique(dname));
Composite Unique Key
If the unique constraint is defined in more than one column i.e. combination of columns ,
it is said to be composite unique key.
create table dept (deptno number(5),
dname varchar2(20),
loc varchar2(20),
constraint dept_compkey unique (deptno,dname));
Constraint Guideline :
1. UNIQUE key constraints can be defined at column or table level definition.
2. Using the table level definition creates a composite unique key.
3. Maximum combination of columns that a composite unique key contain is 16.
Primary Key Constraints
The primary key constraint avoids duplication of rows and does not allow null values ,
when
enforced on a column or a set of columns .If a primary key constraint is assigned to the
combination of columns , then it is said to be a composite primary key.
Syntax Example
At the time of table creation :-
Create table <table_name> column level constraint
(column_name1 datatype(size)
constraint <constraint_name> create table dept ( deptno number(5)
primary key , constraint dept_deptno_pk primary
column_name2 datatype(size), key,dname varchar2(20),
loc varchar2(20));
:
: Table level constraint
,column_namen datatype(size));
create table dept (deptno
number(5),dname varchar2(20),
loc varchar2(20),constraint
dept_deptno_pk primary key(deptno));
After table creation :-
alter table <table_name> add constraint alter table emp add constraint
<constraint_ name> primary key emp_empno_pk primary key(empno) ;
(column_name);
Guidelines :
1. Primary key can be defined at the column or table level.
2. A composite primary key is created by using the table level definition (syntax is
same as of unique constraint)
3. A table can have only one primary key.
4. Primary key constraint can not be defined in an alter table command when a table
contains rows having null values.
[C] Referential Integrity Constraints
It is used to establish a parent- child relationship between two tables having a common
column name. To implement this , we should define the column in the child table as a
foreign key referring to the corresponding parent key.
Guidelines :
1. we can establish a relationship between a primary key or a unique key in the
same table or a different table
2. Foreign key constraint can be defined at the column or table constraint level.
3. A composite foreign key is creating by using the table level definition.
Syntax Example
At the time of table creation :-
column level constraint
create table emp(empno number(5), ename
varchar2(25) not null,job varchar2(15), mgr
number(5), joindate date, salary number(7,2),
comm number(7,2), deptno number(5) constraint
emp_deptno_fk references dept(deptno));
Table Level constraint
create table emp(empno number(5), ename
varchar2(25) not null, job varchar2(15), mgr
number(5), joindate date, salary
number(7,2),comm number(7,2), deptno
number(5) not null, constraint emp_deptno_fk
foreign key (deptno) references dept(deptno));
After table creation :-
alter table emp
add constraint emp_deptno_fk foreign key
(deptno) references dept (deptno);
CONCLUSION: Thus we have successfully completed practical on constraints.