oracle数据库【表复制】insert into select from跟create table as select * from 两种表复制语句区别
create table as select * from和insert into select from两种表复制语句区别
create table targer_table as select * from source_table
insert 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 TABLE Table1
(
a varchar(10) PRIMARY KEY,
b varchar(10),
c varchar(10)
);
CREATE TABLE Table2
(
a varchar(10) PRIMARY KEY,
c varchar(10),
d int
);
--2.创建测试数据:
Insert into Table1 values ('赵', 'asds', '');
Insert into Table1 values ('钱', 'asds', '');
Insert into Table1 values ('孙', 'asds', '');
Insert into Table1 values ('李', 'asds', null);
查询目标表:
select * from Table2
没有记录;
--3.INSERT INTO SELECT语句复制表数据:
Insert into Table2(a, c, d) select a,c,5 from Table1
--4.显示更新后的结果:
select * from Table2;
A C D
1 赵 90 5
2 钱 100 5
3 孙 80 5
4 李 5
注意:D字段的值全部是常量5;
--5.删除测试表:
drop TABLE Table1
drop TABLE Table2
2、create table as select * from语句
语句形式为:create table targer_table as select * from source_table;
要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2中。示例如下:
--1.创建测试表:
CREATE TABLE Table1
(
a varchar(10) PRIMARY KEY,
b varchar(10),
c varchar(10)
);
--2.创建测试数据:
Insert into Table1 values ('赵', 'asds', '');
Insert into Table1 values ('钱', 'asds', '');
Insert into Table1 values ('孙', 'asds', '');
Insert into Table1 values ('李', 'asds', null);
--3.create table as select * from语句创建表Table2并复制数据:
create table TABLE2 as select * from TABLE1;
--4.显示更新后的结果:
select * from Table2
A B C
1 赵 asds 90
2 钱 asds 100
3 孙 asds 80
4 李 asds
--5.删除测试表:
drop TABLE Table1
drop 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 select from跟create table as select * from 两种表复制语句区别的更多相关文章
- 问题:oracle select into;结果:oracle SELECT INTO 和 INSERT INTO SELECT 两种表复制语句详解
oracle SELECT INTO 和 INSERT INTO SELECT 两种表复制语句详解 (2011-07-08 08:59:47) 转载▼ 标签: it 分类: oracle 我们经常会遇 ...
- select into from和insert into select from两种表复制语句区别
select into from和insert into select from两种表复制语句都是将源表source_table的记录插入到目标表target_table,但两句又有区别. 第一句(s ...
- PostgreSQL SELECT INTO和INSERT INTO SELECT 两种表复制语句
SELECT INTO和INSERT INTO SELECT两种表复制语句都可以用来复制表与表之间的数据,但是它们之间也有区别. 建表语句: bas_custom_rel表 CREATE TABLE ...
- select into 、 insert into select 、create table as select复制表
Insert是T-sql中常用语句,Insert INTO table(field1,field2,...) values(value1,value2,...)这种形式的在应用程序开发中必不可少.但 ...
- sqlserver不能直接create table as select
sqlserver不能直接create table as select 在sqlserver 下想复制一张表的,想到oracle下直接create table xxx as select * from ...
- INNODB与MyISAM两种表存储引擎区别
mysql数据库分类为INNODB为MyISAM两种表存储引擎了,两种各有优化在不同类型网站可能选择不同,下面小编为各位介绍mysql更改表引擎INNODB为MyISAM技巧. 常见的mysql表引擎 ...
- Hive的两种表
1.内部表 内部表Load数据有两种方式:① Load data ***:②hdfs dfs -put ****.这是因为在Metastore文件,即mysql的hive数据库的“SDS”表中,保存着 ...
- 慎用create table as select,一定要注意默认值的问题
再做一些数据迁移时候,很多人会使用create table as select * from table where id=-1的方式来年建立一摸一样的表,但是这样做有个很大的弊端,不能将原表中的d ...
- MySQL Innodb的两种表空间方式
要说表空间,MySQL的表空间管理远远说不上完善.换句话说,事实上MySQL根本没有真正意义上的表空间管理.MySQL的Innodb包含两种表空间文件模式,默认的共享表空间和每个表分离的独立表空间.只 ...
随机推荐
- js判断一个数是不是正整数
js判断一个数是不是正整数 function isPositiveNum(s){//是否为正整数 var re = /^[0-9]*[1-9][0-9]*$/ ; return re.test(s) ...
- HTTPS——https下的静态资源中如何使用http的地址
前言 今天在改博皮的时候,将一些图片上传到七牛,但是引入的时候出问题了,因为七牛cnd设置的不是https域名,所以加载资源的时候导致自动转为https请求. 步骤 错误的写法 background: ...
- 测试工具( Burp Suite)介绍了解篇
Mac 安装 Burp Suite破解版,参考链接: https://www.jianshu.com/p/3224c2308ffa 建议:目前官网的最新版为2.1.4.建议使用1.7.36版本,有破解 ...
- only_full_group_by
ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregate ...
- c字符数组之两头堵模型
char *其实就是char[length]的首元素地址 实验环境:centos7下qt5.11 中文char类型占3个字节 char[length]="特别车队"其实等价于ch ...
- 3种方法实现CSS隐藏滚动条并可以滚动内容
隐藏滚动条的同时还需要支持滚动,我们经常在前端开发中遇到这种情况,最容易想到的是加一个iscroll插件,但其 实现在CSS也可以实现这个功能,我已经在很多地方使用了,下面一起看看这三种方法. 方法1 ...
- [CSS] Create a Card Flip Animation with CSS
Animation can be a powerful way to enhance a user experience. In this lesson, we'll walk through the ...
- git submodule 使用小结
git submodule 使用小结 原文链接 http://blog.gezhiqiang.com/2017/03/08/git-submodule/###### Git Submodule 允许一 ...
- mongoDB sh.status() too many chunks to print
too many chunks to print, use verbose if you want to force print 想要看到详细的信息则需要执行: mongos> sh.statu ...
- Kubernetes 学习6 Pod控制器应用进阶
一.资源配置清单 1.自主式Pod资源 2.资源的清单格式,大多数清单格式都遵循如下条件: a.一级字段:apiVersion(group/version),kind,metadata(name,na ...