DBMS - All Pracs
DBMS - All Pracs
PRACTICAL 1
(Design a database for hospital with a set of patients and a set of medical
doctors. Associate with each patient a log of various tests and examination
conducted.)
DATABASE CREATION
use Olive;
create TABLE DOCTOR
(
SerialNumber int IDENTITY (1,1) PRIMARY KEY,
DOCTORId int NOT NULL UNIQUE,
LastNAME varchar(50) NOT NULL,
FirstNAME varchar(50) NOT NULL,
PHONE varchar(50) NULL,
Specialization varchar(50) NOT NULL,
visits_only_on varchar(9) NULL,
);
select* from DOCTOR;
drop table DOCTOR;
i. find the set of patients who live in “Vashi” and were examined on 8.05.10
--> select* from PATIENT where Area='Vashi' and DATE_of_examination= '2010-05-08'
ii. List the various tests and examination conducted on each patient
--> select Test_performed,PATIENTId,FirstNAME from PATIENT
iii. Find the name of the doctors who visit only on Tuesday
--> select* from DOCTOR where visits_only_on='Tuesday'
PRACTICAL 2
(Design a database of a university registrar’s office which maintains data about
the course offerings, including course number, year, semester, section number,
instructor(s), timings, and classroom.)
DATABASE CREATION
use prac1;
create table COURSE
(
CourseID INT PRIMARY KEY,
YEAR int,
Semester VARCHAR(50),
SectionNumber int,
InstructorID int,
Timings VARCHAR(100),
ClassID int,
CourseName varchar(50) NOT NULL,
FOREIGN KEY (ClassID) REFERENCES CLASSROOM(ClassroomID),
FOREIGN KEY(InstructorID) REFERENCES Instructor(InstructorID)
);
select* from COURSE;
drop TABLE COURSE;
i. Retrieve the name of all courses taught by professor Jain in 2009 and 2010.
select CourseName from COURSE c
where InstructorID in
(select InstructorID from INSTRUCTOR i where InstructorName='Jain' and c.year
in(2009,2010))
iii. Update a course of your choice and delete the course which you don’t like.
UPDATE COURSE
Set Timings= '8:00 AM - 9:30 AM'
WHERE CourseNumber = 101;
DATABASE
EXPERIMENT 12
(Design a database to keep track of information for an art museum. The museum has a
collection of ART_ OBJECTS. Each ART_ OBJECTS has a unique IdNo, an Artist (if
known), a Year (when it was created, if known), a Title, and a Description.
a. Create a view which gives description on artist their ART_ OBJECTS.
b. Give description of all the ART_OBJECTS which were created in the year 2001
c. List all the female artists whose name starts with “Re”
d. Write any one trigger
e. Write any one procedure or function)
SELECT Artist
FROM ArtObject
WHERE Artist LIKE 'Re%'
PRACTICAL 13
(Design a database for a small private airport database that is used to keep track of
airplanes, their owners, airport employees, and pilots
a. List all the pilots of the plane “Kingfisher
b. Retrieve the list of all employees who were appointed between 01.01.2017 and
01.01.2019.
c. Update the database of your choice and delete which you don’t like.
d. Write any one trigger
e. Write any one procedure or function)
SELECT *
FROM Employee
WHERE HireDate BETWEEN '2017-01-01' AND '2019-01-01';
CREATE TRIGGER PreventAirplaneDeletion
ON Airplane
INSTEAD OF DELETE
AS
BEGIN
IF EXISTS (SELECT * FROM deleted d JOIN Pilot p ON d.PilotID = p.PilotID)
BEGIN
RAISERROR ('Cannot delete airplane with associated pilot', 16, 1);
END
ELSE
BEGIN
DELETE FROM Airplane WHERE RegistrationNo IN (SELECT RegistrationNo FROM
deleted);
END
END;
EXPERIMENT 14
(Consider the following relations for a database that keeps track of business trips of
salespersons in a sales office: SALESPERSON(Sid, Name, Start_Year, Dept_No)
TRIP(Sid, From_City, To_City, Departure_Date, Return_Date, Trip_ID) Expense(Trip_ID,
Account, Amount)
a. Give the details (all attributes of Trip relation) for trips that exceeded Rs. 2000 in
expenses
b. Print the ENO of salesman who took trip to ‘Honolulu’
c. Print the total trip expenses incurred by the salesman with ENO = ‘234-56- 7890’
d. Write any one trigger
e. Write any one procedure or function)
SELECT Sid
FROM TRIP
WHERE To_City = 'Honolulu';
SELECT SUM(e.Amount) AS TotalExpenses
FROM Expense e
JOIN TRIP t ON e.Trip_ID = t.Trip_ID
JOIN SALESPERSON s ON t.Sid = s.Sid
WHERE s.Sid = 101;
EXPERIMENT 15 (Consider the following relations for the database that keeps track of
student enrollment in courses and the books opted for each course: Student(StuNo,
Name, Major, Bdate) Course(Course-id, Cname, Dept) Enroll(StudNo, Course-id,
Quarter, Grade) Book_Adoption(Course-id,Quarter,Book_ISBN) Text(Book_ISBN,
Book_Title, Publisher, Author)
a. List number of courses taken by aa students named ‘Ravi Shankar’ in Winter 2018
b. Produce a list of books for courses offered by the ‘CSE’ department that have used
more than two books,
. List any department that has all its adopted books published by ‘TMH Publishing’.
d. Write any one trigger
e. Write any one procedure or function)
SELECT c.Dept
FROM Course c
LEFT JOIN Book_Adoption ba ON c.Course_id = ba.Course_id
LEFT JOIN Text t ON ba.Book_ISBN = t.Book_ISBN
GROUP BY c.Dept
HAVING COUNT(*) = SUM(CASE WHEN t.Publisher = 'TMH Publishing' THEN 1 ELSE 0
END);
(no soln)
EXPERIMENT 16 (Consider the following relations for a database that keeps track of
auto sales in a car dealership Car(Serial_No, Model, Manufacturer, Price)
Options(serial-No, Option-Name, Price) Sales(Salesperson-id, Serial-No. Date, Sale-
Price) Salesperson(Salesperson-id,Name,Phone)
a. List the serial and Model of cars that have no options.
b. For the salesperson named ‘Jane Doe’, list the following information for all the cars
she sold
c. List the name of the salesperson who have no phone.
d. Write any one trigger
e. Write any one procedure or function)
SELECT Name
FROM Salesperson
WHERE Phone IS NULL;
SELECT o.OrderID
FROM DORDER o
LEFT JOIN SHIPMENT s ON o.OrderID = s.OrderID
WHERE DATEDIFF(DAY, o.Odate, s.Ship_date) > 30 OR s.Ship_date IS NULL;
(no result)
SELECT OrderID
FROM DORDER o
WHERE NOT EXISTS (
SELECT w.WarehouseID
FROM WAREHOUSE w
WHERE w.City = 'New York'
EXCEPT
SELECT s.WarehouseID
FROM SHIPMENT s
WHERE s.OrderID = o.OrderID
);
a. Find the name of the employee who joined in ‘Green Green” project
c. Find the name of the employees who work for both project number 1 and project
number 2
SELECT e.Name
FROM Employee e
JOIN Eve j ON e.EID = j.EID
JOIN Project p ON j.Pcode = p.Code
WHERE p.Name = 'Green Green';
SELECT Name
FROM Employee
WHERE EID NOT IN (SELECT DISTINCT EID FROM Emp_Dependent);
SELECT e.Name
FROM Employee e
JOIN Eve j ON e.EID = j.EID
WHERE j.Pcode IN (1, 2)
GROUP BY e.Name
HAVING COUNT(DISTINCT j.Pcode) = 2;
PRACTICAL 19
(Design the library database which is used to keep track of books, borrowers, and book
loans
a. Retrieve the names of all borrowers who do not have any books checked out
b. How many copies of the book titled The Lost Tribe are owned by each library branch?
c. Retrieve the names, address, and number of books checked out for all borrowers
who have more than five books checked out.
d. Write any one trigger
e. Write any one procedure or function )
SELECT b.Name
FROM Borrowers b
LEFT JOIN BookLoans bl ON b.BorrowerID = bl.BorrowerID
WHERE bl.LoanID IS NULL;
EXPERIMENT 20!!!
( Design the library database which is used to keep track of books, borrowers, and book
loans
a. How many copies of the book titled The Lost Tribe are owned by the library branch
whose name is ‘Sharps town’.
b. For each library branch, retrieve the branch name and total number of books loaned
out from that branch
c. Update the database four new books.
use exp20;
CREATE TABLE Books (
BookID INT PRIMARY KEY,
Title VARCHAR(255),
Author VARCHAR(255),
BranchID INT,
FOREIGN KEY (BranchID) REFERENCES LibraryBranch(BranchID)
);
CREATE TABLE Borrowers (
BorrowerID INT PRIMARY KEY,
Name VARCHAR(100),
Address VARCHAR(255)
);
CREATE TABLE BookLoans (
LoanID INT PRIMARY KEY,
BookID INT,
BorrowerID INT,
CheckoutDate DATE,
ReturnDate DATE,
FOREIGN KEY (BookID) REFERENCES Books(BookID),
FOREIGN KEY (BorrowerID) REFERENCES Borrowers(BorrowerID)
);
CREATE TABLE LibraryBranch (
BranchID INT PRIMARY KEY,
Name VARCHAR(100),
Address VARCHAR(255)
);
-- Insert sample library branch data
INSERT INTO LibraryBranch (BranchID, Name, Address) VALUES
(1, 'Central Library', '123 Main St'),
(2, 'Sharps town', '456 Elm St');