0

I wanto to create a trigger in MySQL but it displays an error message when I run the creation code:

CREATE TRIGGER before_employee_update 
    BEFORE UPDATE ON trigger
    FOR EACH ROW BEGIN

    update users
    SET username= 'krishna',
        password= 'abc';
END

Error is:

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 'trigger FOR EACH ROW BEGIN INSERT INTO users SET username= 'kr' at line 2

This is the code the error points to:

BEFORE UPDATE ON trigger
                 ^^^^^^^
8
  • Seems like we need a more verbose description to help… Commented May 31, 2013 at 7:29
  • Not working is not a problem. Can you please show what is your actual problem? Commented May 31, 2013 at 7:30
  • i am new to mysql store procedure, i have table trigger and user Commented May 31, 2013 at 7:30
  • error is some thing like this: -- "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 'trigger FOR EACH ROW BEGIN INSERT INTO users SET username= 'kr' at line 2" Commented May 31, 2013 at 7:32
  • when i run this code in mysql the following error occurs Commented May 31, 2013 at 7:33

2 Answers 2

0

TRIGGER is reserved word in MySQL, you should quote this name -

CREATE TRIGGER before_employee_update
  BEFORE UPDATE
  ON `trigger`
  FOR EACH ROW
BEGIN
  UPDATE users SET username = 'krishna', password = 'abc';
END

As a workaround - rename table trigger.

Sign up to request clarification or add additional context in comments.

1 Comment

Use DROP TRIGGER trigger_name; comamnd. Or also you can do it using free GUI tool - dbForge Studio for MySQL (express edition).
0

The proper syntax for insert is:

insert into users(username, password)
    select 'krishna', 'abc';

Your syntax is close to an update:

update users
SET username= 'krishna',
    password= 'abc';

2 Comments

@Nirdosh . . . The operation is on a different table, so you can use either update or delete.
can u write me a simple trigger so that i can follow it's syntax, because i can't execute a single trigger in mysql.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.