Search code examples
mysqlsqlsyntax-errorddltable-rename

I can't rename a mysql table name... it has a space in it


I've imported a csv into MySQL. PHPMyAdmin helpfully created a table called TABLE 8 (with a space).

When I try to rename in SQL:

RENAME TABLE 8 to gender

I get the error:

#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 '`TABLE 8` to `gender`' at line 1

I have tried back-ticks, quotes... seemingly everything...

I guess it's the space that's causing the problem but I'm out of ideas.


Solution

  • The syntax is wrong, you're missing the table keyword (and yes, note the `s to escape the table name containing a space):

    RENAME TABLE `TABLE 8` TO gender
    

    Alternatively, you could use the fuller syntax:

    ALTER TABLE `TABLE 8` RENAME TO gender