应该在mysql中定义创建TRIGGER的位置

时间:2011-03-14 12:17:17

标签: mysql

SQL查询:

CREATE TRIGGER testref BEFORE INSERT ON users
FOR EACH
ROW BEGIN 
INSERT INTO users_roles
SET uid = NEW.uid;
MySQL说:

#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 3 

4 个答案:

答案 0 :(得分:0)

您只有很小的语法错误,您需要在BEGIN之前移除INSERT

CREATE TRIGGER testref BEFORE INSERT ON users
FOR EACH 
ROW INSERT INTO users_roles
SET uid = NEW.uid;

会工作

答案 1 :(得分:0)

drop table if exists users;
create table users
(
user_id int unsigned not null auto_increment primary key,
username varchar(32) not null,
created_date datetime not null
)
engine=innodb;

delimiter #

create trigger users_before_ins_trig before insert on users
for each row
begin
  set new.created_date = now();
end#

delimiter ;

insert into users (username) values ('f00'),('bar');

mysql> select * from users;
+---------+----------+---------------------+
| user_id | username | created_date        |
+---------+----------+---------------------+
|       1 | f00      | 2011-03-14 12:39:38 |
|       2 | bar      | 2011-03-14 12:39:38 |
+---------+----------+---------------------+
2 rows in set (0.00 sec)

修改

触发器就是您所需要的 - 根据您的架构进行编辑!!

delimiter #

create trigger users_before_ins_trig before insert on users
for each row
begin
  set new.created_date = now();
end#

delimiter ;

答案 2 :(得分:0)

INSERT语法错误。

语法应遵循

之一
INSERT INTO table VALUES (?,?)

UPDATE table SET column = ? [WHERE column = ?)

答案 3 :(得分:0)

其中?? 在phpmyadmin中对数据库运行SQL查询/查询

代码:

delimiter |

CREATE TRIGGER testref AFTER INSERT ON users
  FOR EACH ROW BEGIN
    INSERT INTO beep  SET uid = NEW.uid;
  END;
|

delimiter ;