DBMS_Lab_Manual_mysql_1-6
DBMS_Lab_Manual_mysql_1-6
Program 1
2. Insert any three records in the employee table contains attributes EMPNO, ENAME, JOB,
MANAGER_NO, SAL, COMMISSION and use rollback. Check the result.
3. Add primary key constraint and not null constraint to the employee table.
4. Insert null values to the employee table and verify the result.
Solution
Database changed
Within the Database COMPANY create a table Employee as follows. Use the SHOW TABLES; command to
confirm that the table was indeed created.
EMPNO INT,
ENAME VARCHAR(255),
JOB VARCHAR(255),
MANAGER_NO INT,
COMMISSION DECIMAL(10, 2)
);
+-------------------+
| Tables_in_COMPANY |
+-------------------+
| Employee |
+-------------------+
You can verify the structure of this newly created Employee table using the DESC command.
+------------+---------------+------+-----+---------+-------+
+------------+---------------+------+-----+---------+-------+
+------------+---------------+------+-----+---------+-------+
+------------------+-----------+-----------------------+
+------------------+-----------+-----------------------+
| mahendra |% | caching_sha2_password |
| dbuser | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+
In order to find what privileges have already been granted to a MySQL user, you can use the SHOW
GRANTS command:
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
Database changed
START A TRANSACTION
mysql> START TRANSACTION;
mysql> INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)
mysql> COMMIT;
+-------+---------------+---------+------------+---------+------------+
+-------+---------------+---------+------------+---------+------------+
+-------+---------------+---------+------------+---------+------------+
mysql> INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)
mysql> INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)
+-------+---------------+-------------+------------+---------+------------+
+-------+---------------+-------------+------------+---------+------------+
+-------+-------------+-------------+------------+---------+------------+
+-------+-------------+-------------+------------+---------+------------+
+-------+-------------+-------------+------------+---------+------------+
mysql> ROLLBACK;
+-------+---------------+---------+------------+---------+------------+
+-------+---------------+---------+------------+---------+------------+
+-------+---------------+---------+------------+---------+------------+
1 row in set (0.00 sec)
You can now see how the rollback operation can be used above.
Adding Constraints
mysql> ALTER TABLE Employee ADD CONSTRAINT pk_employee PRIMARY KEY (EMPNO);
+------------+---------------+------+-----+---------+-------+
+------------+---------------+------+-----+---------+-------+
+------------+---------------+------+-----+---------+-------+
mysql> INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)
Since EMPNO field is the primary key it cannot have duplicate values, hence we see that the insert
operation fails when provided with a duplicate value.
Add Not Null Constraint
mysql> INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)
+-------+---------------+---------+------------+---------+------------+
+-------+---------------+---------+------------+---------+------------+
+-------+---------------+---------+------------+---------+------------+
We just illustrated as to how to add not null constraint to the Employee table. We see that the first insert
doesn’t violate null constraint, however the second insert does violate null constraint as ENAME field
cannot be null.
Program 2
Create a table called Employee that contain attributes EMPNO, ENAME, JOB, MGR, SAL &
execute the following.
Solution
Database changed
EMPNO INT,
ENAME VARCHAR(255),
JOB VARCHAR(255),
MGR INT,
SAL DECIMAL(10, 2)
);
+---------------------+
| Tables_in_COMPANY02 |
+---------------------+
| Employee |
+---------------------+
+-------+---------------+------+-----+---------+-------+
+-------+---------------+------+-----+---------+-------+
+-------+---------------+------+-----+---------+-------+
+------------+---------------+------+-----+---------+-------+
+------------+---------------+------+-----+---------+-------+
We have added a column COMMISSION using the ALTER command, which is shown above.
mysql> INSERT INTO Employee (EMPNO, ENAME, JOB, MGR, SAL, COMMISSION)
VALUES (101, 'Radha Bai', 'Manager', NULL, 5000.00, 1000.00), (102, 'Krishna Kumar', 'Developer',
101, 4000.00, NULL), (103, 'Abdul Sattar', 'Salesperson', 102, 3000.00, 500.00), (104, 'Bob Johnson',
'Accountant', 101, 4500.00, NULL), (105, 'Amartya Sen', 'HR Manager', 101, 4800.00, 800.00);
+-------+---------------+-------------+------+---------+------------+
+-------+---------------+-------------+------+---------+------------+
+-------+---------------+-------------+------+---------+------------+
mysql> UPDATE Employee SET JOB = 'Senior Developer' WHERE EMPNO = 102;
+-------+---------------+------------------+------+---------+------------+
+-------+---------------+------------------+------+---------+------------+
+------------+---------------+------+-----+---------+-------+
+------------+---------------+------+-----+---------+-------+
+------------+---------------+------+-----+---------+-------+
+-------+---------------+------------------+------------+---------+------------+
+-------+---------------+------------------+------------+---------+------------+
Queries using aggregate functions (COUNT, AVG, MIN, MAX, SUM), Group by, Order by.
1. Create Employee table containing all Records E_id, E_name, Age, Salary.
Solution
Database changed
E_name VARCHAR(255),
Age INT,
Salary DECIMAL(10, 2)
);
+--------+---------------+------+-----+---------+-------+
+--------+---------------+------+-----+---------+-------+
+--------+---------------+------+-----+---------+-------+
mysql> INSERT INTO Employee (E_id, E_name, Age, Salary) VALUES (1, 'Samarth', 30, 50000.00),
(2, 'Ramesh Kumar', 25, 45000.00), (3, 'Seema Banu', 35, 60000.00), (4, 'Dennis Anil', 28, 52000.00),
(5, 'Rehman Khan', 32, 58000.00), (6, 'Pavan Gowda', 40, 70000.00), (7, 'Shruthi Bhat', 27, 48000.00),
(8, 'Sandesh Yadav', 29, 51000.00), (9, 'Vikram Acharya', 33, 62000.00), (10, 'Praveen Bellad', 26,
46000.00), (11, 'Sophia Mary', 31, 55000.00), (12, 'Darshan Desai', 34, 63000.00);
+------+----------------+------+----------+
+------+----------------+------+----------+
| 1 | Samarth | 30 | 50000.00 |
+------+----------------+------+----------+
+----------------+
| TotalEmployees |
+----------------+
| 12 |
+----------------+
+--------+
| MaxAge |
+--------+
| 40 |
+--------+
+--------+
| MinAge |
+--------+
| 25 |
+--------+
+----------------+----------+
| E_name | Salary |
+----------------+----------+
| Samarth | 50000.00 |
+----------------+----------+
+----------+---------------+
| Salary | EmployeeCount |
+----------+---------------+
| 50000.00 | 1|
| 45000.00 | 1|
| 62000.00 | 2|
| 52000.00 | 2|
| 58000.00 | 1|
| 70000.00 | 1|
| 48000.00 | 1|
| 46000.00 | 1|
| 55000.00 | 1|
| 63000.00 | 1|
+----------+---------------+
In these queries:
• ORDER BY Salary ASC sorts the employees based on their salaries in ascending order.
• GROUP BY Salary groups employees by their salaries and counts the number of employees for
each salary.
Program 4
Create a row level trigger for the customers table that would fire for INSERT or UPDATE or DELETE
operations performed on the CUSTOMERS table. This trigger will display the salary difference between the
old & new Salary.
Solution
Database changed
NAME VARCHAR(255),
AGE INT,
ADDRESS VARCHAR(255),
SALARY DECIMAL(10, 2)
);
To achieve the desired functionality of capturing changes on INSERT, UPDATE, or DELETE operations and
displaying the salary difference in MySQL, you’ll need to create separate row-level triggers for each
operation (INSERT, UPDATE, DELETE). These triggers will capture the OLD and NEW values of
the SALARY column and display the salary difference when an INSERT, UPDATE, or DELETE operation
occurs. Here’s how you can do it:
mysql>DELIMITER //
mysql>CREATE TRIGGER after_insert_salary_difference
BEGIN
END;//
mysql>DELIMITER ;
mysql>DELIMITER //
BEGIN
END;//
mysql>DELIMITER ;
mysql>DELIMITER //
mysql>CREATE TRIGGER after_delete_salary_difference
BEGIN
END;//
mysql>DELIMITER ;
Once the triggers are created, you can perform INSERT, UPDATE, or DELETE operations on
the CUSTOMERS table to observe the salary difference messages generated by the triggers.
For example:
+-----------------------------+
| SAL_DIFF |
+-----------------------------+
+-----------------------------+
+-------------------------------------------+
| SAL_DIFF |
+-------------------------------------------+
+-------------------------------------------+
+----------------------------+
| SAL_DIFF |
+----------------------------+
+----------------------------+
Each operation (INSERT, UPDATE, DELETE) will trigger the respective trigger
(after_insert_salary_difference, after_update_salary_difference, after_delete_salary_difference),
which will display the salary change or difference associated with that operation.
By using separate triggers for each operation and utilizing the OLD and NEW keywords appropriately
within the trigger bodies, you can effectively capture and handle changes to the SALARY column in
the CUSTOMERS table in MySQL. You can adjust the trigger logic and message formatting as needed based
on your specific requirements.
Program 5
Create cursor for Employee table & extract the values from the table. Declare the variables, Open the
cursor & extract the values from the cursor. Close the cursor.
Solution
mysql>USE COMPANY05;
E_id INT,
E_name VARCHAR(255),
Age INT,
Salary DECIMAL(10, 2)
);
VALUES
To create a cursor for the Employee table, extract values using the cursor, and then close the cursor in
MySQL, you’ll need to use stored procedures that support cursor operations.
mysql>DELIMITER //
mysql>CREATE PROCEDURE fetch_employee_data()
BEGIN
FROM Employee;
SET @finished = 1;
OPEN emp_cursor;
SET @finished = 0;
cursor_loop: LOOP
LEAVE cursor_loop;
END IF;
SELECT CONCAT('Employee ID: ', emp_id, ', Name: ', emp_name, ', Age: ', emp_age, ', Salary: ',
emp_salary) AS Employee_Info;
END LOOP;
CLOSE emp_cursor;
END//
mysql>DELIMITER ;
• We declare variables (emp_id, emp_name, emp_age, emp_salary) to store values retrieved from
the cursor.
• A cursor (emp_cursor) is declared to select E_id, E_name, Age, and Salary from
the Employee table.
• We declare a continue handler (CONTINUE HANDLER) for NOT FOUND condition to handle the
end of cursor data.
• The cursor is opened (OPEN emp_cursor), and a loop (cursor_loop) is used to fetch each row from
the cursor.
• We fetch values into the variables and process them within the loop (for demonstration, we print
the values using a SELECT statement).
• The loop continues until all rows are fetched (@finished = 1).
Once the stored procedure fetch_employee_data is created, you can execute it to fetch and process data
from the Employee table:
| Employee_Info |
+----------------------------------------------------------+
+----------------------------------------------------------+
+---------------------------------------------------------------+
| Employee_Info |
+---------------------------------------------------------------+
+---------------------------------------------------------------+
+-------------------------------------------------------------+
| Employee_Info |
+-------------------------------------------------------------+
+-------------------------------------------------------------+
+--------------------------------------------------------------+
| Employee_Info |
+--------------------------------------------------------------+
+--------------------------------------------------------------+
+--------------------------------------------------------------+
| Employee_Info |
+--------------------------------------------------------------+
+--------------------------------------------------------------+
• A cursor (emp_cursor) is declared for the Employee table to select E_id, E_name, Age,
and Salary.
• The cursor is opened (OPEN emp_cursor), and the FETCH statement retrieves the first row from
the cursor into the declared variables.
• A WHILE loop processes each row fetched by the cursor (SQLSTATE() = '00000' checks for
successful fetching).
• Within the loop, you can perform operations or output the values of each row.
• The CLOSE statement closes the cursor after processing all rows.
This example demonstrates how to create and use a cursor in MySQL to extract values from
the Employee table row by row. Adjust the cursor query and processing logic based on your table structure
and desired operations.
Program 6
Write a PL/SQL block of code using parameterized Cursor, that will merge the data available in the newly
created table N_RollCall with the data available in the table O_RollCall. If the data in the first table already
exist in the second table then that data should be skipped.
Solution
To accomplish this task in MySQL, we can use a stored procedure with a parameterized cursor to merge
data from one table (N_RollCall) into another table (O_RollCall) while skipping existing data. We’ll iterate
through the records of N_RollCall and insert them into O_RollCall only if they do not already exist.
First, let’s create the N_RollCall and O_RollCall tables with similar structure:
mysql>USE ROLLCALL;
student_name VARCHAR(255),
birth_date DATE
);
student_name VARCHAR(255),
birth_date DATE
);
VALUES
Let’s insert some sample data into the N_RollCall table, including records that are common
with O_RollCall:
VALUES
mysql>DELIMITER //
BEGIN
FROM N_RollCall;
OPEN n_cursor;
cursor_loop: LOOP
IF done THEN
LEAVE cursor_loop;
END IF;
IF NOT EXISTS (
SELECT 1
FROM O_RollCall
END IF;
END LOOP;
CLOSE n_cursor;
END//
mysql>DELIMITER ;
• The stored procedure merge_rollcall_data uses a cursor (n_cursor) to iterate through the records
of the N_RollCall table.
• Inside the cursor loop (cursor_loop), each record (n_id, n_name, n_date) from N_RollCall is
fetched and checked against the O_RollCall table.
• If the record does not already exist in O_RollCall (checked using NOT EXISTS), it is inserted
into O_RollCall.
• The cursor loop continues until all records from N_RollCall have been processed.
After executing the procedure, verify the records in the O_RollCall table to confirm that new records
from N_RollCall have been inserted, while existing common records have been skipped:
+------------+--------------+------------+
+------------+--------------+------------+
+------------+--------------+------------+