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

DBMS Lab Notes

The document summarizes several lab reports exploring different SQL concepts: 1) The Cartesian Product lab report describes how Cartesian Products combine every row from one table with every row from another, potentially resulting in a large dataset. 2) The JOIN lab report explains different types of JOINs (INNER, LEFT, RIGHT, FULL) and their uses in combining rows from multiple tables. 3) The Views lab report shows how views provide a simplified interface to query data by encapsulating complex queries and abstracting the underlying database structure. 4) The Trigger lab report demonstrates how triggers automatically perform actions in response to data changes, such as logging changes to an audit trail table. 5) The Stored

Uploaded by

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

DBMS Lab Notes

The document summarizes several lab reports exploring different SQL concepts: 1) The Cartesian Product lab report describes how Cartesian Products combine every row from one table with every row from another, potentially resulting in a large dataset. 2) The JOIN lab report explains different types of JOINs (INNER, LEFT, RIGHT, FULL) and their uses in combining rows from multiple tables. 3) The Views lab report shows how views provide a simplified interface to query data by encapsulating complex queries and abstracting the underlying database structure. 4) The Trigger lab report demonstrates how triggers automatically perform actions in response to data changes, such as logging changes to an audit trail table. 5) The Stored

Uploaded by

200121.cse
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Lab Report: Exploring the Cartesian Product

Introduction:

In relational databases, Cartesian Product is a mathematical operation that combines


every row from one table with every row from another table. The result is a set of
combinations where each row from the first table is paired with every row from the
second table. It's essential to be cautious when using Cartesian Product, as it can result
in a large dataset, potentially leading to performance issues.

Tables Overview:

​ Employees Table:
● EmployeeID (Primary Key)
● Name
● DepartmentID
​ Departments Table:
● DepartmentID (Primary Key)
● DepartmentName

Equipment List for Cartesian Product Lab:

1) Computer
2) MSSQL Server
3) SQL Server Management Studio (SSMS)
4) Sample Database
5) Documentation
6) Internet Connection
7) Text Editor or IDE
Experiment:

Observations:

​ Size of the Result Set:


● The result set contains every possible combination of rows from the
Employees and Departments tables.
● The size of the result set is the product of the number of rows in each
table.


​ Columns in the Result Set:
● The columns in the result set include all columns from both tables.
​ Performance Considerations:
● Cartesian Products can generate a large dataset, impacting performance.
● It is crucial to use WHERE clauses to filter the result set and avoid
unnecessary computations.

Use Cases:

​ Generating Combinations:
● Cartesian Product is useful when you need to generate all possible
combinations between two sets of data.
​ Identifying Missing Relationships:
● It can be used to identify missing relationships between tables.

Conclusion:

Understanding Cartesian Product is crucial for effective database query design. While it
has its use cases, it should be used with caution due to the potential for generating
large result sets. Always consider the performance implications and use WHERE
clauses to filter the data appropriately.
Lab Report: Exploring the JOIN Operations

Introduction:

JOIN operations in SQL are fundamental for querying data from multiple tables. They
allow us to combine rows from two or more tables based on a related column. There are
various types of JOINs, including INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN,
each serving different purposes.

Tables Overview:

​ Employees Table:
● EmployeeID (Primary Key)
● Name
● DepartmentID (Foreign Key referencing Departments table)
​ Departments Table:
● DepartmentID (Primary Key
● DepartmentName

Equipment List for JOIN Lab:

1. Computer
2. MSSQL Server
3. SQL Server Management Studio (SSMS)
4. Sample Database
5. Documentation
6. Internet Connection
7. Text Editor or IDE
Experiment:

Observations:

​ INNER JOIN:
● Returns rows where there is a match in both tables based on the specified
condition.
● Result set includes only the common records.

​ LEFT JOIN:
● Returns all rows from the left table and matching rows from the right
table.
● Non-matching rows from the right table have NULL values.

​ RIGHT JOIN:
● Returns all rows from the right table and matching rows from the left
table.
● Non-matching rows from the left table have NULL values.

​ FULL JOIN:
● Returns all rows when there is a match in either the left or right table.
● Non-matching rows in both tables have NULL values.

Use Cases:

​ INNER JOIN:
● Useful when retrieving data that has matching values in both tables.
​ LEFT/RIGHT JOIN:
● Useful when you want all records from one table and matching records
from another.
​ FULL JOIN:
● Useful when you want all records from both tables, with NULLs for
non-matching records.

Conclusion:

JOIN operations are powerful tools for combining data from multiple tables. The choice
of the JOIN type depends on the specific requirements of the query and the desired
outcome. Understanding these operations is crucial for efficient database querying and
report generation.
Lab Report: Exploring the Views

Introduction:

In SQL, a view is a virtual table based on the result of a SELECT query. It allows users to
encapsulate complex queries and present a simplified interface to the database. Views
can be used to restrict access to specific columns, aggregate data, and join tables,
providing a layer of abstraction over the underlying database structure.

Tables Overview:

​ Employees Table:
● EmployeeID (Primary Key)
● Name
● DepartmentID (Foreign Key referencing Departments table)
​ Departments Table:
● DepartmentID (Primary Key)
● DepartmentName

Equipment List for View Lab:

1. Computer
2. MSSQL Server
3. SQL Server Management Studio (SSMS)
4. Sample Database
5. Documentation
6. Internet Connection
7. Text Editor or IDE
Experiment:

Observations:

​ View Creation:
● The CREATE VIEW statement allows the creation of a view based on a
SELECT query.
● In the example, the view EmployeeDetails combines data from the
Employees and Departments tables
​ View Querying:
● Views can be queried in the same way as tables.
● Querying the EmployeeDetails view retrieves the specified columns
without directly interacting with the underlying tables.

Use Cases:

​ Simplified Querying:
● Views provide a simplified interface for users by encapsulating complex
queries.
​ Security and Data Restriction:
● Views can be used to restrict access to specific columns, enhancing
security.
​ Abstraction of Data Structure:
● Views abstract the underlying database structure, allowing changes to be
made to the tables without affecting user queries.

Conclusion:

Views are valuable tools in SQL for simplifying data access and enhancing security.
They provide a layer of abstraction that is particularly useful when dealing with complex
database structures or when there is a need to restrict access to certain data.
Lab Report: Exploring the Trigger
Introduction:

Triggers in SQL provide a way to automatically perform actions, such as updating data
or enforcing business rules, in response to predefined events. They are powerful
mechanisms that can enhance data integrity and automate routine tasks. Triggers can
be classified into DML triggers (fired by INSERT, UPDATE, DELETE operations) and DDL
triggers (fired by CREATE, ALTER, DROP operations).

Tables Overview:

​ Employees Table:
● EmployeeID (Primary Key)
● Name
● DepartmentID INT
​ AuditTrail Table:
● TransactionID (Primary Key)
● TableName
● OperationType
● OperationTime

Equipment List for Trigger Lab:

1. Computer
2. MSSQL Server
3. SQL Server Management Studio (SSMS)
4. Sample Database
5. Documentation
6. Internet Connection
7. Text Editor or IDE
Experiment:

Observations:

​ AuditTrail Table:
● An AuditTrail table is created to store information about changes to
other tables.




​ INSERT Trigger:
● An AFTER INSERT trigger (tr_EmployeeInsert) is created on the
Employees table.
● The trigger logs an entry into the AuditTrail table whenever a new record
is inserted into the Employees table.
​ Data Insertion:
● When data is inserted into the Employees table, the trigger is automatically
invoked, and an entry is added to the AuditTrail table.

Use Cases:

​ Change Tracking:
● Triggers can be used to log changes to specific tables, facilitating auditing
and change tracking.
​ Enforcing Business Rules:
● Triggers can enforce business rules by automatically validating or
modifying data before or after certain operations.

Conclusion:

Triggers in SQL provide a mechanism for automating actions in response to database


events. They are versatile tools that can be employed for various purposes, including
auditing, enforcing business rules, and maintaining data integrity.
Lab Report: Exploring the Stored Procedure
Introduction:

Stored procedures in SQL provide a way to encapsulate and execute a sequence of SQL
statements on the database server. They offer advantages such as improved
performance, code modularity, and enhanced security. Stored procedures can accept
parameters, return values, and even include conditional logic, making them powerful
tools for database developers and administrators.

Tables Overview:

​ Employees Table:
● EmployeeID (Primary Key)
● Name
● DepartmentID
​ Departments Table:
● DepartmentID (Primary Key)
● DepartmentName

Equipment List for Stored Procedure Lab:

1. Computer
2. MSSQL Server
3. SQL Server Management Studio (SSMS)
4. Sample Database
5. Documentation
6. Internet Connection
7. Text Editor or IDE
Experiment:
Observations:

​ Stored Procedure Creation:


● The CREATE PROCEDURE statement is used to define a stored procedure
named GetEmployeesByDepartment.
● The procedure accepts a parameter @DeptID to filter employees by
department.
​ Stored Procedure Execution:
● The EXEC statement is used to execute the stored procedure, passing a
specific department ID as a parameter.
● The procedure returns a result set containing employee details for the
specified department.

Use Cases:

​ Code Modularity:
● Stored procedures enhance code modularity by encapsulating logic in a
single, reusable unit.
​ Performance Improvement:
● Execution plans for stored procedures are cached, resulting in potential
performance improvements compared to ad-hoc SQL statements.
​ Security:
● Stored procedures can be used to control access to data, as users only
need execute permissions on the procedure rather than direct table
access.

Conclusion:

Stored procedures in SQL offer a structured and efficient way to manage and execute
SQL statements. They enhance code organization, promote reusability, and contribute to
improved database security and performance.
Lab Report: Exploring the Function
Introduction:

Functions in MSSQL provide a way to encapsulate logic and calculations in a modular


and reusable format. Unlike stored procedures, functions return a single value and are
often used in scenarios where a computation needs to be applied within a SELECT
statement, a WHERE clause, or as part of an expression. Functions can be categorized
as scalar functions, which return a single value, or table-valued functions, which return a
table.

Tables Overview:

​ Employees Table:
● EmployeeID (Primary Key)
● Name
● DepartmentID
​ Departments Table:
● DepartmentID (Primary Key)
● DepartmentName

Equipment List for Function Lab:

1. Computer
2. MSSQL Server
3. SQL Server Management Studio (SSMS)
4. Sample Database
5. Documentation
6. Internet Connection
7. Text Editor or IDE
Experiment:

Observations:

​ Function Creation:
● The CREATE FUNCTION statement is used to define a scalar function
named GetAverageDepartmentID.
● The function accepts a parameter @DeptID and returns a decimal value
representing the average salary.
​ Function Execution:
● The function is executed using a SELECT statement, passing a specific
department ID as a parameter.
● The result is the average salary for employees in the specified department.

Use Cases:

​ Calculations within Queries:


● Scalar functions are useful for embedding calculations directly into
SELECT statements.
​ Reusable Logic:
● Functions can be reused in multiple queries, promoting code reuse and
maintainability.

Conclusion:

Functions in SQL offer a modular and reusable approach to encapsulating logic and
calculations. Scalar functions, in particular, are valuable for scenarios where a single
value needs to be computed and returned. Understanding how to create and utilize
functions enhances the flexibility and efficiency of database queries.
Lab Report: Exploring the HAVING Clause
Introduction:

The HAVING clause in MSSQL is an extension of the WHERE clause and is specifically
designed to filter the results of aggregate functions applied to grouped rows. It allows
for the inclusion or exclusion of groups based on a specified condition. This is
particularly useful when working with grouped data and wanting to filter results based
on aggregated values.

Tables Overview:

​ Orders Table:
● OrderID (Primary Key)
● CustomerID
● OrderDate
● TotalAmount
​ Customers Table:
● CustomerID (Primary Key)
● CustomerName
● Country

Equipment List for HAVING Clause Lab:

1. Computer
2. MSSQL Server
3. SQL Server Management Studio (SSMS)
4. Sample Database
5. Documentation
6. Internet Connection
7. Text Editor or IDE
Experiment:

Observations:

​ HAVING Clause Usage:


● The HAVING clause is used after the GROUP BY clause to filter groups
based on aggregated values.
● In the example, it filters out customers who have placed fewer than three
orders.
Use Cases:

​ Filtering Grouped Data:


● The HAVING clause is valuable when working with grouped data, allowing
for the inclusion or exclusion of groups based on aggregate conditions.

Conclusion:

The HAVING clause in SQL is a powerful tool for filtering grouped data based on
aggregated results. It provides flexibility when dealing with complex queries involving
aggregate functions and grouped rows.
Lab Report: Exploring the GROUP BY Clause
Introduction:

The GROUP BY clause in SQL is utilized to arrange identical data into summary rows
based on specific column values. It is commonly used in conjunction with aggregate
functions like COUNT, SUM, AVG, etc., to generate meaningful insights from grouped
data. GROUP BY transforms individual rows into summary rows, making it a powerful
tool for data analysis.

Tables Overview:

​ Orders Table:
● OrderID (Primary Key)
● CustomerID (Foreign Key referencing Customers table)
● OrderDate
● TotalAmount
​ Customers Table:
● CustomerID (Primary Key)
● CustomerName
● Country

Equipment List for GROUP BY Lab:

1. Computer
2. MSSQL Server
3. SQL Server Management Studio (SSMS)
4. Sample Database
5. Documentation
6. Internet Connection
7. Text Editor or IDE
Experiment:

Observations:

​ GROUP BY Clause Usage:


● The GROUP BY clause is used to group rows by the specified column(s).
● In the example, it groups orders by CustomerID.
Use Cases:

​ Data Aggregation:
● GROUP BY is essential for aggregating data and computing summary
statistics.
​ Summarizing Results:
● GROUP BY is used to transform detailed data into summarized and
meaningful results.

Conclusion:

The GROUP BY clause in SQL is a crucial component for aggregating and summarizing
data based on specific column values. It facilitates the generation of insightful reports
and analysis by organizing data into logical groups.
Lab Report: Exploring Aggregate Functions
Introduction:

Aggregate functions in SQL are essential tools for performing calculations on a set of
values and returning a single result. These functions are often used in conjunction with
the GROUP BY clause to perform calculations on groups of rows. Understanding how to
use aggregate functions is crucial for obtaining meaningful insights from databases.

Tables Overview:

​ Orders Table:
● OrderID (Primary Key)
● CustomerID (Foreign Key referencing Customers table)
● OrderDate
● TotalAmount
​ Customers Table:
● CustomerID (Primary Key)
● CustomerName
● Country

Equipment List for Aggregate Functions Lab:

1. Computer
2. MSSQL Server
3. SQL Server Management Studio (SSMS)
4. Sample Database
5. Documentation
6. Internet Connection
7. Text Editor or IDE
Experiment:

Observations:

​ Aggregate Functions Usage:


● Aggregate functions are applied to sets of values to calculate summary
statistics.
● In the example, COUNT counts the number of orders, SUM calculates the
total sales, AVG computes the average sale, MIN finds the minimum sale,
and MAX determines the maximum sale.

Use Cases:

​ Data Analysis:
● Aggregate functions are vital for extracting meaningful insights and
statistics from large datasets.
​ Reporting:
● Aggregating data is crucial for generating summary reports and
performance indicators.

Conclusion:

Aggregate functions in SQL play a pivotal role in performing calculations on sets of


data, producing summary statistics that are valuable for data analysis and reporting.
These functions provide a way to extract meaningful information from databases
efficiently.
Lab Report: Modifying an Existing Table
Introduction:

Modifying an existing table is a common task in database management. The ALTER


TABLE statement in SQL allows for various modifications to the structure of a table,
providing flexibility in adapting the database schema to evolving requirements.
Understanding how to effectively modify tables is essential for maintaining a
well-designed and adaptable database.

Tables Overview:

​ Employees Table:
● EmployeeID (Primary Key)
● FirstName
● LastName
● DepartmentID (Foreign Key referencing Departments table)
​ Departments Table:
● DepartmentID (Primary Key)
● DepartmentName

Equipment List for Modify Existing Table Lab:

1. Computer
2. MSSQL Server
3. SQL Server Management Studio (SSMS)
4. Sample Database
5. Documentation
6. Internet Connection
7. Text Editor or IDE
Experiment:

Observations:

​ ALTER TABLE Statement:


● The ALTER TABLE statement is used to modify the structure of an existing
table.
● In the example, a new column named Email of data type VARCHAR(100) is
added to the Employees table.
Use Cases:

​ Adapting to Business Changes:


● Modifying tables allows for the adaptation of the database schema to
changing business requirements.
​ Improving Data Storage:
● Adding or altering columns can optimize data storage and retrieval based
on evolving data needs.

Conclusion:

The ALTER TABLE statement in SQL is a powerful tool for modifying existing tables,
enabling database administrators and developers to adjust the schema to meet
changing requirements. It provides a mechanism for continuous improvement and
adaptation of the database structure.
Lab Report: Deleting a Table

Introduction:

Deleting a table is a critical operation that permanently removes both the table structure
and its data from the database. The DROP TABLE statement in SQL is used to execute
this action. Understanding how to properly delete a table is crucial to avoid unintended
data loss and to ensure the integrity of the database schema.

Tables Overview:

​ Employees Table:
● EmployeeID (Primary Key)
● FirstName
● LastName
● DepartmentID (Foreign Key referencing Departments table)
​ Departments Table:
● DepartmentID (Primary Key)
● DepartmentName

Equipment List for Deleting a Table Lab:

1. Computer
2. MSSQL Server
3. SQL Server Management Studio (SSMS)
4. Sample Database
5. Documentation
6. Internet Connection
7. Text Editor or IDE
Experiment:
Observations:

​ DROP TABLE Statement:


● The DROP TABLE statement is used to delete an existing table and its
associated data.
● In the example, the Employees table is deleted from the database.

Use Cases:

​ Removing Unnecessary Tables:


● Deleting tables that are no longer needed helps maintain a clean and
efficient database.
​ Database Cleanup:
● Deleting tables can be part of routine database cleanup procedures to
optimize database performance.

Conclusion:

The DROP TABLE statement in SQL is a powerful command for permanently removing
tables from the database. It should be used with caution, and careful consideration
must be given to the potential impact on data integrity and application functionality.
Lab Report: Exploring Nested Queries

Introduction:

Nested queries in SQL provide a powerful mechanism for writing more complex and
flexible queries. A nested query is a query that is embedded within another query,
allowing the inner query to execute and provide results to the outer query.
Understanding how to construct and use nested queries is essential for performing
advanced operations and obtaining specific subsets of data.

Tables Overview:

​ Orders Table:
● OrderID (Primary Key)
● CustomerID (Foreign Key referencing Customers table)
● OrderDate
● TotalAmount
​ Customers Table:
● CustomerID (Primary Key)
● CustomerName
● Country

Equipment List for Nested Query Lab:

1. Computer
2. MSSQL Server
3. SQL Server Management Studio (SSMS)
4. Sample Database
5. Documentation
6. Internet Connection
7. Text Editor or IDE
Experiment:

Observations:

​ Nested Query Usage:


● The nested query is used to retrieve CustomerID values for customers
from 'USA' in the Customers table.
● The outer query then retrieves orders from the Orders table where the
CustomerID is in the list obtained from the nested query.
Use Cases:

​ Filtering Orders based on Customer Country:


● Nested queries are useful for filtering orders based on conditions related
to customer information.
​ Dynamic Criteria:
● Nested queries provide a dynamic way to set criteria for data retrieval
based on related tables.

Conclusion:

Nested queries in SQL, demonstrated using the Orders and Customers tables, provide a
flexible and powerful mechanism for retrieving data based on conditions related to
other tables. This capability enhances the precision and adaptability of SQL queries.
Lab Report: Exploring Entity-Relationship (ER) Diagram

Introduction:

ER diagrams play a crucial role in database design, providing a visual representation of


the structure and relationships within a database. BigER is an extension for Visual
Studio Code that allows for the creation and visualization of ER diagrams directly within
the development environment. This lab focuses on the basics of using BigER to create a
simple ER diagram based on a fictional database scenario.

Database Overview:

Consider a simple database scenario with two entities: Employees and Departments.

​ Employees Entity:
● Attributes: EmployeeID (Primary Key), FirstName, LastName,
DepartmentID (Foreign Key referencing Departments table)
​ Departments Entity:
● Attributes: DepartmentID (Primary Key), DepartmentName

Equipment List for ER Diagram Lab:

1. Computer
2. VS Code
3. Internet Connection
4. BigER Extension
Experiment:

Observations:

​ ER Diagram Representation:
● The ER diagram visually represents the Employees and Departments
entities, their attributes, and the relationship between them.
​ Entity-Relationship Visualization:
● The diagram provides a clear representation of how entities are related,
allowing for a better understanding of the database structure.

Use Cases:

​ Database Design:
● ER diagrams are crucial for designing and communicating the structure of
a database before implementation.

​ Visualizing Relationships:
● ER diagrams help in visualizing and understanding the relationships
between entities in a database.

Conclusion:

BigER in Visual Studio Code offers a convenient way to create and visualize ER
diagrams for database design. Understanding the components of an ER diagram,
including entities, attributes, and relationships, is essential for effective database
modeling.
Lab Report: Exploring Database Normalization

Introduction:

Database normalization is a set of rules applied to relational database schemas to


minimize data redundancy and dependency. The normalization process ensures that
data is stored efficiently, and relationships between tables are well-defined. The primary
goals of normalization include eliminating data anomalies, improving data integrity, and
simplifying database maintenance.

Tables Overview:

Consider a simplified scenario with a denormalized table named Employees:

● Employees Table:
● EmployeeID (Primary Key)
● FirstName
● LastName
● DepartmentName
● DepartmentLocation

Equipment List for Normalization Lab:

1. Computer
2. MSSQL Server
3. SQL Server Management Studio (SSMS)
4. Sample Database
5. Documentation
6. Internet Connection
7. Text Editor or IDE
Experiment:

Denormalized Table (Before Normalization):

CREATE TABLE Employees (


EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
DepartmentName VARCHAR(50),
DepartmentLocation VARCHAR(50)
);

First Normal Form (1NF):

In 1NF, each attribute value must be atomic (indivisible). The denormalized table
violates 1NF because the DepartmentName and DepartmentLocation columns are not
atomic.

Normalized Tables (1NF):

-- Table: Departments
CREATE TABLE Departments (
DepartmentID INT PRIMARY KEY,
DepartmentName VARCHAR(50),
DepartmentLocation VARCHAR(50)
);

-- Table: Employees
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
DepartmentID INT,
FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID)
);
Second Normal Form (2NF):

In 2NF, all non-key attributes must be fully functionally dependent on the entire primary
key. In our case, there is no partial dependency, so the denormalized table is already in
2NF.

Third Normal Form (3NF):

In 3NF, all non-key attributes must be functionally dependent only on the primary key.
The denormalized table violates 3NF because DepartmentLocation is transitively
dependent on the primary key through DepartmentName.

Normalized Tables (3NF):

-- Table: EmployeeDetails
CREATE TABLE EmployeeDetails (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
DepartmentID INT,
FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID)
);

-- Table: Departments
CREATE TABLE Departments (
DepartmentID INT PRIMARY KEY,
DepartmentName VARCHAR(50),
DepartmentLocation VARCHAR(50)
);

In this case, EmployeeDetails contains information about employees, and Departments


contains information about departments. The DepartmentLocation attribute is no
longer stored in the EmployeeDetails table, eliminating the transitive dependency.
Use Cases:

​ Improved Data Integrity:


● Normalization improves data integrity by minimizing redundancy and
dependency issues.
​ Simplified Maintenance:
● Normalized databases are easier to maintain and modify as changes are
localized to specific tables.

Conclusion:

Normalization is a crucial process in relational database design, promoting efficiency,


integrity, and simplicity. By adhering to normalization principles, databases can be
structured to minimize redundancy and ensure a high level of data integrity.

You might also like