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 两种表复制语句区别的更多相关文章

  1. 问题:oracle select into;结果:oracle SELECT INTO 和 INSERT INTO SELECT 两种表复制语句详解

    oracle SELECT INTO 和 INSERT INTO SELECT 两种表复制语句详解 (2011-07-08 08:59:47) 转载▼ 标签: it 分类: oracle 我们经常会遇 ...

  2. select into from和insert into select from两种表复制语句区别

    select into from和insert into select from两种表复制语句都是将源表source_table的记录插入到目标表target_table,但两句又有区别. 第一句(s ...

  3. PostgreSQL SELECT INTO和INSERT INTO SELECT 两种表复制语句

    SELECT INTO和INSERT INTO SELECT两种表复制语句都可以用来复制表与表之间的数据,但是它们之间也有区别. 建表语句: bas_custom_rel表 CREATE TABLE ...

  4. select into 、 insert into select 、create table as select复制表

    Insert是T-sql中常用语句,Insert INTO table(field1,field2,...)  values(value1,value2,...)这种形式的在应用程序开发中必不可少.但 ...

  5. sqlserver不能直接create table as select

    sqlserver不能直接create table as select 在sqlserver 下想复制一张表的,想到oracle下直接create table xxx as select * from ...

  6. INNODB与MyISAM两种表存储引擎区别

    mysql数据库分类为INNODB为MyISAM两种表存储引擎了,两种各有优化在不同类型网站可能选择不同,下面小编为各位介绍mysql更改表引擎INNODB为MyISAM技巧. 常见的mysql表引擎 ...

  7. Hive的两种表

    1.内部表 内部表Load数据有两种方式:① Load data ***:②hdfs dfs -put ****.这是因为在Metastore文件,即mysql的hive数据库的“SDS”表中,保存着 ...

  8. 慎用create table as select,一定要注意默认值的问题

    再做一些数据迁移时候,很多人会使用create table  as select * from table where id=-1的方式来年建立一摸一样的表,但是这样做有个很大的弊端,不能将原表中的d ...

  9. MySQL Innodb的两种表空间方式

    要说表空间,MySQL的表空间管理远远说不上完善.换句话说,事实上MySQL根本没有真正意义上的表空间管理.MySQL的Innodb包含两种表空间文件模式,默认的共享表空间和每个表分离的独立表空间.只 ...

随机推荐

  1. 创建型模式(三) 抽象工厂模式(Abstract Factory)

    一.动机(Motivation) 在软件系统中,经常面临着“一系列相互依赖的对象”的创建工作:同时,由于需求的变化,往往存在更多系列对象的创建工作. 如何应对这种变化?如何绕过常规的对象创建方法(ne ...

  2. Python win32gui调用窗口到最前面

    Python win32gui调用窗口到最前面 0要写一个轮询几个重要页面的程序,不停的在大屏上进行刷新,通过pywin32模块下的SetForegroundWindow函数调用时,会出现error: ...

  3. Linux 安装网络yum地址

    rpm -Uhv http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm   rpm -Uhv http:/ ...

  4. docker 构建自己的image 镜像文件

    docker build 构建自己的镜像文件. 1.在本地工程中运行生成一个springboot的可运行的jar. 因为我习惯用eclipse,所以在eclipse下新建一个springboot的工程 ...

  5. 使用自签CA,Server,client证书和双向认证

    服务端代码 package main import ( "crypto/tls" "crypto/x509" "google.golang.org/g ...

  6. python - 使用psutils

    oshelper.py #encoding=utf-8 import psutil import datetime #查看cpu的信息 print u"CPU 个数 %s"%psu ...

  7. Linux 的磁盘格式化、挂载、磁盘检验、df、du、fdisk、free命令的使用

    df:列出文件系统的整体磁盘使用量du:检查磁盘空间使用量fdisk:用于磁盘分区 free:查看内存占用情况 一.df命令列出系统的整体磁盘使用量 df命令参数功能:检查文件系统的磁盘空间占用情况. ...

  8. CSS渐变色边框,解决border设置渐变后,border-radius无效的问题

    需求:用css设置渐变边框通过border-image来实现渐变色边框 <div class="content"></div> .content { wid ...

  9. sed 替换文件内容

    方法1:sed -i 's/被替换的内容/要替换成的内容/' file 方法2:sed 's/被替换的内容/要替换成的内容/g' file > file.out:mv file.out file ...

  10. std_msgs/String.msg

    from std_msgs.msg import String http://docs.ros.org/api/std_msgs/html/msg/String.html