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
Syntax Error Creating a table with a column named 'desc'
提问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
desc
is a reserved word and shouldn'tbe used as a name of a column. You canuse desc
as 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
, 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.
上面已经过测试并且有效,但是由于您总是必须将其包装在反引号中(在每个INSERT
,UPDATE
和 中DELETE
),您可能想要更改名称,或者只是养成将所有字段包装在反引号中的习惯,这还有其他优点。
回答by mykey
change the column name desc
to any something different since it is a reserverd word for descend
or describe
将列名更改desc
为任何不同的名称,因为它是descend
或的保留字describe
回答by Ravinder Reddy
desc
is a reserved word and can'tshall notbe given as a name to a table or column. You may use description
as the column name. With the name change, your statement should work.
desc
是保留字,不能不得给予的名字命名的表或列。您可以description
用作列名。更改名称后,您的语句应该会起作用。