MySql delimiter的作用是什麼_Mysql

來源:互聯網
上載者:User
MYSQL匯出一個SQL後:
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 ;

其中DELIMITER 定好結束符為"$$", 然後最後又定義為";", MYSQL的預設結束符為";".



詳細解釋:


其實就是告訴mysql解譯器,該段命令是否已經結束了,mysql是否可以執行了。
預設情況下,delimiter是分號;。在命令列用戶端中,如果有一行命令以分號結束,
那麼斷行符號後,mysql將會執行該命令。如輸入下面的語句
mysql> select * from test_table;
然後斷行符號,那麼MySQL將立即執行該語句。

但有時候,不希望MySQL這麼做。在為可能輸入較多的語句,且語句中包含有分號。
如試圖在命令列用戶端中輸入如下語句
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;
預設情況下,不可能等到使用者把這些語句全部輸入完之後,再執行整段語句。
因為mysql一遇到分號,它就要自動執行。
即,在語句RETURN '';時,mysql解譯器就要執行了。
這種情況下,就需要事先把delimiter換成其它符號,如//或$$。
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;//
這樣只有當//出現之後,mysql解譯器才會執行這段語句


例子:

mysql> delimiter //

mysql> CREATE PROCEDURE simpleproc (OUT param1 INT)
-> BEGIN
-> SELECT COUNT(*) INTO param1 FROM t;
-> END;
-> //
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ;

mysql> CALL simpleproc(@a);
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT @a;
+------+
| @a |
+------+
| 3 |
+------+
1 row in set (0.00 sec)






本文代碼在 MySQL 5.0.41-community-nt 下運行通過。

編寫了個統計網站訪問情況(user agent)的 MySQL 預存程序。就是下面的這段 SQL 代碼。

drop procedure if exists pr_stat_agent;

-- call pr_stat_agent ('2008-07-17', '2008-07-18')

create procedure pr_stat_agent
(
   pi_date_from  date
  ,pi_date_to    date
)
begin
   -- check input
   if (pi_date_from is null) then
      set pi_date_from = current_date();
   end if;

   if (pi_date_to is null) then
      set pi_date_to = pi_date_from;
   end if;

   set pi_date_to = date_add(pi_date_from, interval 1 day);

   -- stat
   select agent, count(*) as cnt
     from apache_log
    where request_time >= pi_date_from
      and request_time <  pi_date_to
    group by agent
    order by cnt desc;
end;

我在 EMS SQL Manager 2005 for MySQL 這個 MySQL 圖形用戶端下可以順利運行。但是在 SQLyog MySQL GUI v5.02 這個用戶端就會出錯。最後找到原因是沒有設定好 delimiter 的問題。預設情況下,delimiter “;” 用於向 MySQL 提交查詢語句。在預存程序中每個 SQL 陳述式的結尾都有個 “;”,如果這時候,每逢 “;” 就向 MySQL 提交的話,當然會出問題了。於是更改 MySQL 的 delimiter,上面 MySQL 預存程序就編程這樣子了:

delimiter //;     -- 改變 MySQL delimiter 為:“//”

drop procedure if exists pr_stat_agent //

-- call pr_stat_agent ('2008-07-17', '2008-07-18')

create procedure pr_stat_agent
(
   pi_date_from  date
  ,pi_date_to    date
)
begin
   -- check input
   if (pi_date_from is null) then
      set pi_date_from = current_date();
   end if;

   if (pi_date_to is null) then
      set pi_date_to = pi_date_from;
   end if;

   set pi_date_to = date_add(pi_date_from, interval 1 day);

   -- stat
   select agent, count(*) as cnt
     from apache_log
    where request_time >= pi_date_from
      and request_time <  pi_date_to
    group by agent
    order by cnt desc;
end; //

delimiter ; //   -- 改回預設的 MySQL delimiter:“;”

當然,MySQL delimiter 符號是可以自由設定的,你可以用 “/” 或者“$$” 等。但是 MySQL 預存程序中比較常見的用法是 “//” 和 “$$”。上面的這段在 SQLyog 中的代碼搬到 MySQL 命令用戶端(MySQL Command Line Client)卻不能執行。

mysql> delimiter //;     -- 改變 MySQL delimiter 為:“//”
mysql>
mysql> drop procedure if exists pr_stat_agent //
    ->
    -> -- call pr_stat_agent ('2008-07-17', '2008-07-18')
    ->
    -> create procedure pr_stat_agent
    -> (
    ->    pi_date_from  date
    ->   ,pi_date_to    date
    -> )
    -> begin
    ->    -- check input
    ->    if (pi_date_from is null) then
    ->       set pi_date_from = current_date();
    ->    end if;
    ->
    ->    if (pi_date_to is null) then
    ->       set pi_date_to = pi_date_from;
    ->    end if;
    ->
    ->    set pi_date_to = date_add(pi_date_from, interval 1 day);
    ->
    ->    -- stat
    ->    select agent, count(*) as cnt
    ->      from apache_log
    ->     where request_time >= pi_date_from
    ->       and request_time <  pi_date_to
    ->     group by agent
    ->     order by cnt desc;
    -> end; //
    ->
    -> delimiter ; //   -- 改回預設的 MySQL delimiter:“;”
    -> //
    -> //
    -> //
    -> ;
    -> ;
    ->

真是奇怪了!最後終於發現問題了,在 MySQL 命令列下運行 “delimiter //; ” 則 MySQL 的 delimiter 實際上是 “//;”,而不是我們所預想的 “//”。其實只要運行指令 “delimiter //” 就 OK 了。

mysql> delimiter //     -- 末尾不要符號 “;”
mysql>
mysql> drop procedure if exists pr_stat_agent //
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> -- call pr_stat_agent ('2008-07-17', '2008-07-18')
mysql>
mysql> create procedure pr_stat_agent
    -> (
    ->    pi_date_from  date
    ->   ,pi_date_to    date
    -> )
    -> begin
    ->    -- check input
    ->    if (pi_date_from is null) then
    ->       set pi_date_from = current_date();
    ->    end if;
    ->
    ->    if (pi_date_to is null) then
    ->       set pi_date_to = pi_date_from;
    ->    end if;
    ->
    ->    set pi_date_to = date_add(pi_date_from, interval 1 day);
    ->
    ->    -- stat
    ->    select agent, count(*) as cnt
    ->      from apache_log
    ->     where request_time >= pi_date_from
    ->       and request_time <  pi_date_to
    ->     group by agent
    ->     order by cnt desc;
    -> end; //
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> delimiter ;  -- 末尾不要符號 “//”
mysql>

順帶一提的是,我們可以在 MySQL 資料庫中執行在檔案中的 SQL 代碼。例如,我把上面預存程序的代碼放在檔案 d:\pr_stat_agent.sql 中。可以運行下面的代碼建立預存程序。

mysql> source d:\pr_stat_agent.sql
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

source 指令的縮寫形式是:“\.”

mysql> \. d:\pr_stat_agent.sql
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

最後,可見 MySQL 的用戶端工具在有些地方是各自為政,各有各的一套。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.