Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clone MySQL database under a different name with the same name and the same tables and rows/content using SQL query

Tags:

sql

mysql

I know how to clone tables e.g.:

CREATE TABLE recipes_new LIKE production.recipes; 
INSERT recipes_new 
SELECT * FROM production.recipes;

But I don't know how to clone e.g. a database_old to database_new database with all the tables and rows from database_old.

So, only the name of the database will change. Everything else stays the same.

Right now I am cloning it by exporting the database in phpmyadmin ad then creating a new database and importing it to the new database.

But I guess there must be a more efficient way of doing this task via SQL query like that one for cloning tables.

IMPORTANT! It need to be done from SQL query window in phpmyadmin and not from a shell command line.

Thanks in advance for you suggestion how to do that.

like image 353
Derfder Avatar asked Feb 27 '13 11:02

Derfder


People also ask

How do I copy a table data from one database to another database?

Using SQL Server Management StudioOpen the table with columns you want to copy and the one you want to copy into by right-clicking the tables, and then clicking Design. Click the tab for the table with the columns you want to copy and select those columns. From the Edit menu, click Copy.


3 Answers

have you tried using MySQL Dump?

$ mysqldump yourFirstDatabase -u user -ppassword > yourDatabase.sql
$ echo "create database yourSecondDatabase" | mysql -u user -ppassword
$ mysql yourSecondDatabase -u user -ppassword < yourDatabase.sql
like image 99
Freddie Fabregas Avatar answered Sep 21 '22 16:09

Freddie Fabregas


IMPORTANT! It need to be done from SQL query window in phpmyadmin and not from a shell command line.

First create a blank database:

 CREATE DATABASE `destination` DEFAULT CHARACTER SET 
    latin1 COLLATE latin1_swedish_ci;

Then use the command show tables;

 show source.tables;

and then run the command for each DB table (Optimized Create table and inserting rows) as:

 create table destination.table select * from source.table;

and other way is using like command:

  create table destination.table like source.table

and then inserting rows;

  insert into destination.table select * from source.table
like image 27
Vineet1982 Avatar answered Sep 20 '22 16:09

Vineet1982


If phpMyAdmin is available for the database, you can clone it by following these steps:

  1. Select required database
  2. Click on the operation tab
  3. In the operation tab, go to the "copy database"-option and type your desired clone-db-name into the input field
  4. Select "Structure and data" (Depends on your requirement)
  5. Check the boxes, "CREATE DATABASE before copying" and "Add AUTO_INCREMENT value"
  6. Click "GO"

Tested with phpMyAdmin 4.2.13.3

like image 38
Syed Abdul Qadeer Avatar answered Sep 21 '22 16:09

Syed Abdul Qadeer