To study the role of delimiter in MySQL

Source: Internet
Author: User
Tags command line end sql mysql return client

What can delimiter in MySQL do? Many people may have such doubts, the following is to introduce you to the role of delimiter MySQL, for your reference.

After MySQL exports one sql:

 
  
  
  1. DELIMITER $$
  2. DROP TRIGGER IF EXISTS ' Updateegopriceondelete ' $$
  3. CREATE
  4. TRIGGER ' Updateegopriceondelete ' after the DELETE on ' CustomerInfo '
  5. For each ROW BEGIN
  6. DELETE from Egoprice WHERE Customerid=old.customerid;
  7. end$$
  8. DELIMITER;

Where delimiter a terminator is "$$" and then finally defined as ";", MySQL's default terminator is ";".

Detailed Explanation:

In fact, tell the MySQL interpreter, whether the command is over, whether MySQL can be executed.

By default, delimiter is a semicolon;. In a command-line client, if a line of commands ends with a semicolon,

Then, after the carriage return, MySQL will execute the command. If you enter the following statement

Mysql> select * from test_table;

And then enter, then MySQL will execute the statement immediately.

But sometimes, you don't want MySQL to do this. A statement containing a semicolon is included in the statement for which you may enter more.

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 to 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 (S, N-10), ' ... ', right (s, 5));
  13. Mysql> End IF;
  14. Mysql> End IF;
  15. Mysql> end;

By default, it is not possible to wait until the user has completed all of these statements and then execute the entire paragraph.

Because MySQL once encountered a semicolon, it will automatically execute.

That is, the MySQL interpreter will execute when the statement return ';

In this case, you need to replace delimiter with other symbols, 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 to 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 (S, N-10), ' ... ', right (s, 5));
  14. Mysql> End IF;
  15. Mysql> End IF;
  16. Mysql> end;//

This is the only time the MySQL interpreter executes this statement when//appears.

The above is the role of delimiter in MySQL introduced.




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.