Bug #1066 Syntax error in MySQL dumps with reserved words
Submitted: 16 Aug 2003 11:35 Modified: 17 Aug 2004 0:45
Reporter: Mathieu De Zutter Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server: Command-line Clients Severity:S3 (Non-critical)
Version:4.0.13 OS:Linux (Linux 2.4.21 debian/unstable)
Assigned to: CPU Architecture:Any

[16 Aug 2003 11:35] Mathieu De Zutter
Description:
When you use a reserved word like ``group'' in a table and you dump it with mysqldump, it won't get correctly `quoted`, resulting in a syntax error.

How to repeat:
shell$ mysql -D test
mysql> CREATE TABLE bug (`group` int);

shell$ mysqldump --add-drop-table test bug > bug.dump
shell$ cat bug.dump
<snip>
DROP TABLE IF EXISTS bug;
CREATE TABLE bug (
  group int(11) default NULL
) TYPE=MyISAM;
<snip>

shell$ cat bug.dump | mysql -D test
ERROR 1064 at line 12: 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 'group int(11) default NULL
) TYPE=MyISAM' at line 2

Suggested fix:
* Check for reserved words while dumping, and `quote` them if necessary
* Always `quote` when dumping
[18 Aug 2003 9:08] Indrek Siitan
Using reserved words as table/column identifiers is generally a bad idea.
If you cannot avoid it, you can use the -Q option in mysqldump to make
it quote all identifiers. Or, alternatively, you can insert the following lines
 into your /etc/my.cnf:

  [mysqldump]
  quote-names
[19 Aug 2003 5:52] Mathieu De Zutter
Will all respect, I do consider it as a bug.
It's not because you only get this through bad (but legal!) practice and you can imagine an easy workaround that this isn't a bug. 
The way to reproduce the bug is completely legal (legal user input) and mysqldump returns an illegal dump (illegal program output).

This reply is rather principal, I don't mind using that -Q option.
[3 Aug 2004 16:59] Jay Thompson
I am a relatively new mysql admin/user and I love this product.

However, I would have to agree with Mattieu and would "strongly" suggest
the mysql developers patch this. I had the same problem with mysqldump and
spent 2 full days searching discussion groups for a solution. There are literally
thousands of entries out there of people having the same problem (with no
solution). Many ended up doing what I did...use phpMyAdmin for database
copies because it works. By the way, the mysql databases are stuff I didn't
write and therefore have little control over how the developers named their
tables/fields. But they are very popular products (mambo, phpMyAdmin).

The decision on the mysql developers part to default --quote-names to "off"
confuses me. When would having it "on" ever cause a problem? Or, asked another
way, why would anyone want it off. If it corrects so many problems so easily,
why not turn it on by default (or leave it "on" permanently and remove it as
an option)?
[3 Aug 2004 20:17] Sergei Golubchik
the option cannot be removed or changed to be on by default, as it will make MySQL to generate completely incompatible syntax, that uses backquotes - sothing that other databases do not support.

On the other hand, it should do "smart" quoting by default - that is, it should quote reserved words even if --quote-names is off, otherwise the dump is unusable. It was fixed moths ago in 4.1 tree.

We'll see if the fix is small and safe enough to be backported to 4.0.
[4 Aug 2004 5:45] Paul DuBois
Identifier quoting is the default now in MySQL 4.1.x.
Changing it in 4.0.x is riskier because that is the
production (stable) series.
[17 Aug 2004 0:45] Brian Aker
Thank you for taking the time to report a problem.  Unfortunately
you are not using a current version of the product your reported a
problem with -- the problem might already be fixed. Please download
a new version from http://www.mysql.com/downloads/

If you are able to reproduce the bug with one of the latest versions,
please change the version on this bug report to the version you
tested and change the status back to "Open".  Again, thank you for
your continued support of MySQL.