What is the role of delimiter in MySql? Many may have such doubts. The following describes the role of delimiter in MySql for your reference.
After MYSQL exports an SQL statement:
- DELIMITER $$
- DROP TRIGGER IF EXISTS `updateegopriceondelete`$$
- CREATE
- TRIGGER `updateegopriceondelete` AFTER DELETE ON `customerinfo`
- FOR EACH ROW BEGIN
- DELETE FROM egoprice WHERE customerId=OLD.customerId;
- END$$
- DELIMITER ;
Set the DELIMITER terminator to "$", and then define it as ";". The default terminator of MYSQL is ";".
Explanation:
In fact, it is to tell the mysql interpreter whether the command has ended and whether mysql can be executed.
By default, delimiter is a semicolon ;. In the command line client, if a command line ends with a semicolon,
After you press enter, mysql will execute this command. Enter the following statement.
Mysql> select * from test_table;
Press enter, and MySQL will immediately execute the statement.
But sometimes, you don't want MySQL to do this. The statement contains a semicolon.
If you try to enter the following statement in the command line client:
- mysql> CREATE FUNCTION `SHORTEN`(S VARCHAR(255), N INT)
- mysql> RETURNS varchar(255)
- mysql> BEGIN
- mysql> IF ISNULL(S) THEN
- mysql> RETURN ;
- mysql> ELSEIF N<15 THEN
- mysql> RETURN LEFT(S, N);
- mysql> ELSE
- mysql> IF CHAR_LENGTH(S) <=N THEN
- mysql> RETURN S;
- mysql> ELSE
- mysql> RETURN CONCAT(LEFT(S, N-10), ..., RIGHT(S, 5));
- mysql> END IF;
- mysql> END IF;
- mysql> END;
By default, it is impossible for the user to execute the entire statement after entering all these statements.
Mysql runs automatically when it encounters a semicolon.
That is, the mysql interpreter will execute the RETURN; statement.
In this case, you need to replace delimiter with another symbol, such as // or $.
- mysql> delimiter //
- mysql> CREATE FUNCTION `SHORTEN`(S VARCHAR(255), N INT)
- mysql> RETURNS varchar(255)
- mysql> BEGIN
- mysql> IF ISNULL(S) THEN
- mysql> RETURN ;
- mysql> ELSEIF N<15 THEN
- mysql> RETURN LEFT(S, N);
- mysql> ELSE
- mysql> IF CHAR_LENGTH(S) <=N THEN
- mysql> RETURN S;
- mysql> ELSE
- mysql> RETURN CONCAT(LEFT(S, N-10), ..., RIGHT(S, 5));
- mysql> END IF;
- mysql> END IF;
- mysql> END;//
In this way, the mysql interpreter will execute this statement only when // appears.
The preceding section describes the role of delimiter in MySql.