我要在xampp中创建一个触发器。
CREATE TRIGGER testref BEFORE INSERT ON test1
FOR EACH ROW
BEGIN
INSERT INTO test2 SET a2 = NEW.a1;
DELETE FROM test3 WHERE a3 = NEW.a1;
UPDATE test4 SET b4 = b4 + 1 WHERE a4 = NEW.a1;
END;
但我有错误:
CREATE TRIGGER testref BEFORE INSERT ON test1
FOR EACH
ROW
BEGIN
INSERT INTO test2
SET a2 = NEW.a1;
MySQL said: Documentation
#1064 - You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version
for the right syntax to use near '' at line 4
之前,我创建了4个表:
CREATE TABLE test1(a1 INT);
CREATE TABLE test2(a2 INT);
CREATE TABLE test3(a3 INT NOT NULL AUTO_INCREMENT PRIMARY KEY);
CREATE TABLE test4(
a4 INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
b4 INT DEFAULT 0
);
请帮帮我。谢谢你!
答案 0 :(得分:0)
首先更改分隔符,否则过程中的分号会破坏语法。请参阅MySQL文档中此page on creating procedures的一半示例(下面复制以节省时间)。
mysql> delimiter //
mysql> CREATE PROCEDURE simpleproc (OUT param1 INT)
-> BEGIN
-> SELECT COUNT(*) INTO param1 FROM t;
-> END//
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> CALL simpleproc(@a);
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @a;
+------+
| @a |
+------+
| 3 |
+------+
1 row in set (0.00 sec)
更新:或许以下......
DELIMITER //
CREATE TRIGGER testref BEFORE INSERT ON test1
FOR EACH ROW
BEGIN
INSERT INTO test2 SET a2 = NEW.a1;
DELETE FROM test3 WHERE a3 = NEW.a1;
UPDATE test4 SET b4 = b4 + 1 WHERE a4 = NEW.a1;
END//
DELIMITER ;
答案 1 :(得分:0)
看看你的phpmyadmin版本中是否有DILIMITER字段,就在SQL编辑器下面。在那里设置分隔符,例如 - '$$',并编写此SQL代码 -
CREATE TRIGGER testref BEFORE INSERT ON test1
FOR EACH ROW
BEGIN
INSERT INTO test2 SET a2 = NEW.a1;
DELETE FROM test3 WHERE a3 = NEW.a1;
UPDATE test4 SET b4 = b4 + 1 WHERE a4 = NEW.a1;
END$$
如果你的phpmyadmin没有这个选项(它可以是旧版本),并且如果可以使用其他工具连接到服务器,那么尝试使用另一个MySQL客户端,例如: - dbForge Studio for MySQL(免费快递版)。
答案 2 :(得分:0)
使用以下命令创建触发器
delimiter |
CREATE TRIGGER testref BEFORE INSERT ON test1
FOR EACH ROW
BEGIN
INSERT INTO test2 SET a2 = NEW.a1;
DELETE FROM test3 WHERE a3 = NEW.a1;
UPDATE test4 SET b4 = b4 + 1 WHERE a4 = NEW.a1;
END;
|
那是......你必须使用分隔符..
分隔符用于定义查询的结尾。默认情况下,它是半列(;)。这里我们使用DELIMITER命令来更改默认分隔符,以便我们可以使用';' insdie触发器定义