Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Special Characters in MySQL Table Name

Tags:

sql

mysql

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.

like image 602
peasant13337 Avatar asked May 04 '12 05:05

peasant13337


People also ask

Can MySQL table name have special characters?

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.

Can SQL table name have special characters?

Names can contain only alphanumeric characters and must begin with an alphabetic character or an underscore (_). Database names must begin with an alphabetic character, and cannot begin with an underscore.

What are the rules for naming a table in MySQL?

By default, MySQL encloses column names and table names in quotation marks. Table names can use any character that is allowed in a file name except for a period or a forward slash. Table names must be 32 characters or less because SAS does not truncate a longer name.


3 Answers

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.

like image 189
deceze Avatar answered Oct 16 '22 17:10

deceze


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

like image 12
Christian Avatar answered Oct 16 '22 18:10

Christian


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
like image 4
VibhaJ Avatar answered Oct 16 '22 19:10

VibhaJ