peasant13337
peasant13337

Reputation: 225

Special Characters in MySQL Table Name

I created a table as follows:

CREATE TABLE IF NOT EXISTS 'e!' (
`aa` int(11) unsigned NOT NULL auto_increment,
`showName` TEXT NOT NULL default '',
`startDateTime` DATETIME NOT NULL default '',
`endDateTime` DATETIME NOT NULL default '',
PRIMARY KEY  (`aa`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8

Then tried to insert with the query:

INSERT INTO e! (showname, startDateTime, endDateTime) VALUES('E! News ', '2012-05-03 19:00:00', '2012-05-03 20:00:00')

And it errors due to the ! in the table name, I'm assuming ! is a special character in mysql. I tried to escape it but the query still failed.

So, can I have special characters like ! or & in the table name? If yes, then I probably have to encode them somehow?

Thanks.

Upvotes: 17

Views: 43754

Answers (7)

ttt
ttt

Reputation: 6809

Permitted characters in unquoted identifiers:

ASCII: [0-9,a-z,A-Z$_] (basic Latin letters, digits 0-9, dollar, underscore)

Extended: U+0080 .. U+FFFF

Permitted characters in quoted identifiers include the full Unicode Basic Multilingual Plane (BMP), except U+0000:

ASCII: U+0001 .. U+007F

Extended: U+0080 .. U+FFFF

ASCII NUL (U+0000) and supplementary characters (U+10000 and higher) are not permitted in quoted or unquoted identifiers.

Identifiers may begin with a digit but unless quoted may not consist solely of digits.

Database, table, and column names cannot end with space characters.

Source: https://dev.mysql.com/doc/refman/8.0/en/identifiers.html

Upvotes: 0

sdjuan
sdjuan

Reputation: 719

You need back-ticks around the e!

Also, your datetimes need default values that resolve to a datetime.

Upvotes: 0

Valeriy Gorbatikov
Valeriy Gorbatikov

Reputation: 3519

This is Object Names standards for MySQL. According it you can't use "!" symbol as part of table name.

Upvotes: -1

Your Common Sense
Your Common Sense

Reputation: 157872

If you have whatever special requirements for the table identifiers, that means there is something wrong with your database architecture and/or with understanding database architecture.

You'd better correct these architectural mistakes instead of enforcing silly identifiers

Upvotes: 2

VibhaJ
VibhaJ

Reputation: 2256

Try with this:

    CREATE TABLE IF NOT EXISTS `e!` (
`aa` int(11) unsigned NOT NULL auto_increment,
`showName` TEXT NOT NULL default '',
`startDateTime` DATETIME NOT NULL ,
`endDateTime` DATETIME NOT NULL ,
PRIMARY KEY  (`aa`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8

Upvotes: 4

deceze
deceze

Reputation: 522081

Quote your ambiguous or "special" table names with a back tick:

INSERT INTO `e!` ...

Or better, don't use special characters in table names to avoid such problems.

Upvotes: 27

Christian
Christian

Reputation: 19740

According to the docs, you can't:

Identifiers are converted to Unicode internally. They may contain these characters:

  • Permitted characters in unquoted identifiers: ASCII: [0-9,a-z,A-Z$_] (basic Latin letters, digits 0-9, dollar, underscore) Extended: U+0080 .. U+FFFF

  • Permitted characters in quoted identifiers include the full Unicode Basic Multilingual Plane (BMP), except U+0000: ASCII: U+0001 .. U+007F Extended: U+0080 .. U+FFFF

Source: http://dev.mysql.com/doc/refman/5.5/en/identifiers.html

Upvotes: 13

Related Questions