15

How can I rename the MySQL database name using query?

I tried rename database via phpmyadmin and getting the following error,

SQL query:

    RENAME DATABASE test TO test_bkp

MySQL said: 

    #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 'DATABASE test To test_bkp' at line 1
1
  • 3
    The whole point of phpMyAdmin is to offer a GUI, so that you don't have to write such queries yourself Commented Nov 30, 2012 at 10:58

9 Answers 9

18

In phpmyadmin you can just click on your database, then go to the Operations tab which lets you rename it.

Sign up to request clarification or add additional context in comments.

2 Comments

that is also removed in latest XAMPP
No, it depends on MySQL version and PMA version
12

It says here that:

This statement was added in MySQL 5.1.7 but was found to be dangerous and was removed in MySQL 5.1.23. It was intended to enable upgrading pre-5.1 databases to use the encoding implemented in 5.1 for mapping database names to database directory names (see Section 9.2.3, “Mapping of Identifiers to File Names”). However, use of this statement could result in loss of database contents, which is why it was removed. Do not use RENAME DATABASE in earlier versions in which it is present.

You can import all data into a new database then delete the old one.

It is also possible to use the RENAME TABLE statement using same from/to table name but different from/to database. There are a few catches.

CREATE DATABASE `test_bkp`;

RENAME TABLE 
`test`.`table1` TO `test_bkp`.`table1`,
`test`.`table2` TO `test_bkp`.`table2`,
`test`.`table3` TO `test_bkp`.`table3`;

1 Comment

The 5.1 manual is no longer on Oracle's site. (But probably can be found elsewhere.) Feel free to add a comment to bugs.mysql.com/86289 . Or at least click on "Affects me".
2

It's been disabled by the developers. See http://dev.mysql.com/doc/refman/5.1/en/rename-database.html

Adding to this, I'd use a copy (then delete) rather than a rename. Have a look at Cloning a MySQL database on the same MySql instance

1 Comment

I got the error, SQL query: ALTER DATABASE test_bkp UPGRADE DATA DIRECTORY NAME; MySQL said: #1221 - Incorrect usage of ALTER DATABASE UPGRADE DATA DIRECTORY NAME and name
0

You can do this in MySQL < 5.1.23 with

RENAME {DATABASE | SCHEMA} db_name TO new_db_name;

In later versions have a look at

ALTER {DATABASE | SCHEMA} [db_name]

2 Comments

I tried the query, getting error again, SQL query: RENAME { DATABASE | SCHEMA}test TO test_bkp; MySQL said: #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 '{DATABASE | SCHEMA} test TO test_bkp' at line 1
Hi, {DATABASE | SCHEMA} is just a placeholder, use DATABASE.
0

Ok, so if the rename option doesn't show, here a quick way of cloning a database using phpMyAdmin:

  • Open the database you want to copy
  • Click the Operations tab.
  • where it says "Copy database to:" type in the name of the new database. Select "structure and data" to copy everything or "Structure only" Check the box "CREATE DATABASE before copying" to create a new database. Check the box "Add AUTO_INCREMENT value." Click on the Go button to proceed. NB before deleting the old database, make sure that passwords have also copied over.

Comments

0

in wamp server click on databases then choose your database from header menu choose operations make the new name it will rename the database but actually it create a new database copy all the data then drop the old data base

Comments

0
  1. Export current database.
  2. create a new database with a name you want.
  3. import old database file on the new database.

Comments

0

Well, it's possible to rename your database but here's the logic behind the rename option.

1. Xampp creates a new database with the name
2. Xampp copies all table and data in it from the previous database
3. Xampp migrates data to the new database table and drops the previous database

Steps to rename Database

1. Click the Operations option in the nav pane
2. Find the ' Rename database to ' Spot, and input new name in the input field
3. Tick the ' Adjust privileges ' checkbox
4. Click go

I hope this works! :)

Comments

-1

you can export your databse table, create a new one and import table to the new database! also you can rename your data base using phpmyadmin by selecting your database and click on operations tab and rename your database! the last and not recommended way is create a new databse and use this, but you may lost your data!

CREATE DATABASE new_db_name / DROP DATABASE old_db_name

1 Comment

Hamid, did you read that Salman's post. He already mentioned that thought.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.