1/*
2
3Delimiters other than the default ; are typically used when defining functions, stored procedures, and triggers wherein you must define multiple statements. You define a different delimiter like $$ which is used to define the end of the entire procedure, but inside it, individual statements are each terminated by ;. That way, when the code is run in the mysql client, the client can tell where the entire procedure ends and execute it as a unit rather than executing the individual statements inside.
4
5Note that the DELIMITER keyword is a function of the command line mysql client (and some other clients) only and not a regular MySQL language feature. It won't work if you tried to pass it through a programming language API to MySQL. Some other clients like PHPMyAdmin have other methods to specify a non-default delimiter.
6
7*/
8
9/* Example:*/
10
11DELIMITER $$
12/* This is a complete statement, not part of the procedure, so use the custom delimiter $$ */
13DROP PROCEDURE my_procedure$$
14
15/* Now start the procedure code */
16CREATE PROCEDURE my_procedure ()
17BEGIN
18 /* Inside the procedure, individual statements terminate with ; */
19 CREATE TABLE tablea (
20 col1 INT,
21 col2 INT
22 );
23
24 INSERT INTO tablea
25 SELECT * FROM table1;
26
27 CREATE TABLE tableb (
28 col1 INT,
29 col2 INT
30 );
31 INSERT INTO tableb
32 SELECT * FROM table2;
33
34/* whole procedure ends with the custom delimiter */
35END$$
36
37/* Finally, reset the delimiter to the default ; */
38DELIMITER ;
39