sql - CASCADE behaviour on the drop of a foreign key -


i not clear happens when "foreign key constraint" deleted specifying option cascade.

for instance, consider command

alter table table1 drop constraint foreignkeytotable2 cascade. 

what option cascade supposed in case? happen if omitted it? , if wrote restrict instead of cascade?

note: example of query excerpted "ramez elmasri, shamkant b. navathe - fundamentals of database systems, end of chapter 5".

the cascade option drop constraint needed when dropping primary keys, not when dropping foreign key.

consider example in postgres:

create table t1 (id integer, constraint pk_one primary key (id)); create table t2 (id integer primary key, id1 integer references t1); 

when try run:

alter table t1 drop constraint pk_one; 

you get:

error: cannot drop constraint pk_one on table t1 because other objects depend on   detail: constraint t2_id1_fkey on table t2 depends on index pk_one   hint: use drop ... cascade drop dependent objects too.  

if run:

alter table t1 drop constraint pk_one cascade; 

you get:

notice:  drop cascades constraint t2_id1_fkey on table t2 

telling foreign key needed primary key dropped well.


note not dbms support cascading drop. postgres , oracle do.
mysql, sql server or firebird not. need drop foreign keys manually in dbms.


Comments