Copy a table in SQL

In a relational database, sometimes there is a need to copy a table in SQL. This post talks about several different scenarios on doing this, and how to use SQL to accomplish each scenario.

Copy a table with all data

To copy a table with all the data, simply create a new table and populate the table with SELECT * from the original table. This will copy over the table structure as well as all the data that was in the original table.

The syntax you would use is

CREATE TABLE TABLE_NAME_2
SELECT * FROM TABLE_NAME 1;

Please note that this will not copy over the constraints or indexes associated with this table.

Continue reading