关于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