0% found this document useful (0 votes)
14 views10 pages

Slip4 6

Uploaded by

anurajbadole
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)
14 views10 pages

Slip4 6

Uploaded by

anurajbadole
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/ 10

bank=# create table route(route_no int primary key, src varchar(30), dest varchar(30),no_of_stations int);

CREATE TABLE

bank=# insert into route values (1,'Hadapsar','Katraj',20);

INSERT 0 1

bank=# insert into route values (2,'Hadapsar','Kothru',25);

INSERT 0 1

bank=# insert into route values (3,'Hadapsar','Chinchwad',30);

INSERT 0 1

bank=# insert into route values (4,'Nigadi','Hadapsar',30);

INSERT 0 1

bank=# insert into route values (5,'Kothru','katraj',22);

INSERT 0 1

bank=# select * from route;

route_no | src | dest | no_of_stations

----------+----------+-----------+----------------

1 | Hadapsar | Katraj | 20

2 | Hadapsar | Kothru | 25

3 | Hadapsar | Chinchwad | 30

4 | Nigadi | Hadapsar | 30

5 | Kothru | katraj | 22

(5 rows)

bank=# create table Bus

(bus_no int primary key, capacity int not null, depot_name char(30),route_no int references
route(route_no));

CREATE TABLE

bank=# insert into Bus values(101,40,'Hadapsar',1);

INSERT 0 1

bank=# insert into Bus values(102,50,'Hadapsar',2);


INSERT 0 1

bank=# insert into Bus values(103,50,'Hadapsar',3);

INSERT 0 1

bank=# insert into Bus values(104,40,'Nigadi',4);

INSERT 0 1

bank=# insert into Bus values(105,50,'Kothru',5);

INSERT 0 1

bank=# select * from bus;

bus_no | capacity | depot_name | route_no

--------+----------+--------------------------------+----------

101 | 40 | Hadapsar | 1

102 | 50 | Hadapsar | 2

103 | 50 | Hadapsar | 3

104 | 40 | Nigadi | 4

105 | 50 | Kothru | 5

(5 rows)

bank=# create table Driver (Driver_no int primary key, driver_name varchar(20),license_no int,address
varchar(20),age int, salary float);

CREATE TABLE

bank=# insert into Driver values(1,'Krish',1001,'pune',25,20000);

INSERT 0 1

bank=# insert into Driver values(2,'vijay',2001,'pune',35,22000);

INSERT 0 1

bank=# insert into Driver values(3,'Ajay',3001,'pune',35,25000);

INSERT 0 1

bank=# insert into Driver values(4,'Sanjay',4001,'pune',50,50000);

INSERT 0 1

bank=# insert into Driver values(5,'Akash',5001,'pune',30,20000);


INSERT 0 1

bank=# select * from Driver;

driver_no | driver_name | license_no | address | age | salary

-----------+-------------------+----------------+-----------+-------+--------

1 | Krish | 1001 | pune | 25 | 20000

2 | vijay | 2001 | pune | 35 | 22000

3 | Ajay | 3001 | pune | 35 | 25000

4 | Sanjay | 4001 | pune | 50 | 50000

5 | Akash | 5001 | pune | 30 | 20000

(5 rows)

bank=# create table bus_driver(bus_no int references Bus(bus_no),Driver_no integer references


Driver(Driver_no),Date_of_Duty date,shift int check(shift in(1,2)));

CREATE TABLE

bank=# insert into bus_Driver values(101,1,'2022-12-26',1);

INSERT 0 1

bank=# insert into bus_Driver values(102,2,'2022-12-26',2);

INSERT 0 1

bank=# insert into bus_Driver values(103,3,'2022-12-26',1);

INSERT 0 1

bank=# insert into bus_Driver values(104,4,'2021-12-26',2);

INSERT 0 1

bank=# insert into bus_Driver values(105,5,'2020-12-26',1);

INSERT 0 1

bank=# select * from bus_driver;

bus_no | driver_no | date_of_duty | shift

--------+-----------+--------------+-------

101 | 1 | 2022-12-26 | 1

102 | 2 | 2022-12-26 | 2
103 | 3 | 2022-12-26 | 1

104 | 4 | 2021-12-26 | 2

105 | 5 | 2020-12-26 | 1

(5 rows)
======================================Slip 4===========================================================

Q.1 Create view

1. Solve by Students

2.Solve by Students

Q.2

1. bank=# create or replace function f7()

returns trigger as '

declare

begin if NEW.age > 18 and NEW.age < 35 then

raise notice ''valid Entry'';

else raise exception ''Age is not Appropriate'';

end if; return null;

end;

'language 'plpgsql';

CREATE FUNCTION

bank=# create trigger trg_marks after insert or update on driver for each row execute procedure f7();

CREATE TRIGGER

bank=# insert into driver values (6,'manoj',6001,'pune',45,32000);

ERROR: Age is not Appropriate

CONTEXT: PL/pgSQL function f7() line 1 at RAISE

2. bank=# create or replace function f8(rno int)

returns int as '

declare rec record;

begin

for rec in select driver_name,license_no, age, salary from driver

where route_no=rno
loop raise notice ''% % % %'',rec.driver_name, rec.license_no,rec.age, rec.salary;

end loop;

return 1;

end;

'language 'plpgsql';

CREATE FUNCTION

bank=# select f8(3);


===================================Sleep 5==========================================================

Q.1 Create view

1. Solve by Students

2.Solve by Students

Q.2

1. bank=# create or replace function chk_Inv_sal()


returns trigger as '
declare
begin
if NEW.salary >= 0 then raise notice ''valid entry'';
else raise exception ''Invalid salary'';

end if;

return null;

end;

'language 'plpgsql';

CREATE FUNCTION
bank=# create trigger trg_In after insert or update on driver for each row execute procedure chk_Inv_sal();
CREATE TRIGGER
bank=# insert into driver values(6,'amo',6001,'pune',60,60000);
NOTICE: valid entry

2. bank=# create or replace function print_driver(name text)


returns int as '
declare c2 cursor for
select date_of_duty from driver a,bus b,bus_driver c
where driver_name=name and a.driver_no=c.driver_no;
d_date date;
begin open c2;
loop fetch c2 into d_date;
EXIT WHEN NOT FOUND;
raise notice''%'',d_date;
end loop;
close c2;
return 1;
end;
'language 'plpgsql';
CREATE FUNCTION
bank=# select print_driver('Ajay');
NOTICE: 2022-12-26
NOTICE: 2022-12-26
NOTICE: 2022-12-26
NOTICE: 2022-12-26
NOTICE: 2022-12-26
print_driver
--------------
1
(1 row)
==========================================Sleep 6=====================================================

Q.1 Create view

1. Solve by Students

2.Solve by Students

Q.2

1. bank=# create or replace function print_bus()

returns trigger as '

declare

begin

if (old.capacity < 20) then raise notice '' deleting bus no % data having capacity < 20'',old.bus_no; end
if;

return null;

end;

'language'plpgsql';

CREATE FUNCTION

bank=# create trigger t3 after delete on bus for each row execute procedure print_bus();

CREATE TRIGGER

bank=# delete from bus where capacity <=20;

DELETE 0

2 bank=# create or replace function print_driver()


returns int as '
declare
c2 cursor for select driver_name,license_no,age, salary from driver a,bus b,bus_driver c
where route_no=1 and a.driver_no=c.driver_no and b.bus_no=c.bus_no;
dname varchar(30);
dlic int;
dage int;
dsal float;

begin

open c2;

loop fetch c2 into dname, dlic, dage,dsal;


exit when not found;

raise notice ''% % % %'',dname,dlic,dage,dsal;end loop;close c2;

return 1;

end;

'language 'plpgsql';

CREATE FUNCTION
bank=# select print_driver();
NOTICE: Krish 1001 25 20000
print_driver
--------------
1
(1 row)

You might also like