7) 1. Write A Simple PL/SQL Block To. 1. Print The Fibonacci Series

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

7) 1. Write a simple PL/SQL block to.

1. Print the fibonacci series.


SQL> declare
2 f1 number(3):=1;
3 f2 number(3):=1;
4 f3 number(4);
5 cnt number(2):=2;
6 begin
7 dbms_output.put_line(f1);
8 dbms_output.put_line(f2);
9 loop
10 f3:=f1+f2;
11 dbms_output.put_line(f3);
12 cnt:=cnt+1;
13 exit when cnt=10;
14 f1:=f2;
15 f2:=f3;
16 end loop;
17 end;
18 /

PL/SQL procedure successfully completed.

2. Print the factorial of a given number.

SQL> declare
2 a number(4):=1;
3 b number(4);
4 c number(4);
5 begin
6 b:=&b;
7 c:=b;
8 if b=0 or b=1 then
9 dbms_output.put_line(a);
10 end if;
11 loop
12 a:=b*a;
13 b:b-1;
14 exit when b=1;
15 end loop;
16 dbms_output.put_line('The factorial'||c||'is'||a);
17 end;
18 /
Enter value for b: 1
old 6: b:=&b;
new 6: b:=1;
PL/SQL procedure successfully completed.
3. Print 'NOT confirmed' based on the reservation status, of a particular passenger.

4. Print the total seats available for a particular train and for a particular class
SQL> declare
2 tno seats.train_no%type;
3 cls seats.class%type;
4 sts seats.total_seats%type;
5 begin
6 tno:=&tno;
7 cls:=&cls;
8 select total_seats into sts from seats where class=cls and train_no=tno;
9 dbms_output.put_line('The number of seats is'||sts);
10 end;
11 /
Enter value for tno: '12027'
old 6: tno:=&tno;
new 6: tno:='12027';
Enter value for cls: '1A','2A','3A','SL'
old 7: cls:=&cls;
new 7: cls:='1A','2A','3A','SL';
cls:='1A','2A','3A','SL';
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.

SQL> declare
2 x ticket.train_number%type;
3 y ticket.date_of_journey%type;
4 cursor psngr_crs is select name,age from passenger_details natural join ticket where
train_number=x and date_of_journey=y;
5 begin
6 x:=&x;
7 y:=&y;
8 for psngr_rec in psngr_crs loop
9 dbms_output.put_line('The name &age of passenger_details is'||psngr_rec.name||' '||
psngr_rec.age);
10 end loop;
11 end;
12 /
Enter value for x: 12027
old 6: x:=&x;
new 6: x:=12027;
Enter value for y: 02-FEB-22
old 7: y:=&y;
new 7: y:=02-FEB-22;
Enter value for age: 18

PL/SQL procedure successfully completed.

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


SQL> set serveroutput on

SQL> declare

2 cursor train is

3 select*from train inner join ticket

4 on train.train_number=ticket.train_number;

5 train_name train%rowtype;

6 begin

7 open train;

8 loop

9 fetch train into train_name;

10 exit when train%notfound;

11 dbms_output.put_line('Train name:'

12 || train_name.name || 'substation:'||train_name.from_station);

13 dbms_output.put_line(train_name.to_station);

14 dbms_output.put_line(train_name.source||','||train_name.destination);

15 end loop;
16 close train;

17 end;

18 /

OUTPUT:

Train name:Chennai Expresssubstation:Chennai

Salem Junction

Eranakulam Junction,Chennai Central

Train name:Andhra Pradesh Expresssubstation:Hyderabad Decan

Nagpur

Hyberabad Decan,New Delhi

Train name:Coimbatoresubstation:Nagercoil Junction

karur

Nagarcoil Junction,Coimbatore Junction

Train name:Shatabdi Expresssubstation:katpadi Junction

Bngalore Cant

Chennai Central,Bangalore Cy junction

Train name:Mumbai Expresssubstation:Tirunelveli

Madurai Junction

Nagercoil Junction,Mumbai cst

PL/SQL procedure successfully completed.

3. Display the fare details of a particular train(use basic exceptions)

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

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

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

2. Write a PL/SQL function to.


1. Get the PNRNo and return the total ticket fare.

2. Get the Passenger name, train no and return the total journey time in hours and
minutes.
9)
Write a Trigger for the following:
1) When a passenger cancels a ticket, do the necessary process and update the cancellation
history table.
create or replace trigger tr_cnl_aft_del_dup after delete on ticket
for each row
declare
cursor psngr_crs is select * from ticket;
psngr_rec psngr_crs%rowtype;
psngr_pnr passenger.pnr_no%type;
begin
open psngr_crs;
loop
fetch psngr_crs into psnge_rec;
exit when psngr_crs%notfound;
if psngr_rec.pnr_no=:old.pnr_no then
psngr_pnr:=psngr_rec.pnr_no;
insert into ticket_psngr values
(:old.pnr_no,:old.transactionid,:old.from_station,:old.to_station,:old.date_of_journey,:o
ld.class,:old.date_of_book,:old.total_ticket_fare,:old.train_number,psngr_rec.serial_no,
psngr_rec.name,psngr_rec.age)
end if;
end loop;
close psngr_crs;
delete from psngr_dup where pnr_no=psngr_pnr;
end;
/
Trigger created.

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


SQL> create or replace trigger trg_aft_upd_trn
2 after update of train_number on train
3 for each row
4 begin
5 update ticket set train_number=:new.train_number where train_number=:old.train_number;
6 update train_ticket_fare set train_no =:new.train_number where train_no=:old.train_number;
7 update train_route set train_no=:new.train_number where train_no=:old.train_number;
8 end;
9 /

Trigger created.

3) When a passenger record is inserted reservation status should be automatically updated

3)
create table fare_audit(
train_no char(5) not null,
last_update date,
new_fare number(10,2),
old_fare number(10,2),
primary key(train_no,last_update)
foreign key (train_no) references train);
Table created.

create or replace trigger trg_trntktfr_aft_update


after update of base_fare on train_ticket_fare
for each row
begin
insert into fare_audit values(:old.train_no,sysdate,:new.base_fare,:old.base_fare);
end;
/

Trigger created.

create or replace trigger train_log


after insert or update or delete on train
declare
typeof varchar(20);
begin
if inserting then typeof:='Insertion';
elseif deleting then typeof:='Deletion';
elseif updatin typeof:='Update;
endif;
insert into alog values(user,typeof,sysdate);
end;
/
Trigger created

You might also like