且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PostgreSQL:外键/删除级联

更新时间:2023-02-02 23:03:12

A使用级联删除的外键意味着,如果删除父表中的记录,则子表中的相应记录将被自动删除。这就是所谓的级联删除。

A foreign key with a cascade delete means that if a record in the parent table is deleted, then the corresponding records in the child table will automatically be deleted. This is called a cascade delete.

您说的是相反的意思,这不是当您从子表中删除时会从父表中删除记录。 / p>

You are saying in a opposite way, this is not that when you delete from child table then records will be deleted from parent table.

UPDATE 1:

ON DELETE CASCADE选项用于指定在父表中删除相应行时是否要在子表中删除行。如果未指定级联删除,则数据库服务器的默认行为将阻止您删除表中的数据(如果其他表引用了该数据)。

ON DELETE CASCADE option is to specify whether you want rows deleted in a child table when corresponding rows are deleted in the parent table. If you do not specify cascading deletes, the default behaviour of the database server prevents you from deleting data in a table if other tables reference it.

如果指定此选项,稍后,当您删除父表中的一行时,数据库服务器还将删除子表中与该行关联的所有行(外键)。级联删除功能的主要优点在于,它可以减少执行删除操作所需的SQL语句的数量。

If you specify this option, later when you delete a row in the parent table, the database server also deletes any rows associated with that row (foreign keys) in a child table. The principal advantage to the cascading-deletes feature is that it allows you to reduce the quantity of SQL statements you need to perform delete actions.

因此,当您从CAT表中删除条目时,行将被删除从书表。 :)

So in your case when user removes entries from CATs table then rows will be deleted from books table. :)

希望这对您有帮助:)