Wednesday, 22 April 2015

How to copy data from one table to another table.?

How to copy data  from one table to another table.?

There are multiple ways to do this.

  1. INSERT INTO SELECT : This method is used when table is already created in the database earlier and data is to be inserted into this table from another table. If columns listed in insert clause and select clause are same, they are not required to list them. 
Syntax :
INSERT INTO "table1" ("column1", "column2", ...)
SELECT "column3", "column4", ...
FROM "table2";

  1. SELECT INTO : This method is used when table is not created earlier and needs to be created when data from one table is to be inserted into newly created table from another table. New table is created with same data types as selected columns.
Syntax :
      SELECT column_name(s)
      INTO newtable
      FROM table1;

No comments:

Post a Comment