Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax Error Creating a table with a column named 'desc'

Tags:

mysql

I can never seem to be able to create MySQL tables with columns of the type TEXT. Here's my MySQL:

CREATE TABLE factions(id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), name VARCHAR(16) NOT NULL, desc TEXT NOT NULL, admins TEXT NOT NULL, mods TEXT, members TEXT, land TEXT, enemies TEXT, allies TEXT)

When it's run, I get this:

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 'desc text NOT NULL, admins text NOT NULL, mods text, members text, land text, en' at line 1

I can't figure out what's wrong! I'm using Java if it makes any difference.

like image 314
Greg Avatar asked Apr 09 '12 15:04

Greg


2 Answers

desc is a reserved word and shouldn't be used as a name of a column. You can use desc as a name of a column, but if you do, you must always wrap it in back-ticks.

CREATE TABLE factions(
   id INT NOT NULL AUTO_INCREMENT,
   PRIMARY KEY(id), 
   name    VARCHAR(16) NOT NULL, 
   `desc`  TEXT NOT NULL, 
   admins  TEXT NOT NULL, 
   mods    TEXT, 
   members TEXT, 
   land    TEXT, 
   enemies TEXT, 
   allies  TEXT
);

The above is tested and works, but since you'll always have to wrap it in back-ticks (in every INSERT, UPDATE and DELETE) you may want to change the name, or just get in the habit of wrapping all fields in back-ticks, which has other advantages.

like image 79
Umbrella Avatar answered Sep 29 '22 08:09

Umbrella


change the column name desc to any something different since it is a reserverd word for descend or describe

like image 26
mykey Avatar answered Sep 29 '22 09:09

mykey