DBMS Lab Notes
DBMS Lab Notes
Introduction:
Tables Overview:
Employees Table:
● EmployeeID (Primary Key)
● Name
● DepartmentID
Departments Table:
● DepartmentID (Primary Key)
● DepartmentName
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:
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
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
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
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:
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
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:
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:
Tables Overview:
Employees Table:
● EmployeeID (Primary Key)
● Name
● DepartmentID
Departments Table:
● DepartmentID (Primary Key)
● DepartmentName
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:
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
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:
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
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:
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
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:
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:
Tables Overview:
Employees Table:
● EmployeeID (Primary Key)
● FirstName
● LastName
● DepartmentID (Foreign Key referencing Departments table)
Departments Table:
● DepartmentID (Primary Key)
● DepartmentName
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:
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
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:
Use Cases:
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
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:
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:
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
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:
Tables Overview:
● Employees Table:
● EmployeeID (Primary Key)
● FirstName
● LastName
● DepartmentName
● DepartmentLocation
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:
In 1NF, each attribute value must be atomic (indivisible). The denormalized table
violates 1NF because the DepartmentName and DepartmentLocation columns are not
atomic.
-- 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.
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.
-- 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)
);
Conclusion: