當你要drop一個table時,如果刪除table的動作會造成trigger或constraint產生矛盾,系統會出現錯誤警告的訊息而不會允許執行.。一個極簡單的例子,例如你有一個員工基本資料表,上面可能有員工編號和員工姓名等欄位,另外有一個員工銷售表,上面有員工編號和員工銷售額兩個欄位,員工薪資表的員工編號欄位為一個foreign key參照到員工基本資料表的員工編號:
SQL> drop table t;
Table dropped.
SQL> drop table t1;
Table dropped.
SQL> create table t (id number,name varchar2(20));
Table created.
SQL> create table t1 (id number,sal number);
Table created.
SQL> alter table t add constraint t_pk primary key (id);
Table altered.
SQL> alter table t1 add constraint t_fk foreign key (id) references t (id);
Table altered.
SQL> insert into t values (1,'JACK');
1 row created.
SQL> insert into t values (2,'MARY');
1 row created.
SQL> COMMIT;
Commit complete.
SQL> insert into t1 values (1,1000);
1 row created.
SQL> insert into t1 values (2,1500);
1 row created.
SQL> commit;
SQL> insert into t1 values (3,200);
insert into t1 values (3,200)
*
ERROR at line 1:
ORA-02291: integrity constraint (SYS.T_FK) violated - parent key not found
(違反了constraint,員工基本資料表根本沒有3號這個員工,何來的銷售紀錄。)
SQL> drop table t;
drop table t
*
ERROR at line 1:
ORA-02449: unique/primary keys in table referenced by foreign keys
(違反了constraint,員工銷售表t1有參照到table t,這個reference relation不允許你drop table t)
SQL> drop table t cascade constraints;
Table dropped.
SQL> select * from t1;
ID SAL
---------- ----------
1 1000
2 1500
SQL> select CONSTRAINT_NAME,TABLE_NAME from dba_constraints where owner = 'SYS' and TABLE_NAME = 'T1'
no rows selected
SQL>
我們可以發現利用Drop table cascade constraints可以以刪除關聯table t的constraint來達成你drop table t的目的,原來屬於t1的foreign key constraint已經跟隨著被刪除掉了,但是,儲存在table t1的資料可不會被刪除,也就是說Drop table cascade constraints 是不影響到儲存於objec裡的row data。