2017-06-14 40 views
2

有沒有什麼辦法可以在oracle中創建索引只有當它們不存在?如何在Oracle中不存在時創建和索引?

喜歡的東西

CREATE INDEX IF NOT EXISTS ord_customer_ix 
    ON orders (customer_id); 
+0

沒有,沒有。只有編程(使用動態SQL)捕捉'ORA-01408'錯誤 –

+0

@NicholasKrasnov「有什麼辦法嗎?」你能告訴我嗎? – Adelin

+0

誤讀了一下;-)。 @ user7294900只是貼了代碼。 –

回答

7

添加一個索引只有當不存在:

declare 
    already_exists exception; 
    columns_indexed exception; 
    pragma exception_init(already_exists, -955); 
    pragma exception_init(columns_indexed, -1408); 
begin 
    execute immediate 'create index ord_customer_ix on orders (customer_id)'; 
    dbms_output.put_line('created'); 
exception 
    when already_exists or columns_indexed then 
    dbms_output.put_line('skipped'); 
end;  
+0

接縫是Ask Tom的官方答案: https://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:54643197712655 –