DBMS_LAB PRACTICAL-by group 3
DBMS_LAB PRACTICAL-by group 3
Course Instructor
Dr Antima Jain
Assistant Professor Senior Grade 1
School of Computing Science and Engineering
VIT Bhopal University, Sehore
Madhya Pradesh
3. Find the aids of all aircraft that can be used on non-stop flights from Bonn to Madras.
SELECT DISTINCT a.aid
FROM Aircraft a
JOIN Flights f ON a.cruisingrange >= f.distance
WHERE f.from_location = 'Bonn' AND f.to_location = 'Madras';
4. Identify the flights that can be piloted by every pilot whose salary is more than
$100,000.
SELECT f.flno
FROM Flights f
WHERE NOT EXISTS (
SELECT e.eid
FROM Employees e
WHERE e.salary > 100000
AND NOT EXISTS (
SELECT c.aid
FROM Certified c
WHERE c.eid = e.eid AND c.aid = f.aid
)
) ;
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.
SELECT DISTINCT e.ename
FROM Employees e JOIN
Certified c ON e.eid = c.eid JOIN
Aircraft a ON c.aid = a.aid
WHERE a.cruisingrange > 3000
AND e.eid NOT IN (
SELECT c1.eid
FROM Certified c1
JOIN Aircraft a1 ON c1.aid = a1.aid
WHERE a1.aname LIKE '%Boeing%'
);
EXPERIMENT- 2
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.
Creating Database
11. Find the names of sailors who have not reserved boat 103.
SELECT DISTINCT S.SNAME
FROM SAILORS S
WHERE S.SID NOT IN (
SELECT R.SID FROM RESERVES R WHERE R.BID = 103
);
12. Find sailors whose rating is better than some sailor called
Rajesh.
SELECT DISTINCT S.SNAME
FROM SAILORS S
WHERES.RATING>(SELECTRATINGFROMSAILORSWHERESNAME='Rajesh');
13. Find the sailor's with the highest rating using ALL.
SELECT SNAME FROM SAILORS WHERE RATING >= ALL
(SELECT RATING FROM SAILORS);
14. To count number SIDs of sailors in Sailors table
SELECT COUNT(*) AS TotalSailors FROM SAILORS;
23. Find the names of sailors who are older than the oldest sailor
with a rating of 10. SELECT SNAME FROM SAILORS WHERE AGE > (SELECT
MAX(AGE) FROM SAILORS WHERE RATING = 10);
24. Display all the sailors according to their ages.
SELECT * FROM SAILORS ORDER BY AGE;
2. Customers
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY AUTO_INCREMENT,
Name VARCHAR(100),
City VARCHAR(50),
Region VARCHAR(50),
State VARCHAR(50)
);
3. Sales
CREATE TABLE Sales (
SaleID INT PRIMARY KEY AUTO_INCREMENT,
FurnitureID INT,
CustomerID INT,
SaleDateDATE,
Quantity INT,
Discount DECIMAL(5, 2),
TotalIncome DECIMAL(10, 2),
FOREIGN KEY (FurnitureID) REFERENCES Furniture(FurnitureID),
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);
#Inserting Sample Data
Furniture Table Data
INSERT INTO Furniture (Name, Type, Category, Material, Price)
VALUES
('Dining Chair', 'Chair', 'Dining Room', 'Wood', 150.00),
('Office Table', 'Table', 'Office', 'Wood', 300.00),
('Wardrobe', 'Wardrobe', 'Bedroom', 'Marble', 800.00),
('Cabinet', 'Cabinet', 'Kitchen', 'Metal', 250.00),
('Sofa', 'Sofa', 'Living Room', 'Fabric', 600.00);
) IS
BEGIN
END;
/
cleaner_no IN NUMBER
) RETURN VARCHAR2 IS
cleaner_location VARCHAR2(100);
BEGIN
RETURN cleaner_location;
END;
/
cleaner_name VARCHAR2(100);
cleaner_salary NUMBER;
cleaner_location VARCHAR2(100);
BEGIN
getCleanerDetails(113, cleaner_name, cleaner_salary);
DBMS_OUTPUT.PUT_LINE('Expected Quantity: ' || v_qty_expected);
DBMS_OUTPUT.PUT_LINE('Quantity on Hand: ' || v_qty_hand);
END LOOP;
END;
EXPERIMENT-5
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.
-- Create Database
CREATE DATABASE AuditSystem;
USE AuditSystem;
-- Create Tables
CREATE TABLE Client_master (
client_no INT PRIMARY KEY,
name VARCHAR(255),
address VARCHAR(255),
bal_due DECIMAL(10, 2)
);
client_no INT,
name VARCHAR(255),
bal_due DECIMAL(10, 2),
operation VARCHAR(10),
userid VARCHAR(255),
update_time DATETIME DEFAULT CURRENT_TIMESTAMP
);
-- Create Triggers
DELIMITER $$
CREATE TRIGGER after_client_delete
AFTER DELETE ON Client_master
FOR EACH ROW
BEGIN
INSERT INTO Auditclient (client_no, name, bal_due, operation, userid, update_time)
VALUES (OLD.client_no, OLD.name, OLD.bal_due, 'DELETE', USER(), NOW());
END$$
-- Perform Operations
UPDATE Client_master
SET bal_due = 450.00
WHERE client_no = 1;
DELIMITER ;
DECLARE
CURSOR doctor_cursor IS
SELECT doctor_id, patient_id
FROM treatments
INTERSECT
SELECT doctor_id, patient_id
FROM prescriptions;
v_doctor_id treatments.doctor_id%TYPE;
v_patient_id treatments.patient_id%TYPE;
exception_occurred EXCEPTION;
BEGIN
OPEN doctor_cursor;
LOOP
' provides both treatment and prescription to Patient ID ' || v_patient_id || '.');
END LOOP;
CLOSE doctor_cursor;
END;
/
EXPERIMENT-8
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
cleaner_no IN NUMBER,
cleaner_name OUT VARCHAR2
, cleaner_salary OUT NUMBER
) IS
BEGIN
SELECT name, salary INTO cleaner_name, cleaner_salary
FROM cleaners
WHERE cleaner_number = cleaner_no
; END;
/
CREATE OR REPLACE FUNCTION getCleanersLocation( cleaner_no IN NUMBER )
RETURN VARCHAR2 IS
cleaner_location VARCHAR2(100);
BEGIN
SELECT depot_address INTO cleaner_location
FROM cleaners
WHERE cleaner_number = cleaner_no;
RETURN cleaner_location;
END;
/
-- Using the function in an SQL statement
DECLARE
cleaner_name VARCHAR2(100);
cleaner_salary NUMBER;
cleaner_location VARCHAR2(100);
BEGIN
getCleanerDetails(113, cleaner_name, cleaner_salary);
cleaner_location := getCleanersLocation(113);
DBMS_OUTPUT.PUT_LINE('Cleaner Name: ' || cleaner_name);
DBMS_OUTPUT.PUT_LINE('Cleaner Location: ' || cleaner_location);
END;
/
OUTPUT :
Experiment-9
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%.
) IS
BEGIN
FROM cleaners
WHERE cleaner_number = cleaner_no;
END;
DECLARE
cleaner_name VARCHAR2(100);
cleaner_salary NUMBER;
BEGIN
END;
/
OUTPUT :
Experiment 10
Create a Trigger that raises an User Defined Error Message and does not allow the
update and Insert operation in the database.
Explanation:
In MySQL, triggers can be used to define actions that occur automatically when certain database
events (like INSERT, UPDATE, or DELETE) take place. To raise a user-defined error message
and block the operation, we use the SIGNAL statement in the trigger.
Trigger Code:
CODE:
DELIMITER $$
DELIMITER ;
DELIMITER $$
I will now execute this query in MySQL and capture screenshots of the following:
INSERT:-
UPDATE:-
Experiment 11
Assumed Schema:
1. Employees Table:
◦ ◦ eid: Employee ID (Primary Key)
◦ ◦ ename: Employee Name
did: Department ID (Foreign Key)
manager_id: Employee ID of the manager (Foreign Key referring to the same
table)
2. Departments Table:
◦ ◦ did: Department ID (Primary Key)
◦ dname: Department Name
manager_id: Manager ID (Foreign Key referencing Employees.eid)
CODE:
CODE:
CODE:
4. Display the names of each employee with the name of his/her boss:
CODE:
5. Display the names of each employee with the name of his/her boss, with a blank for the boss
of the president:
CODE:
2. Insert sample data to reflect the relationships between employees, departments, and
managers.
3. Output
Task-----Create the following schema and answer the given
queries, create a document of your work with screenshot
and then submit (Query with result).
-- Step 1: Create Database
CREATE DATABASE LabAssessment;
USE LabAssessment;
-- Employee Table
CREATE TABLE Employee (
EmpId INT PRIMARY KEY,
EmpName VARCHAR(50),
Department VARCHAR(10),
ContactNo VARCHAR(15),
EmailId VARCHAR(50),
EmpHeadId INT );
Table :- EmpDept
DeptId DeptName Dept_off DeptHead
E-101 HR Monday 105
E-102 Development Tuesday 101
E-103 Hous Keeping Saturday 103
E-104 Sales Sunday 104
E-105 Purchage Tuesday 104
Table :- EmpSalary
EmpId Salary IsPermanent
101 2000 Yes
102 10000 Yes
103 5000 No
104 1900 Yes
105 2300 Yes
Q-1 Find all the name of the employee whose name's 3rd
character is 'h'.
SELECT EmpName
FROM Employee
WHERE SUBSTRING(EmpName, 3, 1) = 'h';
Q-2 Find employee whose department off is Monday.
SELECT EmpName
FROM Employee
WHERE EmpHeadId IN (
SELECT DeptHead FROM EmpDept WHERE Dept_off =
'Monday'
);
Q-4 Select the name and email of the Dept Head who is
not Permanent.
SELECT EmpName, EmailId
FROM Employee
WHERE EmpId IN (
SELECT DeptHead FROM EmpDept
) AND EmpId IN (
SELECT EmpId
FROM EmpSalary
WHERE IsPermanent = 'No'
);
Q-6 How many project started and finished in the same
year.
SELECT COUNT(*) AS Projects_Same_Year
FROM EmpProject
WHERE StartYear = EndYear;