2017-04-19 31 views
0

當我drop表中它也放棄約束?Drop表是否也會放棄約束?

+0

哪些限制? –

+0

Govinda Mahajan已經回答了它......但是謝謝:) –

+2

該答案中的語法對Oracle來說是錯誤的。並且聲明引用該表的視圖被丟棄也是錯誤的。我的問題仍然存在:你指的是哪些限制?主鍵約束或檢查約束不需要「級聯約束」。這只是外鍵約束所必需的。 –

回答

4

這是一個簡單的表格。它有一個索引,一些完整性約束和一個觸發器:

SQL> desc t69 
Name    Null? Type 
------------------ -------- ---------------------------- 
ID     NOT NULL NUMBER 

SQL> select index_name from user_indexes where table_name = 'T69'; 

INDEX_NAME 
------------------------------ 
SYS_C0034158 

SQL> select constraint_name, constraint_type from user_constraints where table_name = 'T69'; 

CONSTRAINT_NAME    C 
------------------------------ - 
SYS_C0034157     C 
SYS_C0034158     P 

SQL> select trigger_name from user_triggers where table_name = 'T69'; 

TRIGGER_NAME 
------------------------------ 
TRG69 

SQL> 

我們可以放棄嗎?我們可以!

SQL> drop table t69; 

Table dropped. 

SQL> select constraint_name, constraint_type from user_constraints where table_name = 'T69'; 

no rows selected 

SQL> select trigger_name from user_triggers where table_name = 'T69'; 

no rows selected 

SQL> 
SQL> select index_name from user_indexes where table_name = 'T69'; 

no rows selected 

SQL> 

什麼也沒有。其他對象引用表時不同。

還有另一張表P23。它由外鍵引用並在視圖中使用。

SQL> create table c23 (id number, p_id number); 

Table created. 

SQL> alter table c23 add foreign key (p_id) references p23; 

Table altered. 

SQL> create view v23 as select * from p23; 

View created. 

SQL> 

那麼我們可以放下這張桌子嗎?

SQL> drop table p23 ; 
drop table p23 
      * 
ERROR at line 1: 
ORA-02449: unique/primary keys in table referenced by foreign keys 


SQL> 

不,我們不能。順便說一句,關於RESTRICT語法,Oracle不支持。有沒有必要爲它,我們不能刪除其強制關係完整性表......除非我們堅持要這麼做:

SQL> drop table p23 cascade constraints; 

Table dropped. 

SQL> desc t23 
Name          Null? Type 
----------------------------------------- -------- ---------------------------- 
COLA            NUMBER 
COLB            NUMBER 
COLC            NUMBER 
GENDER            VARCHAR2(1) 
ID             NUMBER 

SQL> select * from v23 
    2/
select * from v23 
       * 
ERROR at line 1: 
ORA-04063: view "FOX.V23" has errors 


SQL> 

的CASCADE CONSTRAINTS子句刪除表和任何外鍵引用它。子表保持不變。引用表的視圖(以及任何PL/SQL)仍處於無效狀態。