【问题标题】:mysql Database creation with dot用点创建mysql数据库
【发布时间】:2017-03-13 16:09:20
【问题描述】:

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

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 命令编辑器来做到这一点,但我想这样做。请帮忙。

提前致谢

【问题讨论】:

    标签: mysql database shell command


    【解决方案1】:

    如果您使用mysqladmin 实用程序,它可以做到:

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

    如果您想使用 mysql 客户端执行此操作,则需要使用单引号(以避免反引号 (`) 的外壳扩展

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

    【讨论】:

    • 是的,我正在寻找。谢谢
    • 我刚刚编辑了我的答案,以展示如何使用 mysql 而不是 mysqladmin 来做到这一点
    【解决方案2】:

    数据库名称中不能有点。

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

    来自文档:

    “数据库,...被称为标识符”


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

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

    【讨论】:

    • ASCII: U+0001 .. U+007F 确实包含 . 符号 (U+002E)。所以它可以用在带引号的标识符中
    • 是的,我们可以尝试从 mysql 命令编辑器 "create database a.b";
    • @user6457056 我更新了答案以显示带有反引号的解决方案
    猜你喜欢
    • 2010-10-21
    • 1970-01-01
    • 2014-12-31
    • 1970-01-01
    • 2019-07-10
    • 2013-08-08
    • 1970-01-01
    • 2010-10-17
    相关资源
    最近更新 更多