DBMS PRACTICAL
DBMS PRACTICAL
Title VARCHAR(100),
Publisher_Name VARCHAR(50),
Pub_Year INT,
No_of_copies INT,
);
Book_id INT,
Author_Name VARCHAR(100),
);
Address VARCHAR(150),
Phone VARCHAR(20)
);
a) 100 Retrieve details of all books in the Book_id, title, name of publisher, authors
SELECT
B.Book_id,
B.Title,
B.Publisher_Name,
BA.Author_Name
FROM
BOOK B
JOIN BOOK_AUTHORS BA ON B.Book_id = BA.Book_id;
OUTPUT:
b) Get the particulars of borrowers who have borrowed more than 3 books, but from Jan
2017 to Jun2017.
c) Delete a book in BOOK table. Update the contents of other tables to reflect this data
Manipulation operations.
For checking:
SELECT*FROM BOOK;
SELECT*FROM BOOK_AUTHORS;
SELECT*FROM BORROWERS;
d) Create a view of all books and its number of copies that are currently available in the
Library.
INSERT INTO employee (S_No, Name, Designation, Branch) VALUES (1, 'Alice', 'Manager',
'HR');
INSERT INTO employee (S_No, Name, Designation, Branch) VALUES (2, 'Bob', 'Developer',
'IT');
INSERT INTO employee (S_No, Name, Designation, Branch) VALUES (3, 'Charlie', 'Tester',
'QA');
a) Develop a query to grant some privileges of employees table into departments table
c) Develop a query to revoke some privileges of employees table from departments table
SAVEPOINT before_update;
ROLLBACK TO before_update;
e) Demonstrate the user defined procedure for the above employee database
CREATE OR REPLACE PROCEDURE Display_Employees AS
BEGIN
FOR rec IN (SELECT * FROM Employees) LOOP
DBMS_OUTPUT.PUT_LINE('ID: ' || rec.emp_id || ' Name: ' || rec.emp_name);
END LOOP;
END;
5. Create the following tables, Event (eventid, name, description,city) Participant (playerid,
name, eventid, gender, year) Prizes (prizeid, prize-money, eventid, rank,year) Winners
(prizeid, playerid)
a) Choose appropriate primary keys and foreign keys for the tables.
c) Retrieve the name of events where all prize winners are females
SELECT E.name
FROM Event E
JOIN Participant P ON E.eventid = P.eventid
JOIN Winners W ON P.playerid = W.playerid
GROUP BY E.eventid, E.name
HAVING COUNT(DISTINCT CASE WHEN gender = 'Female' THEN playerid END) =
COUNT(DISTINCT playerid);
SELECT E.name
FROM Event E
JOIN Prizes Pr ON E.eventid = Pr.eventid
JOIN Winners W ON Pr.prizeid = W.prizeid
JOIN Participant P ON W.playerid = P.playerid
GROUP BY E.eventid, E.name
HAVING COUNT(DISTINCT CASE WHEN P.gender = 'Female' THEN P.playerid END) =
COUNT(DISTINCT P.playerid);
d) Create a non-updatable view to retrieve the names of all participants who won 1st
prizes along with their event names
e) Write a trigger to make sure that for every new event created, 3 prizes are created in
prizes table. (1st prize - 1500, 2nd - 1000, 3rd 500)
SELECT Mov_Title
FROM MOVIES M
JOIN DIRECTOR D ON M.Dir_id = D.Dir_id
WHERE D.Dir_Name = 'XXXX';
b) Find the movie names where one or more actors acted in two or more movies.
SELECT M.Mov_Title
FROM MOVIES M
JOIN MOVIE_CAST MC ON M.Mov_id = MC.Mov_id
WHERE MC.Act_id IN (
SELECT Act_id
FROM MOVIE_CAST
GROUP BY Act_id
HAVING COUNT(DISTINCT Mov_id) >= 2
);
c) List all actors who acted in a movie before 2010 and also in a movie after 2015 (use
JOIN operation).
a) Compute the total number of male and female students in each semester and in each
section.
b) Calculate the Finalmark (average of best two test marks) and update the
corresponding table for all students.
UPDATE MARKS
SET Finalmark = (GREATEST(Test1 + Test2, Test1 + Test3, Test2 + Test3)) / 2;
UPDATE BANK
SET Balance = 5000
WHERE ROWID IN (SELECT ROWID FROM BANK WHERE ROWNUM = 2);
Cust_Name Account_No
Alice 101
David 104
b) Display the names and account types of all the customers whose account balance is
more than 10,000.
OUTPUT:
Cust_Name Account_Type
Alice Savings
Charlie Savings
Eva Savings
d) Display Account_No, Cust_Name and Branch of all the customers whose account
balance is less than 1,000.
Output:
ORDER Table
C_ID P_ID P_Name P_COST
a) List the names and addresses of all the customers who have ordered products of
costmore than 500.
OUTPUT:
Name Address
b) List the names of all the products ordered whose cost is 1,000 or more.
SELECT DISTINCT P_Name
FROM ORDER
WHERE P_COST >= 1000;
OUTPUT:
P_Name
Laptop
Keyboard
Printer
c) List the product names which are ordered by customers of "City = Delhi".
OUTPUT:
P_Name
Laptop
Mouse
Monitor
Printer
OUTPUT:
OUTPUT:
Total Cost for Alice: 55400
(Laptop: 55000 + Mouse: 400 = 55400)
11. Create the tables SALESMAN (Salesman_id, Name, City, Commission), CUSTOMER
(Customer_id, Cust_Name, City, Grade,Salesman_id), ORDERS (Ord_No, Purchase_Amt,
Ord_Date, Customer_id, Salesman_id)
SALESMAN
CUSTOMER
ORDERS
SELECT Salesman_id
FROM CUSTOMER
GROUP BY Salesman_id
HAVING COUNT(*) > 1;
SALESMAN_ID
-----------
1000
b) List all salesmen and indicate those who have and don’t have customers in their cities
(Use UNION operation.)
UNION
NAME STATUS
-------------------------------------------------- -----------
Arun No Customer
c) Create a view that finds the salesman who has the customer with the highest order of a
day.
View created.
d) Perform the DELETE operation by removing salesman with id 1000. All his orders must
also be deleted.
0 rows deleted.
2 rows deleted.
1 row deleted.
Update a row
UPDATE BANK
SET Balance = 600000
WHERE Acc_No = 101;
1 row updated.
Delete a row
DELETE FROM BANK
WHERE Acc_No = 103;
1 row deleted.
b) Create a PL SQL program that uses the where clause and having clause to retrieve
above 10 lakhs depositor details.
BEGIN
FOR rec IN (
SELECT Cust_Name, SUM(Balance) AS Total_Balance
FROM BANK
GROUP BY Cust_Name
HAVING SUM(Balance) > 1000000
)
LOOP
DBMS_OUTPUT.PUT_LINE('Name: ' || rec.Cust_Name || ' | Balance: ' || rec.Total_Balance);
END LOOP;
END;
/
Name: Priya | Balance: 1200000
PL/SQL procedure successfully completed.
13. a) Create a set of tables for cargo service database, add foreign key constraints and
incorporate referential integrity.
INSERT INTO BOO VALUES (1, 'Distributed Database', 'Pearson', 2022, 5);
1 row created.
INSERT INTO BOO VALUES (2, 'Operating Systems', 'McGraw Hill', 2021, 3);
1 row created.
INSERT INTO BOO VALUES (3, 'Distributed Database Systems', 'Oxford', 2023, 7);
1 row created.
SELECT *
FROM BOO
WHERE Title LIKE '%Distributed Database%';
b) Write a procedure to insert books details in library management system after
purchasing books.
15. a) Create a Product, Sales and purchase table using DDL and DML commands
b) Create a procedure to add 20 records to each table in the database mentioned above.
-- Sales table
INSERT INTO SALE VALUES (100 + i, i, SYSDATE, i + 2);
-- Purchase table
INSERT INTO PURCHSE VALUES (200 + i, i, SYSDATE, i + 5);
END LOOP;
16. a) Using DDL and DML commands, create the employee_personal, Salary, and
Department tables. In addition, determine the minimum, maximum, total, and average
salaries in the database mentioned above.
Function created.
17. a) Create an online purchase database using DDL and DML commands. Use sub
queries to present the information about the items you've purchased.
Create Tables
CREATE TABLE Ite(
Item_ID INT PRIMARY KEY,
Item_Name VARCHAR(50),
Price DECIMAL(10,2),
Stock INT
);
Table created.
b) Write PL SQL Triggers to display available items after a successful purchase and also
display the available items before purchasing.
UPDATE Ite
SET Stock = Stock - :NEW.Quantity
WHERE Item_ID = :NEW.Item_ID;
18. a) Create Omni bus reservation database using DDL and DML commands and also
display the results after applying join operation.
Create Tables
CREATE TABLE Bus (
Bus_ID INT PRIMARY KEY,
Bus_Name VARCHAR(50),
Total_Seats INT
);
Table created.
19. Create a database for a scooter manufacturing company and use the having clause to
display the scooter models based on price.
INSERT INTO Manufacturer VALUES (1, 'ManufacturerX', 'New York'); 1 row created.
INSERT INTO Manufacturer VALUES (2, 'ManufacturerY', 'Los Angeles'); 1 row created.
COMMIT;
END;
/
Procedure created.
20. a) Create and insert records in the flight and passenger tables, and then use various
join operations to display the records.
b) Write a procedure to display month name while passing the month as number
parameter. (Example: if pass parameter as 1 it should display as January.)
21. a) Create a student database table, add constraints (primary key, check), insert rows,
update and delete rows using DDL and DML commands.
b) Create a PL SQL program that uses the where clause to apply the moderation strategy
to those who all have more than 30 marks.
COMMIT;
DBMS_OUTPUT.PUT_LINE('Moderation applied to students with more than 30 marks.');
END;
/
22. a) Create a set of tables for Bus reservation database, add foreign key constraints and
incorporate referential integrity.
b) Create PL SQL triggers for bus reservation system cancellation and reservation actions.
SELECT *
FROM Books
WHERE Title LIKE '%Machine Learning%';
COMMIT;
DBMS_OUTPUT.PUT_LINE('Book details updated successfully.');
END;
/
27. a) Create wedding hall reservation database using DDL and DML commands and also
display the results after applying join operation.
INSERT INTO Customer VALUES (1, 'John Doe', '123 Elm St');
INSERT INTO Customer VALUES (2, 'Jane Smith', '456 Oak St');
COMMIT;
DBMS_OUTPUT.PUT_LINE('Capacity reduced by ' || p_Percentage || '% for Hall ID ' ||
p_Hall_ID);
END;
/
28. Create a database for a car manufacturing company and use the having clause to
display the car models based on price.
b) Write a procedure to insert and update the records in the above database.
COMMIT;
DBMS_OUTPUT.PUT_LINE('Car record inserted.');
END;
/
COMMIT;
DBMS_OUTPUT.PUT_LINE('Car price updated.');
END;
/
29. Create and insert records in the student and course tables, and then use various join
operations to display the records.
PROGRAM:
OUTPUT:
SELECT * FROM Student;
VARIOUS JOINS:
INNER JOIN:
LEFT OUTER JOIN:
b) Write a procedure to display day name while passing the day as number parameter.
(Example: if pass parameter as 1 it should display as Sunday.)
TEXT FILE:plsql29.sql(notepad)
CREATE OR REPLACE PROCEDURE Get_Day_Name(day_num IN NUMBER) AS
BEGIN
CASE day_num
WHEN 1 THEN DBMS_OUTPUT.PUT_LINE('Sunday');
WHEN 2 THEN DBMS_OUTPUT.PUT_LINE('Monday');
WHEN 3 THEN DBMS_OUTPUT.PUT_LINE('Tuesday');
WHEN 4 THEN DBMS_OUTPUT.PUT_LINE('Wednesday');
WHEN 5 THEN DBMS_OUTPUT.PUT_LINE('Thursday');
WHEN 6 THEN DBMS_OUTPUT.PUT_LINE('Friday');
WHEN 7 THEN DBMS_OUTPUT.PUT_LINE('Saturday');
ELSE DBMS_OUTPUT.PUT_LINE('Invalid day number');
END CASE;
END;
/
TO RUN:
SET SERVEROUTPUT ON;
@"C:\Users\Lenovo\Documents\DBMS\plsql29.sql";
EXEC Get_Day_Name(1);
OUTPUT:
Sunday
--------------
EXEC Get_Day_Name(3);
OUTPUT:
Tuesday