你可以在 MySQL 中使用数字作为表名吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/676451/
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
Are you allowed to use numbers as table names in MySQL?
提问by chustar
I'm thinking of having a program that dynamically creates new tables as the need arises. Can I have tables named with just numbers in MySQL?
我正在考虑有一个程序可以根据需要动态创建新表。我可以在 MySQL 中使用仅用数字命名的表吗?
回答by karim79
Rules for naming objects, including tables in MySql:
命名对象的规则,包括MySql中的表:
http://dev.mysql.com/doc/refman/5.1/en/identifiers.html
http://dev.mysql.com/doc/refman/5.1/en/identifiers.html
Identifiers may begin with a digit but unless quotedmay not consist solely of digits.
标识符可以以数字开头,但 除非引用,否则不能仅由数字组成。
So this would be invalid:
所以这将是无效的:
SELECT * FROM 12345;
But the following would be valid:
但以下内容是有效的:
SELECT * FROM `12345`;
Or if running in ANSI mode the following would work:
或者,如果在 ANSI 模式下运行,以下将起作用:
SET @@session.sql_mode=ANSI_QUOTES;
SELECT * FROM "12345";
回答by nickf
as Karim and Steve Weet pointed out, yes, you can, but you'll have to quote them like this:
正如 Karim 和 Steve Weet 指出的那样,是的,你可以,但你必须像这样引用它们:
SELECT * FROM `3516`
Could I suggest perhaps rethinking your script though? Add a prefix: a table named "t3516" won't be as confusing as just "3516".
我可以建议重新考虑你的剧本吗?添加前缀:名为“t3516”的表不会像“3516”那样混乱。
Also, you could convert the number to just use letters rather than any numbers:
此外,您可以将数字转换为仅使用字母而不是任何数字:
table 0 - t_a
table 1 - t_b
table 2 - t_c
table 25 - t_z
table 26 - t_aa
table 27 - t_ab
... etc
回答by sleske
The answer is yes, as given by karim79, as long as you take care to quote the table names. You could of course use a prefix with a numer, eg. mytable1, mytable2, ... ; that would work without quoting.
答案是肯定的,正如 karim79 给出的那样,只要您注意引用表名即可。您当然可以使用带有数字的前缀,例如。mytable1, mytable2, ... ; 这将在不引用的情况下工作。
That said, you should really think about why you want to create so many tables. The accepted way of doing things is to have everything that belongs together in one table. So rather than having table1, table2... you would use one table, and store the number in a column.
也就是说,您应该真正考虑一下为什么要创建这么多表。公认的做事方式是将所有的东西放在一张桌子上。因此,与其使用 table1、table2...,不如使用一张表,并将数字存储在一列中。
That is just the natural way. Your way of doing things could easily lead to many problems (changing DB schema is problematic for backups, makes it hard for other tools to work with the DB because of many tables, schema changes have to be done to all tables). Dynamically altering your schema at runtime is usually not a good idea.
这只是自然的方式。您的做事方式很容易导致许多问题(更改数据库架构对于备份来说是有问题的,由于有很多表,因此其他工具很难使用数据库,必须对所有表进行架构更改)。在运行时动态更改架构通常不是一个好主意。
回答by terrific
i think it should not be preferable at all coz it will be more confusing and difficult to remember. table name should be relevant to the data it is storing. It's a better habit.
我认为它根本不应该是可取的,因为它会更加混乱和难以记住。表名应该与其存储的数据相关。这是一个更好的习惯。
回答by syed arman
use backtick(`) to quote the table name if it is purely numbers. it is below the esc button on keyboard
如果表名是纯数字,请使用反引号(`) 来引用它。它位于键盘上的 esc 按钮下方