Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Select Into and Insert Into from old table?

What is difference between these in terms of constraints *keys* etc.

Select Into Statement

SELECT column1, column2, someInt, someVarChar 
INTO ItemBack1 
FROM table2
WHERE table2.ID = 7

Insert Into Statement

INSERT INTO table1 ( column1, column2, someInt, someVarChar )
SELECT  table2.column1, table2.column2,
FROM    table2
WHERE   table2.ID = 7

and also

Create table ramm as select * from rammayan

Edit 1:

Database SQL Server 2008

like image 568
Trikaldarshiii Avatar asked May 17 '12 07:05

Trikaldarshiii


People also ask

What is the difference between select into and insert into select?

INSERT INTO SELECT vs SELECT INTO: Both the statements could be used to copy data from one table to another. But INSERT INTO SELECT could be used only if the target table exists whereas SELECT INTO statement could be used even if the target table doesn't exist as it creates the target table if it doesn't exist.

Which is faster select into or insert into?

INTO' creates the destination table, it exclusively owns that table and is quicker compared to the 'INSERT … SELECT'. Because the 'INSERT … SELECT' inserts data into an existing table, it is slower and requires more resources due to the higher number of logical reads and greater transaction log usage.

What is the difference between insert and insert into?

If you are using Insert or Insert into both will insert the data in Table. However Insert into is basically used to fatch the data from another table using select command and insert into table where you want to insert the data.

What is select into existing table?

The INSERT INTO SELECT statement copies data from one table and inserts it into another table. The INSERT INTO SELECT statement requires that the data types in source and target tables match. Note: The existing records in the target table are unaffected.


1 Answers

I'm going to assume MySQL here.

The first two are identical, as the documentation states.

The third statement allows for both table creation and population, though your syntax is wrong up there; look at the right syntax for more info.

Update

It's SQL Server =p

SELECT column1, column2, someInt, someVarChar 
INTO ItemBack1 
FROM table2
WHERE table2.ID = 7

The first statement will automatically create the ItemBack1 table, based on table2.

INSERT INTO table1 ( column1, column2, someInt, someVarChar )
SELECT  table2.column1, table2.column2,
FROM    table2
WHERE   table2.ID = 7

The second second statement requires that table1 already exists.

See also: http://blog.sqlauthority.com/2007/08/15/sql-server-insert-data-from-one-table-to-another-table-insert-into-select-select-into-table/


If there's any difference in constraints, it would be because the second statement depends on what you have already created (and if the table is populated, etc.).

Btw, the third statement is Oracle(tm) and is the same as the first statement.

like image 52
Ja͢ck Avatar answered Sep 18 '22 17:09

Ja͢ck