w3resource

Create a MySQL Stored Procedure for Employee Deletion


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.

Solution:

-- Create a stored procedure named `DeleteEmployee`
DELIMITER //
CREATE PROCEDURE DeleteEmployee(IN p_EmployeeID INT)
BEGIN
    -- Delete the employee from the Employees table
    DELETE FROM Employees 
    WHERE EmployeeID = p_EmployeeID;
END //
DELIMITER ;

Explanation:

  • Purpose of the Query:
    • The goal is to automate employee deletion using a stored procedure.
  • Key Components:
    • DELETE FROM: Removes the employee from the table.
    • WHERE: Specifies the employee to delete.
  • Why use Stored Procedures?:
    • Stored procedures ensure consistent and secure deletions.
  • Real-World Application:
    • For example, in a HR system, you might use a stored procedure to remove employees.

For more Practice: Solve these Related Problems:

  • Write a MySQL query to create a stored procedure that deletes a department from the Departments table.
  • Write a MySQL query to create a stored procedure that deletes a project from the Projects table.
  • Write a MySQL query to create a stored procedure that deletes a customer from the Customers table.
  • Write a MySQL query to create a stored procedure that deletes an order from the Orders table.


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous MySQL Exercise: Call the Stored Procedure to Update Salary.
Next MySQL Exercise: Call the Stored Procedure to Delete an Employee.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.