LAB RECORD (1)
LAB RECORD (1)
Systems
REG.NO :_22BCE10470______________
NAME :_Sanjay.R_________________
BRANCH :_CSE(Core)________________
1
SEMESTER:________Fall Semester_______
INDEX
2
EXP.NO: 01
Create Airline Flight Information System using DDL & DML
DATE:26-07-2024
Aim:
To create a Flight Information System using DDL and DML commands in SQL
Query
Consider the following relations containing airline flight information:
Flights (flno: integer, from: string, to: string, distance: integer, departs: time, arrives: time)
Aircraft (aid: integer, aname: string, cruisingrange: integer)
Certified (eid: integer, aid: integer)
Employees (eid: integer, ename: string, salary: integer)
Note that the Employees relation describes pilots and other kinds of employees as well every
pilot is
certified for some aircraft (otherwise, he or she would not qualify as a pilot), and only pilots are
certified to fly. Write the following queries in SQL
1. Find the eids of pilots certified for some Boeing aircraft.
2. Find the names of pilots certified for some Boeing aircraft.
3. Find the aids of all aircraft that can be used on non-stop flights from Bonn to Madras.
4. Identify the flights that can be piloted by every pilot whose salary is more than $100,000.
5. Find the names of pilots who can operate planes with a range greater than 3,000 miles but are
not certified on any Boeing aircraft.
Table Creation
3
Code of employee table:
SQL> create table employee(
2 eid numeric(10) primary key,
3 ename varchar2(25),
4 salary numeric(10));
1 row created.
SQL> insert into aircraft(aid,aname,cruisingrange) values(&aid,'&aname',&cruisingrange);
Enter value for aid: 2
Enter value for aname: boeing 747
Enter value for cruisingrange: 6000
old 1: insert into aircraft(aid,aname,cruisingrange) values(&aid,'&aname',&cruisingrange)
new 1: insert into aircraft(aid,aname,cruisingrange) values(2,'boeing 747',6000)
1 row created.
4
SQL> insert into aircraft(aid,aname,cruisingrange) values(&aid,'&aname',&cruisingrange);
Enter value for aid: 3
Enter value for aname: rafel
Enter value for cruisingrange: 8000
old 1: insert into aircraft(aid,aname,cruisingrange) values(&aid,'&aname',&cruisingrange)
new 1: insert into aircraft(aid,aname,cruisingrange) values(3,'rafel',8000)
1 row created.
1 row created.
1 row created.
1 row created.
5
SQL> insert into employee(eid,ename,salary) values(&eid,'&ename',&salary);
Enter value for eid: 3
Enter value for ename: prateek
Enter value for salary: 50000
old 1: insert into employee(eid,ename,salary) values(&eid,'&ename',&salary)
new 1: insert into employee(eid,ename,salary) values(3,'prateek',50000)
1 row created.
1 row created.
1 row created.
1 row created.
SQL> insert into certified(eid,aid) values(&eid,&aid);
Enter value for eid: 3
Enter value for aid: 3
old 1: insert into certified(eid,aid) values(&eid,&aid)
new 1: insert into certified(eid,aid) values(3,3)
6
1 row created.
SQL> insert into certified(eid,aid) values(&eid,&aid);
Enter value for eid: 4
Enter value for aid: 4
old 1: insert into certified(eid,aid) values(&eid,&aid)
new 1: insert into certified(eid,aid) values(4,4)
1 row created.
1 row created.
1 row created.
7
SQL> insert into flight(flno,from1,to1,distance,departs,arrives)
values(&flno,'&from1','&to1',&distance,'&departs','&arrives');
Enter value for flno: 3
Enter value for from1: delhi
Enter value for to1: amritsar
Enter value for distance: 500
Enter value for departs: 9:45AM
Enter value for arrives: 1:00PM
old 1: insert into flight(flno,from1,to1,distance,departs,arrives)
values(&flno,'&from1','&to1',&distance,'&departs','&arrives')
new 1: insert into flight(flno,from1,to1,distance,departs,arrives)
values(3,'delhi','amritsar',500,'9:45AM','1:00PM')
1 row created.
Executing Query:
1. Find the eids of pilots certified for some Boeing aircraft.
SQL> select eid from employee where eid in (select eid from certified where aid in (select aid
from aircraft where aname like 'boeing%'));
EID
----------
1
2
ENAME
-------------------------
sanjay
Manan
8
3. Find the aids of all aircraft that can be used on non-stop flights from Bonn to Madras.
SQL> select * from flight where departs=’Bonn’ and arrives=’Madras’;
no rows selected
4. Identify the flights that can be piloted by every pilot whose salary is more than $100,000.
SQL> select * from employee where salary>100000;
5. Find the names of pilots who can operate planes with a range greater than 3,000 miles
but are not certified on any Boeing aircraft.
SQL> select * from employee where eid not in (select eid from certified wher
e aid in (select aid from aircraft where aname like 'boeing%'));
Result:
We have successfully created all the tables and inserted values in them and executed all the
queries.
9
EXP.NO: 02
Create Sailor-Boat Reservation System using SQL
DATE:26-07-2024
Aim:
To create a Sailor-Boat Reservation System using DDL and DML commands in SQL.And
executing the 25 queries on it.
Query
SAILORS(SID:INTEGER, SNAME:STRING, RATING:INTEGER, AGE:REAL)
BOATS (BID:INTEGER,BNAME:STRING, COLOR:STRING)
RESERVES (SID:INTEGER,BID:INTEGER, DAY:DATE)
1.Display names & ages of all sailors.
2.Find all sailors with a rating above 7.
3.Display all the names & colors of the boats.
4.Find all the boats with Red color.
5.Find the names of sailors who have reserved boat number 123.
6.Find SIDs of sailors who have reserved Pink Boat;
7.Find the color of the boats reserved by Rajesh.
8.Find names of the sailors who have reserved at least one boat.
9.Find the names of sailors who have reserved a red or a green boat.
10.Find the names of sailors who have reserved boat 103.
11.Find the names of sailors who have not reserved boat 103.
12.Find sailors whose rating is better than some sailor called Rajesh.
13.Find the sailor's with the highest rating using ALL.
14.To count number SIDs of sailors in Sailors table
15.To count numbers of boats booked in Reserves table.
16.To count number of Boats in Boats table.
17.To find age of Oldest Sailor.
18.To find age of Youngest Sailor.
19.Find the average age of sailors with a rating of 10.
20.Count the number of different sailor names.
21.Find the name and age of the oldest sailor.
22.Count the number of Sailors.
23.Find the names of sailors who are older than the oldest sailor with a rating of 10.
24.Display all the sailors according to their ages.
25.To display names of sailors according to alphabetical order.
10
Table Creation
SQL> create table sailors(
2 sid numeric(4) primary key,
3 sname varchar2(25),
4 rating numeric(2),
5 age numeric(3));
Table created.
Table created.
Table created.
1 row created.
1 row created.
1 row created.
11
Inserting values in boats:
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
12
Executing Query:
1.Display names & ages of all sailors.
SQL> select sname,age from sailors;
SNAME AGE
------------------------- ----------
sanjay 19
rohan 25
prem 22
vimal 23
BNAME COLOR
------------------------- ---------------
laxmi pink
vijay blue
vajra yellow
venki black
no rows selected
5.Find the names of sailors who have reserved boat number 123.
SQL> select sname from sailors natural join reserves natural join boats where bid=123;
no rows selected
13
6.Find SIDs of sailors who have reserved Pink Boat
SQL> select sid from sailors natural join reserves natural join boats where color='pink';
SID
----------
102
no rows selected
8.Find names of the sailors who have reserved at least one boat.
SQL> select distinct sname from sailors natural join reserves natural join boats;
SNAME
-------------------------
rohan
sanjay
vimal
prem
9.Find the names of sailors who have reserved a red or a green boat.
SQL> select sname from sailors natural join reserves natural join boats where color='red' or
color='green';
no rows selected
SNAME
-------------------------
vimal
14
11.Find the names of sailors who have not reserved boat 103.
SQL> select sname from sailors natural join reserves natural join boats where bid!='103';
SNAME
-------------------------
sanjay
rohan
prem
12.Find sailors whose rating is better than some sailor called Rajesh.
SQL> select * from sailors where rating>(select rating from sailors where sname='Rajesh');
no rows selected
SNAME
-------------------------
rohan
COUNT(SID)
----------
4
COUNT(*)
----------
4
15
16.To count number of Boats in Boats table.
SQL> select count(*) from boats;
COUNT(*)
----------
4
MAX(AGE)
----------
25
MIN(AGE)
----------
19
AVG(AGE)
----------
25
SNAME COUNT(SNAME)
------------------------- ------------
rohan 1
sanjay 1
vimal 1
prem 1
16
21.Find the name and age of the oldest sailor.
SQL> select sname , age from sailors where age=(select max(age) from sailors);
SNAME AGE
------------------------- ----------
rohan 25
COUNT(*)
----------
4
23.Find the names of sailors who are older than the oldest sailor with a rating of 10.
SQL> select sname from sailors where age >(select max(age) from sailors where rating=10);
no rows selected
17
25.To display names of sailors according to alphabetical order.
Result:
We successfully created the Sailors, Boats, and Reserves tables using DDL and DML commands
in SQL. And all 25 queries were executed successfully.
18
EXP.NO: 03
Database Design for a Wholesale Furniture Company
DATE:30-07-2024
Aim:
To create a Database for a Wholesale Furniture Company using DDL and DML commands in
SQL.And analyzing the data on it.
Query:
Design the data base for a wholesale furniture company. The database
has to allow to analyze the company’s situation at least with respect to the
Furniture, Customers and Time. Moreover, the company needs to analyze:
the furniture with respect to its type (chair, table, wardrobe, cabinet. . . ),
category (kitchen, living room, bedroom, bathroom, office. . . ) and
material (wood, marble. . . ) the customers with respect to their spatial location, by considering at
least cities, regions and states The company is interested in learning at least the quantity, income
and discount of its sales.
Design:
1. Fact Table:
a. FACT_SALES: This is the central fact table that contains the sales transactions. It
includes quantity, income, and discount as measures.
2. Dimension Tables:
a. DIM_FURNITURE: Contains details about each furniture item.
b. DIM_FURNITURE_TYPE: Stores furniture types (chair, table, wardrobe,
cabinet, etc.).
c. DIM_FURNITURE_CATEGORY: Stores furniture categories (kitchen, living
room, bedroom, etc.).
d. DIM_FURNITURE_MATERIAL: Stores furniture materials (wood, marble, etc.).
e. DIM_CUSTOMER: Contains customer information.
f. DIM_CITY, DIM_REGION, DIM_STATE: These tables form a hierarchy for
customer locations.
g. DIM_DATE: A date dimension table for time-based analysis.
19
Table Creation
CREATE TABLE FACT_SALES (
SalesID NUMBER PRIMARY KEY,
DateID NUMBER,
CustomerID NUMBER,
FurnitureID NUMBER,
Quantity NUMBER,
Income NUMBER(10, 2),
Discount NUMBER(5, 2)
);
Table created.
20
CREATE TABLE DIM_FURNITURE (
FurnitureID NUMBER PRIMARY KEY,
FurnitureName VARCHAR2(100),
FurnitureTypeID NUMBER,
FurnitureCategoryID NUMBER,
FurnitureMaterialID NUMBER
);
Table created.
Table created.
Table created.
Table created.
Table created.
21
CREATE TABLE DIM_CITY (
CityID NUMBER PRIMARY KEY,
CityName VARCHAR2(50),
StateID NUMBER
);
Table created.
Table created.
Table created.
Table created.
Inserting Values:
INSERT INTO DIM_FURNITURE_TYPE (FurnitureTypeID, FurnitureTypeName) VALUES
(1, 'Chair');
1 row inserted.
22
INSERT INTO DIM_FURNITURE_TYPE (FurnitureTypeID, FurnitureTypeName) VALUES
(2, 'Table');
1 row inserted.
INSERT INTO DIM_FURNITURE_TYPE (FurnitureTypeID, FurnitureTypeName) VALUES
(3, 'Wardrobe');
1 row inserted.
INSERT INTO DIM_FURNITURE_TYPE (FurnitureTypeID, FurnitureTypeName) VALUES
(4, 'Cabinet');
1 row inserted.
INSERT INTO DIM_FURNITURE_CATEGORY (FurnitureCategoryID,
FurnitureCategoryName) VALUES (1, 'Kitchen');
1 row inserted.
INSERT INTO DIM_FURNITURE_CATEGORY (FurnitureCategoryID,
FurnitureCategoryName) VALUES (2, 'Living Room');
1 row inserted.
INSERT INTO DIM_FURNITURE_CATEGORY (FurnitureCategoryID,
FurnitureCategoryName) VALUES (3, 'Bedroom');
1 row inserted.
INSERT INTO DIM_FURNITURE_MATERIAL (FurnitureMaterialID, FurnitureMaterialName)
VALUES (1, 'Wood');
1 row inserted.
INSERT INTO DIM_FURNITURE_MATERIAL (FurnitureMaterialID, FurnitureMaterialName)
VALUES (2, 'Marble');
1 row inserted.
INSERT INTO DIM_FURNITURE (FurnitureID, FurnitureName, FurnitureTypeID,
FurnitureCategoryID, FurnitureMaterialID) VALUES (1, 'Dining Chair', 1, 1, 1);
1 row inserted.
INSERT INTO DIM_FURNITURE (FurnitureID, FurnitureName, FurnitureTypeID,
FurnitureCategoryID, FurnitureMaterialID) VALUES (2, 'Coffee Table', 2, 2, 1);
1 row inserted.
INSERT INTO DIM_FURNITURE (FurnitureID, FurnitureName, FurnitureTypeID,
FurnitureCategoryID, FurnitureMaterialID) VALUES (3, 'Wardrobe', 3, 3, 1);
1 row inserted.
INSERT INTO DIM_CUSTOMER (CustomerID, CustomerName, CityID) VALUES (1,
'Sanjay', 1);
1 row inserted.
INSERT INTO DIM_CUSTOMER (CustomerID, CustomerName, CityID) VALUES (2, 'Rita',
2);
1 row inserted.
23
INSERT INTO DIM_CUSTOMER (CustomerID, CustomerName, CityID) VALUES (3,
'Vikram', 3);
1 row inserted.
INSERT INTO DIM_CITY (CityID, CityName, StateID) VALUES (1, 'Mumbai', 1);
1 row inserted.
INSERT INTO DIM_CITY (CityID, CityName, StateID) VALUES (2, 'Delhi', 2);
1 row inserted.
INSERT INTO DIM_CITY (CityID, CityName, StateID) VALUES (3, 'Bangalore', 3);
1 row inserted.
INSERT INTO DIM_STATE (StateID, StateName, RegionID) VALUES (1, 'Maharashtra', 1);
1 row inserted.
INSERT INTO DIM_STATE (StateID, StateName, RegionID) VALUES (2, 'Delhi', 2);
1 row inserted.
INSERT INTO DIM_STATE (StateID, StateName, RegionID) VALUES (3, 'Karnataka', 3);
1 row inserted.
INSERT INTO DIM_REGION (RegionID, RegionName) VALUES (1, 'West');
1 row inserted.
INSERT INTO DIM_REGION (RegionID, RegionName) VALUES (2, 'North');
1 row inserted.
INSERT INTO DIM_REGION (RegionID, RegionName) VALUES (3, 'South');
1 row inserted.
INSERT INTO DIM_DATE (DateID, dDate, dDay, dMonth, dYear) VALUES (1, '2024-01-01',
1, 1, 2024);
1 row inserted.
INSERT INTO DIM_DATE (DateID, dDate, dDay, dMonth, dYear) VALUES (2, '2024-01-02',
2, 1, 2024);
1 row inserted.
INSERT INTO DIM_DATE (DateID, dDate, dDay, dMonth, dYear) VALUES (3, '2024-01-03',
3, 1, 2024);
1 row inserted.
INSERT INTO FACT_SALES (SalesID, DateID, CustomerID, FurnitureID, Quantity, Income,
Discount) VALUES (1, 1, 1, 1, 2, 5000.00, 10.00);
1 row inserted.
INSERT INTO FACT_SALES (SalesID, DateID, CustomerID, FurnitureID, Quantity, Income,
Discount) VALUES (2, 2, 2, 2, 1, 3000.00, 5.00);
1 row inserted.
INSERT INTO FACT_SALES (SalesID, DateID, CustomerID, FurnitureID, Quantity, Income,
Discount) VALUES (3, 3, 3, 3, 1, 10000.00, 15.00);
1 row inserted.
24
Result:
We have successfully created the ER Diagram Design for the problem. And we have created the
tables according to the design and we have inserted values in them.
25
EXP.NO: 04
Database Operations with Cursors and Analysis of Shipments Data
DATE:31-07-2024
Aim:
To create a PL/SQL program is to use an implicit cursor to fetch and display the details of
shipments for a particular Ship_id.
Query
Simple script to backup all SQL server database
Create a database table with the following fields:
Field name Data type
Ship_id Number -- This is the ID of a particular Ship Date_expected
Date The date at which the goods are expected to arrive
Qty_expected Number--The quantity that is supposed to arrive
Description Varchar2 --The description of the items
Color Varchar2--The color of the items
Qty_hand Number–The quantity on hand for these items
Itemrate Number—Price of each item.
Write a PL/SQL program that uses implicit cursor to display the data expected quantity expected,
item description, color and quantity on hand for any particular Ship ID number.
Table Creation:
26
Inserting Values:
INSERT INTO SHIPMENTS (Ship_id, Date_expected, Qty_expected, Description, Color,
Qty_hand, Itemrate) VALUES (1, TO_DATE('2024-08-01', 'YYYY-MM-DD'), 100, 'Laptops',
'Silver', 50, 60000);
1 row inserted.
1 row inserted.
1 row inserted.
1 row inserted.
1 row inserted.
Executing Query:
SET SERVEROUTPUT ON;
DECLARE
v_ship_id SHIPMENTS.Ship_id%TYPE;
v_date_expected SHIPMENTS.Date_expected%TYPE;
v_qty_expected SHIPMENTS.Qty_expected%TYPE;
v_description SHIPMENTS.Description%TYPE;
v_color SHIPMENTS.Color%TYPE;
27
v_qty_hand SHIPMENTS.Qty_hand%TYPE;
BEGIN
v_ship_id := 1;
FOR rec IN (SELECT Date_expected, Qty_expected, Description, Color, Qty_hand
FROM SHIPMENTS
WHERE Ship_id = v_ship_id) LOOP
v_date_expected := rec.Date_expected;
v_qty_expected := rec.Qty_expected;
v_description := rec.Description;
v_color := rec.Color;
v_qty_hand := rec.Qty_hand;
DBMS_OUTPUT.PUT_LINE('Date Expected: ' || v_date_expected);
DBMS_OUTPUT.PUT_LINE('Quantity Expected: ' || v_qty_expected);
DBMS_OUTPUT.PUT_LINE('Description: ' || v_description);
DBMS_OUTPUT.PUT_LINE('Color: ' || v_color);
DBMS_OUTPUT.PUT_LINE('Quantity on Hand: ' || v_qty_hand);
END LOOP;
END;
/
Date Expected: 01-AUG-24
Quantity Expected: 100
Description: Laptops
Color: Silver
Quantity on Hand: 50
Result:
We have successfully used the cursor and accessed the specific row from the table.
28
EXP.NO: 05
Create a transparent audit system using Trigger
DATE:05-08-2024
Aim:
To create a transparent audit system using trigger statements in sql.
Query:
Create a transparent audit system for a table Client_master (client_no, name,
address, Bal_due). The system must keep track of the records that are being
deleted or updated. The functionality being when a record is deleted or
modified, the original record details and the date of operation are stored in
the auditclient(client_no, name, bal_due, operation, userid, update) table,
then the delete or update is allowed to go through.
Table Creation:
CREATE TABLE Client_master (
client_no NUMBER PRIMARY KEY,
name VARCHAR2(100),
address VARCHAR2(200),
Bal_due NUMBER(10,2)
);
29
Executing Query:
CREATE OR REPLACE TRIGGER client_audit_trigger
BEFORE DELETE OR UPDATE ON Client_master
FOR EACH ROW
BEGIN
IF DELETING THEN
INSERT INTO auditclient (client_no, name, bal_due, operation, userid, update_date)
VALUES (:OLD.client_no, :OLD.name, :OLD.Bal_due, 'DELETE', USER, SYSDATE);
ELSIF UPDATING THEN
INSERT INTO auditclient (client_no, name, bal_due, operation, userid, update_date)
VALUES (:OLD.client_no, :OLD.name, :OLD.Bal_due, 'UPDATE', USER, SYSDATE);
END IF;
END;
/
Trigger CLIENT_AUDIT_TRIGGER compiled
INSERT INTO Client_master (client_no, name, address, Bal_due) VALUES (1, 'Sanjay', '123
Gandhi Road, Mumbai', 5000.00);
1 row inserted.
INSERT INTO Client_master (client_no, name, address, Bal_due) VALUES (2, 'Priya Patel',
'456 Nehru Street, Delhi', 7500.50);
1 row inserted.
INSERT INTO Client_master (client_no, name, address, Bal_due) VALUES (3, 'Amit Sharma',
'789 Tagore Lane, Kolkata', 3200.75);
1 row inserted.
INSERT INTO Client_master (client_no, name, address, Bal_due) VALUES (4, 'Deepa Reddy',
'321 Bose Avenue, Bangalore', 9000.25);
1 row inserted.
1 row updated.
30
DELETE FROM Client_master WHERE client_no = 3;
1 row deleted.
Result:
We have successfully created the tables and implemented the transparent audit system with the
help of triggers.
31
EXP.NO: 06
Implement the Cursor to manage integrity of data
DATE:05-08-2024
Aim:
To manage and manipulate data in the supplier and parts tables, ensuring data integrity and
consistency through transactional control and operations like reading, printing, and deleting
rows.
Query:
Using the supplier and parts database, write an cursor program to read and
print all parts in part number, deleting every tenth one as you go, and begin a
new transaction after every tenth row. You can use the foreign key delete
CASCADE rule from parts,commit,rollback and savepoint .
Table Creation:
CREATE TABLE Supplier (
supplier_id NUMBER PRIMARY KEY,
supplier_name VARCHAR2(100) NOT NULL,
contact_name VARCHAR2(100),
phone VARCHAR2(20),
email VARCHAR2(100)
);
32
Inserting Values:
INSERT INTO Supplier (supplier_id, supplier_name, contact_name, phone, email) VALUES (1,
'Sanjay Electronics', 'Sanjay', '+91 9876543210', '[email protected]');
1 row inserted.
INSERT INTO Supplier (supplier_id, supplier_name, contact_name, phone, email) VALUES (2,
'Priya Components', 'Priya Sharma', '+91 8765432109', '[email protected]');
1 row inserted.
1 row inserted.
1 row inserted.
1 row inserted.
Executing Quries:
DECLARE
CURSOR parts_cursor IS
SELECT part_number, part_name, description, unit_price, quantity_in_stock, supplier_id
FROM Parts
ORDER BY part_number;
v_part parts_cursor%ROWTYPE;
v_counter NUMBER := 0;
v_total_processed NUMBER := 0;
v_deleted_parts NUMBER := 0;
BEGIN
OPEN parts_cursor;
LOOP
FETCH parts_cursor INTO v_part;
33
EXIT WHEN parts_cursor%NOTFOUND;
v_counter := v_counter + 1;
v_total_processed := v_total_processed + 1;
DBMS_OUTPUT.PUT_LINE('Part Number: ' || v_part.part_number ||
', Name: ' || v_part.part_name ||
', Price: ' || v_part.unit_price ||
', Stock: ' || v_part.quantity_in_stock);
34
Result:
We have successfully implemented the Cursor program using foreign key delete CASCADE rule
from parts, commit, rollback and savepoint for parts database.
35
EXP.NO: 07
Display an exception for patient Database
DATE:05-08-2024
Aim:
To create and Display an exception for patient Database in sql.
Query:
Assuming a patient should not receive both treatment and prescription from the same doctor,
write a program to find out all the doctor who provide both treatment and prescription to the
same patient. In addition, raise and display an exception if this situation occurs.
Table Creation:
36
CREATE TABLE Prescriptions (
prescription_id NUMBER PRIMARY KEY,
patient_id NUMBER,
doctor_id NUMBER,
prescription_date DATE,
FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),
FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)
);
Inserting Values:
INSERT INTO Patients (patient_id, patient_name) VALUES (1, 'Sanjay');
1 row inserted.
INSERT INTO Patients (patient_id, patient_name) VALUES (2, 'Aarav');
1 row inserted.
INSERT INTO Patients (patient_id, patient_name) VALUES (3, 'Isha');
1 row inserted.
INSERT INTO Doctors (doctor_id, doctor_name) VALUES (1, 'Dr. Ramesh');
1 row inserted.
INSERT INTO Doctors (doctor_id, doctor_name) VALUES (2, 'Dr. Priya');
1 row inserted.
INSERT INTO Doctors (doctor_id, doctor_name) VALUES (3, 'Dr. Amit');
1 row inserted.
INSERT INTO Treatments (treatment_id, patient_id, doctor_id, treatment_date) VALUES (1, 1,
1, TO_DATE('2023-08-01', 'YYYY-MM-DD'));
1 row inserted.
INSERT INTO Treatments (treatment_id, patient_id, doctor_id, treatment_date) VALUES (2, 2,
2, TO_DATE('2023-08-02', 'YYYY-MM-DD'));
1 row inserted.
INSERT INTO Treatments (treatment_id, patient_id, doctor_id, treatment_date) VALUES (3, 3,
3, TO_DATE('2023-08-03', 'YYYY-MM-DD'));
1 row inserted.
INSERT INTO Prescriptions (prescription_id, patient_id, doctor_id, prescription_date) VALUES
(1, 1, 1, TO_DATE('2023-08-05', 'YYYY-MM-DD'));
1 row inserted.
INSERT INTO Prescriptions (prescription_id, patient_id, doctor_id, prescription_date) VALUES
(2, 2, 2, TO_DATE('2023-08-06', 'YYYY-MM-DD'));
1 row inserted.
37
INSERT INTO Prescriptions (prescription_id, patient_id, doctor_id, prescription_date) VALUES
(3, 3, 3, TO_DATE('2023-08-07', 'YYYY-MM-DD'));
1 row inserted.
Executing Query:
DECLARE
CURSOR c1 IS
SELECT
t.patient_id, t.doctor_id, pt.patient_name, d.doctor_name
FROM
Treatments t
JOIN Prescriptions p ON t.patient_id = p.patient_id AND t.doctor_id = p.doctor_id
JOIN Patients pt ON t.patient_id = pt.patient_id
JOIN Doctors d ON t.doctor_id = d.doctor_id;
r c1%ROWTYPE;
BEGIN
OPEN c1;
LOOP
FETCH c1 INTO r;
EXIT WHEN c1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Exception: Dr. ' || r.doctor_name || ' provided both treatment
and prescription to patient ' || r.patient_name || ' (Patient ID: ' || r.patient_id || ').');
END LOOP;
CLOSE c1;
END;
/
Exception: Dr. Dr. Ramesh provided both treatment and prescription to patient Sanjay (Patient
ID: 1).
Exception: Dr. Dr. Priya provided both treatment and prescription to patient Aarav (Patient ID:
2).
Exception: Dr. Dr. Amit provided both treatment and prescription to patient Isha (Patient ID: 3).
38
EXP.NO: 08
PL/SQL Block Function
DATE:05-08-2024
Aim:
To create tables and execute the pl/sql block function queries on them.
Query:
Write a PL/SQL block which includes a procedure getCleanerDetails which accepts a cleaner
number and returns the cleaners name and salary. Create a stored function called
getCleanersLocation. This function takes as input a cleaner’s number and returns the cleaner’s
depot address. Call the function from within an SQL statement to select the cleaner’s name and
location for a particular cleaner.
Table Creation:
39
Inserting Values:
INSERT INTO Cleaners (CleanerNumber, CleanerName, Salary) VALUES (1, 'Sanjay', 30000);
1 row inserted.
INSERT INTO Cleaners (CleanerNumber, CleanerName, Salary) VALUES (2, 'Rajesh', 28000);
1 row inserted.
INSERT INTO Cleaners (CleanerNumber, CleanerName, Salary) VALUES (3, 'Priya', 32000);
1 row inserted.
INSERT INTO Cleaners (CleanerNumber, CleanerName, Salary) VALUES (4, 'Amit', 31000);
1 row inserted.
1 row inserted.
INSERT INTO Depots (CleanerNumber, DepotAddress) VALUES (2, '456, Andheri West,
Mumbai');
1 row inserted.
INSERT INTO Depots (CleanerNumber, DepotAddress) VALUES (3, '789, Anna Nagar,
Chennai');
1 row inserted.
INSERT INTO Depots (CleanerNumber, DepotAddress) VALUES (4, '101, Salt Lake, Kolkata');
1 row inserted.
40
Executing Quries:
CREATE OR REPLACE PROCEDURE getCleanerDetails(
p_CleanerNumber IN NUMBER,
p_CleanerName OUT VARCHAR2,
p_Salary OUT NUMBER
) AS
BEGIN
SELECT CleanerName, Salary
INTO p_CleanerName, p_Salary
FROM Cleaners
WHERE CleanerNumber = p_CleanerNumber;
END;
/
DECLARE
v_CleanerName VARCHAR2(100);
v_Salary NUMBER;
v_CleanerNumber NUMBER := 1;
v_Location VARCHAR2(200);
BEGIN
getCleanerDetails(v_CleanerNumber, v_CleanerName, v_Salary);
41
v_Location := getCleanersLocation(v_CleanerNumber);
DBMS_OUTPUT.PUT_LINE('Cleaner Name: ' || v_CleanerName);
DBMS_OUTPUT.PUT_LINE('Cleaner Salary: ' || v_Salary);
DBMS_OUTPUT.PUT_LINE('Cleaner Location: ' || v_Location);
END;
/
Result:
We have successfully implemented the pl\sql block function.
42
EXP.NO: 09
PL/SQL block procedure
DATE:06-08-2024
Aim:
To create tables and execute the pl/sql block procedurequeries on them.
Query:
Write a PL/SQL block which includes a procedure getCleanerDetails which accepts a cleaner
number and returns the cleaners name and salary. The main block should call the procedure with
cleaner number ‘113’ and output this cleaner’s details including the salary which has been
increased by 10%.
Table Creation:
43
Inserting Values:
INSERT INTO Cleaners (CleanerNumber, CleanerName, Salary) VALUES (1, 'Sanjay', 30000);
1 row inserted.
INSERT INTO Cleaners (CleanerNumber, CleanerName, Salary) VALUES (2, 'Rajesh', 28000);
1 row inserted.
INSERT INTO Cleaners (CleanerNumber, CleanerName, Salary) VALUES (3, 'Priya', 32000);
1 row inserted.
INSERT INTO Cleaners (CleanerNumber, CleanerName, Salary) VALUES (4, 'Amit', 31000);
1 row inserted.
1 row inserted.
INSERT INTO Depots (CleanerNumber, DepotAddress) VALUES (2, '456, Andheri West,
Mumbai');
1 row inserted.
INSERT INTO Depots (CleanerNumber, DepotAddress) VALUES (3, '789, Anna Nagar,
Chennai');
1 row inserted.
INSERT INTO Depots (CleanerNumber, DepotAddress) VALUES (4, '101, Salt Lake, Kolkata');
1 row inserted.
44
Executing Queries:
DECLARE
v_CleanerName VARCHAR2(100);
v_Salary NUMBER;
v_CleanerNumber NUMBER := 1;
v_Location VARCHAR2(200);
BEGIN
getCleanerDetails(v_CleanerNumber, v_CleanerName, v_Salary);
v_Location := getCleanersLocation(v_CleanerNumber);
45
DBMS_OUTPUT.PUT_LINE('Cleaner Name: ' || v_CleanerName);
DBMS_OUTPUT.PUT_LINE('Cleaner Salary: ' || v_Salary);
DBMS_OUTPUT.PUT_LINE('Cleaner Location: ' || v_Location);
END;
/
Result:
We have successfully implemented the pl\sql procedures.
46
EXP.NO: 10
Join Queries
DATE:06-08-2024
Aim:
To create tables and execute the Join Queries on them.
Query:
Join Queries: Assume necessary database schema
Display the name of each employee with his department name.
Display a list of all departments with the employees in each department.
Display all the departments with the manager for that department.
Display the names of each employee with the name of his/her boss.
Display the names of each employee with the name of his/her boss with a blank for the boss of
the president.·
Display the employee number and name of each employee who manages other employees with
the number of people he or she manages.
Repeat the display for the last question, but this time display the rows in descending order of the
number of employees managed.
Table Creation:
47
CREATE TABLE Employees2 (
EmpID NUMBER PRIMARY KEY,
EmpName VARCHAR2(100),
DeptID NUMBER,
MgrID NUMBER,
FOREIGN KEY (DeptID) REFERENCES Departments2(DeptID),
FOREIGN KEY (MgrID) REFERENCES Employees2(EmpID)
);
Inserting Values:
INSERT INTO Departments2 (DeptID, DeptName, MgrID) VALUES (1, 'HR', 201);
1 row inserted.
INSERT INTO Departments2 (DeptID, DeptName, MgrID) VALUES (2, 'IT', 202);
1 row inserted.
INSERT INTO Departments2 (DeptID, DeptName, MgrID) VALUES (3, 'Finance', 203);
1 row inserted.
INSERT INTO Employees2 (EmpID, EmpName, DeptID, MgrID) VALUES (201, 'Sanjay', 1,
NULL);
1 row inserted.
INSERT INTO Employees2 (EmpID, EmpName, DeptID, MgrID) VALUES (202, 'Rajesh', 2,
NULL);
1 row inserted.
INSERT INTO Employees2 (EmpID, EmpName, DeptID, MgrID) VALUES (203, 'Priya', 3,
NULL);
1 row inserted.
48
INSERT INTO Employees2 (EmpID, EmpName, DeptID, MgrID) VALUES (204, 'Amit', 1,
201);
1 row inserted.
INSERT INTO Employees2 (EmpID, EmpName, DeptID, MgrID) VALUES (205, 'Vikram', 1,
201);
1 row inserted.
INSERT INTO Employees2 (EmpID, EmpName, DeptID, MgrID) VALUES (206, 'Anita', 2,
202);
1 row inserted.
INSERT INTO Employees2 (EmpID, EmpName, DeptID, MgrID) VALUES (207, 'Ravi', 2,
202);
1 row inserted.
INSERT INTO Employees2 (EmpID, EmpName, DeptID, MgrID) VALUES (208, 'Kiran', 3,
203);
1 row inserted.
INSERT INTO Employees2 (EmpID, EmpName, DeptID, MgrID) VALUES (209, 'Anjali', 3,
203);
1 row inserted.
49
Executing Quries:
Display the name of each employee with his department name.
SELECT e.EmpName, d.DeptName FROM Employees2 e JOIN Departments2 d ON e.DeptID =
d.DeptID;
50
Display all the departments with the manager for that department.
SELECT d.DeptName, m.EmpName AS ManagerName FROM Departments2 d JOIN
Employees2 m ON d.MgrID = m.EmpID;
Display the names of each employee with the name of his/her boss.
SELECT e.EmpName AS EmployeeName, m.EmpName AS BossName FROM Employees2 e
LEFT JOIN Employees2 m ON e.MgrID = m.EmpID;
51
Display the names of each employee with the name of his/her boss with a blank for the boss
of the president.
SELECT e.EmpName AS EmployeeName,COALESCE(m.EmpName, ' ') AS BossName FROM
Employees2 e LEFT JOIN Employees2 m ON e.MgrID = m.EmpID;
Display the employee number and name of each employee who manages other employees
with the number of people he or she manages.
SELECT e.EmpID, e.EmpName, COUNT(emp.EmpID) AS NumberOfEmployeesManaged
FROM Employees2 e JOIN Employees2 emp ON e.EmpID = emp.MgrID GROUP BY
e.EmpID, e.EmpName;
52
Repeat the display for the last question, but this time display the rows in descending order
of the number of employees managed.
SELECT e.EmpID, e.EmpName, COUNT(emp.EmpID) AS NumberOfEmployeesManaged
FROM Employees2 e JOIN Employees2 emp ON e.EmpID = emp.MgrID GROUP BY
e.EmpID, e.EmpName ORDER BY NumberOfEmployeesManaged DESC;
Result:
We have successfully implemented the tables and executed the join queries.
53
EXP.NO: 11
Triggers
DATE:06-08-2024
Aim:
To create tables and execute the Trigger queries on them.
Query:
Create a Trigger that raises an User Defined Error Message and does not allow the update and
Insert operation in the database.
Table Creation:
1 row inserted.
INSERT INTO Products (ProductID, ProductName, Price) VALUES (2, 'Smartphone', 30000);
1 row inserted.
INSERT INTO Products (ProductID, ProductName, Price) VALUES (3, 'Tablet', 20000);
1 row inserted.
54
Executing Quries:
CREATE OR REPLACE TRIGGER prevent_insert_update
BEFORE INSERT OR UPDATE ON Products
FOR EACH ROW
BEGIN
RAISE_APPLICATION_ERROR(-20001, 'Insert and Update operations are not allowed on
the Products table.');
END;
/
Result:
We have successfully implemented the trigger concept and we have created our own exception
55