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

Rdbms Slip Solutions

The document contains SQL commands for creating and managing tables related to a banking system, including branches, customers, loans, and relationships between them. It also includes the creation of views, triggers, and functions to enforce business rules and retrieve specific data. Additionally, it outlines the structure for a transportation system with tables for routes, buses, drivers, and their relationships.

Uploaded by

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

Rdbms Slip Solutions

The document contains SQL commands for creating and managing tables related to a banking system, including branches, customers, loans, and relationships between them. It also includes the creation of views, triggers, and functions to enforce business rules and retrieve specific data. Additionally, it outlines the structure for a transportation system with tables for routes, buses, drivers, and their relationships.

Uploaded by

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

/SLIP 1,2,3

create table branch(bid int primary key,bname varchar(30),bcity varchar(15));


CREATE TABLE

sr=# create table customer(cno int primary key,cname varchar(20),caddress varchar(20),city


varchar(15));
CREATE TABLE

sr=# create table la(lno int primary key,l_amt_required int,l_amt_approved int,ldate date);
CREATE TABLE

sr=# create table ternary(bid int references branch(bid),cno int references customer(cno),lno int
references la(lno));
CREATE TABLE

***********
select * from branch;
bid | bname | bcity
-----+--------+---------
1 | pimpri | pune
2 | anudh | mumbai
3 | anudh | puna
4 | pimpri | manjari
5 | pimpri | swarget
(5 rows)

sr=# select * from customer;


cno | cname | caddress | city
-----+--------+------------+----------
100 | sakshi | rajuri | satara
200 | payal | mg road | pune
300 | sagar | fc road | pune
400 | disha | sb road | swarget
500 | anu | tilak road | baramati
(5 rows)

sr=# select * from la;


lno | l_amt_required | l_amt_approved | ldate
-----+----------------+----------------+------------
10 | 300000 | 500000 | 2023-05-10
20 | 100000 | 400000 | 2022-12-20
30 | 150000 | 600000 | 2021-07-16
40 | 400000 | 300000 | 2020-08-12
50 | 500000 | 700000 | 2019-10-23
(5 rows)

sr=# select * from ternary;


bid | cno | lno
-----+-----+-----
1 | 200 | 50
5 | 400 | 30
3 | 100 | 10
4 | 300 | 20
2 | 200 | 40
4 | 100 | 50
2 | 400 | 30
3 | 200 | 20
(8 rows)
***************************
//SLIP 1

Q1_A VIEW1

create or replace view sidhu as select customer.cname from customer,branch,ternary where


customer.cno=ternary.cno and branch.bid=ternary.bid and branch.bname='pimpri';
CREATE VIEW
sr=# select * from sidhu;
cname
--------
payal
disha
sagar
sakshi
(4 rows)
**************************

Q1_B VIEW2

create or replace view c2 as select customer.cname from customer,branch,la,ternary where


customer.cno=ternary.cno and branch.bid=ternary.bid and la.lno=ternary.lno and bcity='mumbai';
CREATE VIEW
sr=# select * from c2;
cname
-------
payal
disha
sagar
(3 rows)

**************************
Q2_A TRIGGER

create or replace function manuu() returns trigger as'


begin
if old.cno<>new.cno
then
raise exception ''you cannot change customer number'';
end if;
return new;
end;'
language 'plpgsql';

create trigger danu before update on customer for each row execute procedure manuu();

o/p:
update customer set cno=600 where cname ='sakshi';
ERROR: you cannot change customer number
************************

Q2_B function

create or replace function fd(bn varchar(20)) returns void as'

declare
rec record;
begin
for rec in select la.lno,l_amt_required,l_amt_approved,ldate from la,ternary,branch where
branch.bid=ternary.bid and la.lno=ternary.lno and branch.bname=bn
loop
raise notice''loan number :%'',rec.lno;
raise notice''l_amt_required:%'',rec.l_amt_required;
raise notice ''l_amt_approved:%'',rec.l_amt_required;
raise notice ''ldate:%'',rec.ldate;
end loop;
end;'
language 'plpgsql';
O/P:
select fd('pimpri');
NOTICE: loan number :50
NOTICE: l_amt_required:500000
NOTICE: l_amt_approved:500000
NOTICE: ldate:2019-10-23
NOTICE: loan number :30
NOTICE: l_amt_required:150000
NOTICE: l_amt_approved:150000
NOTICE: ldate:2021-07-16
************************

//SLIP 2

Q1_A VIEW1

create or replace view sona as select customer.cno,cname,caddress,city from


customer,la,ternary where la.lno=ternary.lno and customer.cno=ternary.cno and
l_amt_approved=500000;
CREATE VIEW
sr=# select * from sona;
cno | cname | caddress | city
-----+--------+----------+--------
100 | sakshi | rajuri | satara
(1 row)
**********************
Q1_B VIEW2

create or replace view s1 as select la.lno,l_amt_required,l_amt_approved,ldate from


la,ternary,branch where branch.bid=ternary.bid and la.lno=ternary.lno and bname='anudh';
CREATE VIEW
sr=# select * from s1;
lno | l_amt_required | l_amt_approved | ldate
-----+----------------+----------------+------------
10 | 300000 | 500000 | 2023-05-10
40 | 400000 | 300000 | 2020-08-12
30 | 150000 | 600000 | 2021-07-16
20 | 100000 | 400000 | 2022-12-20
20 | 100000 | 400000 | 2022-12-20
(5 rows)

*************************

Q2_A TRIGGER

create or replace function fs1() returns trigger as'


begin
if NEW.l_amt_approved >= NEW.l_amt_required
then
raise exception ''Invalid input'';
end if;
return new;
end;'
language 'plpgsql';

create trigger c3 before insert on la for each row execute procedure fs1();
O/P:
CREATE FUNCTION
CREATE TRIGGER
sr=# insert into la values(100, '1000' ,' 40000' , '17-2-2021');
ERROR: Invalid input
CONTEXT: PL/pgSQL function fs1() line 6 at RAISE
sr=# insert into la values(101, '100000' ,' 40000' , '17-2-2021');
INSERT 0 1
***************************

Q2_B FUNCTION

//NOT CONFIRMED//
//OUTPUT//

create or replace function a1(bn varchar(30)) returns int as'

declare
cnt int;
bid int;
begin
select count(customer.cno) into cnt from customer,branch,ternary where
customer.cno=ternary.cno and branch.bid=ternary.bid and bname=bn;

if bid IS NULL
then
raise notice ''valid branch name'';
return cnt;
else
raise notice''Invalid branch name'';

end if;

end;'
language 'plpgsql';
O/P:
CREATE FUNCTION
sr=# \! vi s2B.sql
sr=# select a1('anudh');
NOTICE: valid branch name
a1
----
5
(1 row)

***********************
//SLIP 3

Q1_A VIEW1

create or replace view n3 as select customer.cname from customer,ternary,la where


la.lno=ternary.lno and customer.cno=ternary.cno and l_amt_required > 200000;
CREATE VIEW
sr=# select * from n3;
cname
--------
payal
sakshi
payal
sakshi
(4 rows)
***********************

Q1_B VIEW2

create or replace view n5 as select branch.bname,customer.cname from


customer,branch,ternary where branch.bid =ternary.bid and customer.cno=ternary.cno;
CREATE VIEW
sr=# select * from n5;
bname | cname
--------+--------
pimpri | payal
pimpri | disha
anudh | sakshi
pimpri | sagar
anudh | payal
pimpri | sakshi
anudh | disha
anudh | payal
anudh | sagar
(9 rows)
************************

Q2_A TRIGGER

create or replace function da() returns trigger as'


begin
if new.cno<=0
then
raise exception''Invalid customer number'';

end if;
return new;
end;'
language 'plpgsql';

create trigger tk before insert on customer for each row execute procedure da();

o/p:
sr=# \i s3A.sql
CREATE FUNCTION
CREATE TRIGGER
sr=# insert into customer values(12,'sanika','rajuri','satara');
INSERT 0 1
sr=# insert into customer values(-1,'sanika','rajuri','satara');
ERROR: Invalid customer number
**************************

Q2_B CURSOR

create or replace function san() returns void as'

declare
rec record;

v1 cursor for select customer.cno,cname,caddress,city,la.l_amt_approved from


customer,la,ternary where customer.cno=ternary.cno and la.lno=ternary.lno;

begin

open v1;
loop
fetch v1 into rec;
exit when not found;
raise notice ''customer number: %'',rec.cno;
raise notice''customer name :%'',rec.cname;
raise notice ''customer address :%'',rec.caddress;
raise notice'' city:%'',rec.city;
end loop;
close v1;
end;'
language 'plpgsql';
O/P:
CREATE FUNCTION
sr=# select san();
NOTICE: customer number: 200
NOTICE: customer name :payal
NOTICE: customer address :mg road
NOTICE: city:pune
NOTICE: customer number: 400
NOTICE: customer name :disha
NOTICE: customer address :sb road
NOTICE: city:swarget
*******************************************************************************************************
/slip 4,5,6
create table route(route_no int primary key,source varchar(20),destination
varchar(20),no_of_startions int);
CREATE TABLE
###############
create table bus(bus_no int primary key,capacity int,depot_name varchar(20),route_no int
references route(route_no));
CREATE TABLE
#################
create table driver(driver_no int primary key,driver_name varchar(20),license_no int,address
varchar(20),age int,salary float);
CREATE TABLE
############
create table bd(bus_no int references bus(bus_no),driver_no int references
driver(driver_no),shift int check (shift in(1,2)),date_of_duty_alloted date);
CREATE TABLE
***************
sr=# select * from route;
route_no | source | destination | no_of_startions
----------+-------------+-------------+-----------------
1 | had | wagholi | 5
2 | shivajinagr | swarget | 4
3 | pune | indapur | 10
4 | bmt | pune | 8
5 | pune | Manpa | 2
(5 rows)

sr=# select * from bus;


bus_no | capacity | depot_name | route_no
--------+----------+------------+----------
101 | 30 | wagholi | 1
102 | 10 | hadpsar | 5
103 | 35 | bmt | 3
104 | 15 | manpa | 2
105 | 25 | swarget | 4
(5 rows)

sr=# select * from driver;


driver_no | driver_name | license_no | address | age | salary
-----------+-------------+------------+---------+-----+--------
10 | sagar | 5 | pune | 40 | 200000
20 | sakshi | 8 | mnpa | 50 | 80000
30 | prathmesh | 5 | mnpa | 35 | 90000
40 | anu | 8 | mnpa | 45 | 100000
50 | sidhhu | 9 | mnpa | 53 | 500000
(5 rows)

sr=# select * from bd;


bus_no | driver_no | shift | date_of_duty_alloted
--------+-----------+-------+----------------------
101 | 20 | 2 | 2004-05-10
102 | 40 | 1 | 2020-05-09
103 | 50 | 2 | 2023-08-08
104 | 20 | 1 | 2023-08-08
102 | 10 | 2 | 2023-05-17
(5 rows)
***************
slip 4
Q1_A view
create or replace view vss as select
driver.driver_no,driver_name,license_no,address,age,salary from driver,bd where
driver.driver_no=bd.driver_no and shift=1;
CREATE VIEW
sr=# select * from vss;
driver_no | driver_name | license_no | address | age | salary
-----------+-------------+------------+---------+-----+--------
20 | sakshi | 8 | mnpa | 50 | 80000
40 | anu | 8 | mnpa | 45 | 100000
(2 rows)
*********
Q_1 view B
sr=# create or replace view vn as select * from driver where salary > 20000;
CREATE VIEW
sr=# select * from vn;
driver_no | driver_name | license_no | address | age | salary
-----------+-------------+------------+---------+-----+--------
10 | sagar | 5 | pune | 40 | 200000
20 | sakshi | 8 | mnpa | 50 | 80000
30 | prathmesh | 5 | mnpa | 35 | 90000
40 | anu | 8 | mnpa | 45 | 100000
50 | sidhhu | 9 | mnpa | 53 | 500000
(5 rows)
***************
Q1_a trigger
reate or replace function rrt() returns trigger as'
declare
begin
if new.age between 18 and 35
then
raise notice''valid input'';
return new;
else
raise notice''Invalid input'';
end if;
end;'
language 'plpgsql';

create trigger nns before insert on driver for each row execute procedure rrt();
o/p:
insert into driver values(11,'sanika',4,'pune',35,'20000');
NOTICE: valid input
INSERT 0 1
********
Q2 B function
create or replace function f34(rn int) returns void as'
declare
rec record;
begin
for rec in select bus.bus_no,capacity,depot_name from bus where route_no=rn
loop
raise notice ''%'',rec.bus_no;
raise notice''%'',rec.capacity;
raise notice''%'',rec.depot_name;
end loop;
end;'
language 'plpgsql';
o/p:
sr=# \! vi s4_B.sql
sr=# \i s4_B.sql
CREATE FUNCTION
sr=# select f34(5);
NOTICE: 102
NOTICE: 10
NOTICE: hadpsar
f34
-----

(1 row
*******
slip 5
Q1_A view
create or replace view ssn as select driver.,bus. from bus,driver where bus_no in(select bus_no
from bd where bus_no=102);
CREATE VIEW
sr=# select * from ssn;
driver_no | driver_name | license_no | address | age | salary | bus_no | capaci
ty | depot_name | route_no
-----------+-------------+------------+---------+-----+--------+--------+-------
---+------------+----------
10 | sagar | 5 | pune | 40 | 200000 | 102 |
10 | hadpsar | 5
20 | sakshi | 8 | mnpa | 50 | 80000 | 102 |
10 | hadpsar | 5
30 | prathmesh | 5 | mnpa | 35 | 90000 | 102 |
10 | hadpsar | 5
40 | anu | 8 | mnpa | 45 | 100000 | 102 |
10 | hadpsar | 5
50 | sidhhu | 9 | mnpa | 53 | 500000 | 102 |
10 | hadpsar | 5
11 | sanika | 4 | pune | 35 | 20000 | 102 |
10 | hadpsar | 5
(6 rows)
************************
Q1_B view
create or replace view v13 as select * from route where route_no in(select route_no from bus
where capacity=30);
CREATE VIEW
sr=# select * from v13;
route_no | source | destination | no_of_startions
----------+--------+-------------+-----------------
1 | had | wagholi | 5
(1 row)

*******************
Q2_A trigger
create or replace function jk() returns trigger as'

begin
if new.salary<=0
then

raise notice ''invalid salary'';


else
raise notice ''valid salary'';
return new;
end if;

end;'
language 'plpgsql';

create trigger sak before insert on driver for each row execute procedure jk();
o/p:
nsert into driver values(98,'sanika',4,'pune',35,'0');
NOTICE: invalid salary

*******************
Q2_B cursor

create or replace function fr(dn varchar(20)) returns void as'


declare
rec record;

c1 cursor for select date_of_duty_alloted from driver,bus,bd where


driver.driver_no=bd.driver_no and bus.bus_no=bd.bus_no and driver_name=dn;
begin
open c1;
loop
fetch c1 into rec;
exit when not found;
raise notice''date of duty alloted: %'',rec.date_of_duty_alloted;
end loop;
close c1;
end;'
language 'plpgsql';

O/P:
sr=# \i s5_B.sql
CREATE FUNCTION
sr=# select fr('sagar');
NOTICE: date of duty alloted: 2023-05-17
fr
----

(1 row)

******************
//SLIP 6

Q1_A VIEW 1

create or replace view vb2 as select driver.driver_name from bd,driver where


driver.driver_no=bd.driver_no and shift=1 and shift=2;
CREATE VIEW
sr=# select * from vb2;
driver_name
-------------
(0 rows)

*******************

Q2_B VIEW 2
create or replace view r1 as select route.* from route,bus where route.route_no=bus.route_no
and bus_no=101;
CREATE VIEW
sr=# select * from r1;
route_no | source | destination | no_of_startions
----------+--------+-------------+-----------------
1 | had | wagholi | 5
(1 row)

*******************************
Q2_A TRIGGER

create or replace function f8() returns trigger as'


begin
if old.capacity=20
then
raise exception ''Bus with capscity < 20 cannot be deleted'';
end if;
end;
'language 'plpgsql';

create trigger tt after delete on buss for each row execute procedure f8();

O/P:

*******************************
Q2_B FUNCTION

create or replace function fnn() returns void as'


declare
rec record;

c7 cursor for select * from bus where route_no=1;


begin
open c7;
loop
fetch c7 into rec;
exit when not found;
raise notice ''%'',rec.bus_no;
raise notice '' %'',rec.capacity;
raise notice ''%'',rec.depot_name;
end loop;
close c7;
end;'
language 'plpgsql';
O/P:
r=# \i s6_B.sql
CREATE FUNCTION
sr=# select fnn();
NOTICE: 101
NOTICE: 30
NOTICE: wagholi
fnn
-----

(1 row)
*************************************************************************************************************
//SLIP 7,8

create table train(tno int primary key,tname varchar(20),dtime time,atime time,source


varchar(20),desti varchar(20),no_res_bogies int,bogie_cap int);
CREATE TABLE

sr=# create table pass(pid int primary key,pname varchar(20),addr varchar(20),age int,gender
char);
CREATE TABLE

create table tiket(tno int references train(tno),pid int references pass(pid),tiket_no int primary
key,bn int,nfb int,tdate date,tamt decimal,stautu char);
CREATE TABLE

***********************
select * from train;
tno | tname | dtime | atime | source | desti | no_res_bogies |
bogie_cap
-----+--------------+----------+----------+---------+---------+---------------+-
----------
1 | shatabdi exp | 03:00:00 | 05:00:00 | indore | pune | 15 |
25
2 | rajdhani exp | 05:00:00 | 08:00:00 | pune | indapur | 50 |
100
3 | rajdhani exp | 05:00:00 | 10:00:00 | gadital | manjari | 15 |
24
4 | shatabdi exp | 03:00:00 | 07:00:00 | indore | pune | 10 |
25
(4 rows)

sr=# select * from pass;


pid | pname | addr | age | gender
-----+--------+--------+-----+--------
10 | sagar | pune | 14 | m
20 | sakshi | pune | 18 | f
30 | sanika | mumbai | 20 | f
40 | anu | bmt | 14 | m
(4 rows)

sr=# select * from tiket;


tno | pid | tiket_no | bn | nfb | tdate | tamt | stautu
-----+-----+----------+-----+-----+------------+---------+--------
2 | 20 | 101 | 220 | 33 | 2021-05-04 | 2000.00 | w
4 | 10 | 102 | 222 | 34 | 2022-04-19 | 3000.00 | c
3 | 40 | 103 | 224 | 35 | 2022-03-02 | 2000.00 | w
1 | 30 | 104 | 225 | 36 | 2022-01-01 | 4000.00 | c
2 | 20 | 105 | 226 | 37 | 2020-05-02 | 2000.00 | w
3 | 10 | 106 | 227 | 38 | 2022-01-01 | 3000.00 | c
1 | 10 | 107 | 224 | 35 | 2022-03-02 | 2000.00 | c
(7 rows)
****************************

//SLIP 7

Q1_A VIEW1

create or replace view b12 as select pname from pass,tiket,train where train.tno=tiket.tno and
pass.pid=tiket.pid and tname='shatabdi exp ' and tdate='2-3-2022' and stautu='w';
CREATE VIEW
sr=# select * from b12;
pname
-------
(0 rows)
*******************************

Q1_B VIEW2

create or replace view r2 as select pname ,train.* from pass,train,tiket where pass.pid=tiket.pid
and train.tno=tiket.tno and tname='rajdhani exp' and tdate='4-5-2021' limit 3;
CREATE VIEW
sr=# select * from r2;
pname | tno | tname | dtime | atime | source | desti | no_res_b
ogies | bogie_cap
--------+-----+--------------+----------+----------+--------+---------+---------
------+-----------
sakshi | 2 | rajdhani exp | 05:00:00 | 08:00:00 | pune | indapur |
50 | 100
(1 row)
******************

Q2_A TRIGGER
create or replace function n1() returns trigger as'

begin
if new.bogie_cap<>25
then
raise exception ''It must be greter than 25'';
end if;
return new;
end;'
language 'plpgsql';

create trigger r1 before insert OR update on train for each row execute procedure n1();

O/P:
CREATE FUNCTION
CREATE TRIGGER
sr=# insert into train values(6,'shatabdi exp','03:00:00','05:00:00','indore','pune',15,25);
INSERT 0 1
sr=# insert into train values(7,'shatabdi exp','03:00:00','05:00:00','indore','pune',15,30);
ERROR: It must be greter than 25
************************
Q2_B CURSOR

create or replace function m1() returns void as'


declare
rec record;

c9 cursor for select train.tno,tname, dtime, atime


,source,desti,no_res_bogies,bogie_cap from train,tiket,pass where pass.pid=tiket.pid and
train.tno=tiket.tno and tdate=''19-4-2022'' and stautu=''c'';
begin
open c9;
loop
fetch c9 into rec;
exit when not found;

raise notice''tno:%'',rec.tno;
raise notice''tname:%'',rec.tname;
raise notice ''dtime:%'',rec.dtime;
raise notice ''atime:%'',rec.atime;
raise notice ''source:%'',rec.source;
raise notice''no_res_bogies:%'',rec.no_res_bogies;
raise notice ''bogie_cap:%'',rec.bogie_cap;
end loop;
close c9;
end;'
O/P:
\i s7_B.sql
CREATE FUNCTION
sr=# select m1();
NOTICE: tno:4
NOTICE: tname:shatabdi exp
NOTICE: dtime:03:00:00
NOTICE: atime:07:00:00
NOTICE: source:indore
NOTICE: no_res_bogies:10
NOTICE: bogie_cap:25
m1
----
********************

//SLIP 8

Q1_A VIEW1

sr=# create or replace view t1 as select pass.pname from pass,train,tiket where


train.tno=tiket.tno and pass.pid=tiket.pid and tname='shatabdi exp' and stautu='c'and
tdate='2-3-2022';
CREATE VIEW
sr=# select * from t1;
pname
-------
sagar
(1 row)
*******************

Q1_B VIEW2

create or replace view t2 as select count(tiket.tno) from train,tiket where train.tno=tiket.tno and
tdate='1-1-2022' and tname='rajdhani exp'and stautu='c';
CREATE VIEW
sr=# select * from t2;
count
-------
1
(1 row)
********************
Q2_A TRIGGER

create or replace function f1() returns trigger as'

begin
if NEW.age>5
then
raise exception ''Age above 5 years will be charged the full fare'';

end if;
return new;
end;'
language 'plpgsql';

create trigger f111 after insert on pass for each row execute procedure f1();
O/P:
CREATE FUNCTION
CREATE TRIGGER
sr=# insert into pass values(102,'anu','bmt',6,'m');
ERROR: Age above 5 years will be charged the full fare
CONTEXT: PL/pgSQL function f1() line 6 at RAISE

sr=# insert into pass values(102,'anu','bmt',4,'m');


INSERT 0 1
***********************

Q2_B FUNCTION

create or replace function l1() returns void as'

declare
rec record;
begin
for rec in select train.tno,tname, dtime, atime ,source,desti,no_res_bogies,bogie_cap
from train,tiket,pass where pass.pid=tiket.pid and train.tno=tiket.tno and tdate=''2-5-2020'' and
stautu=''w'' order by tname
loop
raise notice ''tno:%'',rec.tno;
raise notice ''tname:%'',rec.tname;
raise notice ''dtime:%'',rec.dtime;
raise notice ''atime:%'',rec.atime;
raise notice ''source:%'',rec.source;
raise notice ''no_res_bogies:%'',rec.no_res_bogies;
raise notice ''bogie_cap:%'',rec.bogie_cap;

end loop;
end;'
language 'plpgsql';
O/P:

sr=# select l1();


NOTICE: tno:2
NOTICE: tname:rajdhani exp
NOTICE: dtime:05:00:00
NOTICE: atime:08:00:00
NOTICE: source:pune
NOTICE: no_res_bogies:50
NOTICE: bogie_cap:100
l1
----

(1 row)
*************************************************************************************************************
slip-9,10,11

create table project(pno int primary key,pname varchar(20),ptype varchar(20),duration int);


CREATE TABLE
sr=# create table employee(eno int primary key,ename varchar(20),qualification
varchar(15),joining_date date);
CREATE TABLE
sr=# create table pe(pno int references project(pno),eno int references
employee(eno),start_date_of_project date,no_of_hours_worked int);
CREATE TABLE

sr=# select * from project;


pno | pname | ptype | duration
-----+----------+----------+----------
1 | Robotics | done | 3
2 | Robotics | working | 5
3 | ERP | Not done | 2
4 | ERP | done | 1
5 | science | working | 4
(5 rows)

sr=# select * from employee;


eno | ename | qualification | joining_date
-----+-----------+---------------+--------------
100 | sakshi | phd | 2022-10-01
200 | sagar | mca | 2021-05-10
300 | prathmesh | mcs | 2022-05-15
400 | siddhu | mca | 2023-05-09
500 | anu | phd | 2023-07-01
(5 rows)

sr=# select * from pe;


pno | eno | start_date_of_project | no_of_hours_worked
-----+-----+-----------------------+--------------------
2 | 100 | 2023-03-12 | 100
1 | 300 | 2023-09-02 | 60
5 | 200 | 2020-05-18 | 20
3 | 400 | 2020-05-20 | 90
4 | 500 | 2021-03-09 | 110
(5 rows)
*********
Q1 view A
^
sr=# create or replace view v1 as select pname,ptype,start_date_of_project from project,pe
where project.pno=pe.pno order by start_date_of_project;
CREATE VIEW
sr=# select * from v1;
pname | ptype | start_date_of_project
----------+----------+-----------------------
science | working | 2020-05-18
ERP | Not done | 2020-05-20
ERP | done | 2021-03-09
Robotics | working | 2023-03-12
Robotics | done | 2023-09-02
(5 rows)
*********
view B
create or replace view vl as select employee.eno,ename,qualification,joining_date from
employee,project,pe where pe.eno=employee.eno and pe.pno=project.pno and
pname='Robotics';
CREATE VIEW
sr=# select * from vl;
eno | ename | qualification | joining_date
-----+-----------+---------------+--------------
100 | sakshi | phd | 2022-10-01
300 | prathmesh | mcs | 2022-05-15
400 | siddhu | mca | 2023-05-09
(3 rows)
****************
Q2_A trigger
create or replace function f1() returns trigger as'
declare
begin
if new.duration>0
then
raise notice ''valid duration'';
return new;
else
raise notice ''invalid duration'';
end if;
end;'
language 'plpgsql';

create trigger t11 before insert on project for each row execute procedure f1();

O/P:
CREATE FUNCTION
CREATE TRIGGER
sr=# insert into project values(11,'sakshi','robotics',0);
NOTICE: invalid duration
*****************
Q2_B function
create or replace function ff1(pn varchar(20)) returns void as'
declare
rec record;
c1 cursor for select ename from employee,project,pe where employee.eno=pe.eno and
project.pno=pe.pno and pname=pn;
begin
open c1;
loop
fetch c1 into rec;
exit when not found;
raise notice ''name of employee details:%'',rec.ename;
end loop;

close c1;
end;'
language 'plpgsql';
o/p:
sr=# \! vi s7B.sql
sr=# \i s7B.sql
CREATE FUNCTION
sr=# select ff1('ERP');
NOTICE: name of employee details:siddhu
NOTICE: name of employee details:anu
ff1
-----

(1 row)
*****************
///slip 10
\\Q1 view A
create or replace view vt as select employee.eno,ename,qualification,joining_date from
employee order by joining_date;
CREATE VIEW
sr=# select * from vt;
eno | ename | qualification | joining_date
-----+-----------+---------------+--------------
200 | sagar | mca | 2021-05-10
300 | prathmesh | mcs | 2022-05-15
100 | sakshi | phd | 2022-10-01
400 | siddhu | mca | 2023-05-09
500 | anu | phd | 2023-07-01
(5 rows)
*****************
view B
sr=# create or replace view v2 as select
employee.eno,ename,qualification,joining_date,project.pno,pname,ptype,duration from
employee,project,pe where project.pno=pe.pno and employee.eno=pe.eno and
no_of_hours_worked<100;
CREATE VIEW
sr=# select * from v2;
eno | ename | qualification | joining_date | pno | pname | ptype | du
ration
-----+-----------+---------------+--------------+-----+----------+----------+---
-------
300 | prathmesh | mcs | 2022-05-15 | 1 | Robotics | done |
3
200 | sagar | mca | 2021-05-10 | 5 | science | working |
4
400 | siddhu | mca | 2023-05-09 | 3 | ERP | Not done |
2
400 | siddhu | mca | 2023-05-09 | 6 | Robotics | done |
1
(4 rows)
**************
Q2_A trigger
create or replace function f1() returns trigger as'
declare
begin
if (new.joining_date<now())
then
return new;
else
raise exception ''Invalid date inserted'';
end if;
end;'
language 'plpgsql';

create trigger tt before insert on employee for each row execute procedure f1();
o/p;
r=# \i s10_A.sql
CREATE FUNCTION
CREATE TRIGGER
sr=# insert into employee values(9,'neha','mca','09/10/2023');
INSERT 0 1

************
Q2_B function
create or replace function ft(pn varchar(20)) returns int as'
declare
cnt int;
rec record;

begin
for rec in select * from project
loop
if(rec.pname<>pn)
then
raise notice''Invalid project name'';
else

select count(ename) into cnt from employee where eno in(select eno from pe
where pno in(select pno from project where pname=''pn''));

end if;
end loop;
return cnt;
end;'
language 'plpgsql';
0/p:
sr=# \i s10_BB.sql
CREATE FUNCTION
sr=# select ft('ERP');
NOTICE: Invalid project name
NOTICE: Invalid project name
NOTICE: Invalid project name
NOTICE: Invalid project name
NOTICE: Invalid project name
NOTICE: Invalid project name
ft
----
2
(1 row)
******
slip 11
Q1_A view
sr=# create or replace view vm as select employee.eno,ename,qualification,joining_date from
employee,project,pe where employee.eno=pe.eno and project.pno=pe.pno and pname='ERP';
CREATE VIEW
sr=# select * from vm;
eno | ename | qualification | joining_date
-----+--------+---------------+--------------
400 | siddhu | mca | 2023-05-09
500 | anu | phd | 2023-07-01
(2 rows)
********
Q1_B view2
create or replace view vc as select
employee.eno,ename,qualification,joining_date,project.pno,pname,ptype,duration from
employee,project,pe where employee.eno=pe.eno and project.pno=pe.pno and
no_of_hours_worked>100;
CREATE VIEW
sr=# select * from vc;
eno | ename | qualification | joining_date | pno | pname | ptype | duration
-----+-------+---------------+--------------+-----+-------+-------+----------
500 | anu | phd | 2023-07-01 | 4 | ERP | done | 1
(1 row)
*********
Q2_A trigger
create or replace function ff() returns trigger as'
declare
begin
raise notice ''project record deleted'';
return old;
end;
'language 'plpgsql';

create trigger yy after delete on project for each row execute procedure ff();

o/p:
r=# \i s11_A.sql
CREATE FUNCTION
CREATE TRIGGER
sr=# delete from project;
NOTICE: project record delete
********************************************************************************************
Q2_B function
create or replace function ffg() returns int as'
declare
n int;
begin
select count(*) into n from employee where joining_date < ''03-10-2022'';
return n;
end;
'language 'plpgsql';

o/p:
sr=# \i s11_B.sql
CREATE FUNCTION
sr=# select ffg();
ffg
-----
3
(1 row)
*************************************************************************************************************
//SLIP 12,13,14

create table stud(sno int primary key,sname varchar(20),class varchar(20),address varchar(30));


CREATE TABLE

create table teach(tno int primary key,tname varchar(20),quali char(15),exp int);


CREATE TABLE

create table st(sno int references stud(sno),tno int references teach(tno),subject varchar(20));
CREATE TABLE
******************************

select * from stud;


sno | sname | class | address
-----+--------+-------+---------
1 | sakshi | sybca | pune
2 | ganesh | fybca | pune
3 | rohit | tybca | manjari
4 | sagar | bcs | swarget
5 | payal | sybcs | bmt
(5 rows)

sr=# select * from teach;


tno | tname | quali | exp
-----+---------+-----------------+-----
100 | vinita | phd | 4
200 | kalayni | phd | 10
300 | sarika | phd | 3
400 | sarika | mca | 15
500 | sanika | mcd | 2
(5 rows)

sr=# select * from st;


sno | tno | subject
-----+-----+---------
1 | 200 | DBMS
5 | 300 | CN
4 | 400 | DS
2 | 100 | DBMS
3 | 500 | CN
(5 rows)
***********************

//SLIP 12

Q1_A VIEW 1

create or replace view j1 as select stud.sname from stud where sno in(select sno from st where
tno in( select tno from teach where exp in(select max(exp) from teach)));
CREATE VIEW
sr=# select * from j1;
sname
-------
sagar
(1 row)
***********************

Q1_B VIEW 2

create or replace view j2 as select tname,subject from teach,st where teach.tno=st.tno;


CREATE VIEW
sr=# select * from j2;
tname | subject
---------+---------
kalayni | DBMS
sarika | CN
sarika | DS
vinita | DBMS
sanika | CN
(5 rows)
*************************

Q2_B TRIGGER

create or replace function fd() returns trigger as'

begin
if (new.sno<=0 )
then
raise exception''Invalid student number'';

end if;
return new;
end;'
language 'plpgsql';

create trigger h1 before insert on stud for each row execute procedure fd();
O/P:
\i s12_A.sql
CREATE FUNCTION
CREATE TRIGGER
sr=# insert into stud values(-1,'sakshi','sybca','pune');
ERROR: Invalid student number
CONTEXT: PL/pgSQL function fd() line 6 at RAISE
sr=# insert into stud values(7,'sakshi','sybca','pune');
INSERT 0 1
*******************

Q2_B function

create or replace function n1(sn varchar(20)) returns int as'


declare
cnt int;
begin
select count(stud.sno) into cnt from stud,st where stud.sno=st.sno and
subject=sn;
if cnt is NULL
then
raise exception ''Invalid subject Name'';
end if;
return cnt;
end;'
language 'plpgsql';
O/P:
CREATE FUNCTION
sr=# select n1('CN');
n1
----
2
(1 row)

sr=# select n1('C');


n1
----
0
(1 row)
*******************

//SLIP 13

Q1_A VIEW 1

create or replace view k1 as select * from teach where quali='phd';


CREATE VIEW
sr=# select * from k1;
tno | tname | quali | exp
-----+---------+-----------------+-----
100 | vinita | phd | 4
200 | kalayni | phd | 10
300 | sarika | phd | 3
(3 rows)
********************

Q1_B VIEW 2

create or replace view k2 as select * from stud where address='pune';


CREATE VIEW
sr=# select * from k2;
sno | sname | class | address
-----+--------+-------+---------
1 | sakshi | sybca | pune
2 | ganesh | fybca | pune
-1 | sakshi | sybca | pune
6 | sakshi | sybca | pune
7 | sakshi | sybca | pune
(5 rows)
*********************

Q2_A TRIGGER

create or replace function sa1() returns trigger as'

begin
if new.exp<5
then
raise exception''Invalid experience'';
end if;
return new;
end;'
language 'plpgsql';

create trigger sak2 before insert on teach for each row execute procedure sa1();
O/P:
CREATE FUNCTION
CREATE TRIGGER
sr=# insert into teach values(800,'vinita','phd',10);
INSERT 0 1
sr=# insert into teach values(800,'vinita','phd',3);
ERROR: Invalid experience
**************************
Q2_B CURSOR

create or replace function f12(sn varchar(20)) returns void as'


declare
rec record;
c2 cursor for select teach.* from teach,stud,st where teach.tno=st.tno and
stud.sno=st.sno and sname=sn;
begin
open c2;
loop
fetch c2 into rec;
exit when not found;
raise notice''teacher number:%'',rec.tno;
raise notice ''teacher name:%'',rec.tname;
raise notice ''teacher qualification:%'',rec.quali;
raise notice ''experience:%'',rec.exp;
end loop;
close c2;
end;'
language 'plpgsql';
O/P:
\i s13_B.sql
CREATE FUNCTION
sr=# select f12('sakshi');
NOTICE: teacher number:200
NOTICE: teacher name:kalayni
NOTICE: teacher qualification:phd
NOTICE: experience:10
*************************

//SLIP 14

Q1_A VIEW 1

create or replace view t10 as select * from teach where exp>5;


CREATE VIEW
sr=# select * from t10;
tno | tname | quali | exp
-----+---------+-----------------+-----
200 | kalayni | phd | 10
400 | sarika | mca | 15
800 | vinita | phd | 10
(3 rows)
************************

Q1_B VIEW 2
create or replace view ss1 as select * from teach where tname like 's%';
CREATE VIEW
sr=# select * from ss1;
tno | tname | quali | exp
-----+--------+-----------------+-----
300 | sarika | phd | 3
400 | sarika | mca | 15
500 | sanika | mcd | 2
(3 rows)
************************

Q2_A TRIGGER
//NOT CONFIRM

create or replace function an() returns trigger as'

begin
if new.class <> old.class
then
raise exception ''You cannot update student class '';
end if;
return new;
end;'
language 'plpgsql';

create trigger t53 before update on student for each row execute procedure an();

o/p:
update stud set class='sybca' where sno=4;
UPDATE 1
**********************************************************************************************************

Q2_B FUNCTION

create or replace function fdd(sn varchar(20)) returns int as'

declare
cnt int;
begin
select count(teach.tno) into cnt from stud,teach,st where teach.tno=st.tno and
stud.sno=st.sno and sname=sn;
return cnt;
end;'
language 'plpgsql';
O/P:
select fdd('sakshi');
fdd
-----
1
(1 row)
*************************************************************************************************************
//slip 15,16,17

create table student(rno int primary key,stname varchar(30),city varchar(50),class varchar(30));


CREATE TABLE

create table subject(scode varchar(20) primary key,subname varchar(30));


CREATE TABLE

sr=# create table ss(rno int references student(rno),scode varchar(20) references


subject(scode), marks int);
CREATE TABLE

select * from student;


rno | stname | city | class
-----+---------+---------+-------
1 | sakshi | pune | sybca
2 | ajit | manjari | fybca
3 | snehal | manjari | tybca
4 | madhuri | bekrai | sybcs
5 | ganesh | swarget | tybcs
(5 rows)

sr=# select * from subject;


scode | subname
-------+---------
100 | DBMS
200 | DBMS
300 | CF
400 | DS
500 | CN
(5 rows)

sr=# select * from ss;


rno | scode | marks
-----+-------+-------
2 | 500 | 90
3 | 100 | 30
4 | 200 | 35
4 | 200 | 95
1 | 400 | 97
(5 rows)
********************
//SLIP 15
Q1_A VIEW

sr=# create or replace view sakshi as select stname from student where class='fybca';
CREATE VIEW
sr=# select * from sakshi;
stname
--------
ajit
(1 row)
**********************
Q1_B VIEW

sr=# create or replace view sam as select student.stname,subject.subname,marks from


student,subject,ss where subject.scode=ss.scode and student.rno=ss.rno and marks > 90;
CREATE VIEW
sr=# select * from sam;
stname | subname | marks
---------+---------+-------
madhuri | DBMS | 95
sakshi | DS | 97
(2 rows)
************************
Q2_A TRIGGER
create or replace function ff4() returns trigger as'
begin
if new.rno<=0
then
raise exception ''Invalid Rno'';
end if;
return new;
end;'
language 'plpgsql';

create trigger sam before insert on student for each row execute procedure ff 4();
0/p:
insert into student values(6,'sakshi','pune','sybca');
INSERT 0 1
sr=# insert into student values(0,'sakshi','pune','sybca');
ERROR: Invalid Rno
*************************
Q2_B CURSOR
///NOT CONFIRM ANS// OUTPUT IS NOT CORRECT//
create or replace function d11() returns void as'
declare
rec record;
cc1 cursor for select stname,sum(marks) as total from ss,student where
student.rno=ss.rno group by stname;

begin
open cc1 ;
loop
fetch cc1 into rec;
exit when not found;
raise notice ''Total marks %'',rec.stname;
end loop;
close cc1;

end;'
language 'plpgsql';

o/p:
select d11();
NOTICE: Total marks madhuri
NOTICE: Total marks ajit
NOTICE: Total marks snehal
NOTICE: Total marks sakshi
d11
-----

(1 row)
***********************

************************
//SLIP 16
Q1_A VIEW

create or replace view san as select student.stname from student,subject,ss where


student.rno=ss.rno and subject.scode=ss.scode and marks > 80 and subname='DBMS';
CREATE VIEW
sr=# select * from san;
stname
---------
madhuri
snehal
ajit
(3 rows)
*********************
Q1_B VIEW

create or replace view vb as select * from student where class='tybca';


CREATE VIEW
sr=# select * from vb;
rno | stname | city | class
-----+--------+---------+-------
3 | snehal | manjari | tybca
(1 row)
***********************
Q2_A TRIGGER

create or replace function fd() returns trigger as'


begin
raise notice ''student record is deleted'';
return old;
end;'
language 'plpgsql';

create trigger sd before delete on student for each row execute procedure fd();

o/p:
delete from student where rno =6;
NOTICE: student record is deleted
DELETE 1
************
Q2_B FUNCTION

create or replace function sanu( sn varchar(20)) returns void as'


declare
rec record;
begin
for rec in select subject.scode,subname from subject,student,ss where
student.rno=ss.rno and subject.scode=ss.scode and subname=sn
loop
raise notice''scode is %'',rec.scode;
raise notice ''subject name is %'',rec.subname;
end loop;
end;'
language 'plpgsql';
o/p:
select sanu('CN');
NOTICE: scode is 500
NOTICE: subject name is CN
sanu
------

(1 row)

*************************
//SLIP 17
Q1_A VIEW

create or replace view saks as select stname from student where stname like 'a%';
CREATE VIEW
sr=# select * from saks;
stname
--------
ajit
(1 row)
*************************
Q1_B VIEW

create or replace view ds as select student.rno,stname,city,class from student,ss where


student.rno=ss.rno and ss.marks < 40;
CREATE VIEW
sr=# select * from ds;
rno | stname | city | class
-----+---------+---------+-------
3 | snehal | manjari | tybca
4 | madhuri | bekrai | sybcs
(2 rows)

*************************
Q2_A TRIGGER
create or replace function anuu() returns trigger as'

begin
if new.marks<0 OR new.marks>100
then
raise exception ''marks between in 0 to 100'';
end if;
return new;
end;'
language 'plpgsql';

create trigger tyyy before insert on ss for each row execute procedure anuu();
o/p:
CREATE FUNCTION
CREATE TRIGGER
sr=# insert into ss values(2,500,101);
ERROR: marks between in 0 to 100
*************************
Q_B FUNCTION

create or replace function sakshi(c varchar(20)) returns void as'

declare
rec record;

begin
for rec in select * from student where city=c
loop
raise notice '' rno: %'',rec.rno;
raise notice '' student name:% '',rec.stname;
raise notice '' city:% '',rec.city;
raise notice '' class:% '',rec.class;
end loop;

end;'
language 'plpgsql';
o/p:
select sakshi('manjari');
NOTICE: rno: 2
NOTICE: student name:ajit
NOTICE: city:manjari
NOTICE: class:fybca
NOTICE: rno: 3
NOTICE: student name:snehal
NOTICE: city:manjari
NOTICE: class:tybca
sakshi
--------

(1 row)
******************************************************************************************************
slip: 18,19,20

create table movie(mname varchar(25) primary key,release_year int,budget int);


CREATE TABLE
sr=# create table actor(aname varchar(20) primary key,city varchar(20));
CREATE TABLE
sr=# create table producer(pid int primary key,pname char(30),paddr varchar(30));
CREATE TABLE
create table ma(mname varchar(25) references movie(mname),aname varchar(20) references
actor(aname), role varchar(20),charges int);
CREATE TABLE
sr=# create table pm(pid int references producer(pid),mname varchar(30) references
movie(mname));
CREATE TABLE
**********
sr=# select * from movie;
mname | release_year | budget
-------+--------------+---------
shole | 2018 | 1000000
ved | 2022 | 200000
kagan | 2023 | 3000000
mela | 2021 | 3000000
(4 rows)

sr=# select * from actor;


aname | city
------------------+--------
amitabh bachchan | mumbai
shahajan | pune
varun | pune
anushka | mumbai
nikita | pune
(5 rows)

sr=# select * from producer;


pid | pname | paddr
-----+--------------------------------+--------
1|s | pune
2|c | mumbai
3|a | pune
4|b | mumbai
5|d | pune
(5 rows)

sr=# select * from ma;


mname | aname | role | charges
-------+------------------+----------+----------
shole | amitabh bachchan | director | 2500000
ved | shahajan | producer | 2000000
shole | shahajan | producer | 15000000
shole | amitabh bachchan | director | 2500000
(4 rows)

sr=# select * from pm;


pid | mname
-----+-------
2 | shole
1 | ved
5 | shole
3 | kagan
1 | shole
(5 rows)
************
//slip 18
Q1_A view
reate or replace view vt7 as select aname from actor where city='mumbai';
CREATE VIEW

sr=# select * from vt7;


aname
------------------
amitabh bachchan
anushka
(2 rows)
***************
Q1_B view
create or replace view vt8 as select actor.aname,city from actor,movie ,ma where
actor.aname=ma.aname and movie.mname=ma.mname;
CREATE VIEW
sr=# select * from vt8;
aname | city
------------------+--------
amitabh bachchan | mumbai
shahajan | pune
shahajan | pune
amitabh bachchan | mumbai
(4 rows)
***************
Q2_A trigger
create or replace function fc1() returns trigger as'

if new.budget < 6000000


then
raise exception '' Invalid Input'';
end if;
return new;
end;'
language 'plpggsql';

create trigger tttm before insert on movie for each row execute procedure fc1();
o/p:
insert into movie values('kag','2018','1000000');
INSERT 0 1
***************
Q2_B stored function
create or replace function pn(pn varchar(20)) returns int as'
declare
cnt int;
begin
select count(movie.mname) into cnt from movie,producer,pm where
movie.mname=pm.mname and producer.pid=pm.pid;
return cnt;
end;
'language 'plpgsql';
o/p:
select pn('s');
pn
----
5
(1 row)
*******************
Slip 19
Q1_A view
create or replace view v17 as select actor.aname,city from movie,actor,ma where
movie.mname=ma.mname and actor.aname=ma.aname and movie.mname='shole';
CREATE VIEW
sr=# select * from v17;
aname | city
------------------+--------
amitabh bachchan | mumbai
shahajan | pune
amitabh bachchan | mumbai
(3 rows)
*****************
Q1_B view
create or replace view rt as select producer.pname from producer,pm,movie where
producer.pid=pm.pid and movie.mname=pm.mname group by producer.pname having
count(pm.mname)>2;
CREATE VIEW
sr=# select * from rt;
pname
-------
(0 rows)
***************
Q2_A Trigger
create or replace function f1() returns trigger as'
begin
if new.charges<3000000
then
raise exception ''invalid charges'';
end if;
return new;
end;'
language 'plpgsql';

create trigger tm before insert on ma for each row execute procedure f1();
o/p:
TRIGGER
sr=# insert into ma values('shole','shahajan','producer','400000000');
INSERT 0 1
sr=# insert into ma values('shole','shahajan','producer','00000000');
ERROR: invalid charges
****************
Q2_B stored function

******************
Slip 20
Q1_A view
create or replace view anu as select movie.mname from movie,pm,producer where
producer.pid=pm.pid and movie.mname=pm.mname and producer.pname='s';
CREATE VIEW
sr=# select * from anu;
mname
-------
ved
shole
(2 rows)
*****************
Q1_B view
create or replace view vn1 as select actor.aname from actor where city not in ('mumbai','pune');
CREATE VIEW
sr=# select * from vn1;
aname
-------
(0 rows)
******************
Q2_A Trigger
create or replace function fk() returns trigger as'
begin
if new.release_year>extract(year from now())
then
raise notice ''invalid year'';
return new;

raise notice ''valid year'';


end if;
return new;
end;'
language 'plpgsql';

create trigger anitaaa before insert on movie for each row execute procedure fk();

***********************
Q2_B cursor
create or replace function a6() returns void as'
declare
rec record;

c7 cursor for select movie.mname,charges from movie,actor,ma where


actor.aname=ma.aname and movie.mname=ma.mname and actor.aname=''amitabh
bachchan'';begin
open c7;
loop
fetch c7 into rec;
exit when not found;
raise notice ''Mname %'',rec.mname;
raise notice ''charges %'',rec.charges;
end loop;
close c7;
end;'
language plpgsql;
o/p:
\i s20_B.sql
CREATE FUNCTION
sr=# select a6();
NOTICE: Mname shole
NOTICE: charges 2500000
NOTICE: Mname shole
NOTICE: charges 2500000
a6
----
************************************************************************************************************

You might also like