preload
It’s been a while ….
Sep 25

A nice and simple way of backing up a mysql table without renaming / exporting is to create a new table with the same structure and copy across the data for the current table.

This can be done without having to copy and paste the structure of the table.

Simply replace the [NEW_TABLE] and [OLD_TABLE] markers with your new / old table names.

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

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.

I hope this helps.

2 Responses to “how to backup a mysql table”

  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.

Leave a Reply

 
 
Please enter the word above into the box below, the word is not case sensitive.
 
If you have trouble reading the word above then click here to load a new word.
Security word: