DBMS LAB Manual
DBMS LAB Manual
K.samuel Edward
23WU0101082
Panthers CSE
1.Identification of Attributes
Let's assume a scenario: We are designing a database for an online bookstore.
Attributes for Books table: BookID , Title , Author , Genre , Price , PublishedDate .
Output:
UPDATE Books
SET Price = 12.99
WHERE BookID = 1;
Output: The price of The Great Gatsby is updated to 12.99.
f. Delete Table and Database
db.createCollection("Products");
Output: Products collection is created.
c. InsertOne and InsertMany
db.Products.find();
db.Products.findOne({ Name: "Laptop" });
Output: Returns all products for find() ; returns the first matching document for findOne() .
db.Products.aggregate([
{ $group: { _id: "$Name", TotalStock: { $sum: "$Stock" } } }
]);
Output: Groups products by Name and calculates the total stock.
b. LIMIT Example
db.Products.find().limit(2);
Output: Returns only the first two products.
c. PROJECT Example
db.Products.countDocuments();
Output: Returns the total number of documents in the Products collection.
g. LOOKUP Example
db.Orders.aggregate([
{
$lookup: {
from: "Customers",
localField: "CustomerID",
foreignField: "CustomerID",
as: "CustomerDetails"
}
}
]);
Output: Performs a join between Orders and Customers collections.
h. OUT Example
db.Products.aggregate([
{ $match: { Stock: { $gt: 50 } } },
{ $out: "HighStockProducts" }
]);
Output: Saves the result of the aggregation into a new collection HighStockProducts.
i. ADDFIELDS Example
db.Products.aggregate([
{ $addFields: { DiscountedPrice: { $subtract: ["$Price", 100] } } }
DBMS LAB manual 7
]);
Output: Adds a new field DiscountedPrice to each product.
1.Search and Index in MongoDB
Creating an Index Example:
Departments Table:
SELECT Employees.Name,
Departments.DepartmentName FROM Employees
INNER JOIN Departments ON Employees.DepartmentID =
Departments.Departme ntID;
Output:
Explanation: Returns only the rows where there is a match in both tables.
b. LEFT JOIN
SELECT Employees.Name,
Departments.DepartmentName FROM Employees
LEFT JOIN Departments ON Employees.DepartmentID =
Departments.Departmen tID;
Output:
Explanation: Returns all rows from the left table (Employees), and matched
rows from the right table (Departments). If no match is found, NULL is returned.
c. RIGHT JOIN
DBMS LAB manual 9
SELECT Employees.Name,
Departments.DepartmentName FROM Employees
RIGHT JOIN Departments ON Employees.DepartmentID =
Departments.Departme ntID;
Output:
Explanation: Returns all rows from the right table (Departments), and
matched rows from the left table (Employees). If no match is found, NULL is returned.
SELECT Employees.Name,
Departments.DepartmentName FROM Employees
FULL OUTER JOIN Departments ON Employees.DepartmentID =
Departments.Dep artmentID;
Output:
Explanation: Returns all rows when there is a match in either table. If there is
no match, NULL is returned for the unmatched rows.
e. CROSS JOIN
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
CROSS JOIN Departments;
DBMS LAB manual 10
Output:
Explanation: Returns the Cartesian product of both tables, i.e., all possible
combinations of rows.
1.Normalization and Normal Forms
Normalization is the process of organizing data to reduce redundancy
and improve data integrity. Here are the normal forms with examples:
a. First Normal Form (1NF)
Rule: Each column must contain atomic (indivisible) values.
Example:
Before 1NF:
SELECT Employees.Name,
Departments.DepartmentName FROM Employees
INNER JOIN Departments ON Employees.DepartmentID =
Departments.Departme ntID;
Output:
| Name | DepartmentName |
|--------------|----------------|
| John Smith | Sales |
| Jane Doe | HR |
| Peter Parker | IT |
Explanation: Returns rows with matching values in both tables.
b. LEFT JOIN
SELECT Employees.Name,
Departments.DepartmentName FROM Employees
LEFT JOIN Departments ON Employees.DepartmentID =
Departments.Departmen tID;
Output:
| Name | DepartmentName |
|--------------|----------------|
| John Smith | Sales |
| Jane Doe | HR |
| Mary Jane | NULL |
| Peter Parker | IT |
Explanation: Returns all records from the left table \Employees), and
matched records from the right table \Departments). Unmatched records have NULL
values.
c. RIGHT JOIN
SELECT Employees.Name,
Departments.DepartmentName FROM Employees
DBMS LAB manual 14
RIGHT JOIN Departments ON Employees.DepartmentID =
Departments.Departme ntID;
Output:
| Name | DepartmentName |
|--------------|----------------|
| John Smith | Sales |
| Jane Doe | HR |
| Peter Parker | IT |
| NULL | Marketing |
d. FULL OUTER JOIN
SELECT Employees.Name,
Departments.DepartmentName FROM Employees
FULL OUTER JOIN Departments ON Employees.DepartmentID =
Departments.Dep artmentID;
Output:
| Name | DepartmentName |
|--------------|----------------|
| John Smith | Sales |
| Jane Doe | HR |
| Peter Parker | IT |
| Mary Jane | NULL |
| NULL | Marketing |
2. Normalization
First Normal Form (1NF)
Example Before 1NF:
| StudentID | Name | Courses |
Department → DeptHead
1.Stored Procedure
Procedure:
Temporal Data:
CREATE TABLE EmployeeSalary (
EmployeeID INT,
Salary DECIMAL(10, 2),
StartDate DATE,
EndDate DATE
);
DBMS LAB manual 17
Insert Data:
Examples Insert
and Query:
SQL Create a
View:
h(key) = key % 10
Hash Table Example:
FacultyName
irstName
DBMS LAB manual 21
Problem 3: Aggregate Functions with
After Normalization:
1. Student-Course
Table: | StudentID |
CourseID |
|-----------|----------| |1|
C101|
2.Course Table:
| CourseID |
CourseName | Professor |
|----------|------------|------------|
| C101 | Math | Prof. Lee |
Problem 5: MongoDB CRUD Operations
a. Create Database and Collection
use OnlineStore;
db.createCollection("Products");
b. Insert Documents
db.Products.insertMany([
{ ProductID: 1, Name: "Laptop", Price: 999.99 },
{ ProductID: 2, Name: "Phone", Price: 599.99 },
{ ProductID: 3, Name: "Tablet", Price: 299.99 }
]);
c. Query Documents
DBMS LAB manual 23
db.Products.find({ Price: { $gt: 500 } });
Expected Output:
d. Update Document