Feed Rss



Sep 25 2008

Backing up a mysql table, using SQL.

category: SQL author:

Today i found a nice and simple way of backing up a mysql table without renaming / exporting. The technique i found creates a new table with the same structure and copy across the data for the current table, sounds simple and it is, take a look at the following.

CREATE TABLE [NEW_TABLE] LIKE [OLD_TABLE];
INSERT INTO [NEW_TABLE] SELECT * FROM [OLD_TABLE];

Simply replace the [NEW_TABLE] and [OLD_TABLE] placeholders with your new and current table names. For example:

CREATE TABLE products_backup LIKE products;
INSERT INTO products_backup SELECT * FROM products;

This will create the table and maintain any indexes / keys etc and it's by far the most efficient way I have found.

I hope this helps.

3 Responses to “Backing up a mysql table, using SQL.”

  1. u24 says:

    Nice. Didn't know about LIKE being used for that.

    There's also SHOW CREATE TABLE foo; Which is handy.

    I also wrote some code to "backup" a table to the necessary SQL statements: http://www.puremango.co.uk/cm_db_sql_106.php

    It's not always perfect, but it's proven very useful more than a few times in the past.

    By the way, your captcha sucks ;)

  2. Surjay says:

    Thanks, it's very nice code.

  3. Rengnathan says:

    This code very nice and easy understanding. thanks.

Leave a Reply