Duplicate a mySQL table with one line of SQL code

CREATE TABLE copyname SELECT * FROM originalname

Too easy!

I'm using this to make a backup of an existing table before running a scheduled data import routine (overwriting the backup_table each time by making a new copy of the original) :



<cfquery datasource="#request.dsn#" name="dupTable">
DROP TABLE backup_table
</cfquery>
<cfquery datasource="#request.dsn#" name="dupTable">
CREATE TABLE backup_table SELECT * FROM data_table
</cfquery>

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Good find!

It's just as easy to do it in SQL Server, too.

<cfquery datasource="#request.dsn#" name="dupTable">
SELECT * INTO backup_table FROM data_table
</cfquery>

If backup_table doesn't already exist, it automatically creates it.
# Author Eric Cobb | 5/20/09 9:42 AM
@Eric - thanks for that tip. I love simple.
# Author Michael Evangelista | 5/20/09 1:33 PM
blogcfc 5.9.1.002 by raymond camden
contact michael evangelista