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

Week 1_merged_merged (1) (1) (1)

The document is a term work report for a DBMS Lab submitted by Shreya Paithani as part of her Bachelor of Technology program. It includes various SQL exercises such as creating tables, inserting data, applying constraints, and performing queries related to a hospital database and employee management. The report also contains acknowledgments, a certificate of authenticity, and structured SQL solutions for assignments and lab work.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Week 1_merged_merged (1) (1) (1)

The document is a term work report for a DBMS Lab submitted by Shreya Paithani as part of her Bachelor of Technology program. It includes various SQL exercises such as creating tables, inserting data, applying constraints, and performing queries related to a hospital database and employee management. The report also contains acknowledgments, a certificate of authenticity, and structured SQL solutions for assignments and lab work.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

Term work

of

DBMS Lab (PCS-503)


Submitted in partial fulfillment of the requirement for the V semester

Bachelor of Technology
By
SHREYA PAITHANI

22012686
Under the Guidance of
Mr. Rahul Singh
Assistant Professor
Department of CSE

DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
GRAPHIC ERA HILL UNIVERSITY, BHIMTAL CAMPUS
SATTAL ROAD, P.O. BHOWALI DISTRICT
NAINITAL-263132
CERTIFICATE

The term work of Java Programming Lab , being submitted by SHREYA PAITHANI D/O
Mr. Suraj Singh University Roll Number 2261647 to Graphic Era Hill University, Bhimtal
Campus for the award of bona fide work carried out by her. She has worked under my
guidance and supervision and fulfilled the requirement for the submission of this work report.

(Mr. Rahul Singh) (Dr. Ankur Singh Bisht)

Assistant Professor HOD, CSE Dept.


ACKNOWLEDGEMENT

I take immense pleasure in thanking Honorable Mr .Rahul Singh (Assistant

Professor, Dept. of CSE, GEHU Bhimtal Campus) for allowing me to carry out this practical

work under his excellent and optimistic supervision. This has all been possible due to his

novel inspiration, able guidance and useful suggestions that have helped me in developing

my subject concepts as a student.

I want to extend thanks to our President Prof. (Dr.) Kamal Ghanshala for providing

us all infrastructure and facilities to work in need without which this work would not be

possible.

(SHREYA PAITHANI)
WEEK
1

1. Create a Scenario based ER-Models with the entities. (Hospital


Details like: Wards, Patients, Doctor, Bills etc)
SOLUTION :
CREATE TABLE Hospital (
Hospital_ID INT PRIMARY KEY,
Name VARCHAR(50),
Location VARCHAR(100)
);
CREATE TABLE Ward (
Ward_ID INT PRIMARY KEY,
Hospital_ID INT,
Ward_Name VARCHAR(50),
Capacity INT,
FOREIGN KEY (Hospital_ID) REFERENCES Hospital(Hospital_ID)
);
CREATE TABLE Patient (
Patient_ID INT PRIMARY KEY,
Ward_ID INT,
Name VARCHAR(50),
Age INT,
Gender VARCHAR(10),
Admission_Date DATE,
FOREIGN KEY (Ward_ID) REFERENCES Ward(Ward_ID)
);
CREATE TABLE Doctor (
Doctor_ID INT PRIMARY KEY,
Name VARCHAR(50),
Specialty VARCHAR(50),
Phone_Number VARCHAR(15)
);
CREATE TABLE Bill (
Bill_ID INT PRIMARY KEY,
Patient_ID INT,
Total_Amount DECIMAL(10, 2),
Payment_Status VARCHAR(20),
FOREIGN KEY (Patient_ID) REFERENCES Patient(Patient_ID)
);
OUTPUT:
3. Insert random data in each column of all the table.
SOLUTION:
INSERT INTO Hospital (Hospital_ID, Name, Location) VALUES
(1, 'Sunrise Hospital', 'Bangalore'),(2, 'Green Valley Hospital',
'Mumbai');
INSERT INTO Ward (Ward_ID, Hospital_ID, Ward_Name, Capacity)
VALUES (1, 1, 'General Ward', 20), (2, 1, 'ICU', 5),(3, 2, 'Pediatric
Ward', 15);
INSERT INTO Patient (Patient_ID, Ward_ID, Name, Age, Gender,
Admission_Date) VALUES (1, 1, 'Aarav', 30, 'Male', '2024-10-01'),
(2, 2, 'Diya', 25, 'Female', '2024-10-03'), (3, 3, 'Kiran', 12, 'Male',
'2024-10-04');
INSERT INTO Doctor (Doctor_ID, Name, Specialty, Phone_Number)
VALUES (1, 'Dr. Anjali', 'Cardiologist', '9876543210'),(2, 'Dr.
Ramesh', 'Pediatrician', '9876543211');
INSERT INTO Bill (Bill_ID, Patient_ID, Total_Amount,
Payment_Status) VALUES (1, 1, 5000.00, 'Paid'),(2, 2, 15000.00,
'Pending'),(3, 3, 8000.00, 'Paid');
INSERT INTO Doctor_Patient (Doctor_ID, Patient_ID) VALUES
(1, 1), (2, 3);

OUTPUT:
4. Update the table by applying some conditions.(For example:
using alter command)
SOLUTION :
ALTER TABLE Patient ADD Contact_Number VARCHAR(15);
UPDATE Patient SET Contact_Number = '9998887776'
WHERE Patient_ID = 1;
OUTPUT:
5. Apply the DELETE and DROP command, and then review the
results.
SOLUTION:
DELETE FROM bill WHERE Patient_ID = 2;
DROP TABLE Doctor_Patient;
OUTPUT:
WEEK
2

1. Create a user and provide the GRANT privileges to the user on the
database then REVOKE the given privileges.
SOLUTION:
CREATE USER 'hospital_user'@'localhost' IDENTIFIED BY 'password123';
GRANT ALL PRIVILEGES ON hospital_db.* TO 'hospital_user'@'localhost';
REVOKE ALL PRIVILEGES ON hospital_db.* FROM 'hospital_user'@'localhost';

OUTPUT:
2. Insert any five records in the previous schema and apply the
rollback. Also check the results.
SOLUTION :
START TRANSACTION;
INSERT INTO Patient (Patient_ID, Ward_ID, Name, Age, Gender,
Admission_Date, Contact_Number) VALUES
(4, 1, 'Vikas', 45, 'Male', '2024-10-06', '9876543212'),
(5, 3, 'Meera', 28, 'Female', '2024-10-06', '9876543213'),
(6, 2, 'Raj', 32, 'Male', '2024-10-06', '9876543214'),
(7, 1, 'Pooja', 23, 'Female', '2024-10-06', '9876543215'),
(8, 3, 'Kabir', 36, 'Male', '2024-10-06', '9876543216');
ROLLBACK;
SELECT * FROM Patient WHERE Patient_ID >= 3;
OUTPUT:
3.Add default, check, unique and not null constraints to the schema.
SOLUTION:
ALTER TABLE Patient MODIFY Age INT NOT NULL;
ADD CONSTRAINT chk_age CHECK (Age > 0),
ADD CONSTRAINT unique_contact UNIQUE (Contact_Number),
MODIFY Gender VARCHAR(10) DEFAULT 'Unknown';

OUTPUT:
4. Insert NULL values and check the results.

SOLUTION:
INSERT INTO Patient (Patient_ID, Ward_ID, Name, Age, Gender, Admission_Date,
Contact_Number)
VALUES (9, 1, 'Priya', NULL, NULL, '2024-10-06', NULL);

OUTPUT:
5. Add duplicate value and try to make a column as primary key,
check what happen to the table
SOLUTION:
INSERT INTO Patient (Patient_ID, Ward_ID, Name, Age, Gender,
Admission_Date, Contact_Number) VALUES (10, 2, 'Sohail', 29, 'Male', '2024-
10-06', '9876543215');
ALTER TABLE Patient ADD PRIMARY KEY (Contact_Number);

OUTPUT:
ASSIGNMENT 1

1. Create Table
1. Create a table named Employees with the following columns: o EmployeeID (integer,
primary key, auto-increment) o FirstName (varchar, 50 characters) o LastName (varchar, 50
characters) o HireDate (date) o Salary (decimal, 10, 2)

2. Create a table named Departments with the following columns: o DepartmentID (integer,
primary key) o DepartmentName (varchar, 100 characters) o Location (varchar, 100 characters)

Solution Query:
CREATE TABLE Employees (

EmployeeID INT PRIMARY KEY AUTO_INCREMENT,

FirstName VARCHAR(50),

LastName VARCHAR(50) NOT NULL,

HireDate DATE,

Salary DECIMAL(10, 2)

);

CREATE TABLE Departments (

DepartmentID INT PRIMARY KEY,

DepartmentName VARCHAR(100),

DepartmentLocation VARCHAR(100)

);
2. Alter Table
3. Add a column Email (varchar, 100 characters) to the Employees table.

4. Change the data type of the Salary column in the Employees table to decimal(12, 2).

5. Rename the Location column in the Departments table to DepartmentLocation

Solution Query:

ALTER TABLE Employees ADD Email VARCHAR(100);

ALTER TABLE Employees MODIFY Salary DECIMAL(12, 2);

ALTER TABLE Departments RENAME COLUMN Location TO DepartmentLocation;


3. Drop Table
6. Drop the Departments table from the database.

7. Drop the Email column from the Employees table.

Solution Query:
DROP TABLE Departments;

ALTER TABLE Employees DROP COLUMN Email;

4. Constraints
8. Create a unique constraint on the Email column in the Employees table.

9. Add a foreign key constraint to the Employees table linking DepartmentID to


DepartmentID in the Departments table.

10. Add a NOT NULL constraint to the LastName column in the Employees table.

Solution Query:
ALTER TABLE Employees ADD CONSTRAINT UC_Email UNIQUE(Email);

ALTER TABLE Employees

ADD CONSTRAINT FK_Department

FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID);

ALTER TABLE Employees MODIFY LastName VARCHAR(50) NOT NULL;


5. Indexes
11. Create an index on the LastName column in the Employees table.

12. Create a unique index on the DepartmentName column in the Departments table.

Solution Query:
CREATE INDEX idx_LastName ON Employees(LastName);

CREATE UNIQUE INDEX idx_DepartmentName ON Departments(DepartmentName);


6. Table Relationships
13. Create a Projects table with the following columns: o ProjectID (integer, primary key) o
ProjectName (varchar, 100 characters) o StartDate (date) o EndDate (date) o DepartmentID
(integer)

14. Add a foreign key constraint to the Projects table linking DepartmentID to DepartmentID
in the Departments table.

Solution Query:
CREATE TABLE Projects (

ProjectID INT PRIMARY KEY,

ProjectName VARCHAR(100),

StartDate DATE,

EndDate DATE,

DepartmentID INT,

FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID)

);
8. Table Modification
18. Alter the Employees table to set a default value of 1000.00 for the Salary column.

19. Change the HireDate column in the Employees table to allow NULL values.

20. Drop the foreign key constraint from the Employees table that links to the Departments
table.

Solution Query:
ALTER TABLE Employees ALTER COLUMN Salary SET DEFAULT 1000.00;

ALTER TABLE Employees MODIFY HireDate DATE NULL;

ALTER TABLE Employees DROP FOREIGN KEY FK_Department;


ASSIGNMENT 2

1. Find all employees whose job title starts with 'Manager' and were hired
between January 1, 2022, and December 31, 2023.
SOLUTION :
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
JobTitle VARCHAR(100),
HireDate DATE,
Email VARCHAR(100),
PhoneNumber VARCHAR(20),
Salary DECIMAL(10, 2)
);
SELECT *FROM Employees WHERE JobTitle LIKE 'Manager%'
AND HireDate BETWEEN '2022-01-01' AND '2023-12-31';
OUTPUT:
2. Retrieve all products where the product name contains 'Pro' and the
price is between $50 and $150.
SOLUTION:
CREATE TABLE Products (
ProductID INT PRIMARY KEY,
ProductName VARCHAR(100),
Price DECIMAL(10, 2)
);
SELECT *FROM Products WHERE ProductName LIKE '%Pro%'
AND Price BETWEEN 50 AND 150;
OUTPUT:
3. List all employees whose email address starts with 'johndoe' and their
salary is between $40,000 and $70,000.
SOLUTION:
SELECT *FROM Employees WHERE Email LIKE 'johndoe%'
AND Salary BETWEEN 40000 AND 70000;
OUTPUT:
4. Find all departments where the department name contains 'IT' and the
budget is between $10,000 and $50,000.
SOLUTION:
CREATE TABLE Departments (
DepartmentID INT PRIMARY KEY,
DepartmentName VARCHAR(100),
Budget DECIMAL(15, 2)
);
SELECT *
FROM Departments
WHERE DepartmentName LIKE '%IT%'
AND Budget BETWEEN 10000 AND 50000;

OUTPUT:
5. Retrieve all projects where the project name ends with 'Launch' and the
start date is between January 1, 2024, and June 30, 2024.
SOLUTION:
CREATE TABLE Projects (
ProjectID INT PRIMARY KEY,
ProjectName VARCHAR(100),
StartDate DATE
);
SELECT *
FROM Projects
WHERE ProjectName LIKE '%Launch'
AND StartDate BETWEEN '2024-01-01' AND '2024-06-30';
OUTPUT:
6. List all employees whose phone number contains '555' and whose hire
date is between January 1, 2021, and December 31, 2022.
SOLUTION:
SELECT *FROM Employees
WHERE PhoneNumber LIKE '%555%'
AND HireDate BETWEEN '2021-01-01' AND '2022-12-31';

OUTPUT:
7. Find all customers with an address that starts with '123 Main St' and who
made purchases totaling between $200 and $1,000 in the last year.
SOLUTION:
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
CustomerName VARCHAR(100),
Address VARCHAR(200),
TotalPurchases DECIMAL(10, 2),
LastPurchaseDate DATE
);
SELECT * FROM Customers WHERE Address LIKE '123 Main St%'
AND TotalPurchases BETWEEN 200 AND 1000
AND LastPurchaseDate BETWEEN DATEADD(YEAR, -1, GETDATE())
AND GETDATE();

OUTPUT:
8. Retrieve all employees whose first name contains 'Ann' and their salary is
between $30,000 and $60,000.
SOLUTION:
SELECT *FROM Employees WHERE FirstName LIKE '%Ann%'
AND Salary BETWEEN 30000 AND 60000;
OUTPUT:
9. List all orders where the order description includes 'Special Offer' and
the order date is between July 1, 2023, and December 31, 2023.
SOLUTION:
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
OrderDescription VARCHAR(200),
OrderDate DATE
);
SELECT *
FROM Orders
WHERE OrderDescription LIKE '%Special Offer%'
AND OrderDate BETWEEN '2023-07-01' AND '2023-12-31';

OUTPUT:
10. Find all suppliers whose name contains 'Tech' and their contract end
date is between January 1, 2024, and December 31, 2024.
SOLUTION:
CREATE TABLE Suppliers (
SupplierID INT PRIMARY KEY,
SupplierName VARCHAR(100),
ContractEndDate DATE
);
SELECT *
FROM Suppliers
WHERE SupplierName LIKE '%Tech%'
AND ContractEndDate BETWEEN '2024-01-01' AND '2024-12-31';

OUTPUT:
ASSIGNMENT 3

1. Retrieve all rows from the Customers table sorted by LastName in


ascending order.

SOLUTION:
CREATE TABLE Customers (
-> CustomerID INT PRIMARY KEY,
-> CustomerName VARCHAR(255),
-> LastName VARCHAR(255),
-> Email VARCHAR(255)
-> );
SELECT * FROM Customers
ORDER BY LastName ASC;

OUTPUT:
2. Retrieve all rows from the Orders table sorted by OrderDate in descending
order.
SOLUTION:
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
CustomerID INT,
OrderDate DATE,
Status VARCHAR(50),
CompletionDate DATE,
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);
SELECT * FROM Orders ORDER BY OrderDate DESC;
OUTPUT:
3. List all products sorted first by CategoryID in ascending order and then by
ProductName in descending order.
SOLUTION :
CREATE TABLE Products (
ProductID INT PRIMARY KEY,
ProductName VARCHAR(255),
CategoryID INT,
Price DECIMAL(10, 2)
);
SELECT * FROM Products
ORDER BY CategoryID ASC, ProductName DESC;
OUTPUT:
4. Sort all employees by HireDate with NULL values appearing last.

SOLUTION:
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
EmployeeName VARCHAR(255),
DepartmentID INT,
Department VARCHAR(255),
HireDate DATE,
Salary DECIMAL(10, 2)
);

SELECT * FROM Employees


ORDER BY HireDate IS NULL, HireDate ASC;

OUTPUT:
3. Retrieve all employees and sort them by the calculated column
YearsAtComp

SOLUTION:
SELECT *, DATEDIFF(YEAR, HireDate, GETDATE()) AS YearsAtCompany
FROM Employees
ORDER BY YearsAtCompany DESC;

OUTPUT:
4. Sort employees so that those in the 'Sales' department appear first, followed
by 'HR', then 'IT', and all other departments afterward.
SOLUTION:
SELECT * FROM Employees
ORDER BY
CASE
WHEN Department = 'Sales' THEN 1
WHEN Department = 'HR' THEN 2
WHEN Department = 'IT' THEN 3
ELSE 4
END;
OUTPUT:
5. List all books sorted by the length of the Title in ascending order.
SOLUTION:
CREATE TABLE Books (
BookID INT PRIMARY KEY,
Title VARCHAR(255)
);
SELECT * FROM Books
ORDER BY Title ASC;
OUTPUT:
6. Retrieve all departments sorted by the total number of employees
(EmployeeCount) in descending order.
SOLUTION:
CREATE TABLE Departments (
DepartmentID INT PRIMARY KEY,
DepartmentName VARCHAR(255),
EmployeeCount INT
);
SELECT * FROM Departments
ORDER BY EmployeeCount DESC;
OUTPUT:
11. Find all orders where the Status is 'Completed' and sort them by
CompletionDate in ascending order. If CompletionDate is NULL, place them at
the end.
SOLUTION:
SELECT * FROM Orders
WHERE Status = 'Completed'
ORDER BY CompletionDate IS NULL, CompletionDate ASC;
OUTPUT:
12. Retrieve all employees sorted by DepartmentID in ascending order and then
by Salary in descending order.
SOLUTION:
SELECT * FROM Employees
ORDER BY DepartmentID ASC, Salary DESC;
OUTPUT:
14. Sort all products first by CategoryID in ascending order and then by Price in
descending order, and if Price is NULL, list those at the end.
SOLUTION:
SELECT * FROM Products
ORDER BY CategoryID ASC, Price IS NULL, Price DESC;

OUTPUT:
15. Retrieve all invoices sorted by InvoiceDate in ascending order and then by the
TotalAmount (calculated as Quantity * UnitPrice) in descending order.
SOLUTION:
CREATE TABLE Invoices (
InvoiceID INT PRIMARY KEY,
OrderID INT,
Quantity INT,
UnitPrice DECIMAL(10, 2),
InvoiceDate DATE,
FOREIGN KEY (OrderID) REFERENCES Orders(OrderID)
);
SELECT *, (Quantity * UnitPrice) AS TotalAmount
FROM Invoices
ORDER BY InvoiceDate ASC, TotalAmount DESC;

OUTPUT:
18. Retrieve the top 10 most recent orders (OrderDate) for the year 2023, sorted
by OrderDate in descending order.
SOLUTION:
SELECT * FROM Orders
WHERE EXTRACT(YEAR FROM OrderDate)=2024
ORDER BY OrderDate DESC
LIMIT 10;
OUTPUT:

You might also like