Study the role of delimiter in MySql

Source: Internet
Author: User

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:

 

 
 
  1. DELIMITER $$     
  2. DROP TRIGGER IF EXISTS `updateegopriceondelete`$$     
  3. CREATE     
  4.     TRIGGER `updateegopriceondelete` AFTER  DELETE ON  `customerinfo`     
  5.     FOR EACH ROW BEGIN     
  6. DELETE FROM egoprice  WHERE customerId=OLD.customerId;     
  7.     END$$     
  8. 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:

 

 
 
  1. mysql> CREATE FUNCTION `SHORTEN`(S VARCHAR(255), N INT)     
  2. mysql>     RETURNS varchar(255)     
  3. mysql> BEGIN     
  4. mysql> IF ISNULL(S) THEN     
  5. mysql>     RETURN ;     
  6. mysql> ELSEIF N<15 THEN     
  7. mysql>     RETURN LEFT(S, N);     
  8. mysql> ELSE     
  9. mysql>     IF CHAR_LENGTH(S) <=N THEN     
  10. mysql>    RETURN S;     
  11. mysql>     ELSE     
  12. mysql>    RETURN CONCAT(LEFT(S, N-10), ..., RIGHT(S, 5));     
  13. mysql>     END IF;     
  14. mysql> END IF;     
  15. 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 $.

 

 
 
  1. mysql> delimiter //     
  2. mysql> CREATE FUNCTION `SHORTEN`(S VARCHAR(255), N INT)     
  3. mysql>     RETURNS varchar(255)     
  4. mysql> BEGIN     
  5. mysql> IF ISNULL(S) THEN     
  6. mysql>     RETURN ;     
  7. mysql> ELSEIF N<15 THEN     
  8. mysql>     RETURN LEFT(S, N);     
  9. mysql> ELSE     
  10. mysql>     IF CHAR_LENGTH(S) <=N THEN     
  11. mysql>    RETURN S;     
  12. mysql>     ELSE     
  13. mysql>    RETURN CONCAT(LEFT(S, N-10), ..., RIGHT(S, 5));     
  14. mysql>     END IF;     
  15. mysql> END IF;     
  16. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.