search
HomeDatabaseMysql TutorialMySQL数据库中delimiter的作用概述_MySQL

以下的文章主要是向大家描述的是MySQL数据库中delimiter的作用是什么?我们一般都认为这个命令和存储过程关系不大,到底是不是这样的呢?以下的文章将会给你相关的知识,望你会有所收获。

其实就是告诉MySQL解释器,该段命令是否已经结束了,MySQL数据库是否可以执行了。默认情况下,delimiter是分号;。在命令行客户端中,如果有一行命令以分号结束,那么回车后,MySQL将会执行该命令。如输入下面的语句

<ol><li>MySQL> select * from test_table; </li></ol>

然后回车,那么MySQL将立即执行该语句。

但有时候,不希望MySQL这么做。在为可能输入较多的语句,且语句中包含有分号。如试图在命令行客户端中输入如下语句

<ol>
<li>MySQL> CREATE FUNCTION `SHORTEN`(S VARCHAR(255), N INT)  </li>
<li>MySQL> RETURNS varchar(255)  </li>
<li>MySQL> BEGIN  </li>
<li>MySQL> IF ISNULL(S) THEN  </li>
<li>MySQL> RETURN '';  </li>
<li>MySQL> ELSEIF NMySQL> RETURN LEFT(S, N);  </li>
<li>MySQL> ELSE  </li>
<li>MySQL> IF CHAR_LENGTH(S) MySQL> RETURN S;  </li>
<li>MySQL> ELSE  </li>
<li>MySQL> RETURN CONCAT(LEFT(S, N-10), '...', RIGHT(S, 5));  </li>
<li>MySQL> END IF;  </li>
<li>MySQL> END IF;  </li>
<li>MySQL> END; </li>
</ol>

默认情况下,不可能等到用户把这些语句全部输入完之后,再执行整段语句。因为MySQL一遇到分号,它就要自动执行。即,在语句RETURN '';时,MySQL数据库解释器就要执行了。这种情况下,就需要事先把delimiter换成其它符号,如//或$$。

<ol>
<li>MySQL> delimiter //  </li>
<li>MySQL> CREATE FUNCTION `SHORTEN`(S VARCHAR(255), N INT)  </li>
<li>MySQL> RETURNS varchar(255)  </li>
<li>MySQL> BEGIN  </li>
<li>MySQL> IF ISNULL(S) THEN  </li>
<li>MySQL> RETURN '';  </li>
<li>MySQL> ELSEIF NMySQL> RETURN LEFT(S, N);  </li>
<li>MySQL> ELSE  </li>
<li>MySQL> IF CHAR_LENGTH(S) MySQL> RETURN S;  </li>
<li>MySQL> ELSE  </li>
<li>MySQL> RETURN CONCAT(LEFT(S, N-10), '...', RIGHT(S, 5));  </li>
<li>MySQL> END IF;  </li>
<li>MySQL> END IF;  </li>
<li>MySQL> END;//  </li>
</ol>

这样只有当//出现之后,MySQL解释器才会执行这段语句

例子:

<ol>
<li>MySQL> delimiter //   </li>
<li>MySQL> CREATE PROCEDURE simpleproc (OUT param1 INT)   </li>
<li>-> BEGIN   </li>
<li>-> SELECT COUNT(*) INTO param1 FROM t;   </li>
<li>-> END;   </li>
<li>-> //   </li>
<li>Query OK, 0 rows affected (0.00 sec)   </li>
<li>MySQL> delimiter ;   </li>
<li>MySQL> CALL simpleproc(@a);   </li>
<li>Query OK, 0 rows affected (0.00 sec)   </li>
<li>MySQL> SELECT @a;   </li>
<li>+------+   </li>
<li>| @a |   </li>
<li>+------+   </li>
<li>| 3 |   </li>
<li>+------+   </li>
<li>1 row in set (0.00 sec)   </li>
</ol>

本文代码在 MySQL 5.0.41-community-nt 下运行通过。

编写了个统计网站访问情况(user agent)的 MySQL 数据库存储过程。就是下面的这段 SQL 代码。

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

我在 EMS SQL Manager 2005 for MySQL 这个 MySQL 图形客户端下可以顺利运行。但是在 SQLyog MySQL GUI v5.02 这个客户端就会出错。最后找到原因是没有设置好 delimiter 的问题。

默认情况下,delimiter “;” 用于向 MySQL 提交查询语句。在存储过程中每个 SQL 语句的结尾都有个 “;”,如果这时候,每逢 “;” 就向 MySQL 提交的话,当然会出问题了。于是更改 MySQL 的 delimiter,上面 MySQL 存储过程就编程这样子了:

delimiter //; -- 改变 MySQL delimiter 为:“//”

<ol>
<li>drop procedure if exists pr_stat_agent //  </li>
<li>-- call pr_stat_agent ('2008-07-17', '2008-07-18')  </li>
<li>create procedure pr_stat_agent  </li>
<li>(  </li>
<li>pi_date_from date  </li>
<li>,pi_date_to date  </li>
<li>)  </li>
<li>begin  </li>
<li>-- check input  </li>
<li>if (pi_date_from is null) then  </li>
<li>set pi_date_from = current_date();  </li>
<li>end if;  </li>
<li>if (pi_date_to is null) then  </li>
<li>set pi_date_to = pi_date_from;  </li>
<li>end if;  </li>
<li>set pi_date_to = date_add(pi_date_from, interval 1 day);  </li>
<li>-- stat  </li>
<li>select agent, count(*) as cnt  </li>
<li>from apache_log  </li>
<li>where request_time >= pi_date_from  </li>
<li>and request_time group by agent  </li>
<li>order by cnt desc;  </li>
<li>end; //  </li>
<li>
<font style="background-color: #ff0000">delimiter ;</font>  </li>
</ol>

改回默认的 MySQL delimiter:“;”

当然,MySQL delimiter 符号是可以自由设定的,你可以用 “/” 或者“$$” 等。但是 MySQL数据库 存储过程中比较常见的用法是 “//” 和 “$$”。上面的这段在 SQLyog 中的代码搬到 MySQL 命令客户端(MySQL Command Line Client)却不能执行。

MySQL> delimiter //; -- 改变 MySQL delimiter 为:“//”

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

改回默认的 MySQL delimiter:“;”

<ol>
<li>-> //  </li>
<li>-> //  </li>
<li>-> //  </li>
<li>-> ;  </li>
<li>-> ;  </li>
<li>-> </li>
</ol>

真是奇怪了!最后终于发现问题了,在 MySQL 命令行下运行 “delimiter //; ” 则 MySQL 的 delimiter 实际上是 “//;”,而不是我们所预想的 “//”。其实只要运行指令 “delimiter //” 就 OK 了。

MySQL> delimiter // -- 末尾不要符号 “;”

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

末尾不要符号 “//”

<ol><li>MySQL> </li></ol>

顺带一提的是,我们可以在 MySQL 数据库中执行在文件中的 SQL 代码。例如,我把上面存储过程的代码放在文件 d:/pr_stat_agent.sql 中。可以运行下面的代码建立存储过程。

<ol>
<li>MySQL> source d:/pr_stat_agent.sql  </li>
<li>Query OK, 0 rows affected (0.00 sec)  </li>
<li>Query OK, 0 rows affected (0.00 sec)  </li>
</ol>

source 指令的缩写形式是:“/.”

<ol>
<li>MySQL> /. d:/pr_stat_agent.sql  </li>
<li>Query OK, 0 rows affected (0.00 sec)  </li>
<li>Query OK, 0 rows affected (0.00 sec)  </li>
</ol>

最后,可见 MySQL数据库的客户端工具在有些地方是各自为政,各有各的一套。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Apr 16, 2025 am 12:20 AM

ACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.

MySQL: Database Management System vs. Programming LanguageMySQL: Database Management System vs. Programming LanguageApr 16, 2025 am 12:19 AM

MySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.

MySQL: Managing Data with SQL CommandsMySQL: Managing Data with SQL CommandsApr 16, 2025 am 12:19 AM

MySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.

MySQL's Purpose: Storing and Managing Data EffectivelyMySQL's Purpose: Storing and Managing Data EffectivelyApr 16, 2025 am 12:16 AM

MySQL is an efficient relational database management system suitable for storing and managing data. Its advantages include high-performance queries, flexible transaction processing and rich data types. In practical applications, MySQL is often used in e-commerce platforms, social networks and content management systems, but attention should be paid to performance optimization, data security and scalability.

SQL and MySQL: Understanding the RelationshipSQL and MySQL: Understanding the RelationshipApr 16, 2025 am 12:14 AM

The relationship between SQL and MySQL is the relationship between standard languages ​​and specific implementations. 1.SQL is a standard language used to manage and operate relational databases, allowing data addition, deletion, modification and query. 2.MySQL is a specific database management system that uses SQL as its operating language and provides efficient data storage and management.

Explain the role of InnoDB redo logs and undo logs.Explain the role of InnoDB redo logs and undo logs.Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?Apr 15, 2025 am 12:15 AM

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

What is the Using temporary status in EXPLAIN and how to avoid it?What is the Using temporary status in EXPLAIN and how to avoid it?Apr 15, 2025 am 12:14 AM

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor