oracle中复制表和数据 && 多表插入语句

创建测试表和测试数据

create table test  (id number,name varchar(10));
  insert into  test values(1,‘liufang‘);
  insert into  test values(2,‘xiaozhang‘);
  insert into  test values(3,‘dawei‘);
  insert into  test values(4,‘laotan‘);
  insert into  test values(5,‘laotan‘);
  insert into  test values(6,‘laotan‘);
  insert into  test values(7,‘dawei‘);

1、复制表和表中的数据,我们可以通过下面的语句来实现

(注:复制表不包含默认值和约束等信息,使用下面方法复制表和数据后需要重新见默认值及索引、约束信息等)

create table test2 as select * from test;

也可以待条件去复制

create table test3 as select * from test where name=‘laotan‘

还可以先去定义表,然后再添加数据

create table test2 as select * from test where 2=1;

insert into test2 select * from test;

2、多表插入语句

先指定复制两个测试表(指定列复制)

create table emp1 as select sequen,ename,sal from emp where 1=2;
create table emp2 as select sequen,ename,cid from emp where 1=2;

下面我们使用四种多表插入语句

a、 insert all 无条件插入
insert all
  into emp1(sequen,ename,sal) values (sequen,ename,sal)
    into emp2(sequen,ename,cid) values (sequen,ename,cid)
    select sequen,ename,sal,cid from emp ;

insert all 有条件插入:上面是没有加条件的,同时向表中插入数据,而且两个表的数据条数也一致,下面是加上不同的条件插入

insert all
    when sal>=2000 then
  into emp1(sequen,ename,sal) values (sequen,ename,sal)
    when cid in(1,2) then
    into emp2(sequen,ename,cid) values (sequen,ename,cid)
    select sequen,ename,sal,cid from emp ;

b、insert first插入(前面根据条件插入后是有相同的数据插入的,如果不想两个表中数据有重复相同的可以使用insert first)
insert first
    when sal>=2000 then
  into emp1(sequen,ename,sal) values (sequen,ename,sal)
    when cid in(1,2) then
    into emp2(sequen,ename,cid) values (sequen,ename,cid)
    select sequen,ename,sal,cid from emp ;

insert first 语句中,当地一个表符合条件后,的二个表就不在插入对饮的行,表emp1中不会出现和表emp2相同的数据

这就是两种插入方式的不同之处

时间: 2024-07-29 12:27:15

oracle中复制表和数据 && 多表插入语句的相关文章

Oracle中复制表结构和表数据

Oracle中复制表结构和表数据 1. 复制表结构及其数据(目标表不存在): create table table_name_new as select * from table_name_old; 2. 只复制表结构(目标表不存在,加入一个永远不可能成立的条件.): create table table_name_new as select * from table_name_old where 1=2; 3. 只复制表数据(目标表已经存在): 如果两个表结构一样: insert into t

Oracle多表插入语句Insert All/Insert First

关于INSERT ALL和INSERT FIRST 一.无条件 INSERT ALL 二.条件 INSERT ALL 三.条件 INSERT FIRST Insert-Select 使用Insert Select实现同时向多个表插入记录 一.无条件 INSERT ALL --------------------------------------------------------------------------------------------- INSERT ALL insert_in

多表插入语句

多表插入语句分为以下四种: ①无条件INSERT. ②有条件INSERT ALL. ③转置INSERT. ④有条件INSERT FIRST. 首先创建测试用表: CREATE TABLE emp( empno NUMBER(4), ename VARCHAR2(10), job VARCHAR2(9), deptno NUMBER(2) ); INSERT INTO emp VALUES(7500, 'A', 'SALESMAN', 10); INSERT INTO emp VALUES(750

如何在Oracle中复制表结构和表数据

1. 复制表结构及其数据: create table table_name_new as select * from table_name_old 2. 只复制表结构: create table table_name_new as select * from table_name_old where 1=2; 或者: create table table_name_new like table_name_old 3. 只复制表数据: 如果两个表结构一样: insert into table_na

如何在Oracle中复制表结构和表数据 【转载】

1. 复制表结构及其数据: create table table_name_new as select * from table_name_old 2. 只复制表结构: create table table_name_new as select * from table_name_old where 1=2; 或者: create table table_name_new like table_name_old 3. 只复制表数据: 如果两个表结构一样: insert into table_na

【转】如何在Oracle中复制表结构和表数据

特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/mao2080/ 1. 复制表结构及其数据 create table table_name_new as select * from table_name_old 2. 只复制表结构 create table table_name_new as select * from table_name_ol

最新省市区数据,sql插入语句

--省数据 insert into Province (ProvinceName)  values('北京市'); insert into Province (ProvinceName)  values('天津市'); insert into Province (ProvinceName)  values('上海市'); insert into Province (ProvinceName)  values('重庆市'); insert into Province (ProvinceName) 

SQL 基础之子查询、多表插入、merge 语句、跟踪一段时间数据变化(二十)

使用子查询处理数据 可以使用子查询中的数据操纵语言(DML)语句: 使用内嵌视图检索数据 从一张表向另一张表复制数据 基于另一张表的值更新表中数据 基于另一张表的值删除表中的行 使用子查询作为数据源检索数据 select department_name, city from departments natural join (select l.location_id, l.city, l.country_id from loc l join countries c on(l.country_id

六、数据透视表

一.数据透视表 1.创建数据透视表 插入--数据透视表,选择确定.如果文件格式是[xls],那么创建的透视表是老版的(跟Excel2003的一样),文件格式是[xlsx],那么创建的透视表是新版的. 把新版设置成老版的透视表:右键透视表--数据透视表选项--显示--经典数据透视表格局 数据透视表有四个区域:报表筛选.列标签.行标签.数值 2.更改数据透视表汇总方式 双击[求和项:??],进行[值字段设置],更改汇总方式 在行标签也可以进行[字段设置],能更改[分类汇总] 3.汇总多列数据 汇总多