Experiment 2 Title Installation of MySQL and Practice of Advanced DDL & DML Commands Aim To install MySQL Server and perform advanced database operations using DDL and DML commands in order to understand database creation, manipulation, and maintenance. Software / Tools Required ● MySQL Server (Version 8.0 or above) ● MySQL Workbench ● Windows / Linux Operating System ● Minimum 4 GB RAM Theory MySQL is a widely used open - source Relational Database Management System (RDBMS). SQL commands are categorized ba sed on their functionality. DDL commands define the structure of database objects such as databases and tables. These commands make permanent changes to the database schema. DML commands are used to manipulate data within tables. They allow insertion, retr ieval, updating, and deletion of records. Proper use of DDL and DML commands ensures data consistency, integrity, and efficient database management. Code -- Create Database CREATE DATABASE CompanyDB; USE CompanyDB; PCU, School of Engineering & Technology 4 Academic Year:2025 - 2026 Sem: IV Course: Database Management System Laboratory Course code: UBTDS211 -- Create Table CREATE TABLE Employee ( Emp_ID INT PRIMARY KEY, Emp_Name VARCHAR(50) NOT NULL, Department VARCHAR(30), Salary DECIMAL(10,2) ); -- Insert Records INSERT INTO Employee VALUES (1, 'Rahul', 'IT', 60000); INSERT INTO Employee VALUES (2, 'Anita', 'HR', 55000); INSERT INTO Employee VALUES (3, 'Suresh', 'Finance', 65000); -- Update Record UPDATE Employee SET Salary = 70000 WHERE Emp_ID = 1; -- Delete Record DELETE FROM Employee WHERE Emp_ID = 2; -- Display Records SELECT * FROM Employee; Conclusion Thus, MySQL was successfully installed and configured. DDL and DML commands were executed to create database objects and manipulate data effec tively, providing hands - on experience with relational database operations. Experiment 3 Aim To understand and implement advanced SQL queries using ANY, ALL, IN, EXISTS and set operations such as UNION and INTERSECT for effective data retrieval. Software / Tools Required ● MySQL Server 8.x or above ● MySQL Workbench ● Windows / Linux Operating System Theory Advanced SQL operators are used to perform complex queries involving subqueries and set operations: ● IN: Checks whether a value matches any v alue in a list or subquery. ● ANY: Returns TRUE if the comparison is true for at least one value in the subquery result. ● ALL: Returns TRUE only if the comparison is true for all values in the subquery result. ● EXISTS: Tests for the existence of rows ret urned by a subquery. Set Operations: ● UNION: Returns all distinct rows from two SELECT statements. ● INTERSECT: Returns common rows from two SELECT statements (conceptual support in SQL; simulated in MySQL). These operations help in combining and comparin g results from multiple tables efficiently. Procedure 1. Create required tables and insert sample data. 2. Execute queries using IN, ANY, ALL, and EXISTS. 3. Apply set operations such as UNION and INTERSECT. PCU, School of Engineering & Technology 7 Academic Year:2025 - 2026 Sem: IV Course: Database Management System Laboratory Course code: UBTDS211 4. Analyze query results. Code -- Create Database CREATE DATABASE QueryDB; USE QueryDB; -- Create Tables CREATE TABLE Student ( Stud_ID INT PRIMARY KEY, S tud_Name VARCHAR(50), Department VARCHAR(30) ); CREATE TABLE Result ( Stud_ID INT, Marks INT ); -- Insert Data INSERT INTO Student VALUES (1, 'Amit', 'CSE'); INSERT INTO Student VALUES (2, 'Neha', 'AI&ML'); INSERT INTO Student VALUES (3, 'Ravi', 'CSE'); INSERT INTO Result VALUES (1, 85); INSERT INTO Result VALUES (2, 92); PCU, School of Engineering & Technology 8 Academic Year:2025 - 2026 Sem: IV Course: Database Management System Laboratory Course code: UBTDS211 INSERT INTO Result VALUES (3, 70); -- IN Operator SELECT Stud_Name FROM Student WHERE Stud_ID IN (SELECT Stud_ID FROM Result WHERE Marks > 80); -- ANY Operator SELECT Stud_Name FROM Student WHERE Stud_ID = ANY (SELECT Stud_ID FROM Result WHERE Marks > 80); -- ALL Operator SELECT Stud_Name FROM Student WHERE Stud_ID > ALL (SELECT Stud_ID FROM Result WHERE Marks < 90); -- EXISTS Operator SELECT Stud_Name FROM Student S WHERE EXISTS (SELECT * FROM Result R WHERE S.Stud_ID = R.Stud_ID AND R.Marks > 90); -- UNION Operator SELECT Stud_Name FROM Stud ent WHERE Department = 'CSE' UNION SELECT Stud_Name FROM Student WHERE Department = 'AI&ML'; -- INTERSECT (Simulated in MySQL) SELECT Stud_ID FROM Student WHERE Stud_ID IN (SELECT Stud_ID FROM Result WHERE Marks > 80); PCU, School of Engineering & Technology 9 Academic Year:2025 - 2026 Sem: IV Course: Database Management System Laboratory Course code: UBTDS211 Output ● Queries successfully executed using ANY, ALL, IN, EXISTS. ● Combined and filtered results obtained using UNION and INTERSECT logic. Conclusion The execution of SQL queries using ANY, ALL, IN, EXISTS, and set operations such as UNION and INTERSECT helped in understanding advanced data retrieval techniques. These operators enable efficient comparison, filtering, and combination of data f rom multiple tables, which is essential for solving real - world database queries. Experiment 4 Aim To understand and implement SQL queries using aggregate functions, GROUP BY, HAVING, and ORDER BY clauses for summarized and sorted data analysis. Software / Tools Required ● MySQL Server 8.x or above ● MySQL Workbench ● Windows / Linux Operating System Theory Aggregate functions perform calculations on a set of values and return a single summarized value. These functions are commonly used with GROUP BY to categorize data. Common aggregate functions: ● COUNT() – Counts number of rows ● SUM() – Calculates total value ● AVG() – Calculates average value ● MIN() – Finds minimum value ● MAX() – Finds maximum value The GROUP BY clause groups rows that have the sam e values into summary rows. The HAVING clause is used to filter groups, whereas ORDER BY sorts the result set in ascending or descending order. Procedure 1. Create the required database and table. 2. Insert sample records. 3. Apply aggregate functions on table data. 4. Use GROUP BY and HAVING for grouped conditions. PCU, School of Engineering & Technology 11 Academic Year:2025 - 2026 Sem: IV Course: Database Management System Laboratory Course code: UBTDS211 5. Sort results using ORDER BY clause. Code -- C reate Database CREATE DATABASE AggregateDB; USE AggregateDB; -- Create Table CREATE TABLE Employee ( Emp_ID INT PRIMARY KEY, Emp_Name VARCHAR(50), Department VARCHAR(30), Salary INT ); -- Insert Records INSERT INTO Employee VALUES (1, 'Amit', 'CSE', 60000); INSERT INTO Employee VALUES (2, 'Neha', 'AI&ML', 75000); INSERT INTO Employee VALUES (3, 'Rahul', 'CSE', 50000); INSERT INTO Employee VALUES (4, 'Pooja', 'AI&ML', 80000); INSERT INTO Employee VALUES (5, 'Kiran', 'ECE', 45000); -- Aggregate Functio ns SELECT COUNT(*) AS Total_Employees FROM Employee; SELECT AVG(Salary) AS Average_Salary FROM Employee; SELECT MAX(Salary) AS Highest_Salary FROM Employee; PCU, School of Engineering & Technology 12 Academic Year:2025 - 2026 Sem: IV Course: Database Manag ement System Laboratory Course code: UBTDS211 SELECT MIN(Salary) AS Lowest_Salary FROM Employee; -- GROUP BY Clause SELECT Department, AVG(Salary) AS Avg_Salary FROM Employee GROUP BY Department; -- HAVING Clause SELECT Department, AVG(Salary) AS Avg_Salary FROM Employee GROUP BY Department HAVING AVG(Salary) > 60000; -- ORDER BY Clause SELECT * FROM Employee ORDER BY Salary DESC; Conclusion The use of aggregate functions along with GROUP BY, HAVING, and ORDER BY clauses enabled efficient summariz ation, filtering, and sorting of data. These SQL features are essential for analytical queries and decision - making in real - world database applications. Experiment 5 Aim To understand and implement SQL JOIN operations such as INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN (simulated) and SELF JOIN for retrieving related data from multiple tables. Software / Tools Required ● MySQL Server 8.x or above ● MySQL Workbench ● Windows / Linux Operating System Theory A JOIN is used to combine rows from two or more tables based on a related column between them. Joins help retrieve meaningful data by establishing relationships between tables. Types of Joins: ● INNER JOIN: Returns only the matching records from both tables. ● LEFT JOIN: Returns all records from the left table and matched records from the right table. PCU, School of Engineering & Technology 15 Academic Year:2025 - 2026 Sem: IV Course: Database Management System Laboratory Course code: UBTDS211 ● RIGHT JOIN: Returns all records from the right table and matched records from the left table. ● FULL JOIN: Returns all records when there is a match in either table (not directly supported in MySQL; simulated using UNION). ● SELF JOIN: A table is joined with itself to compare rows within the same table. Joins are essential in relational databases for data normalization and efficient querying. Procedure 1. Create the required database and tables. 2. Insert sample records into the tables. 3. Execute queries using different types of JOIN operations. 4. Observe an d analyze the output of each JOIN. 5. Understand how data is combined from multiple tables. Code -- Create Database CREATE DATABASE JoinDB; USE JoinDB; PCU, School of Engineering & Technology 16 Academic Year:2025 - 2026 Sem: IV Course: Database Management System Laboratory Course code: UBTDS211 -- Create Tables CREATE TABLE Department ( Dept_ID INT PRIMARY KEY, Dept_Name VARCHAR(30) ); CREATE TABLE Employee ( Emp_ID INT PRIMARY KEY, Emp_Name VARCHAR(50), Dept_ID INT, Salary INT ); -- Insert Da ta into Department INSERT INTO Department VALUES (1, 'CSE'); INSERT INTO Department VALUES (2, 'AI&DS'); INSERT INTO Department VALUES (3, 'ECE'); -- Insert Data into Employee INSERT INTO Employee VALUES (101, 'Amit', 1, 50000); PCU, School of Engineering & Technology 17 Academic Year:2025 - 2026 Sem: IV Course: Database Management System Laboratory Course code: UBTDS211 INSERT INTO Employee VALUES (102, 'Neha', 2, 60000); INSERT INTO Employee VALUES (103, 'Ravi', 1, 45000); INSERT INTO Employee VALUES (104, 'Sita', NULL, 40000); -- INNER JOIN SELECT Emp_Name, Dept_Name FROM Employee INNER JOIN Department ON Employee.Dept_ID = Department.Dept_ID; -- LEFT JOIN SELECT Emp_Name, Dept_Name FROM Employee LEFT JOIN Department ON Employee.Dept_ID = Dep artment.Dept_ID; -- RIGHT JOIN SELECT Emp_Name, Dept_Name FROM Employee RIGHT JOIN Department ON Employee.Dept_ID = Department.Dept_ID; PCU, School of Engineering & Technology 18 Academic Year:2025 - 2026 Sem: IV Course: Database Management System Laboratory Course code: UBTDS211 -- FULL JOIN (Simulated using UNION) SELECT Emp_Name, Dept_Name FROM Employee LEFT JOIN Department ON Employee.Dept_ID = Department.Dept_ID UNION SELECT Emp_Name, Dept_Name FROM Employee RIGHT JOIN Department ON Employee.D ept_ID = Department.Dept_ID; -- SELF JOIN SELECT A.Emp_Name AS Employee, B.Emp_Name AS Colleague FROM Employee A, Employee B WHERE A.Dept_ID = B.Dept_ID AND A.Emp_ID <> B.Emp_ID; Output PCU, School of Engineering & Technology 19 Academic Year:2025 - 2026 Sem: IV Course: Database Management System Laboratory Course code: UBTDS211 ● INNER JOIN displays only employees assigned to departments. ● LEFT JOIN displays all employees including those without departments. ● RIGHT JOIN displays all departments including those without employees. ● FULL JOIN (simulated) displays all records from both tables. ● SELF JOIN displays employees working in the same department. Conclusion The experiment successfully demonstrated the use of various SQL JOIN operations to retrieve related data from multiple tables. JOINs are a fundamental concept in relational databases and enable efficient querying, data consistency, and meaningful data relationships in real - world applications. Experiment 6 Aim To understand the concept of indexing and implement different types of indexes on database tables to improve query performance. Software / Tools Required ● MySQL Server 8.x or above ● MySQL Workbench ● Windows / Linux Operating System Theory An index is an ordered data stru cture that improves the speed of data retrieval operations on a database table. Indexes are created on one or more columns of a table and help reduce the time required to access records. Types of indexes in MySQL: ● PRIMARY Index – Automatically created on primary key ● UNIQUE Index – Ensures all values in the indexed column are unique ● INDEX (Simple Index) – Improves search performance ● COMPOSITE Index – Created on multiple columns While indexes improve SELECT query performance, they may slightly reduce INSERT, UPDATE, and DELETE performance due to maintenance overhead. Procedure 1. Create a database and table. 2. Insert sufficient records into the table. 3. Create different types of indexes. 4. Execute SELECT queries to observe improved performance. 5. D rop indexes when not required. PCU, School of Engineering & Technology 21 Academic Year:2025 - 2026 Sem: IV Course: Database Management System Laboratory Course code: UBTDS211 Code -- Create Database CREATE DATABASE IndexDB; USE IndexDB; -- Create Table C REATE TABLE Student ( Student_ID INT, Name VARCHAR(50), Department VARCHAR(30), Email VARCHAR(50) ); -- Insert Records INSERT INTO Student VALUES (1, 'Amit', 'CSE', 'amit@gmail.com'); INSERT INTO Student VALUES (2, 'Neha', 'AI&ML', 'neha@gmail.com'); INSERT INTO Student VALUES (3, 'Rahul', 'CSE', 'rahul@gmail.com'); INSERT INTO Student VALUES (4, 'Pooja', 'ECE', 'pooja@gmail.com'); -- Create Index CREATE INDEX idx_dept ON Student(Department); -- Create Unique Index CREATE UNIQUE INDEX idx_email ON St udent(Email); PCU, School of Engineering & Technology 22 Academic Year:2025 - 2026 Sem: IV Course: Database Management System Laboratory Course code: UBTDS211 -- Create Composite Index CREATE INDEX idx_name_dept ON Student(Name, Department); -- Use Index in Query SELECT * FROM Student WHERE Department = 'CSE'; -- Drop Index DROP INDEX idx_dept ON Student; Conclusion The implementation of indexes significantly improves the efficiency of data retrieval operations. By creating appropriate indexes on frequent ly searched columns, query performance can be optimized while maintaining database integrity. Experiment 7 Aim To understand and implement exception handling mechanisms in MySQL using handlers to manage runtime errors during database operations. Software / Tools Required ● MySQL Server 8.x or above ● MySQL Workbench ● Windows / Linux Operating System Theory Exception handling in DBMS refers to the mechanism used to detect, manage, and respond to runtime errors that occur during database operations . In MySQL, exception handling is primarily implemented within stored procedures using DECLARE HANDLER, SIGNAL, and RESIGNAL statements. Errors in database systems may occur due to various reasons such as violation of integrity constraints, duplicate key i nsertion, division by zero, invalid input values, or logical errors in SQL statements. Without exception handling, such errors can abruptly terminate execution and may lead to inconsistent database states. MySQL supports exception handling through the use of handlers, which are blocks of code that are executed automatically when a specific error condition occurs. Types of handlers in MySQL: ● CONTINUE Handler: After handling the exception, execution continues with the next statement. ● EXIT Handler: After handling the exception, execution of the block is terminated. Error conditions can be specified using: ● SQLSTATE values (standardized 5 - character error codes) ● MySQL error numbers ● User - defined conditions using the SIGNAL statement The SIGNAL statement is used to explicitly raise an exception, while RESIGNAL is used to re - throw the current exception after partial handling. Exception handling improves the PCU, School of Engineering & Technology 27 Academic Year:2025 - 2026 Sem: IV Course: Database Manageme nt System Laboratory Course code: UBTDS211 reliability, robustness, and fault tolerance of database applications by ensuring graceful error recovery and meaningful error messages. Procedure 1. Create a database and table. 2. Write a stored procedure. 3. De clare exception handlers. 4. Execute the procedure to observe handled exceptions. Code -- Create Database CREATE DATABASE ExceptionDB; USE ExceptionDB; -- Create Table CREATE TABLE Account ( Acc_ID INT PRIMARY KEY, Acc_Name VARCHAR(50), Balance INT ); -- Insert Records INSERT INTO Account VALUES (1, 'Ravi', 5000); INSERT INTO Account VALUES (2, 'Neha', 3000); PCU, School of Engineering & Technology 28 Academic Year:2025 - 2026 Sem: IV Course: Database Management System Laboratory Course code: UBTDS211 -- Stored Procedure with Exception Handling DELIMITER // CREATE PROCEDURE WithdrawAmount(IN acc INT, IN amt INT) BEGIN DECLARE insufficient_funds CONDITION FOR SQLSTATE '45000'; DECLARE EXIT HANDLER FOR insufficient_funds BEGIN SELECT 'Insufficient Balance. T ransaction Failed.' AS Message; END; IF (SELECT Balance FROM Account WHERE Acc_ID = acc) < amt THEN SIGNAL insufficient_funds; ELSE UPDATE Account SET Balance = Balance - amt WHERE Acc_ID = acc; SELECT 'Transaction Successful.' AS Message; END IF; END // DELIMITER ; -- Call Procedure CALL WithdrawAmount(2, 4000); Conclusion Exception handling was successfully implemented using handlers in MySQL. This ensures controlled execution of database operations and prevents abrupt termination due to runtime errors. Experiment 8 Aim To understand and implement database triggers in MySQL for automatic execution of actions in response to specific database events. Software / Tools Required ● MySQL Server 8.x or above ● MySQL Workbench ● Windows / Linux Operating Syst em Theory A trigger is a special type of stored program in a database that is automatically executed (or fired) when a specified event occurs on a table. Triggers are mainly used to enforce business rules, maintain audit trails, ensure data integrity, and perform automatic validations. In MySQL, triggers are associated with tables and can be activated by the following events: ● INSERT ● UPDATE ● DELETE Triggers can be classified based on timing: ● BEFORE Triggers: Executed before the triggering event occurs ● AFTER Triggers: Executed after the triggering event occurs. MySQL supports the use of OLD and NEW keywords inside triggers: ● OLD: Refers to the existing data before modification. ● NEW: Refers to the new data being inserted or updated. Triggers help in automating repetitive tasks and maintaining consistency without requiring manual intervention from application programs. PCU, School of Engineering & Technology 31 Academic Year:2025 - 2026 Sem: IV Course: Database Management System Laboratory Course co de: UBTDS211 Procedure 1. Create the required database and tables. 2. Write trigger definitions using BEFORE or AFTER clauses. 3. Perform INSERT, UPDATE, or DELETE operations. 4. Observe automatic execution of trigger actions. Code -- Create Database