开发者

where should define create TRIGGER in mysql

开发者 https://www.devze.com 2023-02-16 13:03 出处:网络
SQL query: CREATE TRIGGER testref B开发者_Go百科EFORE INSERT ON users FOR EACH ROW BEGIN INSERT INTO users_roles

SQL query:

CREATE TRIGGER testref B开发者_Go百科EFORE INSERT ON users
FOR EACH
ROW BEGIN 
INSERT INTO users_roles
SET uid = NEW.uid;

MySQL said:

#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 


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)

EDIT

The trigger is all you need - edit as appropriate for your schema !!

delimiter #

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

delimiter ;


The INSERT syntax is wrong.

The syntax should follow one of

INSERT INTO table VALUES (?,?)

or

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


You have only a small syntax error, you need to remove the BEGIN before INSERT

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

Will work


where?? Run SQL query/queries on database in phpmyadmin

code:

delimiter |

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

delimiter ;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号