Create table from another table

In order to create a table from another table with same schema, you can use the following command:

CREATE TABLE tablename_backup AS SELECT * FROM tablename;

If only certain columns of the original table are to be created as part of the new table, you can select column names within the create and select statement as follows:

CREATE TABLE tablename_backup(COL1, COL2, COL3) AS SELECT(COL1, COL2, COL3) FROM tablename;

If just the table schema of the tablename is required in the new backup table, use a select query run on tablename such that it returns no rows, e.g.

CREATE TABLE tablename_backup AS SELECT * FROM tablename WHERE id < 0;
Advertisements
Posted in SQL

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s