1

I'm learning SQL and I'm stuck on one of the review questions and can't find an answer in the text book. When you 'delete cascade'

What is it 'used' with?

  • a. it is used together with a primary key constraint
  • b. it is used together with a unique constraint
  • c. it is used together with a referential constraint
  • d. it is used together with a type constraint

I want to say primary key because it is going to identify the correct tuple when it cascades? Am I right/wrong, I just don't know which one is right.

Thanks.

4

2 回答 2

1

它与引用约束一起使用。

更多关于参照约束

于 2013-08-24T02:51:06.873 回答
1

它会删除对该记录具有 FK 的所有记录。

见小提琴


CREATE TABLE foo (
   id  serial, 
   num int, 
   PRIMARY KEY (id)
);
INSERT INTO foo(num) VALUES(1),(2),(3),(4);

CREATE TABLE bar ( 
   foo_id bigint unsigned,
   FOREIGN KEY (foo_id) REFERENCES foo (id) ON DELETE CASCADE
);
INSERT INTO bar(foo_id) VALUES (1),(2),(3),(4);

DELETE FROM bar WHERE foo_id = 3;  -- notice 3 is only removed from bar
DELETE FROM foo WHERE id = 2;      -- notice 2 is removed from both foo and bar

当记录 fromfoo被删除时,任何引用该记录的键并已delete cascade指定的约束也将被自动删除。如果没有该规范,当foo删除记录时,如果在事务结束时仍然存在任何外部依赖项,数据库将发送错误消息。

于 2013-08-24T02:51:10.130 回答