我不清楚当删除指定选项级联的“外键约束”时会发生什么。
例如,考虑以下命令
ALTER TABLE table1 DROP CONSTRAINT foreignKeyToTable2 CASCADE.
在这种情况下,选择级联应该做什么?如果我漏掉了它会怎么样?如果我写的是限制而不是级联呢?
注意:这个查询示例摘自"Ramez Elmasri,Shamkant B. Navathe -数据库系统的基本原理,第5章末尾“。
发布于 2016-03-26 12:53:31
删除约束的cascade
选项仅在删除主键时才需要,而在删除外键时不需要。
请考虑Postgres中的这个示例:
create table t1 (id integer, constraint pk_one primary key (id));
create table t2 (id integer primary key, id1 integer references t1);
当你试图逃跑时:
alter table t1 drop constraint pk_one;
你得到:
ERROR: cannot drop constraint pk_one on table t1 because other objects depend on it
Detail: constraint t2_id1_fkey on table t2 depends on index pk_one
Hint: Use DROP ... CASCADE to drop the dependent objects too.
如果你跑:
alter table t1 drop constraint pk_one cascade;
你得到:
NOTICE: drop cascades to constraint t2_id1_fkey on table t2
告诉您需要主键的外键也被删除了。
请注意,并非所有DBMS都支持级联下降。波斯特格斯和甲骨文。
MySQL、Server或Firebird不这样做。您需要在那些DBMS中手动删除外键。
https://stackoverflow.com/questions/36235073
复制相似问题