所以我创建了一个触发器来更新插件上的另一个表,没有发现错误并且触发器存在(列在SHOW TRIGGERS
中,但是在我插入一个表后没有更新到另一个表。
触发器:
CREATE TRIGGER setmapsquare AFTER INSERT ON users
FOR EACH ROW
UPDATE map SET player_id = NEW.player_id WHERE map_id = NEW.startsquare;
它不会抛出任何错误,所以我现在不知道如何继续:/
编辑:插入也在这里进行:
$q = $DBC -> prepare( "
INSERT INTO
users
( username, password, email, startsquare )
VALUES ( ?, ?, ?, (SELECT map_id FROM map WHERE player_id IS NULL ORDER BY RAND() LIMIT 1) );" );
插入的行1, 'Testjuuser', '2bd7fc520207eabc4026989728def05b541a863e', 'test@test', 38, 2014-08-31 09:20:28, 0, 0
并且map_id 38仍然具有NULL作为其player_id
答案 0 :(得分:1)
无法复制问题所描述的行为。 INSERT
语句应该失败并显示错误消息:
Error Code: 1442. Can't update table 'map' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
重现这一步的步骤:
CREATE TABLE users (
player_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(20),
password VARCHAR(80),
email VARCHAR(260),
startsquare INT
);
CREATE TABLE map (
map_id INT,
player_id INT
);
为表格地图插入一些值
INSERT INTO map (map_id, player_id) VALUES
(37, NULL),
(38, NULL),
(39, NULL);
创建触发器
DELIMITER //
CREATE TRIGGER setmapsquare AFTER INSERT ON users
FOR EACH ROW
UPDATE map SET player_id = NEW.player_id WHERE map_id = NEW.startsquare;
//
DELIMITER ;
尝试此插入
INSERT INTO users (username, password, email, startsquare) VALUES
(
'Testjuuser',
'2bd7fc520207eabc4026989728def05b541a863e',
'test@test',
(SELECT map_id FROM map WHERE player_id IS NULL ORDER BY RAND() LIMIT 1)
);
并收到上面显示的错误消息see this demo
注意:您必须在选择之前注释掉38并删除破折号。
虽然硬编码38将按预期处理:
INSERT INTO users (username, password, email, startsquare) VALUES
(
'Testjuuser',
'2bd7fc520207eabc4026989728def05b541a863e',
'test@test',
38
);
<强>结论强>
实际的触发器语句不是问题中显示的,或者存在错误,但INSERT应该完全失败。要检查触发器语句,可以对INFORMATION_SCHEMA
使用以下查询:
SELECT
trigger_schema, -- the database,
event_object_table, -- table
action_timing, -- should be 'after'
event_manipulation, -- should be 'INSERT'
action_statement -- the actual code
FROM
INFORMATION_SCHEMA.TRIGGERS
WHERE
trigger_name = 'setmapsquare';
action_statement的内容在我的情况下
UPDATE map SET player_id = NEW.player_id WHERE map_id = NEW.startsquare