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

dbms practical file

The document contains a series of lab assignments related to database management systems, focusing on creating and manipulating Entity-Relationship (E-R) diagrams, relational models, and executing SQL commands. It covers various topics including DDL and DML commands, aggregate functions, integrity constraints, and joins. Each assignment provides solutions in the form of SQL commands and explanations for creating tables, inserting data, and querying databases.

Uploaded by

pandey.aayush191
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

dbms practical file

The document contains a series of lab assignments related to database management systems, focusing on creating and manipulating Entity-Relationship (E-R) diagrams, relational models, and executing SQL commands. It covers various topics including DDL and DML commands, aggregate functions, integrity constraints, and joins. Each assignment provides solutions in the form of SQL commands and explanations for creating tables, inserting data, and querying databases.

Uploaded by

pandey.aayush191
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

DBMS 102P 04321102024

LAB ASSIGNMENT - 1

Q1. Introduction to E-R Diagram, Design an E-R Diagram for the Student-
Project, Patient-Doctor Database.

SOL:
Introduction to E-R Diagram
An Entity-Relationship (E-R) Diagram is a visual tool used in database design
to represent the structure of a database. It illustrates the entities (objects or
concepts), their attributes (properties), and the relationships between them. E-R
diagrams are essential for understanding the data requirements and designing a
relational database.

1. Student-Project Database

Entities and Attributes:


1. Student:
Attributes: Roll No. (Primary Key), S name, Class, Age,
P.id (Foreign Key),Subject, Phone No.
2. Project:
Attributes: Pid (Primary Key), P name, Duration

Relationships:
Assign:
 Links Student and Project.
 A student can be assigned to one or more projects, and a project can be
assigned to one or more students (many-to-many relationship).

E-R Diagram Design:


 Entities: Represented as rectangles.
 Attributes: Represented as ovals connected to their respective entities.
 Relationships: Represented as diamonds connecting the entities.
DBMS 102P 04321102024

2. Patient-Doctor Database

Entities and Attributes:


1. Patient:
Attributes: P.ID (Primary Key), Name, Age, Gender, Blood
group, Disease

2. Doctor:
Attributes: Doc.ID (Primary Key), Name, Specialist, Dep.id (Foreign Key)

3. Department:
Attributes: Dep.ID (Primary Key), Name, Location, Head

Relationships:
Consult: Links Patient, Doctor, and Department.
Attributes: P.ID (from Patient), Doc.ID (from Doctor), Dep.ID (from
Department)
DBMS 102P 04321102024
DBMS 102P 04321102024

LAB ASSIGNMENT – 2

Q2. Design an E-R Diagram for College Database.

Sol:
DBMS 102P 04321102024

LAB ASSIGNMENT - 3

Q3.Convert the E-R diagram into a Relational model with proper


constraints.

Sol:
1. STUDENT TABLE:
CMD:
CREATE TABLE Student (
USN VARCHAR(20) PRIMARY KEY,
SName VARCHAR(100) NOT NULL,
Address VARCHAR(255),
Phone VARCHAR(15),
Gender CHAR(1)
);

Output:

2. SEMSEC TABLE:

CMD:
CREATE TABLE SEMSEC (
SSID VARCHAR(20) PRIMARY KEY,
Sem INT,
Sec VARCHAR(10)
);

Output:
DBMS 102P 04321102024

3. CLASS TABLE:

CMD:
CREATE TABLE Class (
USN VARCHAR(20),
SSID VARCHAR(20),
PRIMARY KEY (USN, SSID),
FOREIGN KEY (USN) REFERENCES Student(USN),
FOREIGN KEY (SSID) REFERENCES SEMSEC(SSID)
);

Output:

4. SUBJECT TABLE:

CMD:
CREATE TABLE Subject (
Subcode VARCHAR(20) PRIMARY KEY,
Title VARCHAR(100) NOT NULL,
Sem INT,
Credits INT
);

Output:
DBMS 102P 04321102024

5. IAMARKS TABLE:

CMD:
CREATE TABLE IAMARKS (
USN VARCHAR(20),
Subcode VARCHAR(20),
SSID VARCHAR(20),
Test1 INT,
Test2 INT,
Test3 INT,
FinalIA INT,
PRIMARY KEY (USN, Subcode, SSID),
FOREIGN KEY (USN) REFERENCES Student(USN),
FOREIGN KEY (Subcode) REFERENCES Subject(Subcode),
FOREIGN KEY (SSID) REFERENCES SEMSEC(SSID)
);
Output:
DBMS 102P 04321102024

LAB ASSIGNMENT - 4

Q4.Write queries to execute following DDL commands:


 CREATE: Create the structure of a table with at least five columns
 ALTER: Change the size of a particular column.
 Add a new column to the existing table.
 Remove a column from the table.
 DROP: Destroy the table along with its data.

Sol:

1. CREATE: Create the structure of a table with at least five columns


CMD:
CREATE TABLE Employee (
Employee_ID INT PRIMARY KEY,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
Department VARCHAR(100),
Salary DECIMAL(10, 2) );
Output:

2. ALTER: Change the size of a particular column.


CMD:
ALTER TABLE Employee
MODIFY COLUMN FirstName VARCHAR(100);

Output:
DBMS 102P 04321102024

3. Add a new column to the existing table.


CMD:
ALTER TABLE Employee
ADD COLUMN Email VARCHAR(100);

Output:

4. Remove a column from the table.


CMD:
ALTER TABLE Employee
DROP COLUMN LastName;

Output:

5. DROP: Destroy the table along with its data.


CMD:
DROP TABLE Employee;

Output:
DBMS 102P 04321102024

LAB ASSIGNMENT - 5

Q5.Write queries to execute following DML commands :


● INSERT: Insert five records in each table.
● UPDATE: Modify data in single and multiple columns in a table.
● DELETE: Delete selective and all records from a table.

Sol:
Create table student
CMD:
CREATE TABLE Student (
USN VARCHAR(20) PRIMARY KEY,
SName VARCHAR(100) NOT NULL,
Address VARCHAR(255),
Phone VARCHAR(15),
Gender CHAR(1)
);
desc Student;

Output:

1. INSERT: Insert five records in each table

CMD:
INSERT INTO Student
(USN, SName, Address, Phone, Gender) VALUES
('S001', 'John Doe', '123 Main St', '555-1234', 'M'),
('S002', 'Jane Smith', '456 Elm St', '555-5678', 'F'),
('S003', 'Alice Johnson', '789 Oak St', '555-8765', 'F'),
('S004', 'Bob Brown', '321 Pine St', '555-4321', 'M'),
('S005', 'Charlie Davis', '654 Maple St', '555-6789', 'M');
DBMS 102P 04321102024

Output:

2. UPDATE: Modify data in single and multiple columns in a table.

Update a single column:


CMD:
UPDATE Student
SET Address = '999 New St'
WHERE USN = 'S001';

Output:

Update multiple columns:


CMD:
UPDATE Student
SET Address = '888 Updated St', Phone = '555-9999'
WHERE USN = 'S002';

Output:
DBMS 102P 04321102024

3. DELETE: Delete selective and all records from a table.

Delete selective records:


CMD:
DELETE FROM Student
WHERE USN = 'S005';

Output:

Delete all records:


CMD:
DELETE FROM Student;

Output:
DBMS 102P 04321102024

LAB ASSIGNMENT – 6

Q6. Write queries to execute following DML command :

1. SELECT: Retrieve the entire contents of the table.

2. Retrieve the selective contents (based on provided conditions) from a


table. WHERE CLAUSE.

3. Retrieve contents from a table based on various operators i.e. string


operators, logical operators and conditional operator, Boolean
operators.

4. Sort the data in ascending and descending order in a table on the


basis of one column or more than one column.

SOL:

1. Retrieve Entire Table

CMD:
SELECT * FROM STUDENT;

Output:

2. Retrieve Selective Contents (WHERE Clause)

CMD:
SELECT * FROM STUDENT
WHERE ADDRESS = 'Saadh Nagar';
DBMS 102P 04321102024

Output:

3. Use of operators

a. String Operator (LIKE)

CMD:

SELECT * FROM STUDENT

WHERE ADDRESS LIKE '%Nagar%';

Output:

b. Logical Operators (AND / OR)

CMD:
SELECT * FROM STUDENT
WHERE ADDRESS = 'gurugram' AND GENDER = 'M';

Output:
DBMS 102P 04321102024

c. Conditional Operator

CMD:

SELECT * FROM STUDENT

WHERE ID > 102;

Output:

d. Boolean Operator (NOT)


CMD:
SELECT * FROM STUDENT
WHERE NOT ADDRESS = 'Saadh Nagar';
Output:

4. Sorting Data
1. Ascending Order by NAME :
CMD:
SELECT * FROM STUDENT
ORDER BY NAME ASC;
DBMS 102P 04321102024

Output:

2. Descending Order by PHONE


CMD:
SELECT * FROM STUDENT
ORDER BY PHONE DESC;
Output:
DBMS 102P 04321102024

LAB ASSIGNMENT – 7

Q.7 Create table using following integrity Constraints: Primary Key, Unique
Key, Not Null, Check, Default, and Foreign Key.
Sol:
-- Parent Table: Departments
CREATE TABLE Departments (
DeptID INT PRIMARY KEY, -- Primary Key
DeptName VARCHAR(100) UNIQUE NOT NULL -- Unique + Not Null
);

-- Child Table: Employees


CREATE TABLE Employees (
EmpID INT PRIMARY KEY, -- Primary Key
EmpName VARCHAR(100) NOT NULL, -- Not Null
Email VARCHAR(100) UNIQUE, -- Unique
Age INT CHECK (Age >= 18 AND Age <= 65), -- Check constraint
Salary DECIMAL(10, 2) DEFAULT 30000.00, -- Default value
DeptID INT, -- Foreign Key
CONSTRAINT fk_dept FOREIGN KEY (DeptID) REFERENCES Departments(DeptID)
);

INSERT INTO Departments (DeptID, DeptName) VALUES


(1, 'Human Resources'),
(2, 'Finance'),
(3, 'Engineering');
INSERT INTO Employees (EmpID, EmpName, Email, Age, Salary, DeptID) VALUES
(101, 'Alice Johnson', '[email protected]', 28, 45000.00, 1),
(102, 'Bob Smith', '[email protected]', 35, DEFAULT, 2), -- uses default salary
(103, 'Charlie Lee', '[email protected]', 40, 60000.00, 3),
(104, 'Diana Prince', '[email protected]', 30, 70000.00, 1);
DBMS 102P 04321102024

LAB ASSIGNMENT – 8
Q.8. Write queries to execute the following Aggregate functions: Sum, Avg,
Count, and Minimum and Maximum value of a numeric column of a table using
aggregate function.
Sol:
-- Step 1: Create a table named 'Sales'
CREATE TABLE Sales (
id INT PRIMARY KEY,
product_name VARCHAR(100),
quantity INT,
price DECIMAL(10, 2)
);

-- Step 2: Insert sample data into 'Sales'


INSERT INTO Sales (id, product_name, quantity, price) VALUES
(1, 'Laptop', 5, 1200.00),
(2, 'Smartphone', 10, 800.00),
(3, 'Tablet', 8, 450.00),
(4, 'Monitor', 6, 300.00),
(5, 'Keyboard', 15, 50.00);

-- Step 3: Aggregate Functions

-- a) SUM of price
SELECT SUM(price) AS total_price FROM Sales;

-- b) AVG (average) of price


SELECT AVG(price) AS average_price FROM Sales;
DBMS 102P 04321102024

-- c) COUNT of records
SELECT COUNT(*) AS total_records FROM Sales;

-- d) MINIMUM price
SELECT MIN(price) AS lowest_price FROM Sales;

-- e) MAXIMUM price
SELECT MAX(price) AS highest_price FROM Sales;
DBMS 102P 04321102024

LAB ASSIGNMENT – 9
Q.9 Retrieve data from a table using alias names.
Sol:
CREATE TABLE Pup (
id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
department VARCHAR(50)
);
INSERT INTO Pup (id, first_name, last_name, department)
VALUES
(1, 'John', 'Doe', 'Sales'),
(2, 'Jane', 'Smith', 'HR'),
(3, 'Michael', 'Brown', 'IT'),
(4, 'Emily', 'Davis', 'Marketing');

SELECT
e.first_name AS fname,
e.last_name AS lname,
e.department AS dept
FROM
Pup AS e;
Output:
DBMS 102P 04321102024

LAB ASSIGNMENT – 10
Q.10. Retrieve data from a table using nested queries.
Sol:
CREATE TABLE SOS (
EmpID INT PRIMARY KEY,
Name VARCHAR(50),
Department VARCHAR(50),
Salary INT
);
INSERT INTO SOS (EmpID, Name, Department, Salary) VALUES
(1, 'Alice', 'HR', 50000),
(2, 'Bob', 'IT', 70000),
(3, 'Charlie', 'Finance', 60000),
(4, 'David', 'IT', 75000),
(5, 'Eve', 'HR', 52000);

Using nested queries:


SELECT Name, Salary
FROM SOS
WHERE Salary > (
SELECT AVG(Salary)
FROM Employees
);
Output:

Example 2: Get names of employees who work in the same department as


'Bob'
SELECT Name
FROM Employees
WHERE Department = (
SELECT Department
FROM Employees
WHERE Name = 'Bob'
)
DBMS 102P 04321102024

AND Name != 'Bob';

Example 3: Get the highest-paid employee


SELECT Name, Salary
FROM Employees
WHERE Salary = (
SELECT MAX(Salary)
FROM Employees
);
DBMS 102P 04321102024

LAB ASSIGNMENT – 11
Q. 11 Retrieve data from more than one table using inner join, left
join, right join and full outer join.
Sol:
CREATE TABLE JOIN2(
ID INT PRIMARY KEY,
NAME VARCHAR(50),
SALARY NUMERIC,
MARKS INT,
CITY VARCHAR(50)
);
INSERT INTO JOIN2 VALUES (166, 'Ayush', 900000, 98, 'DELHI');
INSERT INTO JOIN2 VALUES (15, 'Kartik', 900000, 65, 'GHAZIABAD');
INSERT INTO JOIN2 VALUES (143, 'Gaurav', 755555, 64, 'MUMBAI');
INSERT INTO JOIN2 VALUES (104, 'Pankaj', 533333, 44, 'BHAVALI');
INSERT INTO JOIN2 VALUES (105, 'Aradhya', 53663333, 98, 'PRAYAGRAJ');
INSERT INTO JOIN2 VALUES (101, 'ARya', 3354333, 98, 'PUNE');
INSERT INTO JOIN2 VALUES (102, 'OKAYY BHAI', 30000, 95, 'RAJASTHAN');
Master Table:

-- INNER JOIN
SELECT * FROM
JOIN1 AS c
INNER JOIN JOIN2 AS p
ON c.ID = p.ID;
Output:
DBMS 102P 04321102024

-- LEFT JOIN
SELECT * FROM
JOIN1 AS c
LEFT JOIN JOIN2 AS p
ON c.ID = p.ID;
Output:

-- RIGHT JOIN
SELECT * FROM
JOIN1 AS c
RIGHT JOIN JOIN2 AS p
ON c.ID = p.ID;
Output:
DBMS 102P 04321102024

LAB ASSIGNMENT – 12
Q.12 Create index on a column of a table.
Sol:
CREATE TABLE SQL1 (
id INT PRIMARY KEY,
name VARCHAR(100),
department VARCHAR(50),
salary DECIMAL(10, 2)
);
INSERT INTO SQL1 (id, name, department, salary) VALUES
(1, 'Alice Johnson', 'Engineering', 75000),
(2, 'Bob Smith', 'Sales', 60000),
(3, 'Carol White', 'Engineering', 80000),
(4, 'David Brown', 'Marketing', 55000),
(5, 'Eva Green', 'Sales', 62000),
(6, 'Frank Black', 'Engineering', 77000),
(7, 'Grace Hall', 'Marketing', 53000);
Select * from SQL1;

-- Create an Index
Select * from SQL1;
CREATE INDEX idx_department ON SQL1(department);
-- Use the Index
SELECT * FROM SQL1 WHERE department = 'Engineering';
DBMS 102P 04321102024

LAB ASSIGNMENT – 13
Q. 13 Consider the following schema of a library management system. Write
the SQL queries for the questions given below:
EMPLOYEE (EmployeeID, EmployeeName, Street, City)
COMPANY (CompanyID, CompanyName, City)
WORKS (EmployeeID, CompanyID, Salary) MANAGES (EmployeeID,
ManagerID) Create the appropriate tables for the given schemas and describe it.

i) Find the names of all employees who work for First Bank Corporation.
ii) Find the names and cities of residence of all employees who work for the
First Bank Corporation.
iii) Find the names, street, and cities of residence of all employees who work
for First.
iv) Find the employees who live in the same cities as the companies for
which they work.
v) Find all employees who live in the same cities and on the same streets as
do their Managers.
vi) Find all employees who do not work for First Bank Corporation.
vii) Find all employees who earn more than every employee of Small Bank
Corporation.
viii) Find all companies located in every city in which Small Bank
Corporation is located.
ix) Find all employees who earn more than the average salary of all
employees of their
Company.
x) Find those companies whose employees earn a higher salary, on average,
than the average salary of first Bank Corporation.

Sol:
Table Creation
CREATE TABLE EMPLOYEE (
EmployeeID INT PRIMARY KEY,
EmployeeName VARCHAR(100),
Street VARCHAR(100),
City VARCHAR(100)
);

CREATE TABLE COMPANY (


CompanyID INT PRIMARY KEY,
CompanyName VARCHAR(100),
City VARCHAR(100)
);
DBMS 102P 04321102024

CREATE TABLE WORKS (


EmployeeID INT,
CompanyID INT,
Salary DECIMAL(10, 2),
PRIMARY KEY (EmployeeID, CompanyID),
FOREIGN KEY (EmployeeID) REFERENCES EMPLOYEE(EmployeeID),
FOREIGN KEY (CompanyID) REFERENCES COMPANY(CompanyID)
);

CREATE TABLE MANAGES (


EmployeeID INT,
ManagerID INT,
PRIMARY KEY (EmployeeID, ManagerID),
FOREIGN KEY (EmployeeID) REFERENCES EMPLOYEE(EmployeeID),
FOREIGN KEY (ManagerID) REFERENCES EMPLOYEE(EmployeeID)
);

Insert Data :
-- Insert into EMPLOYEE
INSERT INTO EMPLOYEE (EmployeeID, EmployeeName, Street, City) VALUES
(1, 'Alice Johnson', '123 Maple St', 'New York'),
(2, 'Bob Smith', '456 Oak Ave', 'Los Angeles'),
(3, 'Carol White', '789 Pine Rd', 'Chicago'),
(4, 'David Brown', '321 Birch Blvd', 'New York'),
(5, 'Eve Black', '654 Cedar Ln', 'Boston');

-- Insert into COMPANY


INSERT INTO COMPANY (CompanyID, CompanyName, City) VALUES
(101, 'First Bank Corporation', 'New York'),
(102, 'Small Bank Corporation', 'Chicago'),
(103, 'Big Data Ltd.', 'Boston'),
(104, 'First Finance', 'Los Angeles');

-- Insert into WORKS


INSERT INTO WORKS (EmployeeID, CompanyID, Salary) VALUES
(1, 101, 85000.00),
(2, 102, 90000.00),
(3, 103, 75000.00),
(4, 101, 80000.00),
(5, 102, 87000.00);

-- Insert into MANAGES


INSERT INTO MANAGES (EmployeeID, ManagerID) VALUES
(2, 1), -- Bob is managed by Alice
(3, 1), -- Carol is managed by Alice
(4, 1), -- David is managed by Alice
(5, 2); -- Eve is managed by Bob
DBMS 102P 04321102024

SQL Queries

i) Find the names of all employees who work for First Bank Corporation.
SELECT E.EmployeeName
FROM EMPLOYEE E
JOIN WORKS W ON E.EmployeeID = W.EmployeeID
JOIN COMPANY C ON W.CompanyID = C.CompanyID
WHERE C.CompanyName = 'First Bank Corporation';

ii) Find the names and cities of residence of all employees who work for the
First Bank Corporation.
SELECT E.EmployeeName, E.City
FROM EMPLOYEE E
JOIN WORKS W ON E.EmployeeID = W.EmployeeID
JOIN COMPANY C ON W.CompanyID = C.CompanyID
WHERE C.CompanyName = 'First Bank Corporation';

iii) Find the names, street, and cities of residence of all employees who work for
First.
SELECT E.EmployeeName, E.Street, E.City
FROM EMPLOYEE E
JOIN WORKS W ON E.EmployeeID = W.EmployeeID
JOIN COMPANY C ON W.CompanyID = C.CompanyID
WHERE C.CompanyName LIKE 'First%';
DBMS 102P 04321102024

iv) Find the employees who live in the same cities as the companies for which
they work.
SELECT DISTINCT E.EmployeeName
FROM EMPLOYEE E
JOIN WORKS W ON E.EmployeeID = W.EmployeeID
JOIN COMPANY C ON W.CompanyID = C.CompanyID
WHERE E.City = C.City;

v) Find all employees who live in the same cities and on the same streets as do
their Managers.
SELECT E.EmployeeName
FROM EMPLOYEE E
JOIN MANAGES M ON E.EmployeeID = M.EmployeeID
JOIN EMPLOYEE MGR ON M.ManagerID = MGR.EmployeeID
WHERE E.City = MGR.City AND E.Street = MGR.Street;

vi) Find all employees who do not work for First Bank Corporation.
SELECT DISTINCT E.EmployeeName
FROM EMPLOYEE E
JOIN WORKS W ON E.EmployeeID = W.EmployeeID
JOIN COMPANY C ON W.CompanyID = C.CompanyID
WHERE C.CompanyName <> 'First Bank Corporation';
DBMS 102P 04321102024

vii) Find all employees who earn more than every employee of Small Bank
Corporation.
SELECT E.EmployeeName
FROM EMPLOYEE E
JOIN WORKS W ON E.EmployeeID = W.EmployeeID
WHERE W.Salary > ALL (
SELECT W2.Salary
FROM WORKS W2
JOIN COMPANY C2 ON W2.CompanyID = C2.CompanyID
WHERE C2.CompanyName = 'Small Bank Corporation'
);

viii) Find all companies located in every city in which Small Bank Corporation is
located.
SELECT DISTINCT C1.CompanyName
FROM COMPANY C1
WHERE NOT EXISTS (
SELECT C2.City
FROM COMPANY C2
WHERE C2.CompanyName = 'Small Bank Corporation'
AND C2.City NOT IN (
SELECT C3.City
FROM COMPANY C3
WHERE C3.CompanyID = C1.CompanyID
)
);
DBMS 102P 04321102024

ix) Find all employees who earn more than the average salary of all employees
of their Company.
SELECT E.EmployeeName
FROM EMPLOYEE E
JOIN WORKS W ON E.EmployeeID = W.EmployeeID
WHERE W.Salary > (
SELECT AVG(W2.Salary)
FROM WORKS W2
WHERE W2.CompanyID = W.CompanyID
);
DBMS 102P 04321102024

x )Find those companies whose employees earn a higher salary, on average,


than the average salary of first Bank Corporation.
SELECT C.CompanyName
FROM COMPANY C
JOIN WORKS W ON C.CompanyID = W.CompanyID
GROUP BY C.CompanyID, C.CompanyName
HAVING AVG(W.Salary) > (
SELECT AVG(W2.Salary)
FROM WORKS W2
JOIN COMPANY C2 ON W2.CompanyID = C2.CompanyID
WHERE C2.CompanyName = 'First Bank Corporation'
);

You might also like