Made of Everything You're Not

If you're a stalker I'd prefer if you didn't kill me. Thanks.
  • Home
  • Projects
  • Portfolio
  • Resume

Archive for July, 2009

MySQL Reserved Keywords

Posted in Programming on July 3rd, 2009 by Eric Lamb – 1 Comment

Here’s a problem I’ve encountered too many times and I, still, keep running up against:

1
2
3
#1064 - 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 
'primary , foo , bar ) VALUES ( '1', 'test', 'me' )' at line 2

This on a query that looks like so:

1
$sql = "INSERT INTO table (primary , foo , bar ) VALUES ( '1', 'test', 'me' )";

Can you see the issue (other than it’s using php)? Look closely…

MySQL

MySQL

The query is syntactically sound; there’s nothing wrong with the formatting at all. But it fails with a 1064 error because of the use of ‘primary’ as a column name. Welcome to the world of MySQL reserved keywords.

Certain words such as SELECT, DELETE, or BIGINT are reserved and require special treatment for use as identifiers such as table and column names.

This may also be true for the names of built-in functions.

Reserved words are permitted as identifiers if you quote them as described in Section 8.2, “Schema Object Names”

This means that there are certain words you just can’t use for MySQL identifiers unless you escape them, in all SQL, with backticks (`). Which essentially means, to me at least, you shouldn’t use reserved keywords. As a rule, I just don’t use backticks in my code unless I have to; it’s always begrudgingly though.

As I said above, this isn’t a new issue to me (or most people for that matter) but I continually run up against it. It’s particularly insidious because the natural instinct is to evaluate the SQL looking for the missing comma or quote, which is a HUGE waste of time.

So which keywords are reserved? Thankfully, MySQL has a list. It’s a good idea to familiarize yourself with the terms on the page and just keep the knowledge in the back of your brain.

It will come up again.

Bookmark and Share
Newer Entries »
  • Subscribe: Entries | Comments
  • About Me

    Email Email
    Twitter Twitter
    310.739.3322
  • Categories

    • Brain Dump
    • Business
    • Code
    • IT
    • Programming
    • Rant
    • Servers
  • Archives

    • February 2012
    • October 2011
    • August 2011
    • July 2011
    • June 2011
    • May 2011
    • April 2011
    • March 2011
    • February 2011
    • January 2011
    • December 2010
    • November 2010
    • October 2010
    • September 2010
    • August 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • September 2009
    • August 2009
    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008

Copyright © 2008 - 2012 Eric Lamb - All rights reserved