MySQL DELIMITER // 在触发器中做什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1346637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does DELIMITER // do in a Trigger?
提问by Kevin
DELIMITER //
What is the of use of it?
它有什么用?
回答by chaos
It changes the statement delimiter from ;
to //
. This is so you can write ;
in your trigger definition without the MySQL client misinterpreting that as meaning you're done with it.
它将语句分隔符从 更改;
为//
。这样您就可以;
在触发器定义中编写,而不会被 MySQL 客户端误解为意味着您已经完成了它。
Note that when changing back, it's DELIMITER ;
, not DELIMITER;
as I've seen people try to do.
请注意,当改回来时,它是DELIMITER ;
,而不是DELIMITER;
像我看到的人们尝试做的那样。
回答by Zed
In SQL you close each statement with a delimiter, which is by default a semicolon (;). In a trigger you need to write multiple statements, each ending in a semicolon. To tell MySQL that those semicolons are not the end of your trigger statement, you temporarily change the delimiter from ; to //, so MySQL will know that the trigger statement only ends when it econunters a //.
在 SQL 中,您使用分隔符关闭每个语句,默认情况下是分号 (;)。在触发器中,您需要编写多个语句,每个语句以分号结尾。要告诉 MySQL 这些分号不是触发器语句的结尾,您可以暂时将分隔符从 ; 到 //,因此 MySQL 将知道触发器语句仅在它计算 // 时才结束。
回答by Khoa TruongDinh
Add an example:
When working with mysql shell command, we used ;
delimiter to close each statement. However, in case, we want to build the store procedures and triggers, we need to add the semicolon ;
in these statements also.
添加示例:
在使用 mysql shell 命令时,我们使用;
分隔符来关闭每个语句。但是,如果我们要构建存储过程和触发器,我们还需要;
在这些语句中添加分号。
delimiter //
create trigger log_students after insert on students
for each row
begin
insert into log_students(change_by, change_at) values(USER(), NOW());
end//
delimiter ;
回答by winwaed
Simple sets the end of statement delimiter (;
semi-colon in standard, default SQL).
Changing the character could be useful if you want to use ;
in your SQL, or you are using embedded SQL (where it might lead to confusion). similarly the //
in your example could lead to confusion in embedded SQL, or you might want to use it in your SQL. So imply use DELIMITER
to set the delimiter that is appropriate for your application and needs.
Simple 设置语句结束分隔符(;
标准中的分号,默认 SQL)。如果您想;
在 SQL 中使用,或者您正在使用嵌入式 SQL(这可能会导致混淆),更改字符可能会很有用。同样//
,您的示例中的 可能会导致嵌入式 SQL 中的混淆,或者您可能希望在您的 SQL 中使用它。所以暗示使用DELIMITER
来设置适合您的应用程序和需要的分隔符。
回答by Sorin Mocanu
Read the (ummmm) mysql documentation.
阅读(嗯)mysql 文档。
delimiter
is the marker for the end of each command you send to the mysql command line client.
delimiter
是您发送到 mysql 命令行客户端的每个命令结束的标记。
delimiter
is not only related to triggers, but defining triggers and stored procedures is one strong use case as you wish them to contain semicolons (;) which are otherwise the default delimiter
.
delimiter
不仅与触发器有关,而且定义触发器和存储过程是一个强大的用例,因为您希望它们包含分号 (;),否则它们是默认的delimiter
.