Search code examples
mysqlsqlsql-insertmysql-error-1064

SQL Desc column name not accepted?


I've have created a SQL database, which I'm connected to with MySQL Workbench. I have one column that I for some reason can't use in the query, which Workbench actually recognize. My Query looks looked like this:

INSERT INTO `mcfluid` (desc, type, maxcap, curcap) VALUES ('tank', 'water',1000,1000);

This didn't work for some reason, but when I changed it to this:

INSERT INTO `mcfluid` (`desc`, type, maxcap, curcap) VALUES ('tank', 'lava',1000,1000);

It for some reason did?

Why is this?

My table looks like this:

id int(11) AI PK 
desc varchar(100) 
type varchar(100) 
maxcap bigint(20) 
curcap bigint(20)

Solution

  • desc is a reserved word in SQL - it's a keyword for the order by clause that denotes the ordering in a descing order (e.g.: select * from mytable order by mycolumn DESC).

    Escaping it with backticks allows MySQL to understand you've meant to use it as a name and not a syntactic element.