数据库插入数据之select into from与insert into select区别详解

2024-11-24 79阅读 0评论

可能第一次接触select...into...from...和insert into...select...有很多人都会误解, 从表面上看都是把相关信息查询出来,然后添加到一个表里,其实还远远没有这么简单,接下来,小猪就用最普通的表述给大家介绍一下这两者间的区别。

步骤/方法

1、首先,我们来看一下insert into select语句,其语法形式为:insert into table2(field1,field2,...) select value1,value2,... from table1。

 

1、数据库插入数据之select into from与insert into select区别详解 - 奥多云

2、这里的要求就是table2必须已经存在,如果不存在,系统则会提示对象无效。

 

2、数据库插入数据之select into from与insert into select区别详解 - 奥多云

3、同时因为table2已经存在,所以我们就可以任意的向table2中加入符合列类型的内容,当然也包括常量,也许通过下面的图,你就会发现,在导入常量时候的不寻常现象了。

 

3、数据库插入数据之select into from与insert into select区别详解 - 奥多云

4、接下来说一下select into from 语句,该语法形式为:select vale1, value2 into table2 from table1。

 

4、数据库插入数据之select into from与insert into select区别详解 - 奥多云

5、这里要求的是mytable1不存在,因为在插入的时候,系统会自动创建mytable1,如果之前mytable1已经被创建,系统就会提示已经存在表。

 

5、数据库插入数据之select into from与insert into select区别详解 - 奥多云

6、就是因为这个特性,就会给我们带来很多的方便,首先,我如果只想要mytable2表中的id列的话那我只需要将id列select出来,然后加入到新表中即可。

 

6、数据库插入数据之select into from与insert into select区别详解 - 奥多云

7、那如果我想要mytable2中的name列,那我也仅仅是需要把name列select出来,然后添加到新表中即可。

 

7、数据库插入数据之select into from与insert into select区别详解 - 奥多云

end

注意事项

应用范围由实践者自己找到答案~

文章版权声明:除非注明,否则均为奥多云原创文章,转载或复制请以超链接形式并注明出处。

发表评论

快捷回复: 表情:
评论列表 (暂无评论,79人围观)

还没有评论,来说两句吧...

取消
微信二维码
微信二维码
支付宝二维码