Rename Database in MySQL

MySQL had supported renaming Database in some earlier versions, but due to some security issues it was removed. So, if you create a Database, you do not have a command to directly rename it. There is a workaround though so that you need not create and populate all the tables to the new database.

Consider you have a database errorDB and you need to rename it to correctDB.
All you have to do is to create a database with the name correctDB and copy the tables in errorDB to correct DB.


create database errorDB;
use errorDB;
create table users (id int,name varchar(50));
insert into users values (1,'Kumar');
select * from users;

Now you have a Database errorDB with a table users and a record 1,Kumar in it.
Create the new Database correctDB and copy the table to the new DB.


create database correctDB;
rename table errorDB.users TO correctDB.users;
use correctDB;
select * from users;


Now you have the Database correctDB with the table users and the record 1,Kumar in it. You can simply drop the schema for errorDB.


Comments

Popular posts from this blog

Hide notification content in Android lock screen for Messages, email, whatsapp and selected apps.

Array functions in JavaScript