可以使用数字来命名 MySQL 表列吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7975417/
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 21:30:38  来源:igfitidea点击:

Can a number be used to name a MySQL table column?

mysqlddl

提问by Daniel Hunter

I have a table that has column names like 25, 50, 100, etc..

我有一个表,其列名如 25、50、100 等。

When trying to update the table I get an error, no matter how I do it

尝试更新表时,无论我如何操作,都会出现错误

UPDATE table SET '25'='100' WHERE id = '1'

I have tried quoting and backticking every which way but without success.

我曾尝试以任何方式引用和反引号,但没有成功。

The error is always along the lines of:

错误总是沿着以下路线:

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 ''25'=100 WHERE id=1' at line 1

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行的 ''25'=100 WHERE id=1' 附近使用的正确语法

If I change the column name to twentyfive- I don't have a problem, but that's not what I want. Is it possible to use a number as a column name?

如果我将列名更改为twentyfive- 我没有问题,但这不是我想要的。是否可以使用数字作为列名?

回答by Matthew

From the docs:

文档

Identifiers may begin with a digit but unless quoted may not consist solely of digits.

标识符可以以数字开头,但除非引用,否则不能仅由数字组成。

Which means you must quote it with back ticks like `25`:

这意味着您必须用反勾号引用它,例如“25”:

UPDATE table SET `25`='100' WHERE id='1'

回答by Bryan Moodie

As others have said, you can back-tick the namesof tables, columns, etc. Just make sure you're not back-ticking your valuesotherwise it'll interpret them as column names. So in your example only the 25 needs to be back ticked:

正如其他人所说,您可以对表、列等的名称进行反勾。只要确保您没有对进行反勾,否则它会将它们解释为列名称。因此,在您的示例中,仅需要重新勾选 25:

UPDATE table SET `25`=100 WHERE id=1

回答by gview

Check here: http://dev.mysql.com/doc/refman/5.0/en/identifiers.html

在这里查看:http: //dev.mysql.com/doc/refman/5.0/en/identifiers.html

Identifiers may begin with a digit but unless quoted may not consist solely of digits.

标识符可以以数字开头,但除非引用,否则不能仅由数字组成。

So yes, you can do it -- you need to use backtics around the name.

所以是的,你可以做到——你需要在名称周围使用反引号。

回答by Jon Newmuis

If you need it to be based on a number for some reason, you can append some simple prefix to the number in all cases (e.g. "col25", "col87", etc).

如果出于某种原因需要它基于数字,则可以在所有情况下为数字附加一些简单的前缀(例如“col25”、“col87”等)。

回答by Larry Lustig

Names in MySQL can startwith a digit, but cannot be entirelydigits — they cannot be confusable with an integer. So, no.

在MySQL名称可以开始以一个数字,而不能是完全的数字-他们不能混淆使用整数。所以不行。

And if you start your identifier with a digit, you should be pretty sure you're sticking with MySQL because many (most?) other database engines require an alpha character at the front of column names.

如果您的标识符以数字开头,您应该非常确定您坚持使用 MySQL,因为许多(大多数?)其他数据库引擎需要在列名的前面使用字母字符。

(As stated in the comments, a fully integer column name isallowed if you consistently use the backtick character — if you tried backticks and still got a syntax error, post the precise backtick syntax you used).

(正如评论所说,完全整列名,如果你坚持使用倒引号字符允许的-如果你试图反引号,仍然得到了一个语法错误,张贴您使用的精确反引号语法)。

Also, of course, you're going to produce some difficult to read SQL if you opt for integer-named columns!

此外,当然,如果您选择以整数命名的列,您将产生一些难以阅读的 SQL!