How to define table name or column name which contains 'SPACE' in MySQL?

| | Comments (0) | TrackBacks (0)

To be honest, as an Oracle DBA, I'm not very familar with some of the MySQL SQL syntax, there have some difference between Oracle and MySQL.

Here, I wanna create a table in MySQL, the table name like "Test Table", and one of the column name like "First Name", when I try to create this table, it returns the following error:

mysql> create table "Test Table2" (id int, "First Name" varchar(200));

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"Test Table2" (id int, "First Name" varchar(200))' at line 1

I created this MySQL database by following the normal procedure.

How to fix the problem?

1. You can use ` (backquote) to include the talbe name and column name, like:
create table `Test Table2` (id int, `First Name` varchar(200));

2. Set global 'sql_mode=ANSI_QUOTES'
mysql> set global sql_mode=ANSI_QUOTES;

Comments: This is more similar with 'alter system set ...' in Oracle, once you set this parameter, it will take effect immediately, it will be valid until you restart your MySQL.

3. Modify the my.cnf configuration file, add the following entry in [mysqld] section, and then restart MySQL to take effect.
sql_mode = ANSI_QUOTES

4. Add the option when you start MySQL
mysqld_safe --user=mysql --sql-mode=ANSI_QUOTES &


I prefer the third solution.

0 TrackBacks

Listed below are links to blogs that reference this entry: How to define table name or column name which contains 'SPACE' in MySQL?.

TrackBack URL for this entry: http://www.isoracle.com/mt/mt-tb.cgi/26

Leave a comment