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

蓝咒 提交于 2019-11-26 14:43:11

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`:

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

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

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

Check here: 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.

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).

Names in MySQL can start with a digit, but cannot be entirely digits — they cannot be confusable with an integer. So, no.

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.

(As stated in the comments, a fully integer column name is allowed 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!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!