我是靠谱客的博主 体贴朋友,最近开发中收集的这篇文章主要介绍oracle insert into values select from,oracle数据库【表复制】insert into select from跟create table as select *...,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

create table  as select * from和insert into select from两种表复制语句区别

create table targer_table as select * fromsource_tableinsert into target_table(column1,column2) select column1,column2 from source_table

以上两句都是将源表source_table的记录插入到目标表target_table,但两句又有区别。

第一句(create table  as select * from)要求目标表target_table不存在,因为在插入时会自动创建。

第二句(insert into select from)要求目标表target_table存在,由于目标表已经存在,所以我们除了插入源表source_table的字段外,还可以插入常量,如sql语句:

insert into target_table(column1,column2) select column1,5 from source_table

例中的:5;

无论是create table  as select * from还是insert into select from, from后面的都是源表(source_table);

1、Insert into Select from 语句

语句形式为:Insert into targer_table(field1,field2,...) select value1,value2,... from source_table

要求目标表 targer_table必须存在,由于目标表targer_table已经存在,所以我们除了插入源表source_table的字段外,还可以插入常量。示例如下:

--1.创建测试表:

CREATE TABLETable1

(

avarchar(10) PRIMARY KEY,

bvarchar(10),

cvarchar(10)

);CREATE TABLETable2

(

avarchar(10) PRIMARY KEY,

cvarchar(10),

dint);

--2.创建测试数据:

Insert into Table1 values ('赵', 'asds', '90');Insert into Table1 values ('钱', 'asds', '100');Insert into Table1 values ('孙', 'asds', '80');Insert into Table1 values ('李', 'asds', null);

查询目标表:

select * fromTable2

没有记录;--3.INSERT INTO SELECT语句复制表数据:

Insert into Table2(a, c, d) select a,c,5 from Table1

--4.显示更新后的结果:

select * fromTable2;

A C D1 赵 90 5

2 钱 100 5

3 孙 80 5

4 李 5注意:D字段的值全部是常量5;--5.删除测试表:

drop TABLETable1drop TABLE Table2

2、create table  as select * from语句

语句形式为:create table targer_table as select * from source_table;

要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2中。示例如下:

--1.创建测试表:

CREATE TABLETable1

(

avarchar(10) PRIMARY KEY,

bvarchar(10),

cvarchar(10)

);--2.创建测试数据:

Insert into Table1 values ('赵', 'asds', '90');Insert into Table1 values ('钱', 'asds', '100');Insert into Table1 values ('孙', 'asds', '80');Insert into Table1 values ('李', 'asds', null);--3.create table as select * from语句创建表Table2并复制数据:

create table TABLE2 as select * fromTABLE1;--4.显示更新后的结果:

select * fromTable2

A B C1 赵 asds 90

2 钱 asds 100

3 孙 asds 80

4李 asds--5.删除测试表:

drop TABLETable1drop TABLE Table2

附:

注意:

create table targer_table as select * from source_table是会复制表结构+表数据,

而create table targer_table as select * from source_table where 1=2;只会创建相同的表结构,不会复制表数据。

Create table as select 语句的两点说明

SQL > create table emp_copy as select * from emp where deptno=10;

第一,注意emp_copy表中没有定义任何列名,因为我们在列子句中用通配符从emp表取得数据,让Oracle像emp表中一样生成emp_copy表中的列——相同名称,相同数据类型定义。

第二,SQL*PLUS中可以发出的任何select语句可以放在create table as select 语句中,然后Oracle会自动获得从emp表选择的数据,在进emp_copy表中。但是 如果select语句的列子句中包括特定列清单,则create table子句要列出表中要包括的列,放在括号中,例如:

SQL > create table emp_copy_2 (empno,sal) as select empno, sal from emp where deptno=10;

大家都知道create table a as select * from b可以创建一个与b表结构一样的表,但是在实际应用中最好不要这么创建表。原因是这样只创建表的结构,而不会将原表的默认值一起创建。

说白了,表结构出来了,默认值没有。

另外,但是有一个我对一个大表执行create table a as select * from b时候报了一个temp表空间不足,不知道是什么原因,记录一下。下次发现在处理吧。

最后

以上就是体贴朋友为你收集整理的oracle insert into values select from,oracle数据库【表复制】insert into select from跟create table as select *...的全部内容,希望文章能够帮你解决oracle insert into values select from,oracle数据库【表复制】insert into select from跟create table as select *...所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(73)

评论列表共有 0 条评论

立即
投稿
返回
顶部