Practical Exercises to Master MySQL Stored Procedures and Triggers
Stored Procedures and Triggers Exercises with solutions [20 exercises with solution]
1. Create a Stored Procedure to Insert a New Employee
Write a MySQL query to create a stored procedure that inserts a new employee into the Employees table.
2. Call the Stored Procedure to Insert an Employee
Write a MySQL query to call the InsertEmployee stored procedure to add a new employee.
3. Create a Stored Procedure to Update Employee Salary
Write a MySQL query to create a stored procedure that updates an employee's salary.
4. Call the Stored Procedure to Update Salary
Write a MySQL query to call the UpdateEmployeeSalary stored procedure to update an employee's salary.
5. Create a Stored Procedure to Delete an Employee
Write a MySQL query to create a stored procedure that deletes an employee from the Employees table.
6. Call the Stored Procedure to Delete an Employee
Write a MySQL query to call the DeleteEmployee stored procedure to delete an employee.
7. Create a Trigger to Log Salary Changes
Write a MySQL query to create a trigger that logs salary changes in the Employees table to a SalaryLog table.
8. Create a Trigger to Prevent Invalid Salary Updates
Write a MySQL query to create a trigger that prevents updating an employee's salary to a negative value.
9. Create a Stored Procedure to Calculate Average Salary
Write a MySQL query to create a stored procedure that calculates the average salary of employees in a department.
10. Call the Stored Procedure to Calculate Average Salary
Write a MySQL query to call the CalculateAverageSalary stored procedure and retrieve the average salary.
11. Create a Trigger to Update Last Modified Date
Write a MySQL query to create a trigger that updates the LastModified column in the Employees table whenever a row is updated.
12. Create a Stored Procedure to Count Employees in a Department
Write a MySQL query to create a stored procedure that counts the number of employees in a specific department.
13. Call the Stored Procedure to Count Employees
Write a MySQL query to call the CountEmployeesInDepartment stored procedure and retrieve the employee count.
14. Create a Trigger to Prevent Deleting Active Employees
Write a MySQL query to create a trigger that prevents deleting employees who are marked as active.
15. Stored Procedure to Calculate Total Salary by Department
Write a MySQL query to create a stored procedure that calculates the total salary for a specific department.
16. Call the Stored Procedure to Calculate Total Salary
Write a MySQL query to call the CalculateTotalSalaryByDepartment stored procedure and retrieve the total salary.
17. Create a Trigger to Enforce Maximum Salary
Write a MySQL query to create a trigger that prevents inserting or updating an employee's salary if it exceeds a maximum value.
18. Create a Stored Procedure to Archive Old Employees
Write a MySQL query to create a stored procedure that moves old employees to an ArchivedEmployees table.
19. Call the Stored Procedure to Archive Old Employees
Write a MySQL query to call the ArchiveOldEmployees stored procedure.
20. Create a Trigger to Log Employee Deletions
Write a MySQL query to create a trigger that logs deleted employees to a DeletedEmployeesLog table.
More to Come !
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics