关于enable novalidate 的小实验

关于enable novalidate 的小实验
先创建一个简单的测试表,唯一的约束为主键约束。
create.sql
create table testing01 (id number(5),name varchar2(10),
constraint t_id_pk primary key (id))
/
SQL> @create.sql
Table created.
oracle 会在primary key 或者 unique 约束列上面创建unique索引。
SQL> select index_name,uniqueness,status
  2  from user_indexes
  3  where table_name = 'TESTING01';
INDEX_NAME UNIQUENESS         STATUS
---------- ------------------ ----------------
T_ID_PK    UNIQUE             VALID
使主键约束失效的同时,相当于drop 了对应的索引。
SQL> alter table testing01 disable constraint t_id_pk;
Table altered.
SQL> select index_name,uniqueness,status
  2  from user_indexes
  3  where table_name = 'TESTING01';
no rows selected
插入若干重复测试数据。
SQL> insert into testing01 values(00001,'google');
1 row created.
SQL> insert into testing01 values(00001,'google');
1 row created.
SQL> insert into testing01 values(00001,'google');
1 row created.
SQL> commit;
Commit complete.
尝试enable novalidate 失效的约束。不能启用失效的约束的
原因是不能依照表的定义在相应的列上创建unique 索引,因为
我们已经插入了重复的值。enable 约束的过程其实也是在相应
的列上重建相应的索引。
SQL> alter table testing01 enable novalidate constraint t_id_pk;
alter table testing01 enable novalidate constraint t_id_pk
*
ERROR at line 1:
ORA-02437: 无法验证 (HR.T_ID_PK) - 违反主键
尝试手动创建unique index。
SQL> create unique index t_id_pk  on testing01(id);
create unique index t_id_pk  on testing01(id)
                                *
ERROR at line 1:
ORA-01452: 无法 CREATE UNIQUE INDEX; 找到重复的关键字
手动创建no-unique index。
SQL> create index t_id_pk  on testing01(id);
Index created.
SQL> select index_name,uniqueness,status
  2  from user_indexes
  3  where table_name = 'TESTING01';
INDEX_NAME UNIQUENESS         STATUS
---------- ------------------ ----------------
T_ID_PK    NONUNIQUE          VALID
再次尝试启用,disable 的约束。
SQL> alter table testing01 enable novalidate constraint t_id_pk;
Table altered.
验证下表中现有的数据。
SQL> select * from testing01;
        ID NAME
---------- --------------------
         1 google
         1 google
         1 google

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26110315/viewspace-722820/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/26110315/viewspace-722820/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值