Mysql 创建数据库名称中带有特殊字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/925696/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:22:10  来源:igfitidea点击:

Mysql Create Database with special characters in the name

mysqlspecial-characters

提问by MySQL DBA

I want to create a database which name will have special characters in it. for example, (., - , _, @, #, $, %, &, *)

我想创建一个名称将包含特殊字符的数据库。例如, (., - , _, @, #, $, %, &, *)

can anyone provide any output on this?

任何人都可以提供任何输出吗?

回答by Andre Miller

I would strongly recommend that you do not create databases with such names. But if you absolutely must, here are the restrictions:

我强烈建议您不要使用此类名称创建数据库。但如果你绝对必须,这里是限制

  • No identifier can contain ASCII NUL (0x00) or a byte with a value of 255.
  • Database, table, and column names should not end with space characters.
  • Database and table names cannot contain “/”, “\”, “.”, or characters that are not allowed in file names.
  • 任何标识符都不能包含 ASCII NUL (0x00) 或值为 255 的字节。
  • 数据库、表和列名称不应以空格字符结尾。
  • 数据库和表名不能包含“/”、“\”、“.”或文件名中不允许的字符。

To create a database, you can do the following:

要创建数据库,您可以执行以下操作:

mysql> create database `really@strange*database$name`;

回答by soulmerge

Simple: Don't.

很简单:不要。

You canescape exotic table names using the backtick in mysql, but I don't know if you can use anythinginside the backticks. It will give great amounts of pain during the rest of your software life cycle.

可以使用 mysql 中的反引号转义外来表名,但我不知道您是否可以在反引号内使用任何内容。在你的软件生命周期的其余部分,它会给你带来很大的痛苦。

I would rather recommend creating another table to hold that exotic names.

我宁愿建议创建另一个表来保存那些异国情调的名字。

-- Example:
CREATE TABLE _DatabaseMetadata (
    databaseName VARCHAR(255),
    exoticName VARCHAR(255)
) DEFAULT CHARSET=utf8;

回答by kriplozoik

Short answer:

简短的回答:

  • Don't. I strongly recommend to keep all identifiers consisting of A-Z,a-z,0-9 and _ characters. You can store Your "exotic" name in a column or comment.
  • 别。我强烈建议保留由 AZ、az、0-9 和 _ 字符组成的所有标识符。您可以将您的“异国情调”名称存储在列或评论中。

Long answer:

长答案:

  • You can name your columns, tables, keys, foreign keys, views, even databases using exotic characters but chances are You're gonna regret it in the future.
  • If You insist in doing that, You gonna need quoting Your identifiers in backticks (`).
  • In case Your identifier has to contain another ` inside, You can escape it stating it twice (e.g. exotic`name --> `exotic``name`)
  • For the things not to be so simple, if You use exotic (or even non-conventional) characters in the name of Your database (including a simple space), those characters (to my knowledge, everything except a-z,A-Z,0-9 and _) get escaped into 4-digit hexadecimal quadruplets escaped by @, e.g. `my database` becomes my@0020database. This form is used as a name of a directories/files in which Your databases/tables are stored, and e.g. items in information_schema.INNODB_SYS_FOREIGN, moreover may very well be OS-dependent (meaning, theoretically, You might want to run SHOW VARIABLES LIKE 'version_compile_os'to adapt to it). You see - with exotic names it all gets much, much more complicated and in the end it's not really worth it.
  • 您可以使用外来字符命名您的列、表、键、外键、视图,甚至数据库,但您将来可能会后悔。
  • 如果您坚持这样做,您将需要在反引号 (`) 中引用您的标识符。
  • 如果你的标识符必须包含另一个`,你可以将它转义两次(例如exotic`name --> `exotic`name`)
  • 对于事情不那么简单,如果您在数据库名称中使用异国(甚至非常规)字符(包括一个简单的空格),这些字符(据我所知,除了 az,AZ,0-9 之外的所有字符和 _) 转义为由@ 转义的 4 位十六进制四元组,例如,`my database` 变为 my@0020database。这种形式用作存储您的数据库/表的目录/文件的名称,例如 information_schema.INNODB_SYS_FOREIGN 中的项目,而且很可能依赖于操作系统(这意味着,理论上,您可能希望运行SHOW VARIABLES LIKE 'version_compile_os'以适应它)。你看 - 有了异国情调的名字,一切都会变得更加复杂,最终它并不值得。