mysql用点创建数据库

时间:2017-03-13 16:09:21

标签: mysql database shell command

我想使用以下语法

创建数据库
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 pro这样的SQL编辑器或使用mysql命令编辑器来做到这一点,但我想这样做。请帮忙。

提前致谢

2 个答案:

答案 0 :(得分:4)

如果您使用mysqladmin实用程序,则可以执行以下操作:

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

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

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

答案 1 :(得分:0)

数据库名称中不能包含点。

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

来自文档:

&#34;数据库,......称为标识符&#34;

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

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