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

Zend_Dojo Dijits and Numeric ID’s
Happy New Year from Edmonds Commerce
Easy Security for PHP Scripts
Secure and Insecure Contents in HTTPS

Most Popular Posts

PHP Email Attachment Function Freelance osCommerce UK Ultimate osCommerce Checkout - Fast and Friendly PHP : Dead Easy Excel Export

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

Feed | Respond | Trackback

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

Leave a Reply

  • RSS Feed
  • Categories

    • apache
    • barcode
    • business
    • creloaded
    • curl
    • customer services
    • debugging
    • ecommerce
    • edmondscommerce
    • email
    • excel
    • firefox
    • flash
    • gd
    • graphs
    • hosting
    • icecat
    • internet news
    • javascript
    • link building
    • linux
    • magento
    • management
    • mod_rewrite
    • mysql
    • oscommerce
    • php
    • plesk
    • product catalogue
    • product feed
    • programming
    • regular expressions
    • scraping
    • search engine optimisation
    • security
    • seo
    • spidering
    • ubuntu
    • Uncategorized
    • vps
    • web design
    • web development
    • Windows
    • xampp
    • zip
  • Archives

    • 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

    addons advanced adverts blackhat blocking css development directories find firefox google hosts file html javascript keywords links msn mysql myths operators oscommerce paid links paid placement performance php ppc reciprocal linking replace screen scraping search engine optimisation security seo serp speed spider spidering tuning user friendly vista web web design web developer
  • Random Posts

    • Internet Explorer Security Flaw
    • PHPNW08 - PHP Conference Up North!!
    • PHP and Zip Files
    • Product Feed Integration and Scraping Products from Supplier Web Sites
    • 10 SEO Myths
    • CRELoaded Remove Google Ads -
    • Running Internet Explorer under Ubuntu Linux
    • Faster File Browsing with Tabs for Ubuntu
    • Advanced PHP Debug Function
    • Securing a LAMP Server with mod_security

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

Freelance PHP Web Design UK Commercial Web Design