databases icon
21st
Oct 09

Duplicate Table MySQL

Posted Wednesday 21st October 2009
Today I needed to duplicate a live table before making alterations. I was needed in duplicating an entire table that retained the same attributes and properties with exactly the same rows and columns of data.

First of all you create a new table that contains an identical structure to the old one this is done with LIKE.

1
CREATE TABLE newtable LIKE oldtable

With the structure setup you simply copy the contents with an INSERT query that selects everything from the old table.

1
INSERT INTO newtable SELECT * FROM 'oldtable'

Very simple and useful to remember. When you’ve become accustomed to not having to this via a query it can be a real headache to forget!

1
2
CREATE TABLE newtable LIKE oldtable;
INSERT INTO newtable SELECT * FROM 'oldtable';

Bookmark or share this page:

SociBook del.icio.us Digg Facebook Google Yahoo Buzz StumbleUpon

Related posts:

  1. SQL Injections: Introduction with PHP/MySQL
  2. MD5 and SHA1 “Decrypting” with PHP / MySQL


MSN Contact: contact [at] danielgibbs.net



No Comments

No comments yet.

TrackBack URL

Leave a comment