MySQL 5.0 触发器
2007-11-11 13:52:04 来源:WEB开发网核心提示: CREATE TABLE t25 (s1 INT, s2 CHAR(5), Prima(最完善的虚拟主机管理系统)RY KEY (s1)) ENGINE=INNODB// CREATE TRIGGER t25_bi BEFORE INSERT ON t25 FOR EACH ROW IF LEFT(NEW.s2,1)
CREATE TABLE t25
(s1 INT, s2 CHAR(5),
Prima(最完善的虚拟主机管理系统)RY KEY (s1))
ENGINE=INNODB//
CREATE TRIGGER t25_bi
BEFORE INSERT ON t25
FOR EACH ROW
IF LEFT(NEW.s2,1)<>’A’ THEN SET NEW.s1=0; END IF;//
CREATE TRIGGER t25_bu
BEFORE UPDATE ON t25
FOR EACH ROW
IF LEFT(NEW.s2,1)<>’A’ THEN SET NEW.s1=0; END IF;//
我只需要使用BEFORE INSERT和BEFORE UPDATE语句就行了,删除了触发器不会对表有影响,同时AFTER的触发器也不能修改NEW的过程变量(transition variables)。为了激活触发器,我执行了向表中的行插入s1=0的数据,之后只要执行符合LEFT(s2,1) <> ’A’条件的动作都会失败:转载请注明翻译者陈朋奕及转自:www.phpv.net
INSERT INTO t25 VALUES (0,’a’) /* priming the pump */ //
INSERT INTO t25 VALUES (5,’b’) /* gets error ’23000’ */ //
Don’t Believe The Old MySQL(和PHP搭配之最佳组合) Manual
该抛弃旧的MySQL(和PHP搭配之最佳组合)的手册了
我在这里警告大家不要相信过去的MySQL(和PHP搭配之最佳组合)手册中所说的了。我们已经去掉了关于触发器的错误的语句,但是仍旧有很多旧版本的手册在网上,举个例子,这是一个德国的Url上的:
http://dev.MySQL(和PHP搭配之最佳组合).com/doc/MySQL(和PHP搭配之最佳组合)/de/ANSI_diff_Triggers.html.
这个手册上说触发器就是存储过程,忘掉吧,你也已经看见了,触发器就是触发器,而存储过程还是存储过程。
手册上还说触发器可以从其他表上来删除,或者是当你删除一个事务的时候激发,无论他说的是什么意思,忘掉吧,MySQL(和PHP搭配之最佳组合)不会去实现这些的。转载请注明翻译者陈朋奕及转自:www.phpv.net
最后关于说使用触发器会对查询速度产生影响的说法也是错的,触发器不会对查询产生任何影响。
Bugs
(不好的东西就不翻译了)
On December 14 2004, I did an "Advanced Search" in http://bugs.MySQL(和PHP搭配之最佳组合).com for ’trigger’ or
’triggers’, I found that there were 17 active bugs as of that date. Of course they might disappear
before you read this, but just in case they haven’t, I’ll mention the important ones. If they’re still
there, you’ll have to work around them when you’re trying triggers.
Bug#5859 DROP TABLE does not drop triggers.
(删除表的时候没有自动删除触发器)
When you drop a table, dropping the table’s triggers should be automatic.
Bug#5892 Triggers have the wrong namespace.
(触发器的命名空间有错,你必须在前面加上表的名字才能删除触发器,下面是例子)
You have to say "DROP TRIGGER <table name> . <trigger name>".
The correct way is "DROP TRIGGER <trigger name>".
Bug#5894 Triggers with altered tables cause corrupt databases.
(触发器对表的改变可能会造成数据库数据被破坏)
Do not alter a table that has a trigger on it, until you know this is fixed.
(s1 INT, s2 CHAR(5),
Prima(最完善的虚拟主机管理系统)RY KEY (s1))
ENGINE=INNODB//
CREATE TRIGGER t25_bi
BEFORE INSERT ON t25
FOR EACH ROW
IF LEFT(NEW.s2,1)<>’A’ THEN SET NEW.s1=0; END IF;//
CREATE TRIGGER t25_bu
BEFORE UPDATE ON t25
FOR EACH ROW
IF LEFT(NEW.s2,1)<>’A’ THEN SET NEW.s1=0; END IF;//
我只需要使用BEFORE INSERT和BEFORE UPDATE语句就行了,删除了触发器不会对表有影响,同时AFTER的触发器也不能修改NEW的过程变量(transition variables)。为了激活触发器,我执行了向表中的行插入s1=0的数据,之后只要执行符合LEFT(s2,1) <> ’A’条件的动作都会失败:转载请注明翻译者陈朋奕及转自:www.phpv.net
INSERT INTO t25 VALUES (0,’a’) /* priming the pump */ //
INSERT INTO t25 VALUES (5,’b’) /* gets error ’23000’ */ //
Don’t Believe The Old MySQL(和PHP搭配之最佳组合) Manual
该抛弃旧的MySQL(和PHP搭配之最佳组合)的手册了
我在这里警告大家不要相信过去的MySQL(和PHP搭配之最佳组合)手册中所说的了。我们已经去掉了关于触发器的错误的语句,但是仍旧有很多旧版本的手册在网上,举个例子,这是一个德国的Url上的:
http://dev.MySQL(和PHP搭配之最佳组合).com/doc/MySQL(和PHP搭配之最佳组合)/de/ANSI_diff_Triggers.html.
这个手册上说触发器就是存储过程,忘掉吧,你也已经看见了,触发器就是触发器,而存储过程还是存储过程。
手册上还说触发器可以从其他表上来删除,或者是当你删除一个事务的时候激发,无论他说的是什么意思,忘掉吧,MySQL(和PHP搭配之最佳组合)不会去实现这些的。转载请注明翻译者陈朋奕及转自:www.phpv.net
最后关于说使用触发器会对查询速度产生影响的说法也是错的,触发器不会对查询产生任何影响。
Bugs
(不好的东西就不翻译了)
On December 14 2004, I did an "Advanced Search" in http://bugs.MySQL(和PHP搭配之最佳组合).com for ’trigger’ or
’triggers’, I found that there were 17 active bugs as of that date. Of course they might disappear
before you read this, but just in case they haven’t, I’ll mention the important ones. If they’re still
there, you’ll have to work around them when you’re trying triggers.
Bug#5859 DROP TABLE does not drop triggers.
(删除表的时候没有自动删除触发器)
When you drop a table, dropping the table’s triggers should be automatic.
Bug#5892 Triggers have the wrong namespace.
(触发器的命名空间有错,你必须在前面加上表的名字才能删除触发器,下面是例子)
You have to say "DROP TRIGGER <table name> . <trigger name>".
The correct way is "DROP TRIGGER <trigger name>".
Bug#5894 Triggers with altered tables cause corrupt databases.
(触发器对表的改变可能会造成数据库数据被破坏)
Do not alter a table that has a trigger on it, until you know this is fixed.
更多精彩
赞助商链接