Experiment 7 Aim
Experiment 7 Aim
PNR_NO
----------
7 rows selected.
NAME
--------------------
SACHIN
rahul
rafi
salim
riyaz
1
TICKET_NO NAME
---------- --------------------
1 SACHIN
2 rahul
3 swetha
23 rafi
12 salim
34 riyaz
21 neha
7 rows selected.
4. Display the source and destination having journey time more than 10 hours.
SOURCE DESTINATION
---------- --------------------
HYD BAN
SEC BAN
HYD MUM
5. Find the ticket number of passenger whose name starts with ‘S’ and ends with ‘H’
SQL> select Ticket_NO from Passenger where Name like'S%'and name like'%N';
TICKET_NO
----------
SQL> select Name from Passenger where age between 20 and 40;
2
NAME
--------------------
swetha
rafi
riyaz
neha
NAME
--------------------
rahul
rafi
riyaz
NAME
neha
rafi
rahul
riyaz
salim
swetha
6 rows selected.
3
Experiment 8
Aim: Practice queries using Aggregate functions, Group by, having and Creation and Dropping
of views
1. Write a query to Display the information present in the Cancellation and Reservation Tables
1 2 sdfgh 1234543 s
1 3 msbtnk 123456789 s
2 2 ldkp 234567891 s
2 2 wertgfds 12212121 n
3 4 dskng 345678912 n
3 5 azxsdcvf 13243546 s
4 2 ddfdsfsdfdsf 3456789 s
4 5 abids 567891234 s
5 2 allbd 891234567 s
5 11 liopujth 43256787 s
6 1 koti 231456781 s
6 31 swebnht 453212345 s
7 2 dbdhfdbhf 90876543 s
7 3 jklhg 2345671 s
14 rows selected.
4
2. Find the distinct PNR_NO that are present
PNR_NO NOOCCURANCES
---------- ------------
1 1
2 1
3 1
4 1
5 1
6 1
7 1
7 rows selected.
3. Find the No of Seats booked for each PNR_NO using GROUP BY Clause.
PNR_NO SUM(NO_OF_SEATS)
---------- ----------------
1 3
6 1
2 2
4 5
5 2
3 6
7 3
7 rows selected.
5
4. Find the number of seats booked in each class where the number of seats is greater than 1.
SQL> select class, sum (No_of_seats) from Reservation where class='a 'or class='b' or class=
---- ---------------
a 13
b 7
c 2
SUM(NO_OF_SEATS)
----------------
22
6
Experiment 9
Creating and dropping views
a) CREATE VIEW
SQL> create view male_pass as select PNR_NO,age from Passenger where sex='m';
View created.
PNR_NO AGE
---------- ----------
1 12
2 43
4 22
5 45
6 32
SQL> create view v1 as select * from Passenger full natural join Reservation;
View created.
b) INSERT
1 row created.
c) DROP VIEW
7
Experiment 10
a) CREATE OR RELPLACE TRIGGER trig1 before insert on Bus for each row
DECLARE a number;
BEGIN
if(:new.Bus_No is Null) then
raise_application_error(-20001,'error:: Bus_No cannot be null');
else
select count(*) into a from Bus where Bus_No =:new. Bus_No;
if(a=1) then
raise_application_error(-20002,'error:: cannot have duplicate Bus_No ');
end if;
end if;
END;
RESULT:
SQL> @trigger
Trigger created.
SQL> select * from Bus;
BUS_NO SOURCE DESTINATION
----- -------------------- --------------------
110 hyd ban
221 hyd chn
412 hyd mum
501 hyd kol
SQL> insert into Bus values(&Bus_No,'&source','&destination');
Enter value for Bus_No: null
Enter value for source: Chen
Enter value for destination: hyd
8
old 1: insert into Bus values(&Bus_No, '&source', '&destination')
new 1: insert into Bus values(null,Chen','hyd')
insert into Bus values(null,’Chen','hyd')
*
ERROR at line 1:
ORA-20001: error::Bus_No cannot be null
35
ORA-06512: at "SYSTEM.TRIG1", line 5
ORA-04088: error during execution of trigger 'SYSTEM.TRIG1'
SQL> /
Enter value for Bus_No: 110
Enter value for source:KOL
Enter value for destination: hyd
old 1: insert into Bus values(&Bus_No, '&source', '&destination')
new 1: insert into Bus values(110,KOL','hyd')
insert into Bus values(110,’KOL','hyd')
*
ERROR at line 1:
ORA-20002: error:: cannot have duplicate Bus_No
ORA-06512: at "SYSTEM.TRIG1", line 9
ORA-04088: error during execution of trigger 'SYSTEM.TRIG1'
9
SQL> @trigger
Trigger created.
c) CREATE OR RELPLACE TRIGGER trig1 before insert on Passenger for each row
DECLARE a number;
BEGIN
if(:new.PNR_NO is Null) then
raise_application_error(-20001,'error:: PNR_NO cannot be null');
else
select count(*) into a from Passenger where PNR_NO =:new. PNR_NO;
if(a=1) then
raise_application_error(-20002,'error:: cannot have duplicate PNR_NO ');
end if;
end if;
END;
SQL> @trigger
Trigger created.
10
Experiment 11
11
Experiment 12
Aim: To write a Cursor to display the list of Male and Female Passengers.
Analyst.
DECLARE
cursor c(jb varchar2) is select Name from Passenger where Sex=m;
pr Passenger.Sex%type;
BEGIN
open c('m');
dbms_RESULT.put_line(' Name of Male Passenger are:');
loop
fetch c into pr;
exit when c%notfound;
dbms_RESULT.put_line(pr);
end loop;
close c;
open c('f');
dbms_RESULT.put_line(' Name of female Passengers are:');
loop
fetch c into em;
exit when c%notfound;
dbms_RESULT.put_line(em);
end loop;
close c;
END;
RESULT:
Name of Male Passenger are:
SACHIN
rahul
rafi
salim
riyaz
12
Name of female Passengers are:
swetha
neha
PL/SQL procedure successfully completed.
13