Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I can create table columns named "key" and "value", but I can't use them later?

Tip of the day: Use backticks, like a boss! Even in your handwriting!

Update: If you take the above advice, you don't need to read the following anymore! Seriously!

Question: I'm a little bit annoyed by this. I can create a table containing columns which are named key and value, but when I want to work with these columns I will see a very nice syntax error explaining that these are reserved keywords for MySQL.

My question is: does anybody know why it is like this? Why I'm not receiving the syntax error in the first place? Is it backed by any reason?

like image 250
Mahdi Avatar asked Jan 14 '23 01:01

Mahdi


1 Answers

Only KEY is a reserved keyword :D.

  • MySQL Reserved Keywords List

just wrap the column name KEY with backtick so you can use it, eg

SELECT `key`
FROM   tableName

or supply the table with alias,

SELECT a.key
FROM   tableName a
like image 53
John Woo Avatar answered Jan 17 '23 18:01

John Woo