MySQL INSERT with table alias

Max Kielland picture Max Kielland · Jan 17, 2011 · Viewed 13.9k times · Source

I happen to have two columns having the same name as two SQL reserved words, Key and Value. When using the SELECT statement I can create a table alias and solve it that way.

Now I'm trying to INSERT data and it seems like you can't create table alias in the INSERT statement.

INSERT INTO attributeStrings ats
(ats.ItemID,ats.Key,ats.Value)
VALUES (3,'Categories','TechGUI')

I get error at 'ats (ats.ItemID,ats.Key,ats.Value) VALUES (3,'Categories','TechGUI')' indicating that alias can't be created.

Are there any ways to solve this without renaming the columns Key and Value?

Answer

Nishant picture Nishant · Jan 17, 2011

Use back-tick to escape reserved words.

  INSERT INTO `attributeStrings` (`ItemID`, `Key`,`Value`) VALUES (3,'Categories','TechGUI')

Looks like insert does not support alias. see here


Edit: ok, the MySQL ref says no alias in insert


It does work

mysql> INSERT INTO `attributeStrings` (`ItemID`, `Key`,`Value`) VALUES (3,'Categories','TechGUI');
Query OK, 1 row affected (0.03 sec)

mysql> select * from attributeStrings;
+--------+------------+---------+
| ItemId | Key        | Value   |
+--------+------------+---------+
|      3 | Categories | TechGUI |
+--------+------------+---------+
1 row in set (0.00 sec)