MySQL 表名中的特殊字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10443050/
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
Special Characters in MySQL Table Name
提问by peasant13337
I created a table as follows:
我创建了一个表如下:
CREATE TABLE IF NOT EXISTS 'e!' (
`aa` int(11) unsigned NOT NULL auto_increment,
`showName` TEXT NOT NULL default '',
`startDateTime` DATETIME NOT NULL default '',
`endDateTime` DATETIME NOT NULL default '',
PRIMARY KEY (`aa`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
Then tried to insert with the query:
然后尝试插入查询:
INSERT INTO e! (showname, startDateTime, endDateTime) VALUES('E! News ', '2012-05-03 19:00:00', '2012-05-03 20:00:00')
And it errors due to the !
in the table name, I'm assuming !
is a special character in mysql. I tried to escape it but the query still failed.
由于!
表名中的错误,我假设它!
是 mysql 中的特殊字符。我试图逃避它,但查询仍然失败。
So, can I have special characters like !
or &
in the table name? If yes, then I probably have to encode them somehow?
所以,我能有特殊字符,如!
或&
在表名?如果是,那么我可能必须以某种方式对它们进行编码?
Thanks.
谢谢。
回答by deceze
Quote your ambiguous or "special" table names with a back tick:
用反勾号引用含糊不清或“特殊”的表名:
INSERT INTO `e!` ...
Or better, don't use special characters in table names to avoid such problems.
或者更好的是,不要在表名中使用特殊字符以避免此类问题。
回答by Christian
According to the docs, you can't:
根据文档,您不能:
Identifiers are converted to Unicode internally. They may contain these characters:
Permitted characters in unquoted identifiers: ASCII: [0-9,a-z,A-Z$_] (basic Latin letters, digits 0-9, dollar, underscore) Extended: U+0080 .. U+FFFF
Permitted characters in quoted identifiers include the full Unicode Basic Multilingual Plane (BMP), except U+0000: ASCII: U+0001 .. U+007F Extended: U+0080 .. U+FFFF
标识符在内部转换为 Unicode。它们可能包含以下字符:
未加引号的标识符中允许的字符: ASCII:[0-9,az,AZ$_](基本拉丁字母、数字 0-9、美元、下划线)扩展:U+0080 .. U+FFFF
带引号的标识符中允许的字符包括完整的 Unicode 基本多语言平面 (BMP),但 U+0000 除外: ASCII:U+0001 .. U+007F 扩展:U+0080 .. U+FFFF
Source: http://dev.mysql.com/doc/refman/5.5/en/identifiers.html
回答by VibhaJ
Try with this:
试试这个:
CREATE TABLE IF NOT EXISTS `e!` (
`aa` int(11) unsigned NOT NULL auto_increment,
`showName` TEXT NOT NULL default '',
`startDateTime` DATETIME NOT NULL ,
`endDateTime` DATETIME NOT NULL ,
PRIMARY KEY (`aa`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
回答by Your Common Sense
If you have whatever special requirements for the table identifiers, that means there is something wrong with your database architecture and/or with understanding database architecture.
如果您对表标识符有任何特殊要求,那意味着您的数据库架构和/或对数据库架构的理解有问题。
You'd better correct these architectural mistakes instead of enforcing silly identifiers
你最好纠正这些架构错误,而不是强制执行愚蠢的标识符
回答by Tadej
Permitted characters in unquoted identifiers:
ASCII: [0-9,a-z,A-Z$_] (basic Latin letters, digits 0-9, dollar, underscore)
Extended: U+0080 .. U+FFFF
Permitted characters in quoted identifiersinclude the full Unicode Basic Multilingual Plane (BMP), except U+0000:
ASCII: U+0001 .. U+007F
Extended: U+0080 .. U+FFFF
ASCII NUL (U+0000) and supplementary characters (U+10000 and higher) are not permitted in quoted or unquoted identifiers.
Identifiers may begin with a digit but unless quoted may not consist solely of digits.
Database, table, and column names cannot end with space characters.
不带引号的标识符中允许的字符:
ASCII: [0-9,az,AZ$_](基本拉丁字母、数字 0-9、美元、下划线)
扩展:U+0080 .. U+FFFF
带引号的标识符中允许的字符包括完整的 Unicode 基本多语言平面 (BMP),但 U+0000 除外:
ASCII: U+0001 .. U+007F
扩展:U+0080 .. U+FFFF
带引号或不带引号的标识符中不允许使用 ASCII NUL (U+0000) 和补充字符(U+10000 及更高版本)。
标识符可以以数字开头,但除非引用,否则不能仅由数字组成。
数据库、表和列名称不能以空格字符结尾。
Source: https://dev.mysql.com/doc/refman/8.0/en/identifiers.html
来源:https: //dev.mysql.com/doc/refman/8.0/en/identifiers.html
回答by sdjuan
You need back-ticks around the e!
你需要在 e 周围打勾!
Also, your datetime
s need default values that resolve to a datetime
.
此外,您的datetime
s 需要解析为 a 的默认值datetime
。
回答by Valeriy Gorbatikov
This is Object Namesstandards for MySQL. According it you can't use "!" symbol as part of table name.
这是 MySQL 的对象名称标准。根据它你不能使用“!” 符号作为表名的一部分。