You are on page 1of 17

DATABASE MANAGEMENT SYSTEM

DIGITAL ASSIGNMENT-4 (LAB)


NAME: JAYATI TRIPATHI
REGISTRATION NO: 18BIT0053
SLOT: L57+L58
FACULTY: prof. ranichandra
QUESTION 7:
1). Write a simple PL/SQL block to.
1. Print the Fibonacci series.
Code
SQL> SET SERVEROUTPUT ON;
SQL> DECLARE
2 a NUMBER :=1;
3 b NUMBER :=1;
4 counter NUMBER := &Enter_number;
5 c NUMBER;
6 BEGIN
7 DBMS_OUTPUT.PUT_LINE(a);
8 DBMS_OUTPUT.PUT_LINE(b);
9 counter := counter-2;
10 for v_count IN 1..counter LOOP
11 c := a+b;
12 a :=b;
13 b :=c;
14 DBMS_OUTPUT.PUT_LINE(c);
15 END LOOP;
16 END;
17 /
Enter value for enter_number: 5
old 4: counter NUMBER := &Enter_number;
new 4: counter NUMBER := 5;
1
1
2
3
5

PL/SQL procedure successfully completed.

SQL> /
Enter value for enter_number: 10
old 4: counter NUMBER := &Enter_number;
new 4: counter NUMBER := 10;
1
1
2
3
5
8
13
21
34
55

PL/SQL procedure successfully completed.

2. Print the Factorial of a given number.


Code
SQL> DECLARE
2 num NUMBER := &Enter_number;
3 fact NUMBER :=1;
4 BEGIN
5 FOR factorial_counter IN 1..num LOOP
6 fact := fact * factorial_counter;
7 END LOOP;
8 DBMS_OUTPUT.PUT_LINE('Factorial of the number ' || num || ' = ' || fact);
9 END;
10 /
Enter value for enter_number: 5
old 2: num NUMBER := &Enter_number;
new 2: num NUMBER := 5;
Factorial of the number 5 = 120

PL/SQL procedure successfully completed.


3. Print 'NOT confirmed' based on the reservation status, of
a particular passenger.
Code
SQL> set serveroutput on;
SQL> DECLARE
2 passenger_pnr_no passenger_details.pnr_no%type := &Passenger_pnr_no;
3 passenger_reservation_status passenger_details.reservation_status%type;
4 BEGIN
5 select reservation_status into passenger_reservation_status from
passenger_details
6 where pnr_no = passenger_pnr_no;
7 IF passenger_reservation_status != 'CNF' THEN
8 DBMS_OUTPUT.PUT_LINE('NOT confirmed');
9 ELSE
10 DBMS_OUTPUT.PUT_LINE('Confirmed');
11 END IF;
12 END;
13 /
Enter value for passenger_pnr_no: 98282987712
old 2: passenger_pnr_no passenger_details.pnr_no%type := &Passenger_pnr_no;
new 2: passenger_pnr_no passenger_details.pnr_no%type := 98282987712;
Confirmed

PL/SQL procedure successfully completed.

SQL> /
Enter value for passenger_pnr_no: 65452345100
old 2: passenger_pnr_no passenger_details.pnr_no%type := &Passenger_pnr_no;
new 2: passenger_pnr_no passenger_details.pnr_no%type := 65452345100;
NOT confirmed

PL/SQL procedure successfully completed.

4. Print the total seats available for a particular train and for
a particular class.
Code
SQL> DECLARE
2 t_num ticket.train_no%TYPE := 12019;
3 t_class ticket.tkt_class%TYPE := '3A';
4 count1 number :=0;
5 count2 number :=0;
6 seat_left number;
7 seat_left_in_train number;
8 BEGIN
9 FOR row IN
10 (select train_no,tkt_class from ticket where train_no = t_num)
11 LOOP
12 count1 := count1+1;
13 IF row.tkt_class = t_class
14 THEN
15 count2 := count2+1;
16 END IF;
17 END LOOP;
18 seat_left := 60 - count2;
19 select count(class) INTO seat_left_in_train
20 FROM seats
21 where train_no = 12019
22 group by train_no;
23 seat_left_in_train := seat_left_in_train*60 - count1;
24 DBMS_OUTPUT.PUT_LINE('Seats available for train '|| t_num || ' = '||
25 seat_left_in_train ||' and for class '|| t_class ||' = '|| seat_left);
26 END;
27 /
Seats available for train 12019 = 119 and for class 3A = 60

PL/SQL procedure successfully completed.

2.) Write a cursor for the following.

1. Retrieve the passenger details for “x” train number and


given journey date.
Code
SQL> SET SERVEROUTPUT ON;
SQL> DECLARE
2 train NUMBER(5);
3 journey_date DATE;
4 CURSOR pas_cur IS
5 SELECT p_name,age FROM passenger_details WHERE pnr_no IN (SELECT pnrno
FROM ticket WHERE train_no =&Train_number AND date_of_journey =
'&journey_date');
6 pas_rec pas_cur%ROWTYPE;
7 BEGIN
8 OPEN pas_cur;
9 LOOP
10 FETCH pas_cur INTO pas_rec;
11 EXIT WHEN pas_cur%NOTFOUND;
12 DBMS_OUTPUT.PUT_LINE(pas_rec.p_name || ' ' || pas_rec.age);
13 END LOOP;
14 CLOSE pas_cur;
15 END;
16 /
Enter value for train_number: 12426
Enter value for journey_date: 19-DEC-19
old 5: SELECT p_name,age FROM passenger_details WHERE pnr_no IN (SELECT
pnrno FROM ticket WHERE train_no =&Train_number AND date_of_journey =
'&journey_date');
new 5: SELECT p_name,age FROM passenger_details WHERE pnr_no IN (SELECT
pnrno FROM ticket WHERE train_no =12426 AND date_of_journey = '19-DEC-19');

PL/SQL procedure successfully completed.

SQL> /
Enter value for train_number: 12426
Enter value for journey_date: 26-DEC-19
old 5: SELECT p_name,age FROM passenger_details WHERE pnr_no IN (SELECT
pnrno FROM ticket WHERE train_no =&Train_number AND date_of_journey =
'&journey_date');
new 5: SELECT p_name,age FROM passenger_details WHERE pnr_no IN (SELECT
pnrno FROM ticket WHERE train_no =12426 AND date_of_journey = '26-DEC-19');
Shreeya Bhuwalka 20

PL/SQL procedure successfully completed.

2. Display the train name(once) and the substation names.


Code
SQL> SET SERVEROUTPUT ON;
SQL> DECLARE
2 t train%ROWTYPE;
3 tr train_route%ROWTYPE;
4 CURSOR Train1 IS SELECT * FROM train;
5 N NUMBER(10);
6 CURSOR T_R IS SELECT * FROM train_route WHERE train_no = N;
7 BEGIN
8 FOR T IN train1
9 LOOP
10 N := T.train_no;
11 DBMS_OUTPUT.PUT_LINE(T.NAME);
12 DBMS_OUTPUT.PUT_LINE('Stations:');
13 FOR tr IN t_r
14 LOOP
15 DBMS_OUTPUT.PUT_LINE(tr.name);
16 END LOOP;
17 END LOOP;
18 END;
19 /
ahemdabad-duronto express
Stations:
Surat Junction

Jammu-tawi express
Stations:
Ludhiana Junction

Howrah-ranchi express
Stations:
Kharagpur Junction

Pune-jammu express
Stations:
Bhopal Junction

Shatabdi Express
Stations:
Katpadi Junction

PL/SQL procedure successfully completed.


3. Display the fare details of a particular train
Code
4. Write a cursor to update the reservation status of the
passengers(generate seat number, if seats have reached
maximum, put wating list number(30% of total seats), if
waiting list number reaches maximum, put PQWL(10%of
total seats), RAC-20%)
Code

QUESTION 8:
1). Write a PL/SQL procedure to.
(High Level)
1. List the details of passengers who has reserved next to “Mr.
X”.
Code

2. PNR No. of a passengers for a given source and a


destination.
Code
SQL> set serveroutput on;
SQL> DECLARE
2
3 source VARCHAR2(40) := '&Enter_source';
4 Dest VARCHAR2(40) := '&Enter_destination';
5 CURSOR pas_cur IS
6 SELECT pnrno from ticket where from_station = source and to_station = dest;
7 pas_rec pas_cur%ROWTYPE;
8 BEGIN
9 OPEN pas_cur;
10 LOOP
11 FETCH pas_cur into pas_rec;
12 EXIT WHEN pas_cur%NOTFOUND;
13 DBMS_OUTPUT.PUT_LINE(pas_rec.pnrno
14 );
15 END LOOP;
16 CLOSE pas_cur;
17 END;
18 /
Enter value for enter_source: jammu
old 3: source VARCHAR2(40) := '&Enter_source';
new 3: source VARCHAR2(40) := 'jammu';
Enter value for enter_destination: delhi
old 4: Dest VARCHAR2(40) := '&Enter_destination';
new 4: Dest VARCHAR2(40) := 'delhi';
87445121012

PL/SQL procedure successfully completed.

2)Write a PL/SQL function to.


1. Get the PNRNo and return the total ticket fare.
Code
SQL> set serveroutput on;
SQL> DECLARE
2 pnr_number ticket.pnrno%TYPE;
3 PD ticket%ROWTYPE;
4 BEGIN
5 pnr_number := '&Enter_pnr';
6 SELECT *INTO PD from ticket where pnrno = pnr_number;
7 DBMS_OUTPUT.PUT_LINE(PD.total_ticket_fare);
8 END;
9 /
Enter value for enter_pnr: 1125458552
old 5: pnr_number := '&Enter_pnr';
new 5: pnr_number := '1125458552';
2500

PL/SQL procedure successfully completed.


2. Get the Passenger name, train no and return the total
journey time in hours and minutes.
Code

QUESTION 9:

1.Write a Trigger for the following:


(High Level)
1. When a passenger cancels a ticket, do the necessary
process and update the cancellation history table.
Code
SQL> create or replace trigger update_cancel_history
2 after delete on ticket
3 for each row
4 DECLARE
5 pnr ticket.pnrno%type;
6 source ticket.from_station%type;
7 destination ticket.to_station%type;
8 journey_date ticket.date_of_journey%type;
9 trainno ticket.train_no%type;
10 pass_name passenger_details.p_name%type;
11 reservation_sta varchar(30);
12 BEGIN
13 pnr := :old.pnrno;
14 source := :old.from_station;
15 destination := :old.to_station;
16 journey_date := :old.date_of_journey;
17 trainno := :old.train_no;
18 select p_name into pass_name from passenger_details
19 where pnr_no = pnr;
20 reservation_sta := 'Booked but cancelled';
21 dbms_output.put_line('Deleting person:'|| pass_name);
22 insert into cancel_history
23
values(pnr,:old.transactionid,source,destination,journey_date,:old.tkt_class,:old.dat
e_of_booking,:old.total_ticket_fare,trainno,pass_name,reservation_sta);
24 end;
25 /

Trigger created.

2. When train number is changed, update it in referencing


tables.
Code
SQL> create or replace trigger update_train
2 after update on train
3 for each row
4 BEGIN
5 update ticket
6 set train_no = :new.train_no
7 where train_no = :old.train_no;
8 update train_ticket_fare
9 set train_no = :new.train_no
10 where train_no = :old.train_no;
11 update train_route
12 set train_no = :new.train_no
13 where train_no = :old.train_no;
14 DBMS_OUTPUT.PUT_LINE('Successfully train number updated from' ||
:old.train_no || ' to ' || :new.train_no || ' for all the tables ');
15 END;
16 /
Trigger created.

3. When a passenger record is inserted reservation status


should be automatically updated.
Code
SQL> create or replace trigger update_reservation
2 AFTER INSERT ON ticket
3 FOR EACH ROW
4 DECLARE
5 reservation_sta passenger_details.reservation_status%TYPE;
6 pnr ticket.pnrno%type;
7 pass_name passenger_details.p_name%TYPE;
8 BEGIN
9 reservation_sta := 'CNF';
10 SELECT p_name INTO pass_name from passenger_details
11 WHERE pnr_no = :new.pnrno;
12 UPDATE passenger_details
13 SET reservation_status = reservation_sta
14 WHERE pnr_no = :new.pnrno;
15 dbms_output.put_line('After insert of '||pnr||' & '||pass_name||' the
reservation status has been updated');
16 END;
17 /

Trigger created.
QUESTION 10:

1. Use TCL commands for your transactions.


(commit,rollback,savepoint).
(Middle Level)
Code
Rollback:
Savepoint:

Commit:
2. Create a role named 'clerk', and give permisson for him to
select only the trains starting from 'Katpadi' along with fare
details.
Code
SQL> create role clerk;
SQL> GRANT select total_ticket_fare, from_station from ticket where from_station
='katpadi' TO clerk;

3. Create a nested table containing


trainno,name,source,destination and passengers who
have booked for it (PNR no,sno, name,age). Find the
passengers whose name start with 'S' and train starts
from 'Katpadi'
Code
CREATE TYPE passenger_t AS OBJECT ( PNR_NO varchar(10), sno name age );
/
Number(5), varchar(100), Number(2)
CREATE TYPE passengers_t IS TABLE OF passenger_t;
CREATE TABLE train ( Trainno number(5), Name varchar(50), Source
varchar(50), Destination varchar(50), Passengers passengers_t );
SQL> SELECT p.* FROM train t , TABLE (t.passengers) p WHERE t.source =
‘Katpadi’ AND p.name LIKE ‘S%

You might also like