- 論壇徽章:
- 0
|
這個(gè)問(wèn)題看似矛盾,是因?yàn)檫@個(gè)索引字段是否未主鍵約束或唯一約束,當(dāng)對(duì)某個(gè)字段建主鍵或者唯一約束時(shí),會(huì)自動(dòng)創(chuàng)建一個(gè)索引,這樣將該index改為unusable時(shí),約束還是enable狀態(tài),當(dāng)有數(shù)據(jù)插入時(shí),檢查數(shù)據(jù)完整性時(shí)候,這個(gè)約束會(huì)去使用這個(gè)index,而這個(gè)index是unusable狀態(tài),就會(huì)報(bào)錯(cuò)。具體測(cè)試如下
創(chuàng)建主鍵約束索引,insert失敗
SQL> create table test(id number,name varchar2(10));
Table created.
SQL> alter table test add constraint pk_test primary key(id);
Table altered.
SQL> select index_name,status from user_indexes;
INDEX_NAME STATUS
------------------------------ --------
PK_TEST VALID
SQL> select index_name,status from user_indexes;
INDEX_NAME STATUS
------------------------------ --------
PK_TEST VALID
SQL> insert into test values (1,'sdfs');
1 row created.
SQL> commit;
Commit complete.
SQL> alter index PK_TEST unusable;
Index altered.
SQL> select index_name,status from user_indexes;
INDEX_NAME STATUS
------------------------------ --------
PK_TEST UNUSABLE
SQL> insert into test values (2,'asdf');
insert into test values (2,'asdf')
*
ERROR at line 1:
ORA-01502: index 'PRODUCT.PK_TEST' or partition of such index is in unusable state
創(chuàng)建不帶約束的索引,insert成功
SQL> drop table test purge;
Table dropped.
SQL> create table test(id number,name varchar2(10));
Table created.
SQL> create index pk_test on test(id);
Index created.
SQL> select index_name,status from user_indexes;
INDEX_NAME STATUS
------------------------------ --------
PK_TEST VALID
SQL> insert into test values (2,'asdf');
1 row created.
SQL> alter index PK_TEST unusable;
Index altered.
SQL> select index_name,status from user_indexes;
INDEX_NAME STATUS
------------------------------ --------
PK_TEST UNUSABLE
SQL> insert into test values (3,'dddffd');
1 row created
創(chuàng)建唯一約束索引,insert失敗,這個(gè)時(shí)候,可以看一下約束的狀態(tài),是enable
SQL> create table test(id number,name varchar2(10));
Table created.
SQL> alter table test add constraint pk_test unique (id);
Table altered.
SQL> select index_name,status from user_indexes;
INDEX_NAME STATUS
------------------------------ --------
PK_TEST VALID
SQL> insert into test values (1,'sdfs');
1 row created.
SQL> commit;
Commit complete.
SQL> alter index PK_TEST unusable;
Index altered.
SQL> select index_name,status from user_indexes;
INDEX_NAME STATUS
------------------------------ --------
PK_TEST UNUSABLE
SQL> insert into test values (2,'asdf');
insert into test values (2,'asdf')
*
ERROR at line 1:
ORA-01502: index 'PRODUCT.PK_TEST' or partition of such index is in unusable
state
select CONSTRAINT_NAME,CONSTRAINT_TYPE,TABLE_NAME,STATUS from user_constraints;
CONSTRAINT_NAME C TABLE_NAME STATUS
------------------------------ - ------------------------------ --------
PK_TEST U TEST ENABLED
創(chuàng)建唯一約束索引,insert時(shí),將約束關(guān)閉,insert成功
SQL> drop table test purge;
Table dropped.
SQL> create table test(id number,name varchar2(10));
Table created.
SQL> alter table test add constraint pk_test unique (id);
Table altered.
SQL> alter index PK_TEST unusable;
Index altered.
SQL> insert into test values (2,'asdf');
insert into test values (2,'asdf')
*
ERROR at line 1:
ORA-01502: index 'PRODUCT.PK_TEST' or partition of such index is in unusable
state
SQL> alter table test disable constraint pk_test;
Table altered.
SQL> insert into test values (2,'asdf');
1 row created.
SQL> select CONSTRAINT_NAME,CONSTRAINT_TYPE,TABLE_NAME,STATUS from user_constraints;
CONSTRAINT_NAME C TABLE_NAME STATUS
------------------------------ - ------------------------------ --------
PK_TEST U TEST DISABLED |
|