0% found this document useful (0 votes)
12 views13 pages

Experiment 7 Aim

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views13 pages

Experiment 7 Aim

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Experiment 7

Aim: Practice the following Queries


1. Display Unique PNR_NO of all Passengers

SQL> select PNR_NO from Passenger;

PNR_NO

----------

7 rows selected.

2. Display all the names of male Passengers

SQL> select Name from Passenger where Sex='m';

NAME

--------------------

SACHIN

rahul

rafi

salim

riyaz

3. Display Ticket numbers and names of all Passengers

SQL> select Ticket_NO,Name from Passenger;

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.

SQL> select source, destination from Ticket where Journey_Dur>10;

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

----------

6. Find the names of the passenger whose age is between 20 and 40

SQL> select Name from Passenger where age between 20 and 40;

2
NAME

--------------------

swetha

rafi

riyaz

neha

7. Display all the name of the passengers beginning with ‘r’

SQL> select Name from Passenger where Name like 'r%';

NAME

--------------------

rahul

rafi

riyaz

8. Display the sorted list of Passenger Names

SQL> select Name from Passenger ORDER BY Name;

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

SQL> select * from Reservation UNION select * from Cancellation;

PNR_NO NO_OF_SEATS ADDRESS CONTACT_NO STATUS

---------- - ---------- -------------------- ---------- --- --------------

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

PNR_NO NO_OF_SEATS ADDRESS CONTACT_NO STATUS

---------- -- --------- -------------------- ----------

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

SQL> SELECT PNR_NO, COUNT (*) AS NoOccurances FROM Passenger GROUP BY


PNR_NO HAVING COUNT (*)>0;

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.

SQL> select PNR_NO, sum (No_of_seats) from Reservation group by PNR_NO;

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=

'c' group by class having sum(No_of_seats)>1;

CLASS SUM (NO_OF_SEATS)

---- ---------------

a 13

b 7

c 2

5. Find the total number of cancelled seats.

SQL> select sum(No_of_seats) from Cancellation;

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.

SQL> select * from male_pass;

PNR_NO AGE

---------- ----------

1 12

2 43

4 22

5 45

6 32

Create a view from two tables with all columns.

SQL> create view v1 as select * from Passenger full natural join Reservation;

View created.

b) INSERT

SQL> insert into male_pass values (&PNR_NO,&age);

Enter value for pnr_no: 12

Enter value for age: 22

old 1: insert into male_pass values(&PNR_NO,&age)

new 1: insert into male_pass values(12,22)

1 row created.

c) DROP VIEW

SQL> drop view male_pass; View dropped.

7
Experiment 10

Aim: Create of insert trigger, delete trigger and update trigger.


1. To write a TRIGGER to ensure that Bus table does not contain duplicate of null values in
Bus_No column.

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'

b) Create Trigger updchek before update on Ticket


For Each Row
Begin
If New.Ticket_No>60 Then
Set New.Ticket_No=Ticket_No;
Else
Set New.Ticket_No=0;
End If
End.

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

Aim: Creation of stored procedures, Execution of procedure and modification of procedure.


Create procedure myproc()
BEGIN
SELECT COUNT(Tickets) from ticket where age>=20;
END;
PL/SOL procedure successfully completed
Create procedure myproc(in_customer_id INT)
BEGIN
DECLARE V_id INT;
DECLARE V_name VARCHAR(30);
DECLARE c1 CURSOR FOR SELECT stdid,stdfirstname FROM students WHERE
stdid=in_customer_id;
OPEN c1;
FETCH c1 into V_id,V_name;
CLOSE c1;
END;
/
PL/SQL procedure successfully completed.

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.

b) To write a Cursor to display List of Passengers from Passenger Table.


DECLARE
cursor c is select PNR_NO, Name, Age, Sex from Passenger ;
i Passenger.PNR_NO%type;
j Passenger.Name%type;
k Passenger.Age%type;
l Passenger.Sex%type;
BEGIN
open c;
dbms_RESULT.put_line('PNR_NO, Name, Age, Sex of Passengers are:= ');
loop
fetch c into i, j, k, l;
exit when c%notfound;
dbms_RESULT.put_line(i||' '||j||' '||k||' '||l);
end loop;
close c;
END;
RESULT:
SQL>@Passenger
PNR_NO NAME AGE SEX
---------- -------------------- ---------- ----------
1 SACHIN 12 m
2 rahul 43 m
3 swetha 24 f
4 rafi 22 m
PL/SQL procedure successfully completed.

13

You might also like