350

Renaming a table is not working in MySQL

RENAME TABLE group TO member;

The error message is

#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 'group 
        RENAME TO member' at line 1

The query is working fine on other tables for me, but not with the table group.

uselpa
  • 18,732
  • 2
  • 34
  • 52
Anil Olakkal
  • 3,734
  • 2
  • 17
  • 17

17 Answers17

568

group is a keyword (part of GROUP BY) in MySQL, you need to surround it with backticks to show MySQL that you want it interpreted as a table name:

RENAME TABLE `group` TO `member`;

added(see comments)- Those are not single quotes.

barlop
  • 12,887
  • 8
  • 80
  • 109
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • 62
    And yes you need the back tick ` and not the single quote ' I did the single quote from habit, and got the error, but maybe this will save someone else 10 seconds – Paul Feb 03 '14 at 18:05
  • Some keywords, such as NAME are still available without back ticks. I think this availability it is a bad practice, which should be removed one day from MySQL – ad4s Oct 19 '16 at 16:13
  • 3
    Or don't use keywords for tables or columns. – wheeleruniverse Sep 22 '18 at 01:29
  • 2
    DO NOT USE KEYWORDS FOR TABLES OR COLUMNS. – mydoglixu Jan 17 '19 at 11:21
152

Please try

RENAME TABLE  `oldTableName` TO  `newTableName`
Vijay Verma
  • 3,660
  • 2
  • 19
  • 27
  • See [the answer above](https://stackoverflow.com/a/12650388/3397217) for why you should consider using the backticks. @coder – LinusGeffarth Nov 20 '17 at 17:21
42

The MySQL syntax for RENAME TABLE statement is the following:

RENAME TABLE <old_table_name> TO <new_table_name>

In your query, you've used group which is one of the keywords in MySQL. Try to avoid MySQL keywords for names while creating tables, field names and so on.

informatik01
  • 16,038
  • 10
  • 74
  • 104
phponwebsites
  • 651
  • 6
  • 9
28
ALTER TABLE old_table_name RENAME new_table_name;

or

RENAME TABLE old_table_name TO new_table_name;
Neeraj Kumar
  • 6,045
  • 2
  • 31
  • 23
24

Table name change

RENAME TABLE old_table_name TO new_table_name;
A.A Noman
  • 5,244
  • 9
  • 24
  • 46
20

Rename a table in MySQL :

ALTER TABLE current_name RENAME new_name;
Hasib Kamal Chowdhury
  • 2,476
  • 26
  • 28
  • 5
    Welcome to SO. Please read this [how-to-answer](http://stackoverflow.com/help/how-to-answer) for providing quality answer. Just show code is not a good practice. – thewaywewere May 06 '17 at 17:36
14

group - is a reserved word in MySQL, that's why you see such 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 'group 
        RENAME TO member' at line 1

You need to wrap table name into backticks:

RENAME TABLE `group` TO `member`;
Boris
  • 492
  • 4
  • 9
13
ALTER TABLE `group` RENAME `member`

group is keyword so you must have to enclose into group

umar_
  • 491
  • 5
  • 17
12

For Mysql 5.6.18 use the following command

ALTER TABLE `old_table` RENAME TO `new_table`

Also if there is an error saying ".... near RENAME TO ..." try removing the tick `

HA S
  • 1,129
  • 12
  • 10
9
RENAME TABLE tb1 TO tb2;

tb1 - current table name. tb2 - the name you want your table to be called.

Koech
  • 415
  • 6
  • 6
9

According to mysql docs: "to rename TEMPORARY tables, RENAME TABLE does not work. Use ALTER TABLE instead."

So this is the most portable method:

ALTER TABLE `old_name` RENAME `new_name`;
sam-6174
  • 3,104
  • 1
  • 33
  • 34
6

Try any of these

RENAME TABLE `group` TO `member`;

or

ALTER TABLE `group` RENAME `member`;
Mohammedshafeek C S
  • 1,916
  • 2
  • 16
  • 26
5

Rename table
Syntax The syntax to rename a table in MySQL is:

ALTER TABLE table_name
RENAME TO new_table_name;

Example
Let's look at an example that shows how to rename a table in MySQL using the ALTER TABLE statement.
or example:

ALTER TABLE contacts
RENAME TO people;
2

Running The Alter Command

1.Click the SQL tab at the top.

2.In the text box enter the following command: ALTER TABLE exampletable RENAME TO new_table_name;

3.Click the go button.

source : https://my.bluehost.com/hosting/help/2158

curiosity
  • 834
  • 8
  • 20
2

You can use

RENAME TABLE `group` TO `member`;

Use back tick (`) instead of single quote (').

-1

Without giving the database name the table is can't be renamed in my case, I followed the below command to rename the table.

RENAME TABLE current_db.tbl_name TO current_db.tbl_name;
Vishnuvardhan
  • 5,081
  • 1
  • 17
  • 33
-6

Right Click on View > New Query

And Type: EXEC sp_rename 'Table', 'NewName'

Then Click on Run button at the top left corner of the page.

Loai Tayem
  • 61
  • 8