Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we rename the database name in MySQL 5.0

Tags:

mysql

I am using MySQL 5.0.

I have created a database named accounts, but now I want to change the database name to FinanceAccounts.

How can I change the database name in MySQL 5.0?

like image 570
sivakumar Avatar asked Mar 27 '09 11:03

sivakumar


People also ask

How can I rename database?

In Object Explorer, expand Databases, right-click the database to rename, and then select Rename. If the database was your default database, see Reset your default database after rename. Refresh the database list in Object Explorer.

What is rename in MySQL?

tbl_name TO other_db. tbl_name; Using this method to move all tables from one database to a different one in effect renames the database (an operation for which MySQL has no single statement), except that the original database continues to exist, albeit with no tables. Like RENAME TABLE , ALTER TABLE ...


2 Answers

The best way is probably to rename each of the tables inside the database to the new name. For example:

Update: There are two steps here

  1. Create a new blank database as you want say "new accounts"

    CREATE DATABASE newaccounts;

  2. Migrate each table one-by-one

    RENAME TABLE accounts.tablename TO newaccounts.tablename;

See http://dev.mysql.com/doc/refman/5.0/en/rename-table.html for more information.

like image 155
brian-brazil Avatar answered Oct 10 '22 20:10

brian-brazil


I think there is only one way (besides renaming directory in the MySQL datadir which will fail for InnoDB tables):

  • create new database (with new name)
  • make dump of old database
  • import dumped data into new database
  • delete old database

To create the new DB:

mysql> CREATE DATABASE new_database;

To create the dump of the old DB:

mysqldump -u "your_username" -p --lock-tables old_database > old_database_dump.sql

To import dumped data into the new DB:

mysql -u "your username" -p new_database  < old_database_dump.sql

To delete the old DB:

mysql> DROP DATABASE old_database;

Bear in mind that your permissions on the old DB will need to be deleted as well. See here for more info: Revoke all privileges for all users on a MySQL DB

MySQL 5.1.7 to MySQL 5.1.22 had a RENAME {DATABASE | SCHEMA} db_name TO new_db_name; command but this one has been removed in MySQL 5.1.23 for being too dangerous.

like image 21
Stefan Gehrig Avatar answered Oct 10 '22 20:10

Stefan Gehrig