Fast data copy with “Create Table Select From” in PL/SQL

Although I work mainly in Java/J2EE, recently I had a requirement in Oracle to take temporary backup of a table and compare the data of original table with this backup table. So I had to copy the data from table A to a temp table TA. First I tried creating temp table normally and copied the data using normal INSERT INTO SELECT query:
INSERT INTO TA SELECT * FROM A
Code language: SQL (Structured Query Language) (sql)
This approach took a lot of time to execute. Then I tried creating same temp table with different syntax. I tried CREATE TABLE SELECT FROM syntax of PL SQL for creating as well as copying the data into temp table:
CREATE TABLE TA AS SELECT * FROM A
Code language: SQL (Structured Query Language) (sql)
The second approach was amazingly fast. The above sql can be generalized to create table as follow:
CREATE TABLE new_table_name [ ( column [, ...] ) ] AS SELECT [ ( column [, ...] ) ] FROM existing table_name
Code language: SQL (Structured Query Language) (sql)
The SELECT statement can be in a complex form where data is been retrieved from several tables. For the new table, column names can be specified by including the column names in a comma-delimited list. Very important point to take note is that there should be the same number of columns specified in the column list preceding the AS clause (optional, if specified) for new table as the number of target columns that are returned by the select statement. If the optional list of columns in parentheses of new table contains different number of rows than the rows the SELECT statement returns, the following error message will be displayed: ERROR: CREATE TABLE/AS SELECT has mismatched column count In its simplest form, CREATE TABLE AS statement will create a complete backup of an existing table including all columns and data simply by using the statement CREATE TABLE new_table_name AS SELECT * FROM existing_table_name. I googled a little to know why exactly the CREATE TABLE SELECT FROM approach is faster then normal INSERT INTO SELECT FROM and came to this blog entry which is saying:
The SELECT INTO statement is very fast, for one reason: the command isn’t logged for backup purposes. More precisely, the command can be inside a transaction and any rollback command will correctly undo its effects. However, the new values aren’t permanently stored in the log file, therefore after this command you can only perform a complete database backup (incremental backup raise errors).

View Comments

  • I have a question.
    [code language="sql"]
    create table tablename as select...
    [/code]
    is not working when function is been used in select statement like
    [code language="sql"]
    create table tablename as (select TO_CHAR(field1,'YYYY-MM-DD') from table2).
    [/code]
    im getting the follwoing error

    ORA-00998: must name this expression with a column alias

    plz let me know if there is any other alrenative create statement accepting the above scenario.
    Kindly help me on this

    • Hi, You need to assign an alias name to TO_CHAR function. For example:
      [code language="sql"]
      create table tablename as (select TO_CHAR(field1,'YYYY-MM-DD') as somedate from table2)
      [/code]

      This will work.

  • Is there a way to use the create table as command and include all the indexes that go with the table? Or must those be created in a separate command?

Recent Posts

  • Java

Java URL Encoder/Decoder Example

Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…

4 years ago
  • General

How to Show Multiple Examples in OpenAPI Spec

Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…

4 years ago
  • General

How to Run Local WordPress using Docker

Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…

4 years ago
  • Java

Create and Validate JWT Token in Java using JJWT

1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…

4 years ago
  • Spring Boot

Spring Boot GraphQL Subscription Realtime API

GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…

4 years ago
  • Spring Boot

Spring Boot DynamoDB Integration Test using Testcontainers

1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…

5 years ago