select into from和insert into select

select into from 和 insert into select都是用来复制表,两者的主要区别为:select into from 要求目标表不存在,因为在插入时会自动创建;insert into select  要求目标表存在

insert into select语法:

Insert into Table2(field1,field2,...) select field1,field2,... from Table1

注意:

  • (1)要求目标表Table2必须存在,并且字段field,field2...也必须存在
  • (2)注意Table2的主键约束,如果Table2有主键而且不为空,则 field1, field2...中必须包括主键
  • (3)注意语法,不要加values,和插入一条数据的sql混了,不要写成:Insert into Table2(field1,field2,...) values (select value1,value2,... from Table1)
  • (4)由于目标表Table2已经存在,所以我们除了插入源表Table1的字段外,还可以插入常量。

select into from语法:

SELECT field1, field2 into Table2 from Table1

注意:

复制出来的table只是复制了数据,表的属性设置是没有复制的,例如主键、索引等

转自:http://www.studyofnet.com/news/182.html

原文地址:https://www.cnblogs.com/tjtest/p/7678484.html