0% found this document useful (0 votes)
126 views7 pages

Practical No 8: Aim: Theory

The document discusses different types of constraints in databases including entity integrity, referential integrity, and domain constraints. It provides examples of creating tables with primary keys, foreign keys, unique constraints, and check constraints. It also demonstrates updating data and different cascading options for foreign key constraints including on delete cascade, on delete set null, on update cascade, and on update set null.

Uploaded by

Abhishek Damani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views7 pages

Practical No 8: Aim: Theory

The document discusses different types of constraints in databases including entity integrity, referential integrity, and domain constraints. It provides examples of creating tables with primary keys, foreign keys, unique constraints, and check constraints. It also demonstrates updating data and different cascading options for foreign key constraints including on delete cascade, on delete set null, on update cascade, and on update set null.

Uploaded by

Abhishek Damani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

PRACTICAL NO 8

Aim: To perform data update and data integrity.


Theory:
Constraints:
There are three types of constraints:
1) Entity integrity :pk, unique
2) Referential : fk
3) Domain: not null, check
1) pk: Primary key avoids redundancy as it doesn’t allow same values to be
printed more than once.
2) unique: each an every entity as a unique id in the institute, no other
entity can have the same id i.e. it cannot be repeated.
3)fk: It establishes a parent child relationship between two tables
referencing the primary key of the parent table.
4)check: in the table we check whether the condition is satisfied by the
entity or not.
5)not null: in the admin table we are keeping the admin username to be
not null as this field cannot be null if an admin is present.

Primary Key
CREATE TABLE train01 (

t_id int PRIMARY KEY,

name varchar NOT NULL,

from_place varchar NOT NULL,

to_place varchar NOT NULL,

arriving_time time NOT NULL,

departure_time time NOT NULL,

day varchar NOT NULL

);

copy train01 from 'D:\College Stuff\DBMS\DBMS(PRAC-Files)\Train1.csv' with (format csv);

select * from train01


Foreign Key
CREATE TABLE passenger01 (

id1 int Primary key,

name varchar NOT NULL,

from_place varchar NOT NULL,

to_place varchar NOT NULL,

train_id int NOT NULL,

date1 date NOT NULL,

day varchar NOT NULL,

foreign key (train_id) references train01(t_id)

);

copy passenger01 from 'D:\College Stuff\DBMS\DBMS(PRAC-Files)\Passenger1.csv' with (format


csv);

select * from passenger01

UNIQUE
CREATE TABLE passenger02 (

id1 int Primary key,

name varchar NOT NULL,

from_place varchar NOT NULL,

to_place varchar unique,

train_id int NOT NULL,

date1 date NOT NULL,


day varchar NOT NULL,

foreign key (train_id) references train01(t_id)

);

copy passenger02 from 'D:\College Stuff\DBMS\DBMS(PRAC-Files)\Passenger2.csv' with (format


csv);

select * from passenger02

Update
CREATE TABLE payment (

id int NOT NULL,

clerk_id int NOT NULL,

amount float NOT NULL

);

copy payment from 'D:\College Stuff\DBMS\DBMS(PRAC-Files)\ payment1.csv' with (format csv);

select * from payment

update payment set amount=amount*1.05;

Check Constraint
CREATE TABLE payment01 (

p_id int,

clerk_id int,

amount int CHECK(amount > 1000)

);
copy payment01 from 'D:\College Stuff\DBMS\DBMS(PRAC-Files)\Payment1.csv' with (format csv);

select * from payment01

On Delete Cascade
create table train001(

t_id varchar primary key,

t_name varchar)

insert into train001 values

('100','ABC'),

('101','ASD'),

('102','AFG'),

('103','AHJ')

create table ticket001(

train_id varchar,

p_name varchar,

seat_no varchar,

foreign key (train_id) references train001(t_id) on delete cascade )

insert into ticket001 values

('100','QWE','72')

Select * from ticket001

delete from train001 where t_id='100'

create table train001(

t_id varchar primary key,

t_name varchar)

insert into train001 values


('100','ABC'),('101','ASD'),('102','AFG'),('103','AHJ')

create table ticket001(

train_id varchar,

p_name varchar,

seat_no varchar,

foreign key (train_id) references train001(t_id) on delete set NULL )

insert into ticket001 values

('100','QWE','72')

Select * from ticket001

delete from train001 where t_id='100'

Violations

On Update Cascade
create table train001(

t_id varchar primary key,

t_name varchar)

insert into train001 values

('100','ABC'),('101','ASD'),('102','AFG'),('103','AHJ')

select * from train001

create table ticket001(

train_id varchar,
p_name varchar,

seat_no varchar,

foreign key (train_id) references train001(t_id) on Update Cascade)

insert into ticket001 values

('100','QWE','72'),('101','ZXC','36'),('102','VBN','25')

select * from ticket001

update train001 set t_id='1200' where t_id='102'

On Update Set NULL


create table train001(

t_id varchar primary key,

t_name varchar)

insert into train001 values

('100','ABC'),('101','ASD'),('102','AFG'),

('103','AHJ')

select * from train001

create table ticket001(

train_id varchar,

p_name varchar,

seat_no varchar,
foreign key (train_id) references train001(t_id) on Update set NUll)

insert into ticket001 values

('100','QWE','72'),

('101','ZXC','36'),

('102','VBN','25')

select * from ticket001

update train001 set t_id='1001' where t_id='101'

You might also like