2.6_ic_constraints
2.6_ic_constraints
By
Mr. Parag R. Sali
Lecturer
Department of Computer Technology
SNJB’s Shri. Hiralal Hastimal ( Jain Brothers)
Polytechnic, Chandwad
Program Name: Computer Engineering Group
Program Code : CO/CM/CW
Semester : Third
Course Title : Database Management System
Course Code : 22319
Integrity Constraints
DATA INTEGRITY CONSTRAINT
Types of Data Integrity Constraint
I/O Constraints :
Primary key
Foreign Key
Unique Key
Check Constraint
I/O CONSTRAINTS
PRIMARY KEY CONSTRAINTS
The PRIMARY KEY constraint uniquely identifies each
record in a table.
Primary keys must contain UNIQUE values, and
cannot contain NULL values.
A table can have only ONE primary key; and in the
table, this primary key can consist of single or multiple
columns
Primary key on CREATE TABLE
Create table student (id number Primary key, name varchar(20),marks number);
Create table emp (id number, name varchar(10),address varchar(10), primary
key(id));
OR
Create table student (id number, name varchar(20),marks number, Primary key
(id));
o "Person_ID" column in the "Orders" table points to the "Person_ID" column in the "Persons" table.
o The "Person_ID" column in the "Persons" table is the PRIMARY KEY in the "Persons" table.
o The "Person_ID" column in the "Orders" table is a FOREIGN KEY in the "Orders" table.
o The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables.
o The FOREIGN KEY constraint also prevents 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.
Foreign key on Create table
CREATE TABLE Orders (Order_ID int PRIMARY KEY,
Order_Number int ,PersonI_D
int FOREIGN KEY REFERENCES Persons (Person_ID)
);
OR
Create table Orders (OrderId int, OrderNumber int, Person_id int, primary key
(OrderId), foreign key(PersonId) references Persons (Person_ID);
OR
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int CHECK (Age>=18)
);
CHECK
ALTER TABLE Persons
ADD CHECK (Age>=18);
OR
ALTER TABLE Persons
ADD CONSTRAINT C1 CHECK (Age>=18 );
Drop a CHECK Constraint
ALTER TABLE Persons
DROP CONSTRAINT C1;
OR
ALTER TABLE Persons
DROP CHECK C1;
Integrity Constraint :
An Integrity Constraint is a mechanism used to prevent invalid
data entry into the table.