Professional Documents
Culture Documents
LAB-PROGRAM NO:1
Roadway Travels:
Roadway travel system has several buses connecting different places in India.
Its main Office is located in one place (i.e. Hyderabad).
The company wants to computerize its operation in the the following areas:
Reservations
Ticketing
Cancellations
AIM: Creation of Entity Sets and identify the Entities, Attributes, Primary Keys for
all the entities and any other keys (i.e Candidate Keys, Foreign Keys etc….)
if any by analyzing above specification carefully.
Entity Set: An Entity set is a set of entities of same type that share same properties
or attributes.
Primary Key: A Primary Key is one or more columns in a table, used to uniquely
each record in a table.
Foreign Key: A Foreign key represents relationship between tables. The existence of
a foreign keys implies that the table with the Foreign Key is relates to the Primary
key table, from which the Foreign Key is derived.
LAB-PROGRAM NO:2
AIM: Draw the E-R Diagram for the Entity Set BUS, PASSENGER,
TICKET, RESERVATION, CANCELLATION and WAITINGLIST.
Relate the entities appropriately and indicate the type of
Relationships among these entity sets.
E-R Model: The Entity Relationship data model considers the Real World
consisting of basic objects.
TYPES OF RELATIONSHIPS:
SOURCE
NAME
PPNO GENDER
JOURNEY_DAT
E
BUS_NO
TICKET_N TICKET
O
SOURCE
DESTINATIO
DEPT_TIM N
E
JOURNEY_DAT
TICKET_N E
O
NO_OF_SEAT
S
PNR_NO RESEVATION
ADRESS
CONTACT_N
STATUS O
JOURNEY_DAT
TICKET_N E
O
NO_OF_SEAT
S
PNR_NO CANCELLATION
ADRESS
CONTACT_N
STATUS O
JOURNEY_DAT
TICKET_N E
O
NO_OF_SEAT
S
PNR_NO WAITINGLIST
ADRESS
CONTACT_N
STATUS O
NAME
PPNO GENDER
RESERVATIO
N BOOKING
JOURNEY_DAT
TICKET_N E
O
M NO_OF_SEAT
S
PNR_NO RESEVATION
ADRESS
CONTACT_N
STATUS O
NAME
PPNO GENDER
TRAVE
L
1
BUS_NO BUS DESTINATIO
N
SOURCE
NAME
PPNO GENDER
CANCE
L
TICKET
TICKET_N JOURNEY_DAT
O E
M NO_OF_SEAT
S
PNR_NO CANCELLATION
ADRESS
CONTACT_N
STATUS O
NAME
PPNO GENDER
ISSUE
TICKET
CONFOR
M
JOURNEY_DAT BUS_NO
E
1
TICKET_N TICKET SOURCE
O
DESTINATIO
DEPT_TIM N
E
NAME
PPNO GENDER
WAITLIS
T
CONFOR
M
JOURNEY_DAT
TICKET_N E
O
M NO_OF_SEAT
S
PNR_NO WAITINGLIST
ADRESS
CONTACT_N
STATUS O
LAB-PROGRAM NO:3
ROW/TUPLE: Each table row/tuple represents a single entity occurrence within the
Entity Set.
COLUMN: Each table column represents an attribute and each column has a distinct
name.
DATA: The intersection of each row and column represents a single data value.
LAB-PROGRAM NO:4
AIM: Creation, Altering and Dropping of database tables (i.e., BUS, TICKET,
PASSENGER, RESERVATION, CANCELLATION, WAITINGLIST).
NOTE: WE CAN HAVE ONLY ONE FOREIGN KEY RELATIONSHIP WITH ONLY
ONE TABLE ONLY. (i.e. here we are using Foreign Key Relationships with 2
different tables in our programs)
Table created.
NOTE: WE CAN HAVE ONLY ONE FOREIGN KEY RELATIONSHIP WITH ONLY
ONE TABLE ONLY. (i.e. here we are using two Foreign Key Relationships with
BUS TABLE)
Table created.
SQL>
Table created.
NOTE: WE CAN HAVE ONLY ONE FOREIGN KEY RELATIONSHIP WITH ONLY
ONE TABLE ONLY. (i.e. here we are using two Foreign Key Relationships with &
PASSENGER & TICKET TABLES)
Table created.
SQL>
NOTE: WE CAN HAVE ONLY ONE FOREIGN KEY RELATIONSHIP WITH ONLY
ONE TABLE ONLY .(i.e here we are using two Foreign Key Relationships with
RESERVATION & TICKET TABLES)
Table created.
NOTE: WE CAN HAVE ONLY ONE FOREIGN KEY RELATIONSHIP WITH ONLY
ONE TABLE ONLY .(i.e here we are using two Foreign Key Relationships with
RESERVATION & TICKET TABLES)
Table created.
SQL>
Table created.
Table altered.
SQL>
Table altered.
no rows selected
SQL>
Table dropped.
Table dropped.
Table dropped.
Table dropped.
Table dropped.
Table dropped.
SQL>
*******************************************************
LAB-PROGRAM NO:5
AIM: DML operations i.e. Selecting, Inserting, Updating and Deleting rows of database
tables (i.e., BUS, TICKET, PASSENGER, RESERVATION, CANCELLATION,
WAITINGLIST).
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
SQL> RUN
1 INSERT INTO BUS
2* VALUES('&BUS_NO','&SOURCE','&DESTINATION')
Enter value for bus_no: AP02-6666
Enter value for source: BANGALORE
Enter value for destination: HYDERABAD
old 2: VALUES('&BUS_NO','&SOURCE','&DESTINATION')
new 2: VALUES('AP02-6666','BANGALORE','HYDERABAD')
1 row created.
SQL> /
Enter value for bus_no: AP02-7777
Enter value for source: KADIRI
Enter value for destination: SHIRIDI
old 2: VALUES('&BUS_NO','&SOURCE','&DESTINATION')
new 2: VALUES('AP02-7777','KADIRI','SHIRIDI')
1 row created.
SQL> /
Enter value for bus_no: AP02-8888
Enter value for source: ANATAPUR
Enter value for destination: KANCHI
old 2: VALUES('&BUS_NO','&SOURCE','&DESTINATION')
new 2: VALUES('AP02-8888','ANATAPUR','KANCHI')
1 row created.
SQL> RUN
1 INSERT INTO BUS
2* VALUES('&BUS_NO','&SOURCE','&DESTINATION')
Enter value for bus_no: AP02-9999
Enter value for source: BANGALORE
Enter value for destination: PUTAAPARTY
old 2: VALUES('&BUS_NO','&SOURCE','&DESTINATION')
new 2: VALUES('AP02-9999','BANGALORE','PUTAAPARTY')
1 row created.
9 rows selected.
SQL>
Table altered.
no rows selected
1 row created.
1 row created.
1 row created.
SQL> RUN
1 INSERT INTO TICKET
2*
VALUES(&TICKET_NO,'&BUS_NO','&JOURNEY_DATE','&SOURCE','&DESTINATI
ON','&DEPT_TIME')
Enter value for ticket_no: 1104
Enter value for bus_no: AP02-4444
Enter value for journey_date: 12/NOV/11
Enter value for source: MADANAPALI
Enter value for destination: OOTY
Enter value for dept_time: 12:30 PM
old 2:
VALUES(&TICKET_NO,'&BUS_NO','&JOURNEY_DATE','&SOURCE','&DESTINATI
ON','&DEPT_TIME')
new 2: VALUES(1104,'AP02-4444','12/NOV/11','MADANAPALI','OOTY','12:30 PM')
1 row created.
SQL> /
Enter value for ticket_no: 1105
Enter value for bus_no: AP02-5555
Enter value for journey_date: 09/APR/11
Enter value for source: TIRUPATI
Enter value for destination: BANGALORE
Enter value for dept_time: 02:30 PM
old 2:
VALUES(&TICKET_NO,'&BUS_NO','&JOURNEY_DATE','&SOURCE','&DESTINATI
ON','&DEPT_TIME')
1 row created.
SQL> RUN
1 INSERT INTO TICKET
2*
VALUES(&TICKET_NO,'&BUS_NO','&JOURNEY_DATE','&SOURCE','&DESTINATI
ON','&DEPT_TIME')
Enter value for ticket_no: 1106
Enter value for bus_no: AP02-6666
Enter value for journey_date: 05/APR/11
Enter value for source: BANGALORE
Enter value for destination: HYDERABAD
Enter value for dept_time: 07:45 AM
old 2:
VALUES(&TICKET_NO,'&BUS_NO','&JOURNEY_DATE','&SOURCE','&DESTINATI
ON','&DEPT_TIME')
new 2: VALUES(1106,'AP02-6666','05/APR/11','BANGALORE ','HYDERABAD','07:45
AM')
1 row created.
SQL> /
Enter value for ticket_no: 1107
Enter value for bus_no: AP02-7777
Enter value for journey_date: 10/APR/11
Enter value for source: KADIRI
Enter value for destination: SHIRIDI
Enter value for dept_time: 05:30 AM
old 2:
VALUES(&TICKET_NO,'&BUS_NO','&JOURNEY_DATE','&SOURCE','&DESTINATI
ON','&DEPT_TIME')
new 2: VALUES(1107,'AP02-7777','10/APR/11','KADIRI','SHIRIDI','05:30 AM')
1 row created.
SQL> /
Enter value for ticket_no: 1108
Enter value for bus_no: AP02-8888
Enter value for journey_date: 20/APR/11
Enter value for source: ANANTAPUR
Enter value for destination: KANCHI
Enter value for dept_time: 01:45 PM
old 2:
VALUES(&TICKET_NO,'&BUS_NO','&JOURNEY_DATE','&SOURCE','&DESTINATI
ON','&DEPT_TIME')
new 2: VALUES(1108,'AP02-8888','20/APR/11','ANANTAPUR','KANCHI','01:45 PM')
1 row created.
SQL> /
Enter value for ticket_no: 1109
Enter value for bus_no: AP02-9999
Enter value for journey_date: 22/APR/11
Enter value for source: BANGALORE
Enter value for destination: PUTAAPARTY
Enter value for dept_time: 09:15 AM
old 2:
VALUES(&TICKET_NO,'&BUS_NO','&JOURNEY_DATE','&SOURCE','&DESTINATI
ON','&DEPT_TIME')
new 2: VALUES(1109,'AP02-9999','22/APR/11','BANGALORE','PUTAAPARTY','09:15
AM')
1 row created.
9 rows selected.
SQL> COMMIT;
Commit complete.
1 row created.
2 VALUES(&PNR_NO,'&NAME',&AGE,'&GENDER','&PPNO');
Enter value for pnr_no: 1222
Enter value for name: SHILPA
Enter value for age: 22
Enter value for gender: F
Enter value for ppno: A0503
old 2: VALUES(&PNR_NO,'&NAME',&AGE,'&GENDER','&PPNO')
new 2: VALUES(1222,'SHILPA',22,'F','A0503')
1 row created.
SQL> /
Enter value for pnr_no: 1223
Enter value for name: KRISHNA
Enter value for age: 25
Enter value for gender: M
Enter value for ppno: A0504
old 2: VALUES(&PNR_NO,'&NAME',&AGE,'&GENDER','&PPNO')
new 2: VALUES(1223,'KRISHNA',25,'M','A0504')
1 row created.
SQL> RUN
1 INSERT INTO PASSENGER
2* VALUES(&PNR_NO,'&NAME',&AGE,'&GENDER','&PPNO')
Enter value for pnr_no: 1224
Enter value for name: MOHAN
Enter value for age: 26
Enter value for gender: M
Enter value for ppno: A0505
old 2: VALUES(&PNR_NO,'&NAME',&AGE,'&GENDER','&PPNO')
new 2: VALUES(1224,'MOHAN',26,'M','A0505')
1 row created.
SQL> /
Enter value for pnr_no: 1225
Enter value for name: SAIDEEPTHI
Enter value for age: 24
Enter value for gender: F
Enter value for ppno: A0506
old 2: VALUES(&PNR_NO,'&NAME',&AGE,'&GENDER','&PPNO')
new 2: VALUES(1225,'SAIDEEPTHI',24,'F','A0506')
1 row created.
1 row created.
SQL> /
Enter value for pnr_no: 1227
Enter value for name: HONEY
Enter value for age: 22
Enter value for gender: F
Enter value for ppno: A0508
old 2: VALUES(&PNR_NO,'&NAME',&AGE,'&GENDER','&PPNO')
new 2: VALUES(1227,'HONEY',22,'F','A0508')
1 row created.
SQL> /
Enter value for pnr_no: 1228
Enter value for name: ARUN
Enter value for age: 25
Enter value for gender: M
Enter value for ppno: A0509
old 2: VALUES(&PNR_NO,'&NAME',&AGE,'&GENDER','&PPNO')
new 2: VALUES(1228,'ARUN',25,'M','A0509')
1 row created.
SQL> /
Enter value for pnr_no: 1229
Enter value for name: MAMATHA
Enter value for age: 23
Enter value for gender: F
Enter value for ppno: A0510
old 2: VALUES(&PNR_NO,'&NAME',&AGE,'&GENDER','&PPNO')
new 2: VALUES(1229,'MAMATHA',23,'F','A0510')
1 row created.
SQL> COMMIT;
Commit complete.
9 rows selected.
SQL>
Table altered.
SQL> COMMIT;
Commit complete.
SQL>
1 row created.
1 row created.
1 row created.
SQL> RUN
1 INSERT INTO RESERVATION
2*
VALUES(&PNR_NO,&TICKET_NO,'&JOURNEY_DATE','&NO_OF_SEATS','&
ADDRESS',&CONTACT_NO,'&STATUS')
Enter value for pnr_no: 1224
Enter value for ticket_no: 1104
Enter value for journey_date: 12/NOV/11
Enter value for no_of_seats: 3
Enter value for address: HNO:1-C-505,MPL
Enter value for contact_no: 8855223311
Enter value for status:
old 2:
VALUES(&PNR_NO,&TICKET_NO,'&JOURNEY_DATE','&NO_OF_SEATS','&
ADDRESS',&CONTACT_NO,'&STATUS')
new 2: VALUES(1224,1104,'12/NOV/11','3','HNO:1-C-505,MPL',8855223311,'')
1 row created.
1 row created.
SQL> RUN
1 INSERT INTO RESERVATION
2*
VALUES(&PNR_NO,&TICKET_NO,'&JOURNEY_DATE','&NO_OF_SEATS','&
ADDRESS',&CONTACT_NO,'&STATUS')
Enter value for pnr_no: 1226
Enter value for ticket_no: 1106
Enter value for journey_date: 05/APR/11
Enter value for no_of_seats: 1
Enter value for address: HNO:3-2-512,ATP
Enter value for contact_no: 3355778899
Enter value for status: YES
old 2:
VALUES(&PNR_NO,&TICKET_NO,'&JOURNEY_DATE','&NO_OF_SEATS','&
ADDRESS',&CONTACT_NO,'&STATUS')
new 2: VALUES(1226,1106,'05/APR/11','1','HNO:3-2-512,ATP',3355778899,'YES')
1 row created.
1 row created.
SQL> RUN
1 INSERT INTO RESERVATION
2*
VALUES(&PNR_NO,&TICKET_NO,'&JOURNEY_DATE','&NO_OF_SEATS','&
ADDRESS',&CONTACT_NO,'&STATUS')
Enter value for pnr_no: 1228
1 row created.
SQL> /
Enter value for pnr_no: 1229
Enter value for ticket_no: 1109
Enter value for journey_date: 22/APR/11
Enter value for no_of_seats: 10
Enter value for address: HNO:5-7-444,MPL
Enter value for contact_no: 9911224433
Enter value for status: YES
old 2:
VALUES(&PNR_NO,&TICKET_NO,'&JOURNEY_DATE','&NO_OF_SEATS','&
ADDRESS',&CONTACT_NO,'&STATUS')
new 2: VALUES(1229,1109,'22/APR/11','10','HNO:5-7-444,MPL',9911224433,'YES')
1 row created.
9 rows selected.
SQL> COMMIT;
Commit complete.
INSERTION OF VALUES INTO CANCELLATION TABLE
Table altered.
SQL>
1 row created.
1 row created.
SQL>
Table altered.
1 row created.
SQL> /
Enter value for pnr_no: 1225
Enter value for ticket_no: 1105
Enter value for journey_date: 09/APR/11
Enter value for no_of_seats: 4
Enter value for address: HNO:2-1-222,TPT
Enter value for contact_no: 9955116677
Enter value for status: YES
old 2:
VALUES(&PNR_NO,&TICKET_NO,'&JOURNEY_DATE','&NO_OF_SEATS','&
ADDRESS',&CONTACT_NO,'&STATUS')
new 2: VALUES(1225,1105,'09/APR/11','4','HNO:2-1-222,TPT',9955116677,'YES')
1 row created.
SQL> /
Enter value for pnr_no: 1228
Enter value for ticket_no: 1108
Enter value for journey_date: 20/APR/11
Enter value for no_of_seats: 4
Enter value for address: HNO:4-3-777,TPT
Enter value for contact_no: 8822334466
Enter value for status: YES
old 2:
VALUES(&PNR_NO,&TICKET_NO,'&JOURNEY_DATE','&NO_OF_SEATS','&
ADDRESS',&CONTACT_NO,'&STATUS')
new 2: VALUES(1228,1108,'20/APR/11','4','HNO:4-3-777,TPT',8822334466,'YES')
1 row created.
SQL>
NOTE:
WHERE CLAUSE IN THE UPDATE COMMAND SELECTS THE TUPLES/ROWS
TO BE MODIFIED FROM A SINGLE RELATION/TABLE.
12 rows selected.
SQL>
1 row updated.
1 row updated.
12 rows selected.
SQL>
NOTE:
WHERE CLAUSE IF ALL RECORDS ( TUPLES/ROWS) TO BE DELETED FROM
SPECIFIED RELATION/TABLE, THEN, NO NEED TO USE
THE WHERE CLAUSE.
1 row deleted.
1 row deleted.
10 rows selected.
****************************************************
LAB-PROGRAM NO:6
AIM: Queries along with Sub Queries on database tables (i.e., BUS, TICKET,
PASSENGER, RESERVATION, CANCELLATION, And WAITINGLIST) using
BETWEEN, ANY, ALL, IN, Exists, NOT EXISTS, UNION, INTERSECT,
Constraints etc...
10 rows selected.
Table altered.
1 row updated.
SQL> /
1 row updated.
2 SET BUS_TYPE='NON_AC'
3 WHERE BUS_NO='AP02-2222';
1 row updated.
1 row updated.
1 row updated.
1 row updated.
1 row updated.
1 row updated.
1 row updated.
1 row updated.
1 row updated.
10 rows selected.
SQL> COMMIT;
Commit complete.
SQL>
-----------------------------------------------------------------------------------------------------------------
CONTACT_NO STATUS
-------------------------------------
1221 1101 25/MAR/11 4 HNO:1-A-501,ATP
9988776655 YES
SQL>
PNR_NO TICKET_NO
--------------------------------------------
1221 1101
1224 1104
1225 1105
1226 1106
1228 1108
PNR_NO TICKET_NO
----------------------------------------
1224 1104
1225 1105
1228 1108
PNR_NO TICKET_NO
--------------------------------------
1221 1101
1226 1106
1224 1104
1225 1105
1228 1108
SQL>
9 rows selected.
PNR_NO NAME
----------------------------------------
1221 CHAITANYA
1222 SHILPA
1223 KRISHNA
1224 MOHAN
1225 SAIDEEPTHI
1226 KIRAN
1227 HONEY
1228 ARUN
1229 MAMATHA
9 rows selected.
SQL>
QUERY-5: Display all the names of Male (M) all passengers only.
NAME
---------------
CHAITANYA
KRISHNA
MOHAN
KIRAN
ARUN
SQL>
QUERY-6: Display the Ticket Numbers and Names of all passengers only.
NAME TICKET_NO
-------------------------------------------------------
CHAITANYA 1101
SHILPA 1102
KRISHNA 1103
MOHAN 1104
SAIDEEPTHI 1105
KIRAN 1106
HONEY 1107
ARUN 1108
MAMATHA 1109
9 rows selected.
SQL>
NAME TICKET_NO
-------------------------------------------------
KRISHNA 1103
KIRAN 1106
SQL>
NAME TICKET_NO
--------------------------------------------------
CHAITANYA 1101
SHILPA 1102
KRISHNA 1103
MAMATHA 1109
SQL>
QUERY-9: Find the Ticket Numbers of passengers whose names which has
‘AI’ in middle or ends with ‘A’.
NAME TICKET_NO
------------------------------------------------
CHAITANYA 1101
SHILPA 1102
KRISHNA 1103
SAIDEEPTHI 1105
MAMATHA 1109
SQL>
NAME TICKET_NO
-----------------------------------------------
SAIDEEPTHI 1105
SQL>
QUERY-11: Find the Names of passengers whose age is between 20 and 25.
9 rows selected.
NAME
---------------
SHILPA
KRISHNA
SAIDEEPTHI
HONEY
ARUN
MAMATHA
6 rows selected.
9 rows selected.
NAME
---------------
SHILPA
SAIDEEPTHI
9 rows selected.
NAME
---------------
ARUN
CHAITANYA
HONEY
KIRAN
KRISHNA
MAMATHA
MOHAN
SAIDEEPTHI
SHILPA
9 rows selected.
SQL>
*****************************************
LAB-PROGRAM NO:7
AIM: Queries using Aggregate functions on database tables (i.e., BUS, TICKET,
PASSENGER, RESERVATION, CANCELLATION, And WAITINGLIST)
( i.e. COUNT, MIN, MAX, SUM & AVG ), GROUP BY, HAVING & creation and
dropping of views.
AGGREGATE FUNCTIONS
COUNT This function is used to Tally (i.e. Count) the number of NON-
NULL values of an attribute
PNR_NO NO_OF_SEATS
----------------------------------------
1221 4
1222 2
1223 6
1224 3
1225 4
1226 1
1227 6
1228 4
1229 10
9 rows selected.
SQL>
COUNT(DISTINCTP.NAME)
---------------------
9
SQL>
QUERY-3: Find the number of Tickets booked for each passenger where the
number of seats is greater than one (i.e. >1).
COUNT(DISTINCT R.TICKET_NO)
-------------------------------------------------
8
SQL>
SUM(C.NO_OF_SEATS)
-------------------------------------
5
SQL>
SQL>
PNR_NO
-----------------------
1221
1222
1223
1224
1225
1226
1227
1228
1229
9 rows selected.
SQL>
COUNT(*)
--------------------
10
SQL>
AVG(P.AGE)
----------
24.6666667
SQL>
MIN(P.AGE)
----------
22
SQL>
MINIMUM_AGE
-----------
22
SQL>
NAME AGE
--------------------------------------
SHILPA 22
SAIDEEPTHI 24
MAMATHA 23
KRISHNA 25
HONEY 22
ARUN 25
6 rows selected.
SQL>
Views
View created.
SQL>
View dropped.
SQL>
*****************************************
LAB-PROGRAM NO:8
NATURAL JOIN
INNER JOIN
OUTER JOIN
LEFT OUTER JOIN
RIGHT OUTER JOIN
FULL OUTER JOIN
JOINS: A join is a query in which data is retrieved from two or more tables.
A join matches data from two or more tables, based on the values of one or
more columns in each table.
Table created.
SQL> edit
Wrote file afiedt.buf
Table created.
EMP_NAME VARCHAR2(15)
CITY VARCHAR2(10)
EMP_NAME VARCHAR2(15)
DEPT_NAME VARCHAR2(15)
SALARY NUMBER(8,2)
INSERTING VALUES:
SQL> edit
Wrote file afiedt.buf
1 row created.
SQL> /
Enter value for emp_name: om
Enter value for dept_name: mumbai
old 1: insert into employee values('&emp_name','&dept_name')
new 1: insert into employee values('om','mumbai')
1 row created.
SQL> run
1* insert into employee values('&emp_name','&dept_name')
Enter value for emp_name: smith
Enter value for dept_name: hyd
old 1: insert into employee values('&emp_name','&dept_name')
1 row created.
SQL> /
Enter value for emp_name: jay
Enter value for dept_name: banglore
old 1: insert into employee values('&emp_name','&dept_name')
new 1: insert into employee values('jay','banglore')
1 row created.
1 row created.
SQL> run
1* insert into employee_salary
values('&emp_name','&dept_name',&salary)
Enter value for emp_name: om
Enter value for dept_name: it
Enter value for salary: 70000
old 1: insert into employee_salary
values('&emp_name','&dept_name',&salary)
new 1: insert into employee_salary values('om','it',70000)
1 row created.
SQL> /
Enter value for emp_name: bill
Enter value for dept_name: computer
Enter value for salary: 8000
1 row created.
SQL> /
Enter value for emp_name: jay
Enter value for dept_name: it
Enter value for salary: 5000
old 1: insert into employee_salary
values('&emp_name','&dept_name',&salary)
new 1: insert into employee_salary values('jay','it',5000)
1 row created.
EMP_NAME CITY
--------------- ----------
hari pune
om mumbai
smith hyd
jay banglore
INNER JOIN: Inner Join returns the matching rows from the tables that
being joined
SQL> edit
Wrote file afiedt.buf
1 select employee.emp_name,employee_salary.salary
2 from employee inner join employee_salary
3* on employee.emp_name= employee_salary.emp_name
SQL> /
EMP_NAME SALARY
--------------- ----------
hari 10000
om 70000
jay 5000
SQL> edit
Wrote file afiedt.buf
1 select *
2 from employee inner join employee_salary
3* on employee.emp_name= employee_salary.emp_name
SQL> /
OUTER JOINS
LEFT OUTER JOIN: The left outer join returns matching rows from the
tables being joined, and also non-matching rows from the left table in the
result and places null values in the attributes that come from the right table.
SQL> edit
Wrote file afiedt.buf
1 select employee.emp_name,salary
2 from employee left outer join employee_salary
3* on employee.emp_name=employee_salary.emp_name
SQL> /
EMP_NAME SALARY
--------------- ----------
hari 10000
om 70000
jay 5000
smith
RIGHT OUTER JOIN: The right outer join returns matching rows from
the tables being joined, and also non-matching rows from the right table in
the result and places null values in the attributes that come from the left
table.
SQL> edit
Wrote file afiedt.buf
1 select *
2 from employee right outer join employee_salary
3* on employee.emp_name=employee_salary.emp_name
4 /
SQL>
SQL> edit
Wrote file afiedt.buf
1 select employee_salary.emp_name,city,salary
2 from employee right outer join employee_salary
3* on employee.emp_name=employee_salary.emp_name
SQL> /
SQL> edit
Wrote file afiedt.buf
1 select employee.emp_name,city,salary
2 from employee right outer join employee_salary
3* on employee.emp_name=employee_salary.emp_name
SQL> /
SQL> edit
Wrote file afiedt.buf
1 select employee.emp_name,city,dept_name,salary
2 from employee right outer join employee_salary
3* on employee.emp_name=employee_salary.emp_name
SQL> /
FULL OUTER JOIN: The full outer join returns matching rows from the
tables being joined, and also non-matching rows from the right table and
left table in the result and places null values in the attributes that come from
the left table and right table.
SQL> edit
Wrote file afiedt.buf
1 select employee.emp_name,city,dept_name,salary
2 from employee full outer join employee_salary
3* on employee.emp_name=employee_salary.emp_name
SQL> /
NATURAL JOIN:
SQL> edit
Wrote file afiedt.buf
1 select *
2* from employee natural join employee_salary
3 /
SQL> edit
Wrote file afiedt.buf
1 select *
2* from employee natural left outer join employee_salary
SQL> /
SQL> edit
Wrote file afiedt.buf
1 select *
2* from employee natural right outer join employee_salary
SQL> /
SQL> edit
Wrote file afiedt.buf
1 select *
2* from employee natural full outer join employee_salary
SQL> /
SQL>
***********************************
LAB-PROGRAM NO:10
LAB-PROGRAM NO:11
1 row created.
SQL> /
Enter value for empno: 1002
Enter value for salary: 1500.00
Enter value for deptno: 10
old 1: INSERT INTO EMPLOYEE VALUES(&EMPNO,&SALARY,&DEPTNO)
new 1: INSERT INTO EMPLOYEE VALUES(1002,1500.00,10)
1 row created.
SQL> /
Enter value for empno: 1003
1 row created.
SQL> RUN
1* INSERT INTO EMPLOYEE VALUES(&EMPNO,&SALARY,&DEPTNO)
Enter value for empno: 1004
Enter value for salary: 6000.25
Enter value for deptno: 20
old 1: INSERT INTO EMPLOYEE VALUES(&EMPNO,&SALARY,&DEPTNO)
new 1: INSERT INTO EMPLOYEE VALUES(1004,6000.25,20)
1 row created.
SQL> /
Enter value for empno: 1005
Enter value for salary: 3550.25
Enter value for deptno: 22
old 1: INSERT INTO EMPLOYEE VALUES(&EMPNO,&SALARY,&DEPTNO)
new 1: INSERT INTO EMPLOYEE VALUES(1005,3550.25,22)
1 row created.
no rows selected
SQL> declare
2 cursor c_emp is select empno,salary from employee where deptno=10;
3 num_empno employee.empno%type;
4 curr_salary employee.salary%type;
5 begin
6 open c_emp;
7 if c_emp%isopen then
8 loop
9 fetch c_emp into num_empno,curr_salary;
10 exit when c_emp%notfound;
11
12 update employee set salary=curr_salary+(curr_salary*0.05)
13 where empno=num_empno;
14
15 insert into emp_raise values(num_empno,sysdate,curr_salary*0.05);
16
17 end loop;
18
19 commit;
20
21 close c_emp;
22
23 else
24
25 dbms_output.put_line('unable to open the cursor');
26
27 end if;
28
29 end;
30
31 /
SQL>
***********************************************************
LAB-PROGRAM NO:12
Writing PL/SQL block for insertion into a table.
INPUT
DECLARE
ENO1 empdet.eno%type;
ENAME1 empdet.name%type;
DEPTNO1 empdet.deptno%type;
BASIC1 empdet.basic%type;
HRA1 empdet.HRA%type;
DA1 empdet.DA%type;
PF1 empdet.pf%type;
NETPAY1 empdet.netpay%type;
BEGIN
ENO1:=&ENO1;
ENAME1:='&ENAME1';
DEPTNO1:=&DEPTNO1;
BASIC1:=&BASIC1;
HRA1:=(B ASIC1*50)/100;
DA1:=(BASIC1*20)/100;
PF1:=(BASIC1*7)/100;
NETPAY1:=BASIC1+HRA1+DA1-PF1;
HRA1,
DA1, PF1, NETPAY1);
END;
RESULT:
SQL> @BASIC
Enter value for eno1: 104
old 11: ENO1:=&ENO1;
new 11: ENO1:=104;
Enter value for ename1: SRINIVAS REDDY
old 12: ENAME1:='&ENAME1';
new 12: ENAME1:='SRINIVAS REDDY';
Enter value for deptno1: 10
old 13: DEPTNO1:=&DEPTNO1;
new 13: DEPTNO1:=10;
Enter value for basic1: 6000
old 14: BASIC1:=&BASIC1;
new 14: BASIC1:=6000;
SQL>/
Enter value for eno1: 105
old 11: ENO1:=&ENO1;
new 11: ENO1:=105;
Enter value for ename1: CIRAJ
old 12: ENAME1:='&ENAME1';
new 12: ENAME1:='CIRAJ';
Enter value for deptno1: 10
old 13: DEPTNO1:=&DEPTNO1;
new 13: DEPTNO1:=10;
Enter value for basic1: 6000
old 14: BASIC1:=&BASIC1;
RESULT
ENO NAME DEPTNO BASIC HRA DA
PF NETPAY
--------- ------------------------------ --------- --------- --------- --------- ---------
-----------------------
101 SANTOSH 10 5000 2500 1000
350 8150
102 SHANKAR 20 5000 2500 1000
350 8150
103 SURESH 20 5500 2750 1100
385 8965
104 SRINIVASA REDDY 10 6000 3000 1200
420 9780
105 CIRAJ 10 6000 3000 1200
420 9780
LAB-PROGRAM NO:13
Armstrong number
INPUT
DECLARE
num number(5);
rem number(5);
s number(5):=0;
num1 number(5);
BEGIN
num:=#
num1:=num;
while(num>0)
loop
rem:=mod(num,10);
s:=s+power(rem,3);
num:=trunc(num/10);
End loop;
if (s=num1)then
dbms_RESULT.put_line(num1||' IS ARMSTRONG NUMBER ');
else
dbms_RESULT.put_line(num1||' IS NOT ARMSTRONG NUMBER ');
End if;
END;
/
RESULT:
SQL>@arm
Enter value for num: 153
old 7: num:=#
new 7: num:=153;
153 IS ARMSTRONG NUMBER
SQL> /
Enter value for num: 123
old 7: num:=#
new 7: num:=123;
123 IS NOT ARMSTRONG NUMBER
LAB-PROGRAM NO:14
Writing a PL/SQL block for checking a number even or odd.
DECLARE
num number(5);
rem number;
BEGIN
num:=#
rem:=mod(num,2);
if rem=0
then
dbms_RESULT.put_line(' Number '||num| ' is Even');
else
dbms_RESULT.put_line(' Number '||num| ' is Odd');
end if;
END;
RESULT:
SQL>start even
Enter value for num: 6
old 5: num:=#
new 5: num:=6;
Number 6 is Even
SQL> /
Enter value for num: 3
old 5: num:=#
new 5: num:=3;
Number 3 is Odd
PL/SQL procedure successfully completed.
LAB-PROGRAM NO:15
SQL> @sum
INPUT truncated to 2 characters
Enter value for num: 456
old 7: num:=#
new 7: num:=456;
SUM OF DIGITS OF 456 IS: 15
PL/SQL procedure successfully completed.
LAB-PROGRAM NO:16
Writing PL/SQL triggers
To write a TRIGGER to ensure that DEPT TABLE does not contain duplicate of null
values in DEPTNO column.
INPUT
CREATE OR RELPLACE TRIGGER trig1 before insert on dept for each row
DECLARE
a number;
BEGIN
if(:new.deptno is Null) then
raise_application_error(-20001,'error::deptno cannot be null');
else
select count(*) into a from dept where deptno=:new.deptno;
if(a=1) then
raise_application_error(-20002,'error:: cannot have duplicate deptno');
end if;
end if;
END;
RESULT:
SQL> @trigger
Trigger created.
SQL> /
Enter value for deptnp: 10
Enter value for dname: manager
Enter value for loc: hyd
old 1: insert into dept values(&deptnp,'&dname','&loc')
new 1: insert into dept values(10,'manager','hyd')
insert into dept values(10,'manager','hyd')
*
ERROR at line 1:
SQL> /
Enter value for deptnp: 50
Enter value for dname: MARKETING
Enter value for loc: HYDERABAD
old 1: insert into dept values(&deptnp,'&dname','&loc')
new 1: insert into dept values(50,'MARKETING','HYDERAB AD')
1 row created.
SQL> select * from dept;
VIWA QUESTIONS
It is a constant value that gives the name, structure of table and the constraints laid on it.
variables with in the object. An object also contains bodies of code that operate on the object.
These bodies of code are called methods. Objects that contain same types of values and the
same methods are grouped together into classes.
A data base schema is specifies by a set of definitions expressed by a special language called
DDL.
It is procedural query language. It consists of a set of operations that take one or two relations
as input and produce a new relation.
39. What is Relational Calculus?
It is an applied predicate calculus specifically tailored for relational databases proposed by
E.F. Codd. E.g. of languages based on it are DSL ALPHA, QUEL.
40. How does Tuple-oriented relational calculus differ from domain-oriented relat ional
calculus
The tuple-oriented calculus uses a tuple variables i.e., variable whose only permitted values
are tuples of that relation. E.g. QUEL
The domain-oriented calculus has domain variables i.e., variables that range over the
underlying domains instead of over relation. E.g. ILL, DEDUCE.
both subsets of R, specifies the following constraint on any relation r of R: if two tuples t1
and t2 exist in r such that t1[X] = t2[X] then t3 and t4 should also exist in r with the
following properties
Ø t3[x] = t4[X] = t1[X] = t2[X]
Ø t3[Y] = t1[Y] and t4[Y] = t2[Y]
Ø t3[Z] = t2[Z] and t4[Z] = t1[Z]
where [Z = (R-(X U Y)) ]
45. What is Lossless join property?
It guarantees that the spurious tuple generation does not occur with respect to relation
schemas after decomposition.
A relation schema R is said to be in 4NF if for ever y Multivalued dependency X Y that holds
over R, one of following is true
Ø X is subset or equal to (or) XY = R.
Ø X is a super key.
55. What is indexing and what are the different kinds of indexing?
Indexing is a technique for determining how quickly specific data can be found.
Types:
56. What is system catalog or catalog relation? How is better known as?
A RDBMS maintains a description of all the data that it contains, information about every
relation and index that it contains. This information is stored in a collection of relations
maintained by the system called metadata. It is also called data dictionary.
57. What is meant by query optimization?
The phase that identifies an efficient execution plan for evaluating a query that has the least
estimated cost is referred to as query optimization.
executed for each row of the parent, this is called a correlated subquery.
A correlated subquery can be easily identified if it contains any references to the parent
subquery columns in its WHERE clause. Columns from the subquery cannot be referenced
anywhere else in the parent query. The following example demonstrates a non-correlated
subquery.
E.g. Select * From CUST Where '10/03/1990' IN (Select ODATE From ORDER Where
CUST.CNUM = ORDER.CNUM)
69. What are the primitive operations common to all record management systems?
Addition, deletion and modification.
70. Name the buffer in which all the commands that are typed in are stored
‘Edit’ Buffer
72. Are the resulting relations of PRODUCT and JOIN operation the same?
No.
PRODUCT: Concatenation of every row in one relation with every row in another.
JOIN: Concatenation of rows from one relation and related rows from another.
75. Which part of the RDBMS takes care of the data dictionary? How
Data dictionar y is a set of tables and database objects that is stored in a special area of the
database and maintained exclusively by the kernel.
78. Define SQL and state the differences between SQL and other conventional
programming Languages
SQL is a nonprocedural language that is designed specifically for data access operations on
normalized relational database structures. The primary difference between SQL and other
conventional programming languages is that SQL statements specify what data operations
should be performed rather than how to perform them.
86. Name two utilities that Oracle provides, which are use for backup and recovery.
Along with the RDBMS software, Oracle provides two utilities that you can use to back up
and restore the database. These utilities are Export and Import.
The Export utility dumps the definitions and data for the specified part of the database to an
operating system binar y file. The Import utility reads the file produced by an export,
recreates the definitions of objects, and inserts the data
If Export and Import are used as a means of backing up and recovering the database, all the
changes made to the database cannot be recovered since the export was performed. The best
you can do is recover the database to the time when the export was last performed.
87. What are stored-procedures? And what are the advantages of using them .
Stored procedures are database objects that perform a user defined operation. A stored
procedure can have a set of compound SQL statements. A stored procedure executes the SQL
commands and returns the result to the client. Stored procedures are used to reduce network
traffic.
88. How are exceptions handled in PL /SQL? Give some of the internal exceptions' name
PL/SQL exception handling is a mechanism for dealing with run-time errors encountered
during procedure execution. Use of this mechanism enables execution to continue if the error
is not severe enough to cause procedure termination.
The exception handler must be defined within a subprogram specification. Errors cause the
Program to raise an exception with a transfer of control to the exception-handler block. After
the exception handler executes, control returns to the block in which the handler was defined.
If there are no more executable statements in the block, control returns to the caller.
User-Defined Exceptions
PL/SQL enables the user to define exception handlers in the declarations area of subprogram
specifications. User accomplishes this by naming an exception as in the following example:
ot_failure EXCEPTION;
In this case, the exception name is ot_failure. Code associated with this handler is written in
the EXCEPTION specification area as follows:
EXCEPTION
when OT_FAILURE then
out_status_code := g_out_status_code;
out_msg := g_out_msg;
The following is an example of a subprogram exception:
EXCEPTION
when NO_DATA_FOUND then
g_out_status_code := 'FAIL';
RAISE ot_failure;
Within this exception is the RAISE statement that transfers control back to the ot_failure
exception handler. This technique of raising the exception is used to invoke all user-defined
exceptions.
System-Defined Exceptions
Exceptions internal to PL/SQL are raised automatically upon error. NO_DATA_FOUND is a
system-defined exception. Table below gives a complete list of internal exceptions.
In addition to this list of exceptions, there is a catch-all exception named OTHERS that traps
all errors for which specific error handling has not been established.
Procedures that are not part of a package are known as stand-alone because they
independently defined. A good example of a stand-alone procedure is one written in a
SQL*Forms application. These types of procedures are not available for reference from other
Oracle tools. Another limitation of stand-alone procedures is that they are compiled at run
time, which slows execution.
102. What are cursors give different types of cursors.
PL/SQL uses cursors for all database information accesses statements. The language supports
the use two types of cursors
Ø Implicit
Ø Explicit
103. What is cold backup and hot backup (in case of Oracle)?
Ø Cold Backup:
It is copying the three sets of files (database files, redo logs, and control file) when the
instance is shut down. This is a straight file copy, usually from the disk directly to tape. You
must shut down the instance to guarantee a consistent copy.
If a cold backup is performed, the only option available in the event of data file loss is
restoring all the files from the latest backup. All work performed on the database since the
last backup is lost.
Ø Hot Backup:
Some sites (such as worldwide airline reservations systems) cannot shut down the database
4NF- A table is in 4NF if and only if, for every one of its non-trivial multivalued dependencies
X →→ Y, X is a super key—that is, X is either a candidate key or a superset thereof.
Sol: 4NF is concerned with a more general type of dependency known as a multivalued
dependency. A table is in 4NF if and only if, for every one of its non-trivial multivalued
dependencies X →→ Y, X is a super key—that is, X is either a candidate key or a superset
thereof.
Multivalued dependencies
If the column headings in a relational database table are divided into three disjoint groupings
X, Y, and Z, then, in the context of a particular row, we can refer to the data beneath each
group of headings as x, y, and z respectively. A multivalued dependency X →→ Y signifies
that if we choose any x actually occurring in the table (call this choice xc), and compile a list
of all the xcyz combinations that occur in the table, we will find that xc is associated with the
same y entries regardless of z.
A trivial multivalued dependency X →→ Y is one in which Y consists of all columns not
belonging to X. That is, a subset of attributes in a table has a trivial multivalued dependency
on the remaining subset of attributes.
A functional dependency is a special case of multivalued dependency. In a functional
dependency X → Y, every x determines exactly one y, never more than one.
Sol: Atomicity: Either all operations of the transaction are properly reflected in the database or
none are.
Consistency: Execution of a transaction in isolation preserves the consistency of the database.
Isolation: Although multiple transactions may execute concurrently, each transaction must be
unaware of other concurrently executing transactions. Intermediate transaction results must be
hidden from other concurrently executed transactions. That is, for every pair of transactions Ti
and Tj, it appears to Ti that either Tj, finished execution before Ti started, or Tj started
execution after Ti finished.
Durability: After a transaction completes successfully, the changes it has made to the
database persist, even if there are system failures.
Sol: A transaction is a unit of program execution that accesses and possibly updates various data
items. To preserve the integrity of data the database system must ensure:
Transaction State
Active –the initial state; the transaction stays in this state while it is executing
Partially committed –after the final statement has been executed.
Failed --after the discovery that normal execution can no longer proceed.
Aborted –after the transaction has been rolled back and the database restored to its state prior
to the start of the transaction. Two options after it has been aborted:
restart the transaction; can be done only if no internal logical error
kill the transaction
Committed –after successful completion
A. Dense Index:
Dense index—Index record appears for every search-key value in the file.
B. Sparse Index:
Sparse Index: contains index records for only some search-key values.
Applicable when records are sequentially ordered on search-key
To locate a record with search-key value Kwe:
Find index record with largest search-key value < K
Search file sequentially starting at the record to which the index record points
C. Primary Index
Primary index: in a sequentially ordered file, the index whose search key specifies the
sequential order of the file.
The search key of a primary index is usually but not necessarily the primary key.
Clustering index: in a sequentially ordered file, the index whose search key specifies the
sequential order of the file. Also called Primary index
Non-clustering index: an index whose search key specifies an order different from the
sequential order of the file. Also called Secondary index.
i) Dead lock?
System is deadlocked if there is a set of transactions such that every transaction in the set is
waiting for another transaction in the set.
******************************************************