You are on page 1of 7

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION o 13

Relational Database Management Systems (9038) Experiment No. 3


Experiment No. 3
1.0 TITLE :
To implement Integrity Constraints on Database object table.
2.0 PRIOR CONCEPTS :
Data Definition Language (DDL).
3.0 NEW CONCEPTS :
Proposition 1 : Integrity Constraint :
An Integrity Constraint is a mechanism used to prevent invalid data entry into the table.
Proposition 2 : Foreign Key :
Foreign key represents relationship between tables. A foreign key is a column (or a group of columns)
whose values are derived from the primary key of other table.
Proposition 3 : Referenced key :
It is a unique or primary key which is defined on a column belonging to the parent table.
Proposition 4 : Child table :
This table depends upon the values present in the referenced key of the parent table, which is
referred by a foreign key.
Proposition 5 : Parent table :
This table determines whether insertion or updation of data can be done in child table. This table
would be referred by childs table foreign key.
Proposition 6 : NULL value :
When an entity does not have a value for an attribute or an attribute value is unknown or attribute
value is missing, then it is said attribute value is NULL.
To impose Domain, Entity and Referential integrity constraints on database object Table.
Types of Integrity Constraints :
Domain Integrity Constraints : NOT NULL, CHECK
It is used to maintain value according to the user specifications.
Entity Integrity Constraints : UNIQUE KEY, PRIMARY KEY.
An entity is any data recorded in a database. Each database represents a table and each
row of a table represents an instance of that entity.
Referential Integrity Constraints : REFERENCE KEY, FOREIGN KEY.
It is used to establish a parent-child relationship between two tables having a common
column.
On delete cascade :
It indicates that when the row in the parent table is deleted, the dependent rows in the child
table will also be deleted.
14 o MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
Experiment No. 3 Relational Database Management Systems (9038)
Not null constraint Check constraint
By default all columns in a table allow Null The Check Constraint defines a condition that
values. When a Not Null constraint is each row must satisfy. A single column can
enforced on a column or a set of columns in have multiple check constraints that
a table, it will not allow null values. reference the column in its definition.
Syntax : Syntax :
Case-A : At the time of table creation Case-A : At the time of table creation
create table <table_name> create table <table_name>
(column_name1 datatype(size), (column_name1 datatype(size)
column_name2 datatype(size) not null, constraint <constraint_name>
: check <condition>,
: .
column_name n datatype(size)); .
.
column_name n datatype(size)) ;
Case-B : After table creation Case-B : After table creation
alter table <table_name> alter table <table_name>
modify <column_name> not null; add constraint <constraint_name>
check<condition>;
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 an integer to create
a unique constraint name.
3. Constraints can be created at the time
of table creation or after the table has
been created.
4. Zero & Null are not equivalent.
Restriction :
Not Null integrity constraint can be defined
using alter table command even when the
table contains rows.
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION o 15
Relational Database Management Systems (9038) Experiment No. 3
Primary Key constraint Unique constraint
The Primary key constraint avoids duplication It is used to prevent the duplication of values
of rows and does not allow null values, when within the rows of a specified column or set of
enforced on a column or set of columns. If a columns in a table. Columns defined with this
primary key constraint is assigned to a constraint can also allow null values.
combination of columns, then it is said to be If unique constraint is defined in more than
a composite primary key. one column i.e. combination of columns, it is
said to be composite unique key.
Syntax : Syntax :
Case-A : At the time of table creation Case-A : At the time of table creation
Syntax-1 : Column level Syntax-1 : Column level
create table <table_name> create table <table_name>
(column_name1 datatype(size) (column_name1 datatype(size),
constraint <constraint_name> primary key, column_name2 datatype(size)
column_name2 datatype(size), constraint <constraint _name> unique,
: :
: :
column_name n datatype(size)) ; column_name n datatype(size));
Syntax-2 : Table level Syntax-2 : Table level
create table <table_name> create table <table_name>
(column_name1 datatype(size), (column_name1 datatype(size),
column_name2 datatype(size), column_name2 datatype(size),
: :
: :
column_name n datatype(size), column_name n datatype(size),
constraint <constraint_name> constraint <constraint _name>
primary key (column name)); unique (column name));
Case-B : After table creation Case-B : After table creation
alter table <table_name> alter table <table_name>
add constraint <constraint_name> add constraint <constraint_name>
primary key (column_name); unique (column_name);
Constraint Guidelines : Constraint Guidelines :
1. Primary key can be defined at the 1. UNIQUE key constraints can be defined
column level or table level. at the column or table level definition.
2. A composite primary key is created by 2. Using the table level definition creates a
using the table level definition (syntax is composite unique key.
same as of unique key constraint). 3. Maximum combination of columns that
3. A table can have only one primary key. a composite unique key can contain is 16.
4. Primary key constraint cannot be defined
in an alter table command when the
table contains rows having null values.
16 o MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
Experiment No. 3 Relational Database Management Systems (9038)
References Foreign Key
It is a unique or primary key which is defined Foreign key represents relationship between
on a column belonging to the parent table. tables. A foreign key is a column (or a group
of columns) whose values are derived from
the primary key of other table.
Syntax : Syntax :
At the time of table creation Syntax-1 : At the time of table creation
Column level create table <table_name>
create table <table_name> (column_name1 datatype(size),
(column_name1 datatype(size), column_name2 datatype(size),
column_name2 datatype(size) :
constraint <constraint_name> :
references <parent_table_name> column_name n datatype(size),
(parent_table_column_name) constraint <constraint_name>
[on delete cascade], references <parent_table_name>
: (parent_table_column_name)
: [on delete cascade]);
:
column_name n datatype(size));
Syntax-2 : After table creation
alter table <table_name>
add constraint <constraint_name>
foreign key <child_table_column_name>
references <parent_table_name>
(parent_table_column_name)
[on delete cascade];
Guidelines :
1. 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.
4. When a relation is created between two tables, it is permissible to delete records in a child
table. But a vice-versa operation i.e. deleting records from a table when it references child
table is not allowed.
5. Rejects an insert or update of a value, if a corresponding value does not currently exist in the
primary key table.
6. On delete cascade allows automatic deletion of child records when a parent record is deleted.
Proposition 7 : Dropping integrity constraints :
Dropping a constraint will allow future insertions or other manipulations without affecting existing
data in the table.
Syntax :
alter table <table_name> drop constraint <constraint_name>;
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION o 17
Relational Database Management Systems (9038) Experiment No. 3
Concept Structure :
4.0 LEARNING OBJECTIVES :
Intellectual Skills :
1. To Implement different integrity constraints on table.
2. To provide accurate and valid data in tables.
3. To understand the concept of primary key, unique key and foreign key.
Motor Skills :
1. Ability to handle keyboard and mouse.
2. Ability to work with editor.
5.0 SAMPLE EXAMPLES :
5.1 Domain Integrity Constraint :
Alter Table Student_Info
Add Constraint Cn1
Check (activity IN (Project,Cultural,Sports));
Output : (Students are required to write the output)
5.2 Entity Integrity Constraint :
5.2.1 UNIQUE Key Constraint :
Alter Table Branch_Details
Add Constraint Branch_PK
UNIQUE(Branch_Name);
Output : (Students are required to write the output)
5.2.2 PRIMARY Key Constraint :
Alter Table Branch_Details
Add Constraint Branch_PK_Code
PRIMARY KEY(Branch_Code);
Output : (Students are required to write the output)
18 o MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
Experiment No. 3 Relational Database Management Systems (9038)
Alter Table Stud_Info
Add Constraint Stud_PK
PRIMARY KEY(Stud_Id);
Output : (Students are required to write the output)
5.3 Referential Integrity Constraint :
Alter Table Stud_Info
Add Constraint Stud_FK Foreign Key(Branch_code)
References Branch_details(Branch_code) on delete cascade;
Output : (Students are required to write the output)
6.0 QUESTIONS :
1. Impose Primary Key Constraint on Item_Code attribute of table Lab_Data, which is
already created in previous experiment.
2. Impose Unique Key Constraint on Title attribute of Book_Details, which is already
created in previous experiment.
3. Create a table Train_Details with the following attribute constraints :
Train_No as Primary Key, Train_Name should not be NULL.
Column Data Type
Train_No Number(5)
Train_Name Varchar2(15)
4. Create a table Passenger_details with the following attribute constraints :
PNR_No as Primary Key, Train_No as Foreign Key,
Train_Name as NOT NULL.
Column Data Type
PNR_No Number(5)
Train_No Number(5)
Train_Name Varchar2(15)
Boarding Varchar2(12)
Destination Varchar2(12)
(Space for Answers)
(Students are required to write the queries and output.)
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION o 19
Relational Database Management Systems (9038) Experiment No. 3
Space for Answers
Signature of Teacher

You might also like