Monday, January 19, 2015

SQL: Create a new table by copying data from existing tables

1. You can use the following SQL to make a copy of an existing table.

      CREATE TABLE <new table name>
      AS
      SELECT [* / <columns separated by comma>]
      FROM <existing table name>
      WHERE ........;

For example, you want to make a copy of your FRUIT table so that you can do some testing commands on it, you can use the following SQL.

      CREATE TABLE FRUIT_COPY AS SELECT * FROM FRUIT;

2. If you just want to create a table having the same columns from an existing table but not the       data, you can add a WHERE clause that never returns true.

      CREATE TABLE FRUIT_COPY
      AS SELECT * FROM FRUIT
            WHERE 1=2;

3. Create a table by copying data from multiple existing tables.

      CREATE TABLE <new table name>
      AS
      SELECT <columns separated by comma>
      FROM <existing table names separated by comma>
      WHERE ........;

       Similarly, by using 'WHERE 1=2' as the where clause, you create an empty new table having the columns you selected from the existing tables. If you would like to copy the data later, you can use the SELECT into SQL to copy the data.

      SELECT <columns separated by comma>
      INTO <new table name>
      FROM <existing table names separated by comma>
      WHERE ........;

--------------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

No comments:

Post a Comment