Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename MySQL database [duplicate]

I created a database with the name of hrms. Now I need to change database name to sunhrm. But, It is disabled in MySQL workbench. Can I do that on the Linux server itself?

like image 337
Dhileepan Avatar asked Aug 30 '12 04:08

Dhileepan


People also ask

How do I rename a SQL database?

If you are using SQL Server, you can set the database to single-user mode to close any open connections and prevent other users from connecting while you are changing the database name. In Object Explorer, expand Databases, right-click the database to rename, and then select Rename.

How do I select duplicates in MySQL?

First, use the GROUP BY clause to group all rows by the target column, which is the column that you want to check duplicate. Then, use the COUNT() function in the HAVING clause to check if any group have more than 1 element. These groups are duplicate.


2 Answers

In case you need to do that from the command line, just copy, adapt & paste this snippet:

mysql -e "CREATE DATABASE \`new_database\`;" for table in `mysql -B -N -e "SHOW TABLES;" old_database` do    mysql -e "RENAME TABLE \`old_database\`.\`$table\` to \`new_database\`.\`$table\`" done mysql -e "DROP DATABASE \`old_database\`;" 
like image 84
jan Avatar answered Sep 19 '22 15:09

jan


I don't think you can do this. Basic answers will work in many cases, and in others cause data corruptions. A strategy needs to be chosen based on heuristic analysis of your database. That is the reason this feature was implemented, and then removed. [doc]

You'll need to dump all object types in that database, create the newly named one and then import the dump. If this is a live system you'll need to take it down. If you cannot, then you will need to setup replication from this database to the new one.

If you want to see the commands that could do this, @satishD has the details, which conveys some of the challenges around which you'll need to build a strategy that matches your target database.

like image 44
New Alexandria Avatar answered Sep 19 '22 15:09

New Alexandria