Delimiters in MySQL
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In MySQL, the semicolon normally marks the end of a SQL statement. But when writing stored procedures, triggers, or functions, you often need semicolons inside the routine body itself. That is why MySQL client tools provide the DELIMITER command: not because the server changed SQL syntax, but because the client needs a different way to know where one full statement ends.
The Default Delimiter
Normally, the MySQL client sends a statement to the server when it reaches ;.
Each semicolon ends one statement.
That works fine until you define a stored routine containing internal statements that also end with semicolons.
Why DELIMITER Is Needed
Consider a procedure:
If you run that directly in the MySQL client with the default delimiter, the client sees the first internal semicolon after SELECT 1; and thinks the statement is finished too early.
The DELIMITER command solves that client-side parsing problem.
Typical Procedure Example
Here is what happens:
- '
DELIMITER $$tells the client to wait for$$instead of;' - semicolons inside the procedure body are now treated as part of the routine definition
- '
DELIMITER ;restores the normal statement terminator afterward'
That is the most common MySQL delimiter pattern.
Important Clarification: DELIMITER Is a Client Command
This is the key concept many beginners miss. DELIMITER is usually handled by the MySQL client or compatible tools, not by the MySQL server as part of SQL itself.
That means:
- it is mainly for interactive clients and SQL script execution tools
- it changes how the client splits statements before sending them
- it is not a general SQL keyword in the same sense as
SELECTorCREATE
This explains why some APIs or database libraries do not understand DELIMITER at all. They may expect you to send one full routine definition as a single string instead.
Functions and Triggers Use the Same Idea
Function example:
Trigger example:
The pattern is the same because the routine body contains internal semicolons.
Choosing a Delimiter
The alternate delimiter can be almost any token that does not conflict with your statement text. Common choices are:
- '
$$' - '
//' - '
;;'
The exact token is less important than consistency and readability.
When You May Not Need DELIMITER
If your application sends the entire routine definition as one statement string through a driver, the driver may not require the DELIMITER command at all. In that environment, DELIMITER can even fail because the driver is not a MySQL shell parser.
So if a migration tool or ORM rejects DELIMITER, check whether it expects raw SQL statements directly rather than MySQL console syntax.
Common Pitfalls
- Thinking
DELIMITERchanges server-side SQL grammar instead of client-side statement splitting. - Forgetting to reset the delimiter back to
;after defining a routine. - Sending
DELIMITERthrough an API or migration framework that does not support client-side shell commands. - Choosing a custom delimiter that accidentally appears inside the routine body.
- Debugging stored procedure syntax while the real problem is only incorrect statement termination.
Summary
- The default MySQL delimiter is
;. - '
DELIMITERis mainly a client-side command used when defining routines that contain internal semicolons.' - Procedures, functions, and triggers often require a temporary alternate delimiter in shell-based workflows.
- Reset the delimiter afterward so normal statements behave as expected.
- Many problems around MySQL delimiters come from confusing client parsing rules with server SQL syntax.

