MySQL MySQL是否允许使用点创建数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/776123/
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
Does MySQL allows to create database with dot?
提问by GrZeCh
Does MySQL allows to create database which has dot (.
) in its name?
MySQL 是否允许创建.
名称中带有点 ( ) 的数据库?
I'm using MySQL 5.1.22.
我正在使用 MySQL 5.1.22。
回答by nickf
You can't use the dot in a database name. Also, I'd avoid using it in any identifier. A common convention is to use underscore instead. It will serve the same purpose and will avoid a LOT of confusion. If you dohave a good reason for using strange and otherwise-illegal characters in a table or field name, then you have to escape it.
不能在数据库名称中使用点。另外,我会避免在任何标识符中使用它。一个常见的约定是使用下划线代替。它将用于相同的目的,并会避免很多混淆。如果您确实有充分的理由在表或字段名称中使用奇怪的或非法的字符,那么您必须对其进行转义。
to escape identifiers in MySQL, use the backtick:
要在 MySQL 中转义标识符,请使用反引号:
SELECT `select`, `some.field name`, `crazy()naming+here`
FROM `my-=+table`
Getting into the habit of backticking all field names regardless of whether you needto is a good practice in my opinion, but that's another story.
在我看来,养成无论是否需要都对所有字段名称进行反引号的习惯是一种很好的做法,但那是另一回事了。
回答by Dave Webb
You can use .
in names from MySQL 5.1.6 according to the documentation.
根据文档,您可以使用.
MySQL 5.1.6 中的名称。
However, as has been said and will said again, pleasedon't do it. For every problem you think you're solving now you'll be creating five which will bite you later on. Since . is used to qualify names - e.g. database.table
or table.column
you'll have to quote your database name every time you use it.*
但是,正如已经说过并将再次说过的那样,请不要这样做。对于您认为现在正在解决的每个问题,您将创建五个稍后会困扰您的问题。自从 。用于限定名称 - 例如,database.table
或者table.column
您每次使用时都必须引用您的数据库名称。*
You can do this with backticks:
你可以用反引号做到这一点:
CREATE TABLE `do.not.do.this` (col INT);
or using double quotes if you set the following option:
或者如果您设置以下选项,则使用双引号:
SET sql_mode='ANSI_QUOTES';
CREATE TABLE "asking.for.problems" (col INT);
* Not strictly true - you have to quote any character that's not alphanumeric or _
or $
, but . is a particularly troublesome option to have in your names.
* 并非严格正确 - 您必须引用任何不是字母数字或_
或的字符$
,而是 。是一个特别麻烦的选项。
回答by Stefan Gehrig
Before MySQL 5.1.6, database and table names cannot contain /
, \
, .
, or characters that are not allowed in file names (see 8.2. Schema Object Names).
In versions after 5.1.6 you have to quote your tablename with a backtick (`
) - but as others also advised: you shouldn't do this to prevent any unnecessary trouble.
在 MySQL 5.1.6 之前,数据库和表名不能包含/
, \
, .
, 或文件名中不允许的字符(请参阅8.2. Schema Object Names)。在 5.1.6 之后的版本中,您必须用反引号 ( `
)引用您的表名- 但正如其他人所建议的:您不应该这样做以防止任何不必要的麻烦。
回答by Alnitak
MySQL 5.0.22 doesn't appear to allow it:
MySQL 5.0.22 似乎不允许它:
% mysqladmin -uroot -pXXX create foo.bar
mysqladmin: CREATE DATABASE failed; error: 'Incorrect database name 'foo.bar''
Even it if it did allow it, I would strongly recommend against it.
即使它确实允许它,我也强烈建议反对它。
At the very least you'd have to escape any reference to that database with backticks in every single query that ever uses it.
至少,您必须在使用它的每个查询中使用反引号转义对该数据库的任何引用。