Our blog

 

MySQL Copy Table from One Database to Another

Sometime in MySQL you want to copy an entire table from one database to a separate database. One example of when you would want to do this is if you are making a backup of a table before you apply some kind of processing to that table. There is nothing worse than ruining a database table and losing valuable information. To make an instant backup is so easy that there really is no excuse not to make backups at important or risky stages.

Here is how to do it:

MySQL:
  1. DROP TABLE IF EXISTS `backup_db.backup_table`;
  2. CREATE TABLE `backup_db.backup_table` SELECT * FROM `live_db.live_table`;

This will delete your existing backup table completely, then will remake it copying all structural information and content from the live_table in the live_db.

Related Blogs
http://www.phpclasses.org/browse/package/4017.html
http://www.sematopia.com/?p=61
http://www.yinfor.com/blog/archives/2008/02/mysql_backup_and_recovery_meth.html

More Reading:

  • no matching posts found..
23 Comments

Julz
June 12th, 2008

Just remember that this won't create the table using the same engine as the existing table and it won't recreate the primary keys or indexes.

To do this you have to use:

CREATE TABLE new_table_name LIKE old_table_name;

then you populate it with the data from the old table with:

INSERT INTO new_table_name SELECT * FROM old_table_name;

If the copied old table has many keys then it may help to speed the INSERT if you turn off the keys using the following before the INSERT:

ALTER TABLE new_table_name DISABLE KEYS;

And then after the INSERT:

ALTER TABLE new_table_name ENABLE KEYS;

These two statements are not supported by all MySQL Engine types though. InnoDB being one of them in MySQL ver 5.x

Cheers
Julz

 

admin
June 13th, 2008

Nice one Julz

Yes if you want to do more than simply backup the table data and want to actually make a proper fully functional copy of the table then the above is definitely the best way to do so.

 

tony
October 2nd, 2008

hi, i want to update my table2 with only the new data that i have entered from table1. hopefully on a daily basis

how wud i go about this? pls help

tony

 

tony
October 3rd, 2008

any one? please

 

rigo
October 12th, 2008

Very useful tip, thanks to Julz and Admin

 

admin
November 20th, 2008

Hi

I made a little function for this query as per Julz advice

PHP:
  1. function backup_table($table_name, $backup_table_name){
  2.     db_query("CREATE TABLE $backup_table_name LIKE $table_name");
  3.     db_query("ALTER TABLE $backup_table_name DISABLE KEYS");
  4.     db_query("INSERT INTO $backup_table_name SELECT * FROM $table_name");
  5.     db_query("ALTER TABLE $backup_table_name ENABLE KEYS");
  6. }

 

admin
November 20th, 2008

note thats using this db_query function

PHP:
  1. function db_query($query){
  2.     $output = mysql_query($query) or die('<h1 style="color: red;">MySQL Error:</h1>Please copy and paste between the lines and email to YOUREMAIL@ADDRESS.COM<br><br>------------------------------------------------------------------------------------<h3>Query:</h3><pre>' . htmlentities($query) . '</pre><h3>MySQL Error:</h3>' . mysql_error() . '<br><br>------------------------------------------------------------------------------------');
  3.     return $output;
  4. }

 

troy
January 13th, 2009

Hey Guys,

Im running the mysql 5.1.2 & myphpadmin 3.1 and this doesnt seem to work.

it tries to copy the table to the same database. Any help please?

 

admin
January 13th, 2009

can you tell us the exact sql you are pasting into the sql box on phpmyadmin?

 

ZIA
January 19th, 2009

Its so nice. Thaks for your conversations. But i need one more thing. how can i transfer a table data from one machine to another in MySQL?.

 

admin
January 19th, 2009

the most usually way would be to dump the table data to an SQL file (export in phpMyAdmin)

then save this file somewhere, upload it to the other machine and then import it.

there are other ways involving getting the machines talkign to each other directly, but for most purposes the dump method is going to be the easiest and therefore best.

 

tanvi
January 30th, 2009

hai,i tried your code..but it is not working

I tried with this in phpmyadmin:

1st db name :employees1 --->table name:hai
2nd db name:employees --->table name:hai

My code is:
DROP TABLE IF EXISTS `employees1.hai`;
CREATE TABLE `employees1.hai` SELECT * FROM `employees.hai`;

but it shows error as in phpmyadmin.:

MySQL said: Documentation
#1103 - Incorrect table name 'employees1.hai' ..

Please help me as early as possible...

 

tanvi
January 31st, 2009

Please help me as soon as possible...
Its very urgent..

 

admin
January 31st, 2009

are they MyISAM tables?

 

Steve Childs
February 2nd, 2009

Make sure that the user you are running the query as has sufficient rights for both the source and destination databases...

Also make sure you're using the right quotes - you shouldn't need to use quotes at all in those queries you posted, try it without.

The queries as posted here do work, I've just done them on two of my tables. The problem lies in your SQL server somewhere, my guess is one of the above reasons.

 

James Campanella
February 11th, 2010

To tanvi, I don't know if you've solved your problem yet or not, but it appears to me that the problem is in your back tick quotes. If you are specifying a database.table the proper backticks should be `database`.`table` for it to work in phpMyAdmin. It is not a bad idea to use quotes in the event you have a database name or field that ends up being a MySQL keyword or SQL command. Hope this helps.

 

admin
February 11th, 2010

Ah yeah well spotted James :)

 

Macoway Advertising
May 1st, 2010

MysQL vs. Oracle ?

 

Macoway Advertising
May 1st, 2010

Where to find the best possible and secure database solutions for big databases ? any suggestions ?

 

Andrei
July 7th, 2010

Hello,
What about the copy to another server without mysqldump and gzip?

 

syed
August 4th, 2010

How can i migrate a table from a database to another with different field names but field values are same. I also have extra columns to insert in new database table?

Please help!!!

Regards,
syed

 

naresh
August 23rd, 2010

<?php

$t_core_dir = dirname( __FILE__ ).DIRECTORY_SEPARATOR;

require_once( $t_core_dir .'core.php' );
helper_begin_long_process();

$db1 = "ams";//first schema name to be copied
$db2 = "newams";//second schema to be created with same data as db1
$query = "use $db1";
db_query($query );
$query1 = "show tables from $db1";
$result1 = db_query($query1 );
$query2 = "create schema $db2";
db_query($query2 );
$row_count1 = db_num_rows( $result1);
for ( $i=0 ; $i < $row_count1 ; $i++ ) {
$t_col = "Tables_in_$db1";
$row = db_fetch_array( $result1 );
$t_copy_from = $db1.'.'.$row["$t_col"];
$t_copy_to = $db2.'.'.$row["$t_col"];
ECHO '' . $t_copy_from . ' ' . $t_copy_to. '';

copy_db1_to_db2_table($t_copy_from,$t_copy_to);
}

function copy_db1_to_db2_table($table_name, $backup_table_name){

db_query("CREATE TABLE $backup_table_name LIKE $table_name");

db_query("ALTER TABLE $backup_table_name DISABLE KEYS");

db_query("INSERT INTO $backup_table_name SELECT * FROM $table_name");

db_query("ALTER TABLE $backup_table_name ENABLE KEYS");

}

?>

 

naresh
August 23rd, 2010

this is for copying one DB schema to Another DB schema

 

 

Leave a Reply