Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create SQL table with the data from another table

Tags:

sql

How do I create a table using data which is already present in another table (copy of table)?

like image 347
Renuka Avatar asked Aug 01 '10 16:08

Renuka


People also ask

How do I get data from one table to another in SQL Server?

Click the tab for the table with the columns you want to copy and select those columns. From the Edit menu, click Copy. Click the tab for the table into which you want to copy the columns. Select the column you want to follow the inserted columns and, from the Edit menu, click Paste.

How can you create a new table with existing data from another table?

A copy of an existing table can be created using a combination of the CREATE TABLE statement and the SELECT statement. The new table has the same column definitions. All columns or specific columns can be selected.

Can we create a table from another table in SQL?

A copy of an existing table can also be created using CREATE TABLE . The new table gets the same column definitions. All columns or specific columns can be selected. If you create a new table using an existing table, the new table will be filled with the existing values from the old table.


1 Answers

The most portable means of copying a table is to:

  1. Create the new table with a CREATE TABLE statement
  2. Use INSERT based on a SELECT from the old table:

    INSERT INTO new_table
    SELECT * FROM old_table
    

In SQL Server, I'd use the INTO syntax:

SELECT *
    INTO new_table
  FROM old_table

...because in SQL Server, the INTO clause creates a table that doesn't already exist.

like image 194
OMG Ponies Avatar answered Oct 18 '22 00:10

OMG Ponies