Easily Duplicate, Copy or Backup Tables in Oracle, PostgreSQL, DB2 and SQLite with Create Table As SQL

In Oracle, PostgreSQL, DB2 and SQLite database system, there is a nice command feature called Create Table As which allows easy duplicating of a table with data from another or a few other tables. The SQL command can also be used to ‘copy and paste’ a table exactly or backup a table data before performing data manipulation query on the original table, just in case the script has error and the backup date can be used to restore to original state. Create Table As will creates a new table built from the content of dataset or result set retrieved by a Select SQL query from a table that already exists within the database.

The syntax of Create Table As SQL statement is:

CREATE TABLE table_name [ ( column_name [, ...] ) ]
AS select [ * ! ( column_name [, ...] ) ] FROM source_table_name

Replace table_name with the name of the new table that will be created. Column name is optional, where you can specify multiple columns by including their names in a comma-delimited list. Else, the structure of the new table will be based on the column names, types and number of columns returned by the Select statement, together with the row data. If you specify the column name, note that there should be the same number of columns specified as are returned by select.

The select statement at the end of create table as command must be valid, and has the number of targets selected matching the number of columns in the optional column list preceding the AS clause. It can be a complex select statement that retrieve data from multiple tables. If optional column list is specified within parentheses, asterisk (*) can no longer be used in the select statement.

For example,

CREATE TABLE demo_backup
AS SELECT * FROM demo;

Above SQL statement will create a exact replica backup table named demo_backup with data and structure (columns) of demo table.

Possible error if you specify the optional column list is:

ERROR: CREATE TABLE/AS SELECT has mismatched column count

If you encounter this error message, this is due to optional list of columns in parentheses contains a different number of rows than the select statement returns. Double check if the number of columns specified is the same with the results that are expected from the select resultset.

Share and contribute or get technical support and help at My Digital Life Forums.



2 Responses to “Easily Duplicate, Copy or Backup Tables in Oracle, PostgreSQL, DB2 and SQLite with Create Table As SQL”

  1. Suraj
    August 27th, 2008 22:01
    1

    Awesome dude!!…. helped me bigtime… simple and effective as it could be… Thanks

  2. John Rockefeller
    September 22nd, 2008 22:47
    2

    When doing this with PostgreSQL you will have to recreate the indexes from the original table manually. The copy command will not do this for you. Does anyone know of a way to do this automatically?

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe without commenting


Custom Search

New Articles

Incoming Search Terms for the Article

oracle copy table - copy table oracle - oracle backup table - backup table oracle - postgres copy table - how to copy a oracle table create select - oracle table backup - backup oracle table - postgresql copy table - table backup oracle - oracle copy table structure - backup table in oracle - oracle duplicate table - postgresql duplicate table - sqlite copy table - copy table in oracle - copy postgresql - oracle table copy - sql backup table - sql copy table - oracle create table as - backup tables oracle - oracle copy table data - table backup in oracle - oracle copy tables - backup table sql - sql create backup table - copy table postgresql - create backup table in oracle - Backup a table in Oracle - copy table sql oracle - copy table sql statement - copy table sqlite - oracle table backup script - backup a table oracle - copy table as - backup oracle structure sql - create backup table sql - oracle sql copy table - oracle copy number - taking backup of a table+oracle - copy table to table oracle - copying tables in DB2 - how to backup oracle table - oracle "copy table" - copy structure of table in oracle - oracle select into copy table - postgres duplicate table - backup of table in oracle - copy data from table in oracle sql -