mysql 使用点创建数据库

mysql Database creation with dot

我想使用以下语法创建数据库

mysql -u<uname> -p<password> -e "create database <name>"

但是每当我尝试在数据库名称中使用点 (.) 时,它都会给我错误。

[root@aee9a1889c3f wpg.master]# mysql -uroot -proot -e "create database 'a.n'"
Warning: Using a password on the command line interface can be insecure.
ERROR 1064 (42000) at line 1: 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 ''a.n'' at line 1

[root@aee9a1889c3f wpg.master]#

[root@aee9a1889c3f wpg.master]#

[root@aee9a1889c3f wpg.master]# mysql -uroot -proot -e "create database `a.n`"
bash: a.n: command not found
Warning: Using a password on the command line interface can be insecure.
ERROR 1064 (42000) at line 1: 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 '' at line 1

我知道我可以使用 sql 编辑器(如 SQL pro)或使用 mysql 命令编辑器来做到这一点,但我想这样做。请帮忙。

提前致谢

数据库名称中不能有点。

以下文档列出了允许的字符:https://dev.mysql.com/doc/refman/5.7/en/identifiers.html

来自文档:

"database, ... are known as identifiers"


不带引号的标识符中允许的字符:

  • ASCII:[0-9,a-z,A-Z$_](基本拉丁字母、数字 0-9、美元、下划线)
  • 扩展:U+0080 .. U+FFFF

引用标识符中允许的字符包括完整的 Unicode 基本多语言平面 (BMP),U+0000 除外:

  • ASCII: U+0001 .. U+007F
  • 扩展:U+0080 .. U+FFFF

更新:

如文档中所述,可以使用反引号:

create database `a.b`;
Query OK, 1 row affected (0.01 sec)

如果您使用 mysqladmin 实用程序,则可以完成:

mysqladmin -u<uname> -p<password> create a.n

如果你想使用 mysql 客户端,你需要使用单引号(以避免 shell 扩展反引号 (`)

mysql -u<uname> -p<password> 'create database `a.n`'