0% found this document useful (0 votes)
2 views

LAB RECORD (1)

The document outlines a series of experiments for a Database Management Systems course at VIT Bhopal University, detailing various SQL tasks such as creating tables and executing queries. It includes specific experiments like creating an Airline Flight Information System and a Sailor-Boat Reservation System, along with the SQL commands used for table creation and data insertion. Each experiment is organized with an aim, queries to be executed, and results achieved.

Uploaded by

Abinash Dash
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

LAB RECORD (1)

The document outlines a series of experiments for a Database Management Systems course at VIT Bhopal University, detailing various SQL tasks such as creating tables and executing queries. It includes specific experiments like creating an Airline Flight Information System and a Sailor-Boat Reservation System, along with the SQL commands used for table creation and data insertion. Each experiment is organized with an aim, queries to be executed, and results achieved.

Uploaded by

Abinash Dash
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

VIT BHOPAL UNIVERSITY

Bhopal-Indore Highway, Kothrikalan, Sehore


Madhya Pradesh - 466114

CSE3001 – Database Management

Systems

REG.NO :_22BCE10470______________
NAME :_Sanjay.R_________________
BRANCH :_CSE(Core)________________

1
SEMESTER:________Fall Semester_______

INDEX

Ex.NO DATE EXPERIMENT NAME PAGE NO.

Create Airline Flight Information System using


1. 26-07-2024 3
DDL & DML

2. 26-07-2024 Create Sailor Information using DDL & DML 10

Design the data base for a wholesale furniture


3. 30-07-2024 19
company
Implement PL/SQL program uses implicit
4. 31-07-2024 26
cursor

5. 5-08-2024 Create a transparent audit system using Trigger 29

Write a cursor program for supplier and parts


6. 5-08-2024 32
database

7. 5-08-2024 Display an exception for patient Database 36

8. 5-08-2024 PL/SQL block function 39

9. 6-08-2024 PL/SQL block procedure 43

10. 6-08-2024 Join Queries 47

11. 6-08-2024 Triggers 54

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

Code of flight table:


SQL> create table flight(
2 flno numeric(4) primary key,
3 from1 varchar2(25),
4 to1 varchar2(25),
5 distance numeric(10),
6 departs varchar2(50),
7 arrives varchar2(50));

Code of aircraft table:


SQL> create table aircraft(
2 aid numeric(5) primary key,
3 aname varchar2(50),
4 cruisingrange numeric(10));

3
Code of employee table:
SQL> create table employee(
2 eid numeric(10) primary key,
3 ename varchar2(25),
4 salary numeric(10));

Code of certified table:


SQL> create table certified(
2 eid numeric(10),
3 aid numeric(5),
4 foreign key(aid) references aircraft,
5 foreign key(eid) references employee);

Inserting value into tables:

Inserting values into aircraft table:

SQL> insert into aircraft(aid,aname,cruisingrange) values(&aid,'&aname',&cru


isingrange);
Enter value for aid: 1
Enter value for aname: boeing
Enter value for cruisingrange: 5000
old 1: insert into aircraft(aid,aname,cruisingrange) values(&aid,'&aname',&cruisingrange)
new 1: insert into aircraft(aid,aname,cruisingrange) values(1,'boeing',5000)

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.

SQL> insert into aircraft(aid,aname,cruisingrange) values(&aid,'&aname',&cru


isingrange);
Enter value for aid: 4
Enter value for aname: privatejet
Enter value for cruisingrange: 10000
old 1: insert into aircraft(aid,aname,cruisingrange) values(&aid,'&aname',&cruisingrange)
new 1: insert into aircraft(aid,aname,cruisingrange) values(4,'privatejet',10000)

1 row created.

Inserting values into employee table:


SQL> insert into employee(eid,ename,salary) values(&eid,'&ename',&salary);
Enter value for eid: 1
Enter value for ename: sanjay
Enter value for salary: 80000
old 1: insert into employee(eid,ename,salary) values(&eid,'&ename',&salary)
new 1: insert into employee(eid,ename,salary) values(1,'sanjay',80000)

1 row created.

SQL> insert into employee(eid,ename,salary) values(&eid,'&ename',&salary);


Enter value for eid: 2
Enter value for ename: manan
Enter value for salary: 90000
old 1: insert into employee(eid,ename,salary) values(&eid,'&ename',&salary)
new 1: insert into employee(eid,ename,salary) values(2,'manan',90000)

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.

SQL> insert into employee(eid,ename,salary) values(&eid,'&ename',&salary);


Enter value for eid: 4
Enter value for ename: vimal
Enter value for salary: 5000000
old 1: insert into employee(eid,ename,salary) values(&eid,'&ename',&salary)
new 1: insert into employee(eid,ename,salary) values(4,'vimal',5000000)

1 row created.

Inserting values into certified table:


SQL> insert into certified(eid,aid) values(&eid,&aid);
Enter value for eid: 1
Enter value for aid: 2
old 1: insert into certified(eid,aid) values(&eid,&aid)
new 1: insert into certified(eid,aid) values(1,2)

1 row created.

SQL> insert into certified(eid,aid) values(&eid,&aid);


Enter value for eid: 2
Enter value for aid: 1
old 1: insert into certified(eid,aid) values(&eid,&aid)
new 1: insert into certified(eid,aid) values(2,1)

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.

Inserting values into flight table:


SQL> insert into flight(flno,from1,to1,distance,departs,arrives)
values(&flno,'&from1','&to1',&distance,'&departs','&arrives');
Enter value for flno: 1
Enter value for from1: coimbatore
Enter value for to1: chennai
Enter value for distance: 400
Enter value for departs: 6:00PM
Enter value for arrives: 9:00AM
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(1,'coimbatore','chennai',400,'6:00PM','9:00')

1 row created.

SQL> insert into flight(flno,from1,to1,distance,departs,arrives)


values(&flno,'&from1','&to1',&distance,'&departs','&arrives');
Enter value for flno: 2
Enter value for from1: delhi
Enter value for to1: bhopal
Enter value for distance: 450
Enter value for departs: 8:00Am
Enter value for arrives: 12:30Pm
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(2,'delhi','bhopal',450,'8:00Am','12:30Pm')

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

2. Find the names of pilots certified for some Boeing aircraft.


SQL> select ename from employee where eid in (select eid from certified wher
e aid in (select aid from aircraft where aname like 'boeing%'));

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;

EID ENAME SALARY


---------- ------------------------- ----------
4 vimal 5000000

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%'));

EID ENAME SALARY


---------- ------------------------- ----------
3 prateek 50000
4 vimal 5000000

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.

SQL> create table boats(


2 bid numeric(4) primary key,
3 bname varchar(25),
4 color varchar(15));

Table created.

SQL> create table reserves(


2 sid numeric(4),
3 bid numeric(4),
4 day varchar(10),
5 foreign key(sid) references sailors,
6 foreign key(bid) references boats);

Table created.

Inserting values in sailors:


SQL> insert into sailors values(101,'sanjay',9,19);

1 row created.

SQL> insert into sailors values(102,'rohan',10,25);

1 row created.

SQL> insert into sailors values(103,'prem',5,22);

1 row created.

SQL> insert into sailors values(104,'vimal',7,23);


1 row created.

11
Inserting values in boats:

SQL> insert into boats values(101,'laxmi','pink');

1 row created.

SQL> insert into boats values(102,'vijay','blue');

1 row created.

SQL> insert into boats values(103,'vajra','yellow');

1 row created.

SQL> insert into boats values(104,'venki','black');

1 row created.

Inserting values in reserves:


SQL> insert into reserves values(101,102,'monday');

1 row created.

SQL> insert into reserves values(102,101,'wednesday');

1 row created.

SQL> insert into reserves values(103,104,'friday');

1 row created.

SQL> insert into reserves values(104,103,'sunday');

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

2.Find all sailors with a rating above 7.

SQL> select * from sailors where rating>7;

SID SNAME RATING AGE


---------- ------------------------- ---------- ----------
101 sanjay 9 19
102 rohan 10 25

3.Display all the names & colors of the boats.


SQL> select bname , color from boats;

BNAME COLOR
------------------------- ---------------
laxmi pink
vijay blue
vajra yellow
venki black

4.Find all the boats with Red color.


SQL> select * from boats where color='red';

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

7.Find the color of the boats reserved by Rajesh.


SQL> select color from sailors natural join reserves natural join boats where sname='Rajesh';

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

10.Find the names of sailors who have reserved boat 103.


SQL> select sname from sailors natural join reserves natural join boats where bid='103';

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

13.Find the sailor's with the highest rating using ALL.


SQL> select sname from sailors where rating>=all(select rating from sailors);

SNAME
-------------------------
rohan

14.To count number SIDs of sailors in Sailors table


SQL> select count(sid) from sailors;

COUNT(SID)
----------
4

15.To count numbers of boats booked in Reserves table.


SQL> select count(*) from reserves;

COUNT(*)
----------
4

15
16.To count number of Boats in Boats table.
SQL> select count(*) from boats;

COUNT(*)
----------
4

17.To find age of Oldest Sailor.


SQL> select max(age) from sailors;

MAX(AGE)
----------
25

18.To find age of Youngest Sailor.


SQL> select min(age) from sailors;

MIN(AGE)
----------
19

19.Find the average age of sailors with a rating of 10.


SQL> select avg(age) from sailors where rating=10;

AVG(AGE)
----------
25

20.Count the number of different sailor names.


SQL> select distinct sname , count(sname) from sailors group by sname;

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

22.Count the number of Sailors.

SQL> select count(*) from sailors;

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

24.Display all the sailors according to their ages.


SQL> select * from sailors order by age;

SID SNAME RATING AGE


---------- ------------------------- ---------- ----------
101 sanjay 9 19
103 prem 5 22
104 vimal 7 23
102 rohan 10 25

17
25.To display names of sailors according to alphabetical order.

SQL> select * from sailors order by sname;

SID SNAME RATING AGE


---------- ------------------------- ---------- ----------
103 prem 5 22
102 rohan 10 25
101 sanjay 9 19
104 vimal 7 23

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.

CREATE TABLE DIM_FURNITURE_TYPE (


FurnitureTypeID NUMBER PRIMARY KEY,
FurnitureTypeName VARCHAR2(50)
);

Table created.

CREATE TABLE DIM_FURNITURE_CATEGORY (


FurnitureCategoryID NUMBER PRIMARY KEY,
FurnitureCategoryName VARCHAR2(50)
);

Table created.

CREATE TABLE DIM_FURNITURE_MATERIAL (


FurnitureMaterialID NUMBER PRIMARY KEY,
FurnitureMaterialName VARCHAR2(50)
);

Table created.

CREATE TABLE DIM_CUSTOMER (


CustomerID NUMBER PRIMARY KEY,
CustomerName VARCHAR2(100),
CityID NUMBER
);

Table created.

21
CREATE TABLE DIM_CITY (
CityID NUMBER PRIMARY KEY,
CityName VARCHAR2(50),
StateID NUMBER
);

Table created.

CREATE TABLE DIM_REGION (


RegionID NUMBER PRIMARY KEY,
RegionName VARCHAR2(50)
);

Table created.

CREATE TABLE DIM_STATE (


StateID NUMBER PRIMARY KEY,
StateName VARCHAR2(50),
RegionID NUMBER
);

Table created.

CREATE TABLE DIM_DATE (


DateID NUMBER PRIMARY KEY,
dDate VARCHAR2(10),
dDay NUMBER,
dMonth NUMBER,
dYear NUMBER
);

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:

CREATE TABLE SHIPMENTS (


Ship_id NUMBER PRIMARY KEY,
Date_expected DATE,
Qty_expected NUMBER,
Description VARCHAR2(255),
Color VARCHAR2(50),
Qty_hand NUMBER,
Itemrate NUMBER
);

Table SHIPMENTS created.

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.

INSERT INTO SHIPMENTS (Ship_id, Date_expected, Qty_expected, Description, Color,


Qty_hand, Itemrate) VALUES (2, TO_DATE('2024-08-05', 'YYYY-MM-DD'), 200,
'Smartphones', 'Black', 150, 30000);

1 row inserted.

INSERT INTO SHIPMENTS (Ship_id, Date_expected, Qty_expected, Description, Color,


Qty_hand, Itemrate) VALUES (3, TO_DATE('2024-08-10', 'YYYY-MM-DD'), 300,
'Headphones', 'Red', 100, 2000);

1 row inserted.

INSERT INTO SHIPMENTS (Ship_id, Date_expected, Qty_expected, Description, Color,


Qty_hand, Itemrate) VALUES (4, TO_DATE('2024-08-15', 'YYYY-MM-DD'), 400, 'Televisions',
'Black', 80, 50000);

1 row inserted.

INSERT INTO SHIPMENTS (Ship_id, Date_expected, Qty_expected, Description, Color,


Qty_hand, Itemrate) VALUES (5, TO_DATE('2024-08-20', 'YYYY-MM-DD'), 500, 'Gaming
Consoles', 'White', 200, 40000);

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

PL/SQL procedure successfully completed.

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)
);

Table CLIENT_MASTER created.

CREATE TABLE auditclient (


client_no NUMBER,
name VARCHAR2(100),
bal_due NUMBER(10,2),
operation VARCHAR2(10),
userid VARCHAR2(30),
update_date DATE
);

Table AUDITCLIENT created.

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.

UPDATE Client_master SET Bal_due = 5500.00 WHERE client_no = 1;

1 row updated.

30
DELETE FROM Client_master WHERE client_no = 3;

1 row deleted.

SELECT * FROM auditclient;

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)
);

Table SUPPLIER created.

CREATE TABLE Parts (


part_number VARCHAR2(20) PRIMARY KEY,
part_name VARCHAR2(100) NOT NULL,
description VARCHAR2(255),
unit_price NUMBER(10,2),
quantity_in_stock NUMBER,
supplier_id NUMBER,
CONSTRAINT fk_supplier
FOREIGN KEY (supplier_id)
REFERENCES Supplier(supplier_id)
ON DELETE CASCADE
);

Table PARTS created.

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.

INSERT INTO Parts (part_number, part_name, description, unit_price, quantity_in_stock,


supplier_id) VALUES ('P001', 'Microprocessor', 'High-speed processor', 150.00, 100, 1);

1 row inserted.

INSERT INTO Parts (part_number, part_name, description, unit_price, quantity_in_stock,


supplier_id) VALUES ('P002', 'RAM Module', '8GB DDR4', 75.50, 200, 1);

1 row inserted.

INSERT INTO Parts (part_number, part_name, description, unit_price, quantity_in_stock,


supplier_id) VALUES ('P003', 'SSD', '500GB Solid State Drive', 120.00, 150, 2);

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);

IF MOD(v_counter, 10) = 0 THEN


DELETE FROM Parts WHERE part_number = v_part.part_number;
v_deleted_parts := v_deleted_parts + 1;
DBMS_OUTPUT.PUT_LINE('Deleted Part Number: ' || v_part.part_number);
COMMIT;
DBMS_OUTPUT.PUT_LINE('Transaction committed. Starting a new transaction.');
SAVEPOINT sanjay_savepoint;
END IF;
END LOOP;
CLOSE parts_cursor;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Processing completed.');
DBMS_OUTPUT.PUT_LINE('Total parts processed: ' || v_total_processed);
DBMS_OUTPUT.PUT_LINE('Total parts deleted: ' || v_deleted_parts);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
ROLLBACK TO sanjay_savepoint;
RAISE;
END;
/

Part Number: P001, Name: Microprocessor, Price: 150, Stock: 100


Part Number: P002, Name: RAM Module, Price: 75.5, Stock: 200
Part Number: P003, Name: SSD, Price: 120, Stock: 150
Processing completed.
Total parts processed: 3
Total parts deleted: 0

PL/SQL procedure successfully completed.

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:

CREATE TABLE Patients (


patient_id NUMBER PRIMARY KEY,
patient_name VARCHAR2(100)
);

Table PATIENTS created.

CREATE TABLE Doctors (


doctor_id NUMBER PRIMARY KEY,
doctor_name VARCHAR2(100)
);

Table DOCTORS created.

CREATE TABLE Treatments (


treatment_id NUMBER PRIMARY KEY,
patient_id NUMBER,
doctor_id NUMBER,
treatment_date DATE,
FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),
FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)
);

Table TREATMENTS created.

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)
);

Table PRESCRIPTIONS created.

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).

PL/SQL procedure successfully completed.


Result:
We have successfully created the tables and implemented and displayed the exception.

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:

CREATE TABLE Cleaners (


CleanerNumber NUMBER PRIMARY KEY,
CleanerName VARCHAR2(100),
Salary NUMBER
);

Table CLEANERS created.

CREATE TABLE Depots (


CleanerNumber NUMBER PRIMARY KEY,
DepotAddress VARCHAR2(200)
);

Table DEPOTS created.

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.

INSERT INTO Depots (CleanerNumber, DepotAddress) VALUES (1, '123, MG Road,


Bangalore');

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;
/

Procedure GETCLEANERDETAILS compiled

CREATE OR REPLACE FUNCTION getCleanersLocation(


p_CleanerNumber IN NUMBER
) RETURN VARCHAR2 AS
v_DepotAddress VARCHAR2(200);
BEGIN
SELECT DepotAddress
INTO v_DepotAddress
FROM Depots
WHERE CleanerNumber = p_CleanerNumber;
RETURN v_DepotAddress;
END;
/
Function GETCLEANERSLOCATION compiled

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;
/

Cleaner Name: Sanjay


Cleaner Salary: 30000
Cleaner Location: 123, MG Road, Bangalore

PL/SQL procedure successfully completed.

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:

CREATE TABLE Cleaners (


CleanerNumber NUMBER PRIMARY KEY,
CleanerName VARCHAR2(100),
Salary NUMBER
);

Table CLEANERS created.

CREATE TABLE Depots (


CleanerNumber NUMBER PRIMARY KEY,
DepotAddress VARCHAR2(200)
);

Table DEPOTS created.

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.

INSERT INTO Depots (CleanerNumber, DepotAddress) VALUES (1, '123, MG Road,


Bangalore');

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:

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;
/

Procedure GETCLEANERDETAILS compiled

CREATE OR REPLACE FUNCTION getCleanersLocation(


p_CleanerNumber IN NUMBER
) RETURN VARCHAR2 AS
v_DepotAddress VARCHAR2(200);
BEGIN
SELECT DepotAddress
INTO v_DepotAddress
FROM Depots
WHERE CleanerNumber = p_CleanerNumber;
RETURN v_DepotAddress;
END;
/
Function GETCLEANERSLOCATION compiled

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;
/

Cleaner Name: Sanjay


Cleaner Salary: 30000
Cleaner Location: 123, MG Road, Bangalore

PL/SQL procedure successfully completed.

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:

CREATE TABLE Departments2 (


DeptID NUMBER PRIMARY KEY,
DeptName VARCHAR2(100),
MgrID NUMBER
);

Table DEPARTMENTS2 created.

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)
);

Table EMPLOYEES2 created.

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;

Display a list of all departments with the employees in each department.


SELECT d.DeptName, e.EmpName FROM Departments2 d LEFT JOIN Employees2 e ON
d.DeptID = e.DeptID ORDER BY d.DeptName, e.EmpName;

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:

CREATE TABLE Products (


ProductID NUMBER PRIMARY KEY,
ProductName VARCHAR2(100),
Price NUMBER
);

Table PRODUCTS created.

Inserting the values:


INSERT INTO Products (ProductID, ProductName, Price) VALUES (1, 'Laptop', 50000);

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;
/

Trigger PREVENT_INSERT_UPDATE compiled

UPDATE Products SET Price = 55000 WHERE ProductID = 1;

Error starting at line : 1 in command -


UPDATE Products SET Price = 55000 WHERE ProductID = 1
Error at Command Line : 1 Column : 8
Error report -
SQL Error: ORA-20001: Insert and Update operations are not allowed on the Products table.
ORA-06512: at "SANJ.PREVENT_INSERT_UPDATE", line 2
ORA-04088: error during execution of trigger 'SANJ.PREVENT_INSERT_UPDATE'

Result:
We have successfully implemented the trigger concept and we have created our own exception

55

You might also like