偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

MySQL數(shù)據(jù)庫(kù)中delimiter的作用概述

數(shù)據(jù)庫(kù) MySQL
今天我們主要向大家描述的是MySQL數(shù)據(jù)庫(kù)中delimiter的作用,以及對(duì)相關(guān)實(shí)際應(yīng)用語(yǔ)句的描述,以下就是文章的主要內(nèi)容描述。

以下的文章主要是向大家描述的是MySQL數(shù)據(jù)庫(kù)中delimiter的作用是什么?我們一般都認(rèn)為這個(gè)命令和存儲(chǔ)過(guò)程關(guān)系不大,到底是不是這樣的呢?以下的文章將會(huì)給你相關(guān)的知識(shí),望你會(huì)有所收獲。

其實(shí)就是告訴MySQL解釋器,該段命令是否已經(jīng)結(jié)束了,MySQL數(shù)據(jù)庫(kù)是否可以執(zhí)行了。默認(rèn)情況下,delimiter是分號(hào);。在命令行客戶端中,如果有一行命令以分號(hào)結(jié)束,那么回車后,MySQL將會(huì)執(zhí)行該命令。如輸入下面的語(yǔ)句

  1. MySQL> select * from test_table; 

然后回車,那么MySQL將立即執(zhí)行該語(yǔ)句。

但有時(shí)候,不希望MySQL這么做。在為可能輸入較多的語(yǔ)句,且語(yǔ)句中包含有分號(hào)。如試圖在命令行客戶端中輸入如下語(yǔ)句

  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; 

默認(rèn)情況下,不可能等到用戶把這些語(yǔ)句全部輸入完之后,再執(zhí)行整段語(yǔ)句。因?yàn)镸ySQL一遇到分號(hào),它就要自動(dòng)執(zhí)行。即,在語(yǔ)句RETURN '';時(shí),MySQL數(shù)據(jù)庫(kù)解釋器就要執(zhí)行了。這種情況下,就需要事先把delimiter換成其它符號(hào),如//或$$。

  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;//  

這樣只有當(dāng)//出現(xiàn)之后,MySQL解釋器才會(huì)執(zhí)行這段語(yǔ)句

 

例子:

 

  1. MySQL> delimiter //   
  2. MySQL> CREATE PROCEDURE simpleproc (OUT param1 INT)   
  3. -> BEGIN   
  4. -> SELECT COUNT(*) INTO param1 FROM t;   
  5. -> END;   
  6. -> //   
  7. Query OK, 0 rows affected (0.00 sec)   
  8. MySQL> delimiter ;   
  9. MySQL> CALL simpleproc(@a);   
  10. Query OK, 0 rows affected (0.00 sec)   
  11. MySQL> SELECT @a;   
  12. +------+   
  13. | @a |   
  14. +------+   
  15. | 3 |   
  16. +------+   
  17. 1 row in set (0.00 sec)   

 

本文代碼在 MySQL 5.0.41-community-nt 下運(yùn)行通過(guò)。

編寫了個(gè)統(tǒng)計(jì)網(wǎng)站訪問(wèn)情況(user agent)的 MySQL 數(shù)據(jù)庫(kù)存儲(chǔ)過(guò)程。就是下面的這段 SQL 代碼。

 

  1. drop procedure if exists pr_stat_agent;  
  2. -- call pr_stat_agent ('2008-07-17', '2008-07-18')  
  3. create procedure pr_stat_agent  
  4. (  
  5. pi_date_from date  
  6. ,pi_date_to date  
  7. )  
  8. begin  
  9. -- check input  
  10. if (pi_date_from is null) then  
  11. set pi_date_from = current_date();  
  12. end if;  
  13. if (pi_date_to is null) then  
  14. set pi_date_to = pi_date_from;  
  15. end if;  
  16. set pi_date_to = date_add(pi_date_from, interval 1 day);  
  17. -- stat  
  18. select agent, count(*) as cnt  
  19. from apache_log  
  20. where request_time >= pi_date_from  
  21. and request_time < pi_date_to 
  22. group by agent  
  23. order by cnt desc;  
  24. end;  

 

我在 EMS SQL Manager 2005 for MySQL 這個(gè) MySQL 圖形客戶端下可以順利運(yùn)行。但是在 SQLyog MySQL GUI v5.02 這個(gè)客戶端就會(huì)出錯(cuò)。***找到原因是沒(méi)有設(shè)置好 delimiter 的問(wèn)題。#p#

默認(rèn)情況下,delimiter “;” 用于向 MySQL 提交查詢語(yǔ)句。在存儲(chǔ)過(guò)程中每個(gè) SQL 語(yǔ)句的結(jié)尾都有個(gè) “;”,如果這時(shí)候,每逢 “;” 就向 MySQL 提交的話,當(dāng)然會(huì)出問(wèn)題了。于是更改 MySQL 的 delimiter,上面 MySQL 存儲(chǔ)過(guò)程就編程這樣子了:

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

 

  1. drop procedure if exists pr_stat_agent //  
  2. -- call pr_stat_agent ('2008-07-17', '2008-07-18')  
  3. create procedure pr_stat_agent  
  4. (  
  5. pi_date_from date  
  6. ,pi_date_to date  
  7. )  
  8. begin  
  9. -- check input  
  10. if (pi_date_from is null) then  
  11. set pi_date_from = current_date();  
  12. end if;  
  13. if (pi_date_to is null) then  
  14. set pi_date_to = pi_date_from;  
  15. end if;  
  16. set pi_date_to = date_add(pi_date_from, interval 1 day);  
  17. -- stat  
  18. select agent, count(*) as cnt  
  19. from apache_log  
  20. where request_time >= pi_date_from  
  21. and request_time < pi_date_to 
  22. group by agent  
  23. order by cnt desc;  
  24. end; //  
  25. delimiter ;  

改回默認(rèn)的 MySQL delimiter:“;”

當(dāng)然,MySQL delimiter 符號(hào)是可以自由設(shè)定的,你可以用 “/” 或者“$$” 等。但是 MySQL數(shù)據(jù)庫(kù) 存儲(chǔ)過(guò)程中比較常見的用法是 “//” 和 “$$”。上面的這段在 SQLyog 中的代碼搬到 MySQL 命令客戶端(MySQL Command Line Client)卻不能執(zhí)行。

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

  1. MySQL> 
  2. MySQL> drop procedure if exists pr_stat_agent //  
  3. -> 
  4. -> -- call pr_stat_agent ('2008-07-17', '2008-07-18')  
  5. -> 
  6. -> create procedure pr_stat_agent  
  7. -> (  
  8. -> pi_date_from date  
  9. -> ,pi_date_to date  
  10. -> )  
  11. -> begin  
  12. -> -- check input  
  13. -> if (pi_date_from is null) then  
  14. -> set pi_date_from = current_date();  
  15. -> end if;  
  16. -> 
  17. -> if (pi_date_to is null) then  
  18. -> set pi_date_to = pi_date_from;  
  19. -> end if;  
  20. -> 
  21. -> set pi_date_to = date_add(pi_date_from, interval 1 day);  
  22. -> 
  23. -> -- stat  
  24. -> select agent, count(*) as cnt  
  25. -> from apache_log  
  26. -> where request_time >= pi_date_from  
  27. -> and request_time < pi_date_to 
  28. -> group by agent  
  29. -> order by cnt desc;  
  30. -> end; //  
  31. -> 
  32. -> delimiter ; 

 改回默認(rèn)的 MySQL delimiter:“;”

  1. -> //  
  2. -> //  
  3. -> //  
  4. -> ;  
  5. -> ;  
  6. -> 

真是奇怪了!***終于發(fā)現(xiàn)問(wèn)題了,在 MySQL 命令行下運(yùn)行 “delimiter //; ” 則 MySQL 的 delimiter 實(shí)際上是 “//;”,而不是我們所預(yù)想的 “//”。其實(shí)只要運(yùn)行指令 “delimiter //” 就 OK 了。

MySQL> delimiter // -- 末尾不要符號(hào) “;”

  1. MySQL> 
  2. MySQL> drop procedure if exists pr_stat_agent //  
  3. Query OK, 0 rows affected (0.00 sec)  
  4. MySQL> 
  5. MySQL> -- call pr_stat_agent ('2008-07-17', '2008-07-18')  
  6. MySQL> 
  7. MySQL> create procedure pr_stat_agent  
  8. -> (  
  9. -> pi_date_from date  
  10. -> ,pi_date_to date  
  11. -> )  
  12. -> begin  
  13. -> -- check input  
  14. -> if (pi_date_from is null) then  
  15. -> set pi_date_from = current_date();  
  16. -> end if;  
  17. -> 
  18. -> if (pi_date_to is null) then  
  19. -> set pi_date_to = pi_date_from;  
  20. -> end if;  
  21. -> 
  22. -> set pi_date_to = date_add(pi_date_from, interval 1 day);  
  23. -> 
  24. -> -- stat  
  25. -> select agent, count(*) as cnt  
  26. -> from apache_log  
  27. -> where request_time >= pi_date_from  
  28. -> and request_time < pi_date_to 
  29. -> group by agent  
  30. -> order by cnt desc;  
  31. -> end; //  
  32. Query OK, 0 rows affected (0.00 sec)  
  33. MySQL> 
  34. MySQL> delimiter ;   

末尾不要符號(hào) “//”

  1. MySQL> 

順帶一提的是,我們可以在 MySQL 數(shù)據(jù)庫(kù)中執(zhí)行在文件中的 SQL 代碼。例如,我把上面存儲(chǔ)過(guò)程的代碼放在文件 d:\pr_stat_agent.sql 中??梢赃\(yùn)行下面的代碼建立存儲(chǔ)過(guò)程。

 

  1. MySQL> source d:\pr_stat_agent.sql  
  2. Query OK, 0 rows affected (0.00 sec)  
  3. Query OK, 0 rows affected (0.00 sec)  

 

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

 

  1. MySQL> \. d:\pr_stat_agent.sql  
  2. Query OK, 0 rows affected (0.00 sec)  
  3. Query OK, 0 rows affected (0.00 sec)  

 

***,可見 MySQL數(shù)據(jù)庫(kù)的客戶端工具在有些地方是各自為政,各有各的一套。

【編輯推薦】

  1. MySQL數(shù)據(jù)庫(kù)和相關(guān)事務(wù)總結(jié)
  2. MySQL數(shù)據(jù)庫(kù)NULL值的處理
  3. MySQL臨時(shí)表的實(shí)際應(yīng)用功能簡(jiǎn)介
  4. MySQL游標(biāo)的使用筆記大全
  5. 對(duì)MySQL數(shù)據(jù)庫(kù)小技巧概述
責(zé)任編輯:佚名 來(lái)源: cnblogs
相關(guān)推薦

2010-10-09 09:04:53

MySql中delim

2009-09-04 09:54:59

數(shù)據(jù)庫(kù)名

2010-10-09 10:29:29

MySQL外鍵

2010-05-31 17:15:39

MySQL數(shù)據(jù)庫(kù)

2011-03-04 13:47:12

專利數(shù)據(jù)庫(kù)

2009-09-18 13:58:00

LINQ查詢數(shù)據(jù)庫(kù)

2011-03-07 13:30:53

Oracle數(shù)據(jù)庫(kù)

2011-05-24 14:27:42

2010-06-01 10:17:01

重啟MySQL數(shù)據(jù)庫(kù)

2011-05-20 10:30:20

ORACLE數(shù)據(jù)庫(kù)性能優(yōu)化

2011-03-28 13:25:27

MySQL數(shù)據(jù)庫(kù)哈希加密

2010-05-20 14:25:25

2011-08-01 23:08:33

MySQL存儲(chǔ)引擎

2018-01-24 20:42:06

數(shù)據(jù)庫(kù)NoSQL驅(qū)動(dòng)力

2009-06-30 13:53:08

前瞻性Oracle數(shù)據(jù)庫(kù)

2010-06-01 17:21:50

MySQL delim

2009-07-31 17:01:21

C#存取Access數(shù)

2009-08-17 17:42:57

C#數(shù)據(jù)庫(kù)操作類

2010-07-14 17:49:39

SQL Server數(shù)

2009-08-25 12:50:32

數(shù)據(jù)庫(kù)常用C#代碼
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)