Database Management Systems Lab Assesment-3 Name: P.Gopichand Reg - No: 18MIS0101 Course Code: SWE1004 Faculty: Jayaram Reddy A Slot: L1+L2

You might also like

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

Database Management Systems

Lab Assesment-3
Name : P.Gopichand
Reg.No: 18MIS0101
Course code : SWE1004
Faculty: Jayaram reddy A
Slot: L1+L2

1. Writea PL/SQL block to displaythereverseof numbers between 1 and 100

SQL> SET SERVEROUTPUT ON;


SQL> declare
2 i number;
3 begin
4 for i in reverse 1..100 loop dbms_output.put_line(i);
5 end loop;
6 end;
7 /
100
98
97
96
95
94
93
92
91
90
89
88
87
86
85
84
83
82
81
80
79
78
77
76
75
74
73
72
71
70
69
68
67
66
65
64
63
62
61
60
59
58
57
56
55
54
53
52
51
50
49
48
47
46
45
44
43
42
41
40
39
38
37
36
35
34
33
32
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
11
10
14
9
13
8
12
7
6
5
4
3
2
1

PL/SQL procedure successfully completed.

2. Write a PL/SQL block to find the greatest of three


numbers.
SQL> declare
2 num1 number;
3 num2 number;
4 num3 number;
5 max1 number;
6 begin
7 num1:=&num1;
8 num2:=&num2;
9 num3:=&num3;
10 if num1>num2 then
11 if num1>num3 then
22 en
d
if;
12 max1:=num1;
23 dbms_outp
13 else ut.put_line('
The largest
14 max1:=num3;
number is:'||
15 end if; max1);

16 else 24 e
n
17 if num2>num3 then d
;
18 max1:=num2;
2
19 else
5
20 max1:=num3;
/
21 end if;
Enter value for num1: 1000
old 7: num1:=&num1;
new 7: num1:=1000;
Enter value for num2: 1500
old 8: num2:=&num2;
new 8: num2:=1500;
Enter value for num3: 3000
old 9: num3:=&num3;
new 9: num3:=3000;
The largest number is:3000

PL/SQL procedure successfully completed.

3. Write a PL/SQL block to generate fibonacci series.


SQL> declare
Enter value for
n: 4

2 f1 number; old 8:

3 f2 number; dbms_output.pu

4 i number; t_line('Enter the

5 c number; value of n:');

6 n number; n:=&n; new 8:

7 begin f1:=0; f2:=1; dbms_output.pu

8 dbms_output.put_line('Enter the value of n:'); n:=&n; t_line('Enter the

9 dbms_output.put_line(f1); value of n:');

10 dbms_output.put_line(f2); n:=4; Enter the

11 for i in 1..n loop c:=f1+f2; value of n:

12 dbms_output.put_line(c); f1:=f2; 0

13 f2:=c; 1

14 end loop; 1

15 end; 2

16 / 3
5
PL/SQL procedure successfully completed.
4. Write a block to raise an exception if the reservation date is less than today's date.
S
Q
SQL> declare L
2 reservation_date date; >
3 today_date date;
4 date_invalid exception; d
5 n number; e
6 begin c
7 reservation_date:=to_date('&reservation_date','dd/mm/yyyy'); l
8 today_date:=sysdate; a
9 n:=reservation_date-today_date; r
10 IF n>0 THEN e
11 RAISE date_invalid; 2 cursor
12 END IF; details_of_f
lights is
13 EXCEPTION select *
from flight;
14 WHEN date_invalid THEN
3 flight
15 dbms_output.put_line('Reservation date is not valid'); details_of_f
16 end; lights
%rowtype;
17 /
Enter value for reservation_date: 12/8/2016
old 7: reservation_date:=to_date('&reservation_date','dd/mm/yyyy');
new 7: reservation_date:=to_date('12/8/2016','dd/mm/yyyy');

PL/SQL procedure successfully completed.


5. Write a cursor to give the details of all the flights.

PL/SQL procedure successfully completed.


4 begin
5 open details_of_flights;
6 loop
7 fetch details_of_flights into flight;
8 exit when details_of_flights%notfound;
9 dbms_output.put_line(flight.flight_number||' '||flight.airline);
10 end loop;
11 close details_of_flights;
12 end;
13 /
17mis0051 kingfisher 17mis0000 indian airlines
17mis1000 jet airlines

PL/SQL procedure successfully completed.


6. Write a cursor to give flight details that range between 1200 and 2900.

SQL> declare
2 cursor f1 is select * from flight
3 where flight_number in(select flight_number from fare where amount between 1200
and 2900);
4 f f1%rowtype;
5 begin
6 open f1;
7 loop
8 fetch f1 into f;
9 exit when f1%notfound;
10 dbms_output.put_line(f.flight_number||' '||f.airline);
11 end loop;
17mis0051
kingfisher

12 close f1;
13 end; PL/SQL
procedure
14 / successfully
completed.
7. Write a procedure to accept the customer name and display the reservation details.

SQL> create or replace procedure details(c_name IN varchar) is


2 begin
3 for arow in(select
flight_number,leg_number,seat_number,customer_name,customer_phone form
seat_reservation
4 where customer_name=c_name)
5 loop
6 dbms_output.put_line('flight_number:'||arow.flight_number);
7 dbms_output.put_line('Leg_number:'||arow.Leg_number);
8 dbms_output.put_line('seat_number:'||arow.seat_number);
9 dbms_output.put_line('customer_name:'||arow.customer_name);
10 dbms_output.put_line('customer_phone:'||arow.customer_phone);
11 end loop;
12 end;
13 /

Warning: Procedure created with compilation errors.


8. Write a procedure(with cursor) to display the company names along with airplane ids.

SQL> create or replace procedure airplane_det as


2 comp varchar1(10);
3 a_id varchar1(10);
4 cursor cmp is select company from airplane_type;
12 dbms_outp
ut.put_line(
comp);
5 begin
13 end loop;
6 open cmp;
14 close cmp;
7 open air_id;
15 loop
8 dbms_output.put_line('company'||' '||'airplane id);
16 fetch air_id
9 loop into a_id;
10 fetch cmp into comp; 17 exit when
air_id
11 exit when cmp%notfound;
%notfound;
18 dbms_output.put_line(a_id);
19 end loop;
20 close air_id;
21 end;
22 /

Warning: Procedure created with compilation errors.

9. Write a function to give the number of flights arriving for a given airport code.

SQL> create function number_of_flight(air_code varchar)


2 return number is
3 counts number(2) :=0;
4 a_code varchar2(10);
5 begin

6 select airport_code into a_code from airport;


7 if a_code=air_code then
8 select count(flight_number) into counts
9 from airport,flight_leg
10 where airport_code=arrival_airport_code
11 group by flight_number;
12 end if;
13 end;
14 /

Function created.
10. Write a function to return the airport name which is having highest number
of airplane types landing.

SQL> create or replace function air_name


2 return varchar0 is
3 aname varchar0(10);
4 begin
5 select name int aname from airport
6 group by name having count(airport_code)=(select max(airport_code) from airport,flight_leg
where airport_code=arrival_airport_code);
7 return aname;
8 end;
9/

Warning: Function created with compilation errors.

11. Write a trigger to update the airplane type in airplane when parent table is updated.

SQL> create or replace trigger type_update


2 after update of airplane_type on airplane
3 for each row
4 declare
5 a_type airplane.airplane_type%type;
6 begin
7 update airplane
8 set airplane_type=a_type;
9 end;
10 /

Trigger created.
12. Write a trigger to delete all the foreign key references when the parent primary key
is deleted. (If you have added cascade while create table ignore this question.)

SQL> create or replace trigger remove_foriegn1


2 after delete on flight
3 for each row
4 begin
5 delete from flight_leg1 where flight_number=:old.flight_number;
6 delete from leg_instance1 where flight_number=:old.flight_number;
7 delete from fare3 where flight_number=:old.flight_number;
8 delete from seat_reservation3 where flight_number=:old.flight_number;
9 end;
10 /

Warning: Trigger created with compilation errors.


13. Write a trigger to raise an exception if reservation date is invalid while insertion.
(should be latter than current date.)
SQL> create or replace trigger reserve_date1
2 after insert on seat_reservation
3 for each row
4 declare
5 t_date date;
6 begin
7 select sysdate into t_date from dual;
8 if(:new.sr_date>t_date) then
9 raise_application_error(-21000,'Invalid reservation date');
10 end if;
11 end;
12 /

Warning: Trigger created with compilation errors.

14. Write a trigger to update the myflight table when flight tuple is inserted.

SQL> create or replace trigger flight_update


2 after insert on flight
3 for each row
4 begin
5 update flight
6 set flight_number=:new.flight_number,
7 airline=:new.airline,weekdays=:new.weekdays;
8 end;
9/
Trigger created.
15. Create a nested table from the airplane database. Create a package with
two functions on the table.
DECLARE
CURSOR c_customers is
SELECT customer_name FROM seat_reservation;
TYPE c_list IS TABLE of varchar(100);
name_list c_list:=c_list();
counter integer :=0;
BEGIN
FOR n IN c_customers LOOP
counter := counter +1;
name_list.extend;
name_list(counter) := n.customer_name;
dbms_output.put_line('Customer('||counter||'):'||name_list(counter));
End Loop;
End;
/

PL/SQL procedure successfully completed.

SQL> create type seat_numbers is table of varchar2(100);


2/

Type created.

SQL> create table seats(rcptno number(5),Name varchar(50),seatno seat_numbers)


2 nested TABLE seatno store as nested_seats;

Table created.

SQL> CREATE OR REPLACE PACKAGE passe_rcp AS


2 PROCEDURE find_rcp(CUS VARCHAR);
3 END passe_rcp;
4/

Package created.

SQL> CREATE OR REPLACE PACKAGE BODY passe_rcp AS


2 PROCEDURE find_rcp(CUS VARCHAR) IS
3 c_sal number;
4 BEGIN
5 SELECT rcptno INTO c_sal

Package body
created.
6 FROM seats WHERE name=cus;
7 dbms_output.put_line('rcptno: '|| c_sal);
8 END find_rcp; S
9 END passe_rcp; Q
10 / L
> create or replace package p1 as 2
procedure listseats;
3 end p1;
4/
Package created.
SQL> CREATE OR REPLACE PACKAGE BODY p1 AS
2 PROCEDURE listseats IS
3 CURSOR c_customers is SELECT name FROM seats;
4 TYPE c_list is TABLE OF varchar;
5 name_list c_list:= c_list();
6 counter integer:=0;
7 BEGIN
8 FOR n IN c_customers
9 LOOP
10 counter := counter +1;
11 name_list.extend;
12 name_list(counter) := n.name;
13 dbms_output.put_line('Customer(' ||counter|| ')'||name_list(counter));
14 END LOOP;
15 END listseats;
16
17 END p1;
18 /

SQL> begin
2 p1.listseats;
3 end;
4/

PL/SQL procedure successfully completed

You might also like