oracle沒有create or replace table

轉自:http://tunps.com/oracle-create-or-replace-table
Oracle數據庫和其他數據庫(比如MySQL)在新建數據表的時候有以下區別:

Sql代碼  收藏代碼
  1. SQL> create or replace table testTb;  
  2. create or replace table testTb  

ORA-00922: 選項缺失或無效 只能使用先drop再create來代替
Sql代碼  收藏代碼
  1. drop table testTb;  
  2. create teble testTb(  
  3.     fid   varchar2(4),  
  4.     fname   varchar2(10)  
  5. );  

可以用create or replace的對象有:functions, procedures, packages, types, synonyms, trigger and views,就是沒有table,也沒有sequence。
drop掉一個並不存在的表報錯:
Sql代碼  收藏代碼
  1. SQL> drop table non_exists;  
  2. drop table non_exists  
  3. ORA-00942:   

表或視圖不存在 drop table容錯的方法是:
Sql代碼  收藏代碼
  1. BEGIN  
  2.   DROP TABLE non_exists_table;  
  3. EXCEPTION  
  4.   WHEN OTHERS THEN  
  5.     IF sqlcode != -0942 THEN RAISE; END IF;  
  6. END;  
  7. / 錯誤代碼:-0942  

drop sequence容錯的方法是:
Sql代碼  收藏代碼
  1. BEGIN  
  2.   DROP SEQUENCE non_exists_sequence;  
  3. EXCEPTION  
  4.   WHEN OTHERS THEN  
  5.     IF sqlcode != -2289 THEN RAISE; END IF;  
  6. END;  
  7. / 錯誤代碼:-2289   


參考:http://docs.oracle.com/cd/B14117_01/server.101/b10759/statements_7002.htm#i2095331
http://stackoverflow.com/questions/1008248/how-do-i-use-create-or-replace
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章