MySQL ALTER PROCEDURE
Master MySQL ALTER PROCEDURE — modifying characteristics (COMMENT, READS SQL DATA, SQL SECURITY DEFINER/INVOKER) of existing stored procedures. Covers SHOW CREATE PROCEDURE for inspection, the drop-and-recreate idiom for changing the body or parameters (since MySQL has no CREATE OR REPLACE PROCEDURE), and the MySQL Workbench GUI flow.
The ALTER PROCEDURE statement modifies an existing stored procedure — but only its characteristics, not its body or parameter list. You can change the comment, the SQL data access mode, the security context, but you cannot change what the procedure actually does or what arguments it takes through ALTER.
ALTER PROCEDURE does not let you modify the procedure's body or change its parameters. To do that, you need to drop the procedure and recreate it. There's no in-place "edit the SQL" form of ALTER for stored procedures in MySQL.Notes on the syntax:
- No parentheses after the procedure name. Just
sp_name, notsp_name(). - One or more characteristics follow the name. They're applied as a set.
- Characteristics not mentioned in the ALTER are left unchanged.
| Characteristic | What it controls |
|---|---|
COMMENT 'string' | Free-text description, visible via SHOW CREATE PROCEDURE |
LANGUAGE SQL | The implementation language. Currently MySQL only supports SQL — kept for ANSI/ISO compatibility |
CONTAINS SQL | Procedure contains SQL but doesn't read or write tables (default) |
NO SQL | Procedure contains no SQL statements |
READS SQL DATA | Procedure reads tables (SELECTs) but doesn't modify them |
MODIFIES SQL DATA | Procedure may run INSERT/UPDATE/DELETE |
SQL SECURITY DEFINER | Procedure runs with the privileges of the user who created it (default) |
SQL SECURITY INVOKER | Procedure runs with the privileges of the user who called it |
DETERMINISTIC / NOT DETERMINISTIC cannot be changed via ALTER PROCEDURE. Despite the docs implying otherwise, MySQL's parser rejects them in ALTER. Drop and recreate the procedure to change determinism.Start with a simple procedure that returns all employees:
DROP PROCEDURE IF EXISTS emp;
DELIMITER //
CREATE PROCEDURE emp()
BEGIN
SELECT * FROM employees;
END //
DELIMITER ;
SHOW CREATE PROCEDURE displays the full definition the way the server has stored it. Append \G instead of ; in the mysql CLI to format the output vertically — much more readable for wide rows:
SHOW CREATE PROCEDURE emp\G
Notice the procedure was stored with DEFINER=`root`@`localhost`. That's the SQL SECURITY DEFINER default — when anyone calls emp, MySQL runs it with root's privileges, regardless of who's calling.
Add a description to the procedure with ALTER PROCEDURE:
ALTER PROCEDURE emp COMMENT 'Get all employees';
SHOW CREATE PROCEDURE emp\G
The new comment now appears in the stored definition. The body itself is unchanged.
You can change several characteristics at once:
ALTER PROCEDURE emp
READS SQL DATA
SQL SECURITY INVOKER
COMMENT 'Get all employees — runs with caller privileges';
The procedure now declares it only reads data, runs with the caller's privileges, and has a useful comment — all in one statement.
To modify what a procedure does, you have to drop and recreate it. The standard pattern:
DROP PROCEDURE IF EXISTS emp;
DELIMITER $$
CREATE PROCEDURE emp(IN dept INT)
READS SQL DATA
COMMENT 'Get employees in a specific department'
BEGIN
SELECT employee_id, first_name, last_name, salary
FROM employees
WHERE department_id = dept
ORDER BY salary DESC;
END$$
DELIMITER ;
CALL emp(6); -- engineering department
CREATE OR REPLACE PROCEDURE — MySQL doesn't. PostgreSQL, Oracle, and SQL Server's recent versions let you replace a procedure atomically. MySQL 8.0 still requires the explicit drop-and-recreate dance. Keep the two statements next to each other in your script and you'll be fine.If you'd rather edit through a GUI, MySQL Workbench gives you a graphical procedure editor. The flow:
- In the schema browser, expand your database, then expand Stored Procedures.
- Right-click the procedure you want to modify, choose Alter Stored Procedure...
- Workbench opens a new tab with the procedure's source — edit anything you like, including the body and parameters.
- Click Apply. Workbench shows you the SQL Script Review window: it generates a
DROP PROCEDUREfollowed by aCREATE PROCEDUREstatement with your edits. - Click Apply again to execute the script. Click Finish when it's done.
- Back up before changing. Save the output of
SHOW CREATE PROCEDUREbefore dropping anything — easy to restore if needed. - Use ALTER for metadata, drop-and-recreate for code. Don't try to work around the body limitation; just embrace the recreate pattern.
- Keep procedure source in version control. The authoritative copy should live in your Git repo, not in the database. Database becomes a deployment target, not the source of truth.
- Wrap deployment in a transaction when possible — though note DDL like
CREATE PROCEDUREauto-commits, so this only helps for the surrounding non-DDL work. - Test in non-production first. The drop-and-recreate window briefly leaves the procedure missing; live applications will see errors during that gap. Schedule changes for low-traffic periods.
- Update the COMMENT when you change behavior so the next person reading
SHOW CREATE PROCEDUREisn't misled.
ALTER PROCEDURE name characteristic ...changes a procedure's characteristics — comment, SQL data access mode, security context — but not its body or parameters.- To change the body or parameters, drop and recreate the procedure. MySQL has no
CREATE OR REPLACE PROCEDURE. - Use
SHOW CREATE PROCEDURE name\Gto inspect a procedure's full definition. - The most useful characteristics to ALTER:
COMMENT,READS SQL DATA/MODIFIES SQL DATA,SQL SECURITY DEFINER/INVOKER. - MySQL Workbench's "Alter Stored Procedure" feature really runs DROP + CREATE under the hood; understand this if you're using it on a live system.