Edmonds Commerce Logo
  • home
    • blog
  • ecommerce
    • product catalogue
    • order processing
    • customer services
    • stock control
    • human resources
    • management information
  • development
    • oscommerce
    • php
    • mysql
    • open source
    • performance tuning
  • design
  • marketing
  • contact us
    • pricing

Edmonds Commerce Blog

Freelance PHP Ecommerce and SEO Developer in the UK

Latest Posts

Magento Most Popular Extensions
Google Chrome for Linux Beta
Firefox Address Bar Lag + Solution
Custom Buttons for Firefox

Most Popular Posts

Magento Developer UK Freelance osCommerce UK Magento Training CRE Loaded UK

MySQL Copy Table from One Database to Another

March 3rd, 2008
Read More mysql, programming, web development

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:

PLAIN TEXT
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

Possibly Relevant Posts:

  • no matches

Feed | Respond | Trackback

15 Responses to “MySQL Copy Table from One Database to Another”

  1. Julz Says:
    June 12th, 2008 at 11:42 pm

    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

  2. admin Says:
    June 13th, 2008 at 8:47 am

    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.

  3. tony Says:
    October 2nd, 2008 at 2:31 pm

    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

  4. tony Says:
    October 3rd, 2008 at 7:57 am

    any one? please

  5. rigo Says:
    October 12th, 2008 at 7:56 pm

    Very useful tip, thanks to Julz and Admin

  6. admin Says:
    November 20th, 2008 at 12:04 pm

    Hi

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

    PLAIN TEXT
    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. }

  7. admin Says:
    November 20th, 2008 at 12:05 pm

    note thats using this db_query function

    PLAIN TEXT
    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. }

  8. troy Says:
    January 13th, 2009 at 2:52 pm

    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?

  9. admin Says:
    January 13th, 2009 at 3:38 pm

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

  10. ZIA Says:
    January 19th, 2009 at 7:33 am

    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?.

  11. admin Says:
    January 19th, 2009 at 9:33 am

    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.

  12. tanvi Says:
    January 30th, 2009 at 1:16 pm

    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...

  13. tanvi Says:
    January 31st, 2009 at 5:47 am

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

  14. admin Says:
    January 31st, 2009 at 10:54 am

    are they MyISAM tables?

  15. Steve Childs Says:
    February 2nd, 2009 at 12:47 am

    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.

Leave a Reply

  • RSS Feed
  • Categories

    • adwords
    • apache
    • barcode
    • business
    • creloaded
    • css
    • curl
    • customer services
    • debugging
    • drupal
    • eclipse
    • ecommerce
    • edmondscommerce
    • email
    • excel
    • firefox
    • flash
    • gd
    • git
    • graphs
    • hosting
    • icecat
    • internet news
    • javascript
    • jquery
    • link building
    • linux
    • mac
    • magento
    • management
    • misc
    • mod_rewrite
    • mysql
    • netbeans
    • open suse
    • oscommerce
    • php
    • plesk
    • portfolio
    • product catalogue
    • product feed
    • programming
    • regular expressions
    • ria
    • scraping
    • search engine optimisation
    • security
    • seo
    • spidering
    • symfony
    • twitter
    • ubuntu
    • Uncategorized
    • usability
    • vps
    • web design
    • web development
    • Windows
    • xampp
    • zend framework
    • zip
  • Archives

    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • September 2009
    • August 2009
    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
  • Tags

    bulk course cre loaded creloaded css custom developer development directories drupal error find firefox git google hosts file html jaunty javascript leeds links linux magento mysql netbeans oscommerce performance php plesk ppc problem replace search engine optimisation seo server symfony table training ubuntu uk virtualbox web web design xml zend framework
  • Random Posts

    • MySQL Desc Table
    • CRELoaded Remove Google Ads -
    • Ubuntu Force Quit
    • Who Needs Photoshop? PHP GD Images and Your Online Store
    • Netbeans Backwards and Forwards
    • Zend Framework Easy SQL Info
    • Apache, mod_rewrite and SEO
    • Drupal Developer UK
    • Selenium Web Application Testing Platform
    • Zend Framework UK
  • Recent Comments

    • admin on Magento Backup Error Filesystem.php on line 234 + Solution
    • admin on Magento Leeds
    • Matthew Dolley on Magento Leeds
    • kash on PHP Email Attachment Function
    • Hussein on PHP Save Images Using cURL
  • Category Specific RSS

    • adwords Feed for all posts filed under adwords
    • apache Feed for all posts filed under apache
    • barcode Feed for all posts filed under barcode
    • business Feed for all posts filed under business
    • creloaded Feed for all posts filed under creloaded
    • css Feed for all posts filed under css
    • curl Feed for all posts filed under curl
    • customer services Feed for all posts filed under customer services
    • debugging Feed for all posts filed under debugging
    • drupal Feed for all posts filed under drupal
    • eclipse Feed for all posts filed under eclipse
    • ecommerce Feed for all posts filed under ecommerce
    • edmondscommerce Feed for all posts filed under edmondscommerce
    • email Feed for all posts filed under email
    • excel Feed for all posts filed under excel
    • firefox Feed for all posts filed under firefox
    • flash Feed for all posts filed under flash
    • gd Feed for all posts filed under gd
    • git Feed for all posts filed under git
    • graphs Feed for all posts filed under graphs
    • hosting Feed for all posts filed under hosting
    • icecat Feed for all posts filed under icecat
    • internet news Feed for all posts filed under internet news
    • javascript Feed for all posts filed under javascript
    • jquery Feed for all posts filed under jquery
    • link building Feed for all posts filed under link building
    • linux Feed for all posts filed under linux
    • mac Feed for all posts filed under mac
    • magento Feed for all posts filed under magento
    • management Feed for all posts filed under management
    • misc Feed for all posts filed under misc
    • mod_rewrite Feed for all posts filed under mod_rewrite
    • mysql Feed for all posts filed under mysql
    • netbeans Feed for all posts filed under netbeans
    • open suse Feed for all posts filed under open suse
    • oscommerce Feed for all posts filed under oscommerce
    • php Feed for all posts filed under php
    • plesk Feed for all posts filed under plesk
    • portfolio Feed for all posts filed under portfolio
    • product catalogue Feed for all posts filed under product catalogue
    • product feed Feed for all posts filed under product feed
    • programming Feed for all posts filed under programming
    • regular expressions Feed for all posts filed under regular expressions
    • ria Feed for all posts filed under ria
    • scraping Feed for all posts filed under scraping
    • search engine optimisation Feed for all posts filed under search engine optimisation
    • security Feed for all posts filed under security
    • seo Feed for all posts filed under seo
    • spidering Feed for all posts filed under spidering
    • symfony Feed for all posts filed under symfony
    • twitter Feed for all posts filed under twitter
    • ubuntu Feed for all posts filed under ubuntu
    • Uncategorized Feed for all posts filed under Uncategorized
    • usability Feed for all posts filed under usability
    • vps Feed for all posts filed under vps
    • web design Feed for all posts filed under web design
    • web development Feed for all posts filed under web development
    • Windows Feed for all posts filed under Windows
    • xampp Feed for all posts filed under xampp
    • zend framework Feed for all posts filed under zend framework
    • zip Feed for all posts filed under zip

Edmonds Commerce related questions? Send us a message or call us on 0844 357 0201.

Freelance PHP Web Design UK Commercial Web Design