Oracle启动和禁用约束及删除违反约束的记录

一、禁用约束

  alter table table_name disable novalidate constraint constraint_name

二、批量导入数据

三、在开启约束之前一定要检查违反约束的记录

  1、执行Oracle中自带的脚本utlexpt1.sql创建exceptions表。该脚本在oracle_home/rdbms/admin目录下

    sql>@oracle_home\rdbms\admin\utlexpt1.sql

  2、执行带有exception是选项的alter语句,将违反约束条件的记录添加到exceptions表中保存

    alter table table_name enable validate constraint constraint_name exceptions into sys.exceptions;

  3、在exceptions表中使用子查询来锁定无效的数据行

    select column1,column2,column3 from table_name where rowid in(

        select row_id from sys.exceptions

    ) for update

  4、根据查询结果修改违反约束的记录行(可以通过rowid来直接update)

  5、重新执行带有alter 的开启约束的语句

    alter table table_name enable validate constraint constraint_name

  6、查询约束的状态是否为enable 、validate

    select constraint_name,constraint_type,status,validated from dba_constraints where owner='OWNER' and table_name='TABLE_NAME';

四、确认exceptions表无用后删除或者truncate表   

  drop table sys.exceptions;

如何将回收站recyclebin中的对像还原?

SQL> flashback table cube_scope to before drop

表名可以是回收站系统的dba_recyclebin.object_name也可以是dba_recyclebin.original_name

但是此时问题来了,我已经用备份的DDL语句重建了一个新的表,这个时候再用此命令还原显然会报错,这个时候怎么办呢,只能还原成一个别名,具体操作命令是

SQL> flashback table cube_scope before drop rename to cube_scope_old

既然恢复了删除前的表中数据,现在只能从cube_scope_old中的数据插入cube_scope中

SQL> insert into cube_scope select * from cube_scope_old t

 

posted @ 2017-06-20 18:26  3002059249  阅读(2005)  评论(0编辑  收藏  举报