0

I'm trying to create the following trigger in PHP admin but get the following error. I've set delimiter to '//' but still no luck. Any help?

For your information the table is called 'users' and I'm trying to add the UUID to the primary key 'user_id'

CREATE TRIGGER user_id_users_insert BEFORE INSERT ON 'users'
FOR EACH ROW
BEGIN
SET NEW.user_id=UUID();
END;

#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 ''users' FOR EACH ROW BEGIN SET NEW.user_id=UUID(); END' at line 1

Minar Mahmud
  • 2,577
  • 6
  • 20
  • 32
Ka Tech
  • 8,937
  • 14
  • 53
  • 78
  • What does `UUID` do ? – Abhik Chakraborty Mar 21 '15 at 10:56
  • 1
    Quoting `'users'` with single-quote `'` characters results in it being parsed as a string literal (which is not valid in the `ON` clause of a `CREATE TRIGGER` statement) rather than as an SQL object identifier (such as a table name, which is what MySQL expects to see) which, if quoted, must instead use the backtick `\`` character (or, alternatively, double-quote `"` characters if MySQL's [`ANSI_QUOTES`](https://dev.mysql.com/doc/en/sql-mode.html#sqlmode_ansi_quotes) SQL mode is enabled). See [When to use single quotes, double quotes, and backticks?](http://stackoverflow.com/q/11321491) – eggyal Mar 21 '15 at 11:05
  • Thanks the backtick worked. This now has been answered! – Ka Tech Mar 21 '15 at 11:16

1 Answers1

0

Quoting 'users' with single-quote ' characters results in it being parsed as a string literal (which is not valid in the ON clause of a CREATE TRIGGER statement) rather than as an SQL object identifier (such as a table name, which is what MySQL expects to see) which, if quoted, must instead use the backtick ` character (or, alternatively, double-quote " characters if MySQL's ANSI_QUOTES SQL mode is enabled). See When to use single quotes, double quotes, and backticks? -- @eggyal

O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • Fair enough... I voted to close as "simple typographical error" instead. – eggyal Mar 21 '15 at 13:20
  • @eggyal, me too. But ya know, SO works better for folks when answers are answers, rather than comments. Even if they're community-wiki answers. – O. Jones Mar 21 '15 at 14:15