2017-06-14 156 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 –