0
0
MySQLquery~10 mins

Creating stored procedures in MySQL - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - Creating stored procedures
Start
Define Procedure Name
Specify Parameters (IN/OUT)
Write SQL Statements
End Procedure Definition
Call Procedure to Execute
Finish
This flow shows how to create a stored procedure by defining its name, parameters, SQL code, ending the definition, and then calling it to run.
Execution Sample
MySQL
DELIMITER $$
CREATE PROCEDURE GetAllUsers()
BEGIN
  SELECT * FROM users;
END$$
DELIMITER ;
This code creates a stored procedure named GetAllUsers that selects all rows from the users table.
Execution Table
StepActionEvaluationResult
1Set DELIMITER to $$DELIMITER changedAllows procedure body to contain ; without ending statement
2CREATE PROCEDURE GetAllUsers()Procedure signature acceptedProcedure named GetAllUsers created
3BEGINStart of procedure bodyReady to write SQL statements
4SELECT * FROM users;Query inside procedureSelects all rows from users table
5END$$End of procedure bodyProcedure definition completed
6DELIMITER ;Reset DELIMITER to ;Normal statement delimiter restored
7CALL GetAllUsers();Execute procedureReturns all rows from users table
8Procedure execution endsNo more stepsFinished
💡 Procedure created and executed successfully; no more steps.
Variable Tracker
VariableStartAfter Step 2After Step 5After Step 7Final
DELIMITER;$$$$;;
Procedure GetAllUsersNot definedDefinedDefinedDefinedDefined
Execution ResultNoneNoneNoneAll rows from users tableAll rows from users table
Key Moments - 3 Insights
Why do we change the DELIMITER before creating the procedure?
We change DELIMITER to $$ (see Step 1) so that the semicolons inside the procedure body don't end the CREATE PROCEDURE statement prematurely.
What happens if we forget to reset DELIMITER back to ; after defining the procedure?
If DELIMITER is not reset (Step 6), normal SQL statements won't end correctly, causing errors when running other commands.
How do we run the stored procedure after creating it?
We use the CALL statement (Step 7) with the procedure name and parentheses to execute it and get the results.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the DELIMITER value after Step 5?
A/
B;
C$$
D:
💡 Hint
Check the 'DELIMITER' row in variable_tracker after Step 5.
At which step is the stored procedure actually executed?
AStep 3
BStep 7
CStep 5
DStep 2
💡 Hint
Look for the CALL statement in the execution_table.
If we omit the BEGIN and END keywords, what will happen?
ASyntax error during creation
BProcedure will still work fine
CProcedure will execute but return no results
DProcedure will execute twice
💡 Hint
Refer to Step 3 and Step 5 where BEGIN and END mark the procedure body.
Concept Snapshot
CREATE PROCEDURE proc_name([parameters])
BEGIN
  SQL statements;
END;
Use DELIMITER to change statement end marker when defining.
Call procedure with CALL proc_name();
Stored procedures group SQL for reuse.
Full Transcript
Creating stored procedures in MySQL involves changing the DELIMITER to avoid conflicts with semicolons inside the procedure, defining the procedure with a name and optional parameters, writing SQL statements inside BEGIN and END blocks, then resetting the DELIMITER. After creation, the procedure is executed using the CALL statement. This process helps organize and reuse SQL code efficiently.