刪除主鍵無法刪除對應索引問題 drop constraint

來源:互聯網
上載者:User

刪除主鍵無法刪除對應索引問題 drop constraint

--在刪除一個表主鍵的時候索引沒有刪掉的問題,如果主鍵索引是和主鍵約束一起建的,
則刪除約束的時候索引會自動刪除掉,如果是先建了索引,然後建立主鍵,則刪除約束的時候索引不會一起被刪除掉

測試:
--建立測試表
create table dbmgr.test_pk as select * from REINSDATA.REINS_PROP_PLAN_ADJ where rownum <1000

--建立主鍵,同時建立索引,這裡using index是預設的,如果不加還是會using index的
--using index
alter table dbmgr.test_pk
add constraint PK_test primary key (DANGERUNITNO, RERISKCODE, TTYID, CERTINO, RISKCODE)
using index

--刪除約束
alter table dbmgr.test_pk drop constraint PK_test

--結果:約束和索引均被刪掉

--再先建立一個唯一約束,然後添加對應的主鍵約束
--create unique index
create unique index dbmgr.PK_test on dbmgr.test_pk(DANGERUNITNO, RERISKCODE, TTYID, CERTINO, RISKCODE)

alter table dbmgr.test_pk
add constraint PK_test primary key (CERTINO,RERISKCODE,RISKCODE,DANGERUNITNO,TTYID)

--刪除約束
alter table dbmgr.test_pk drop constraint PK_test


--結果:此時索引沒有被刪除

--對於這種情況,我們需要加上drop index選項,這樣無論那種情況索引都會被刪除
alter table dbmgr.test_pk drop constraint PK_test drop index;

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.