zoukankan      html  css  js  c++  java
  • 【database】复制表数据到相同备份表

    目的及由来,因为数据库表都采取逻辑删除isDeleted=true/flase,但是之前有些报表或者其他的sql并没有在sql中指明此条件。为了不影响之前代码,所以: 

      1、数据库中创建一张相同的表,把删除的记录保存到备份表bak_table,bak_table的所有字段均与原表一致(包括isDeleted)。然后原表做物理删除。

      (以下可能只适用于oracle)

      一、利用oracle的语法 createt table … as ; insert into…

    1、复制表结构以及数据
      create table target_table as select * from source_table;    
      -- 1、target并不会创建索引、默认值、非空等约束条件  (暂时只知道这3个没有)   
      -- 可能需要target_table不存在
    
    2、只复制表结构
      create table target_table as select * from source_table where 0=1;
      -- 条件成立则复制成立的数据,没有成立的数据则只有表
    
    3、复制数据
    (1)、两个表结构一样
        insert into target_table select * from source_table where id = ?;
    
    (2)、两个表的结构不一样,只复制部分列
       insert into target_table (column1,column2,column3) select column1x,column2x,column3x from source_table;

      二、利用工具pl/sql、navicat导出表结构的sql,在修改成对应的target_table的创建语法。

        这样的好处是,有默认值、非空约束、索引。但触发器不一定能创建,而且target_table不一定需要这些。就如前面说的,备份逻辑删除的数据,其实只要一模一样也不会操作,只是后面可能需要关联查到被删除的数据信息而已。

        (但,可能查询速度相对第一种较慢。)

      三、备注

        没有特别去看上面2种的优劣,只是查找怎么做的时候无意在oralce官方文档看到了下面的东西: ORACLE CREATE TABLE

    PARALLEL Example The following statement creates a table using an optimum number of parallel execution servers to scan employees and to populate dept_80:

    CREATE TABLE dept_80
       PARALLEL
       AS SELECT * FROM employees
       WHERE department_id = 80;
    
    

    Using parallelism speeds up the creation of the table, because the database uses parallel execution servers to create the table.

    After the table is created, querying the table is also faster, because the same degree of parallelism is used to access the table.

      (可是还有因为英语不好,在理解上有错误…><!)

    附录:

      oracle复制表数据,复制表结构

      ORACLE Creating Tables

      ORACLE CREATE TABLE

  • 相关阅读:
    hadoop:WordCount问题总结
    .mata. _root_ (转)
    Hbase笔记:批量导入
    Hbase笔记4 java操作Hbase
    wget
    中国大陆开源镜像站汇总
    全键盘操作Windows
    linux下实用命令
    /dev/null和/dev/zero的区别
    Windows xp下安装sql server2005所碰到的一些问题及解决方法
  • 原文地址:https://www.cnblogs.com/VergiLyn/p/6024374.html
Copyright © 2011-2022 走看看