Unit 3 Integrity Constraints
Unit 3 Integrity Constraints
UNIT-3
Integrity Constraints
Domain Constraints
Data constraints are rules created for entering and preventing users from entering invalid data into
tables.
Oracle permits data constraints to be attached to table columns via SQL sentence that will check
data for integrity. Once data constraints are attached, oracle engine will check data being entered into a
table against the data constraints.
If the data is valid, it is stored in table, else data is rejected.
Even if the single column of the record being entered into the table fails constraints the entire
record is rejected and not stored in the table.
The data constraints can be attached to the column using create table and alter table command.
There are two types of data constraints-
I/O constraints
Business rule constraints
I/O Constraints:
I/O constraints are divided into following different categories.
a) Primary Key:
The primary key data constraints attached to the column ensures that the data entered in the table
column is unique across the entire column and none of the cell belonging to the column are left empty.
b) Foreign Key:
This constraints establishes the relationship between records across the master and details table (parent
and child). It ensures that records can not be inserted into detail or child table if corresponding record in
the master or parent table does not exists and records of master table can not be deleted if
corresponding records in the detail table exists.
In addition to primary key and foreign key Oracle has not null and unique as I/O constraints.
Business Rule Constraints:
Business rule can be implemented into Oracle table by check constraints business rule
validation checks are perform a right operation on the table i.e. insert / update.
Any insert / update statement causes the relevant constraints to be evaluated. The
constraints must be satisfied for the statement to be executed.
SQL Constraints
• SQL constraints are used to specify rules for the data in a table.
• Constraints are used to limit the type of data that can go into a table. This ensures the accuracy and
reliability of the data in the table. If there is any violation between the constraint and the data action,
the action is aborted.
• Constraints can be column level or table level. Column level constraints apply to a column, and table
level constraints apply to the whole table.
The following constraints are commonly used in SQL:
Dayanand Science College Latur.
CLASS: BSC TY Sub: RDBMS Prepared by : Dr. R. B. Shinde
The following SQL creates a PRIMARY KEY on the "ID" column when the "Persons" table is
created:
CREATE TABLE Persons (
ID int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);
DROP a PRIMARY KEY Constraint
To drop a PRIMARY KEY constraint, use the following SQL:
ALTER TABLE Persons
DROP CONSTRAINT PK_Person
Not null constraints:
When column is defined as not null then that column becomes compulsory column. It implies
that a value must be inserted into the column if record is accepted in the table.
By default, a column can hold NULL values.
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.
Syntax : column_name data type (size) Not null)
e.g. create table student (Roll.no number (4), name varchar2(20) Not null, Address
carchar2(20));
Following is the example of Primary KEY, not NULL AND default:
In the following example you can observe that while inserting the values in table we are not
assigning a value to city. Which is by default we set while creating a table so that default value
will be displayed in city column. But if any one want to add another city name in that case he
can insert the name of another
city.
Dayanand Science College Latur.
CLASS: BSC TY Sub: RDBMS Prepared by : Dr. R. B. Shinde
Unique Key
Unique constraints applied on a column when we want to store the unique value i.e. duplicate values
are eliminated in column. A table can have more than one unique key which is not possible in primary
key unique can combine upto 16 columns in a composite unique key.
Syntax :
e.g. create table student (Roll_no number(4) unique, name varchar (10), Address varchar2(10));
In above example we create a table stu2 and assign a unique constraint to SID column. As we insert the
value into STU2 table notice that if we enter the same Number in SID field then it gives the error
“Unique constraint violated”. Unique constraints ensures that duplicate value does not entered into
table while inserting values in that table.
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 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.
In the given example while creating table STU1 CHECK constraint is applied on ID column. Check
constraint checks ID number should always greater than 0.
Dayanand Science College Latur.
CLASS: BSC TY Sub: RDBMS Prepared by : Dr. R. B. Shinde
If we insert the value 0 in ID column then error occurs that “Check Constraint is violated.”
Foreign Key:
Unique constraints applied on a column when we want to store the unique value i.e. duplicate
values are eliminated in column. A table can have more than one unique key which is not
possible in primary key unique can combine upto 16 columns in a composite unique key.
It rejects of value if corresponding value does not currently exists in the master key table.
A foreign key must refer a primary key or unique key column from primary table. It will
reference to the primary key master table, if no column/group of column specified when
creating a foreign key. It requires that foreign key column have matching data type.
A FOREIGN KEY is a key used to link two tables together.
A FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY KEY in
another table.
The table containing the foreign key is called the child table, and the table containing the
candidate key is called the referenced or parent table.
Look at the following two tables:
SQL>create table main (Roll no number(3) primary key, name varchar(10), Address varchar (20));
Create table child(Roll number(3) references main, Ph_no number(10), city varchar(20));
4 RAJESH 1 20 1
3 19 3
Table Name: STU12
4 18 4
ROLNO MARKS ID
Note that ID column in STU12 table points to the ID column of STU1 table.
ID column in the STU1 table is the PRIMARY KEY
ID column in the STU12 table is a FOREIGN KEY.
The FOREIGN KEY constraint also prevents that invalid data from being inserted into the foreign
key column, because it has to be one of the values contained in the table it points to.
Insert the values into STU1 table and display all records.
Dayanand Science College Latur.
CLASS: BSC TY Sub: RDBMS Prepared by : Dr. R. B. Shinde
Now insert the values into STU12 table but note that the ID no present in Master table for
only those ID numbers we can insert the values in child table.
If we are trying to insert the values for rol no 2 which is not present in master table it will gives error
“Integrity constraint is violated by the user and specified parent key is not found in the master table”.