views:

1707

answers:

4

Hello,

does MySQL allows to create database which has dot (".") in its name? I'm using MySQl 5.1.22.

Thanks

+3  A: 

MySQL 5.0.22 doesn't appear to allow it:

% 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.

Alnitak
Yes, don't, even if you can. Same goes for table names with whitespace or unicode or case-sensitivity. You are just asking for trouble that way.
Thilo
+7  A: 

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 do have 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:

SELECT `select`, `some.field name`, `crazy()naming+here`
FROM `my-=+table`

Getting into the habit of backticking all field names regardless of whether you need to is a good practice in my opinion, but that's another story.

nickf
I disagree strongly about back ticking being good practice. MySQL specific. Don't do it unless you have to. The column names should (imho) be pure ^[a-z0-9_]+$, and not clash with reserved words. The postgres equivalent is "Weird Column Name" (which preserves case, spaces, allows reserved words etc).
+4  A: 

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.

Stefan Gehrig
+4  A: 

You can use "." in names from MySQL 5.1.6 according to the documentation.

However, as has been said and will said again, please don'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.*

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.

Dave Webb