yash dbms
yash dbms
SUBMITTED BY
Student Name Sagar
Enrollment Number 230160255012
Section/Group BCA (Machine Learning)
Semester III Semester – 2nd YEAR
Faculty Name Mr. Abhinav Sir
1
CSE2002L: Introduction to database management system
Session: 2024-2025
LIST OF EXPERIMENTS
2
LAB - 1
a) Customers Table
This table contains information about the customers who use the platform.
Each customer is assigned a unique customer ID.
3
(Figure 1.1)
b) Products Table
This table contains details about the products that are available for purchase.
(Figure 1.2)
4
c) Orders Table
5
(Figure 1.3)
a) Users Table
This table contains information about users who borrow books from the
library.
• User ID: A unique identifier for each user. Username: The name of
the user.
• Email: The user’s email address.
• Phone: The user’s phone number.
• Membership Date: The date the user became a member of the library.
6
(Figure 1.4)
b) Books Table
This table stores information about the books available in the library.
7
output
(Figure1.5)
(Figure 1.6)
8
Key Differences Between the Two Datasets:
1. Purpose:
o The Product dataset is about purchasing products
online, focusing on customers, products, and orders.
o The library dataset focuses on borrowing books,
tracking users, available books, and borrowing
transactions.
2. Entities:
o Product system involves Customers, Products, and Orders. o
Library system involves Users, Books, and Borrowed Books.
3. Transactions:
o In Product, the key transaction is placing an order and
purchasing products. o In the library, the transaction
involves borrowing and returning books.
Both datasets provide a useful structure for different types of operations and
are designed to allow SQL queries that connect customers or users to the
items they interact with
9
LAB-2
(Figure 2.1)
-- Create the Employees table
CREATE TABLE Employees ( employee_id INT PRIMARY KEY, first_name
VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL, department_id
INT, salary DECIMAL(10, 2), hire_date DATE, FOREIGN KEY (department_id)
REFERENCES Departments(department_id) );
10
(Figure 2.2)
Output:
(Figure 2.3)
11
-- Create the Employee_Tasks table
CREATE TABLE Employee_Tasks
( employee_id INT, task_id INT,
(Figure 2.4)
12
SQL Commands and Questions
(Figure 2.5)
2. -- Find the first name and last name of all employees with a salary
greater than 50000
(Figure 2.6)
13
3. -- List all departments along with their department names
(Figure 2.7)
(Figure 2.8)
(Figure 2.9)
14
6. -- Retrieve employees who were hired in the year 2020
(Figure 2.10)
(Figure 2.11)
15
(Figure 2.12)
(Figure 2.13)
10.-- List all employees along with the total number of projects they are involved in
16
(Figure 2.14)
(Figure 2.15)
(Figure 2.16)
17
(Figure 2.17)
14.-- Retrieve the details of employees who have the same salary as "John Doe"
(Figure 2.18)
(Figure 2.19)
18
19
LAB-3
(Figure 3.1)
(Figure 3.2)
20
3. Retrieve the details of projects that have not ended yet.
(Figure 3.3)
(Figure 3.4)
21
5. Find the project names that have more than 5 employees assigned.
(Figure 3.5)
(Figure 3.6)
22
7. Retrieve employees who were hired in the last 6 months.
(Figure 3.7)
( )
23
Figure 3.8
9. List the department names with no employees.
(Figure 3.9)
(Figure 3.10)
( )
24
Figure 3.11
12. Get the list of projects along with the number of employees assigned to each.
(Figure 3.12)
(Figure 3.13)
14. List employees who have never worked on a project in their own department.
( )
25
Figure 3.14
( )
26
15. Retrieve the details of employees whose salary is within the top 10% of
their department.
(Figure 3.15)
16. List the names of employees who were hired in the same month they
were assigned their first project.
(Figure 3.16)
27
Lab - 4
Creating table:
(Figure 4.1)
28
1. Additional Exercise 1: Logical Data Independence
(Figure 4.2)
Books:-
(Figure 4.3)
29
2. Additional Exercise 2: Logical Data Independence
(Figure 4.4)
Index activity
(Figure 4.5)
30
Lab-5
Product table:
(Figure 5.1)
Output:
(Figure 5.2)
Book table:
31
(Figure 5.3)
Output:
(Figure 5.4)
Electronics table:
(Figure 5.5)
Output:
(Figure 5.6)
32
(Figure 5.7)
SQL code to create a cricket database with a focus on the unique event
of Yuvraj Singh hitting six sixes in an over.
Players table:
33
(Figure 5.8)
Output:
(Figure 5.9)
Matches table
34
Output: (Figure 5.10)
(Figure 5.11)
Events table
(Figure 5.13)
35
Query to fetch match details along with the unique event
(Figure 5.14)
Output:
(Figure 5.15)
36
Brian Lara's Record-Breaking Innings
Event: Brian Lara scored 400 not out against England in a Test match.
Date: April 10-12, 2004
Venue: Antigua Recreation Ground, St. John's, Antigua.
Players table:
(Figure 5.17)
Matches table:
(Figure 5.18)
37
Output:
(Figure 5.19)
Players table:
(Figure 5.20)
Output:
(Figure 5.21)
38
(Figure 5.22)
Output:
(Figure 5.23)
39
Lab-6
Let's say we have an E-R model with the following tables: Employee (as a
superclass), Manager (specialization subclass), and Staff (specialization
subclass). Using SQL, we could perform relational operations and queries:
40
1.
CREATE TABLE Manager
( emp_id INT PRIMARY
KEY, department
VARCHAR(50),
FOREIGN KEY (emp_id) REFERENCES Employee(emp_id)
);
INSERT INTO Employee (emp_id, name, salary, job_type) VALUES (1, 'Alice', 75000,
'Manager');
(Figure 6.1)
2.
CREATE TABLE Manager
( emp_id INT PRIMARY KEY,
department VARCHAR(50),
FOREIGN KEY (emp_id) REFERENCES Employee(emp_id)
);
INSERT INTO Manager (emp_id, department) VALUES (1, 'Finance');
INSERT INTO Employee (emp_id, name, salary, job_type) VALUES (2, 'Bob', 50000, 'Staff');
41
(Figure 6.2)
3.
CREATE TABLE Staff (
emp_id INT PRIMARY
KEY, shift VARCHAR(10),
FOREIGN KEY (emp_id) REFERENCES Employee (emp_id)
);
INSERT INTO Employee (emp_id, name, salary, job_type) VALUES (2, 'Bob', 50000, 'Staff');
INSERT INTO Staff (emp_id, shift) VALUES (2, 'Day');
42
(Figure 6.3)
(Figure 6.4)
-- b. Join Employees and Managers for specific departments (Join Operation)
SELECT e.name, e.salary, m.department
FROM Employee e
JOIN Manager M ON e.emp_id = m.emp_id
WHERE m.department = 'Finance';
(Figure 6.5)
43
Query all trucks in the Truck subclass
SELECT v.vehicle_id, v.brand, v.model, v.year,
t.payload_capacity FROM Vehicle v
JOIN Truck t ON v.vehicle_id = t.vehicle_id;
(Figure 6.6)
(Figure 6.7)
44
Specialization
Definition: Specialization is the process of defining one or more sub-entities
of a higher-level entity, allowing for more specific attributes or behaviors
for those sub-entities.
Example: In your schema, the Manager and Staff tables represent
specialized roles within the broader Employee category. Managers may
have additional responsibilities and attributes not applicable to regular staff.
Inheritance
Definition: Inheritance allows sub-entities to inherit attributes from a
parent entity, reducing redundancy and promoting consistency.
Example: Both Manager and Staff tables inherit the emp_id, name,
and salary attributes from the Employee table. This means all
employees (regardless of role) share these fundamental attributes.
Generalization
Definition: Generalization is the process of abstracting shared
characteristics from multiple sub-entities to form a more generic
entity.
Example: Querying the Employee table provides a comprehensive view
of all employees, regardless of their specific roles. This allows for easy
reporting and analysis of general employee data. When you join the
Employee table with Manager or Staff, you can retrieve specialized details
while still leveraging the shared attributes defined in the Employee table.
45
Lab-7
Relational Data Model: Defines data structures (tables) with rows (tuples)
and columns (attributes) to store and organize data. It also enforces data
integrity through primary keys, foreign keys, and constraints.
Let's say we have an E-R model with the following tables: Employee (as a
superclass), Manager (specialization subclass), and Staff (specialization
subclass). Using SQL, we could perform relational operations and queries:
The relational data model defines the structure of the database. We begin by
creating the Employee superclass table, which holds the general employee
information, and two subclass tables, Manager and Staff, which inherit from
Employee. Each subclass holds specialized attributes unique to their type.
Sql
Copy code
-- Create Employee Table (Superclass)
CREATE TABLE Employee (
emp_id INT PRIMARY KEY, -- Unique employee ID
name VARCHAR(50), -- Employee's name
salary DECIMAL(10, 2), -- Employee's salary
job_type VARCHAR(10) -- Type of job (Manager or Staff)
);
46
-- Create Staff Table (Subclass of Employee)
CREATE TABLE Staff (
emp_id INT PRIMARY KEY, -- Unique employee ID (foreign key to
Employee)
shift VARCHAR(10), -- Shift the staff member works (e.g.,
Day, Night)
FOREIGN KEY (emp_id) REFERENCES Employee(emp_id)
);
Step 2: Insert Data into the Tables
Now, we insert some sample data into the Employee, Manager, and Staff tables to
simulate real-life employees.
sql
Copy code
-- Insert Employee data
-- Manager Alice in the Finance department
INSERT INTO Employee (emp_id, name, salary, job_type) VALUES (1, 'Alice',
75000, 'Manager');
INSERT INTO Manager (emp_id, department) VALUES (1, 'Finance');
47
1. Employee Table:-
(Figure 7.1)
(Figure 7.2)
2. Manager Table:-
(Figure 7.3)
(Figure 7.4)
48
3. Staff Table:-
(Figure 7.5)
(Figure 7.6)
49
Lab-8
Now let's write SQL queries that simulate relational algebra operations like
selection, projection, and join.
This query selects all employees who are managers and joins their information
from both the Employee and Manager tables.
This query performs a join between the Employee and Manager tables, filtering
for employees who work in the 'Finance' department.
sql
This query retrieves all employee details from the Employee table, regardless of
whether the employee is a manager or staff member.
sql
50
(Figure 7.1)
(Figure 7.2)
51
Lab-9
Example:
sql
(Figure 9.1)
52
2. Projection (π): Selects specific columns from a table.
o In SQL, this is done by simply listing the desired columns in
the SELECT statement.
Example:
sql
(Figure 9.2)
53
Example:
sql
(Figure 9.3)
Example:
sql
54
SELECT name FROM Employee WHERE job_type = 'Manager'
UNION
SELECT name FROM Employee WHERE job_type = 'Staff';
(Figure 9.4)
Example:
sql
-- Get employees who are both Managers and Staff (assuming they exist)
SELECT name FROM Employee WHERE job_type = 'Manager'
INTERSECT
SELECT name FROM Employee WHERE job_type = 'Staff';
55
6. Difference (-): Returns rows that are in the first result set but not in
the second.
o In SQL, this can be done using the EXCEPT operator.
Example:
sql
(Figure 9.5)
56