Drop table cascade constraints

tolywang發表於2010-08-03

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,#39;JACK');
1 row created.
SQL> insert into t values (2,#39;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 wner = #39;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。

 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/35489/viewspace-670044/,如需轉載,請註明出處,否則將追究法律責任。

相關文章