关于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/
本文通过SQL实验展示了如何禁用并重新启用主键约束,以及在此过程中索引的变化。实验中,首先创建了一个包含主键约束的简单测试表,然后禁用了该约束,接着插入重复数据,并尝试启用已失效的约束,最终手动创建了非唯一索引并验证了操作的效果。
3803

被折叠的 条评论
为什么被折叠?



