ghost1349
ghost1349

Reputation: 83

Using MySQL Database Triggers

I have three tables: users, account and accountinfo and I am trying to make a trigger that will add the id from users to the UserID column in the account table. Here is what I tried:

CREATE TRIGGER catchUser BEFORE INSERT ON defaultdatabase.users 
FOR EACH ROW
BEGIN
INSERT INTO defaultdatabase.account(UserID) VALUES (new.id);
END

However, I get an error right after my INSERT statement that says,

Syntax Error: insert 'semicolon'

Why am I getting this error is I have the semicolon or is my trigger just wrong?

I'm using MySQL 5.6 if that makes any difference as well.

Upvotes: 1

Views: 52

Answers (3)

user3111011
user3111011

Reputation: 145

Please insert a semicolon after END.

Upvotes: 0

Mureinik
Mureinik

Reputation: 312289

You need to specify the delimiter:

delimiter //
CREATE TRIGGER catchUser BEFORE INSERT ON defaultdatabase.users 
FOR EACH ROW
BEGIN
INSERT INTO defaultdatabase.account(UserID) VALUES (new.id);
END; //
delimiter ;

Upvotes: 1

Pathik Vejani
Pathik Vejani

Reputation: 4501

Try this:

CREATE TRIGGER catchUser BEFORE INSERT ON defaultdatabase.users 
FOR EACH ROW
BEGIN
INSERT INTO defaultdatabase.account(UserID) VALUES (new.id)
END;

Upvotes: 0

Related Questions