MySQL 语法错误使用名为“desc”的列创建表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10075572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 12:53:48  来源:igfitidea点击:

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

mysql

提问by Greg

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

我似乎永远无法使用 TEXT 类型的列创建 MySQL 表。这是我的 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.

我无法弄清楚出了什么问题!如果它有什么不同,我正在使用Java。

回答by Umbrella

descis a reserved word and shouldn'tbe used as a name of a column. You canuse descas a name of a column, but if you do, you must always wrap it in back-ticks.

desc是保留字,不应用作列名。您可以将其desc用作列的名称,但如果您这样做,则必须始终将其括在反引号中。

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, UPDATEand 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.

上面已经过测试并且有效,但是由于您总是必须将其包装在反引号中(在每个INSERT,UPDATE和 中DELETE),您可能想要更改名称,或者只是养成将所有字段包装在反引号中的习惯,这还有其他优点。

回答by mykey

change the column name descto any something different since it is a reserverd word for descendor describe

将列名更改desc为任何不同的名称,因为它是descend或的保留字describe

回答by Ravinder Reddy

descis a reserved word and can'tshall notbe given as a name to a table or column. You may use descriptionas the column name. With the name change, your statement should work.

desc是保留字,不能不得给予的名字命名的表或列。您可以description用作列名。更改名称后,您的语句应该会起作用。