0% found this document useful (0 votes)
3 views

2.6_ic_constraints

Uploaded by

omeshshewale965
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

2.6_ic_constraints

Uploaded by

omeshshewale965
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Database Management System

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

 Business Rule Constraints :


 Null
 Not Null

 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));

 Primary key on ALTER TABLE


 Alter table stud add primary key(r_n);
OR
 Alter table table_name add constraint constraint_name primary key(field);
 Alter table stud add constraint c1 primary key(r_n);

 Drop a Primary key Constraints


 Alter table stud drop primary key;
OR
FOREIGN KEY CONSTRAINTS
 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.
Orders table:
Persons table:

Order_ID OrderNumber Person_ID


Person_ID Name Age
1 77895 3
1 Jay 30
2 44678 3
2 Ajay 23
3 Priti 20
3 22456 2
4 24562 1

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);

 Foreign key on Alter table


 Alter table Orders add foreign key(PersonId) references Persons (PersonID);
OR
 Alter table Orders add constraint f1 foreign key(PersonId) references Persons
(PersonID);

 Drop a Foreign key constraint


 Alter table Orders drop foreign key f1;
OR
 Alter table Orders drop constraint f1;
UNIQUE KEY CONSTRAINTS
 The UNIQUE constraint ensures that all values in a
column are different.
 Both the UNIQUE and PRIMARY KEY constraints
provide a guarantee for uniqueness for a column or set
of columns.
 A PRIMARY KEY constraint automatically has a
UNIQUE constraint.
 However, you can have many UNIQUE constraints per
table, but only one PRIMARY KEY constraint per
table.
 Unique key on CREATE TABLE
 Create table Student (r_n number unique, name varchar(20)
unique, marks number);
OR
 Create table student (r_n number, name varchar(20), marks
number, Unique (r_n,name));

 Unique key on ALTER TABLE


 Alter table student add Unique (r_n);
OR
 Alter table student add constraint u1 unique (r_n);

 DROP a Unique key Constraints


 Alter table student drop unique key;
OR
 Alter table student drop constraint u1;
BUSINESS RULE CONSTRAINTS
NULL - NOT NULL CONSTRAINTS
 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.
The following SQL ensures that the "ID", "LastName", and
"FirstName" columns will NOT accept NULL values when the
"Persons" table is created:
 NOT NULL on Create Table
 CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);

 NOT NULL on Alter Tabel


 ALTER TABLE Persons
MODIFY Age int NOT NULL;
CHECK CONSTRAINTS
 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.

 The following SQL creates a CHECK constraint on the "Age" column. The CHECK
constraint ensures that the age of a person must be 18, or older:
 CHECK on Create Table
 CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int ,
CHECK (Age>=18)
);

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.

You might also like