MySQL 在 phpMyAdmin 中触发
MySQL trigger in phpMyAdmin
不明白,为什么这不起作用:C
DELIMITER $$
CREATE TRIGGER 'Reset Pass' BEFORE UPDATE ON authme
FOR EACH ROW
BEGIN
IF OLD.`password` <> NEW.`password` THEN
UPDATE authme SET `idVK` = '' WHERE `username` = NEW.`username`;
END IF;
END$$
DELIMITER ;
求助
开头:触发器名称不应该用单引号括起来;你需要反引号 - 或者更好的是,没有引号。
更深层次的问题是触发器无法对它触发的 table 起作用。您的触发器代码可以编译,但运行时会出现错误:
Error: ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG: Can't update table 'authme' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
我了解到您想在密码更改时清空列 idVK
。为此,您只需将其值设置为伪table NEW
:
DELIMITER $$
CREATE TRIGGER Reset_Pass BEFORE UPDATE ON authme
FOR EACH ROW
BEGIN
IF OLD.`password` <> NEW.`password` THEN
SET NEW.`idVK` = '';
END IF;
END$$
DELIMITER ;
Demo on DB Fiddle - 创建上述触发器后:
insert into authme(username, password, idVK) values ('foo', 'bar', 'baz');
update authme set password = 'newbar' where username = 'foo';
select * from authme;
| username | password | idVK |
| -------- | -------- | ---- |
| foo | newbar | |
不明白,为什么这不起作用:C
DELIMITER $$
CREATE TRIGGER 'Reset Pass' BEFORE UPDATE ON authme
FOR EACH ROW
BEGIN
IF OLD.`password` <> NEW.`password` THEN
UPDATE authme SET `idVK` = '' WHERE `username` = NEW.`username`;
END IF;
END$$
DELIMITER ;
求助
开头:触发器名称不应该用单引号括起来;你需要反引号 - 或者更好的是,没有引号。
更深层次的问题是触发器无法对它触发的 table 起作用。您的触发器代码可以编译,但运行时会出现错误:
Error: ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG: Can't update table 'authme' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
我了解到您想在密码更改时清空列 idVK
。为此,您只需将其值设置为伪table NEW
:
DELIMITER $$
CREATE TRIGGER Reset_Pass BEFORE UPDATE ON authme
FOR EACH ROW
BEGIN
IF OLD.`password` <> NEW.`password` THEN
SET NEW.`idVK` = '';
END IF;
END$$
DELIMITER ;
Demo on DB Fiddle - 创建上述触发器后:
insert into authme(username, password, idVK) values ('foo', 'bar', 'baz');
update authme set password = 'newbar' where username = 'foo';
select * from authme;
| username | password | idVK |
| -------- | -------- | ---- |
| foo | newbar | |