I have a table where reach row is linked to itself using a parent_id field.
我有一个表,其中使用parent_id字段将到达行链接到自身。
PERSON TABLE: id name age parent_id
PERSON TABLE:id name age parent_id
When I delete a row, I wish for the child row to relink to the parent row of the row that is being deleted.
当我删除一行时,我希望子行重新链接到正在删除的行的父行。
Can this be done using a constraint? Or must I manually code this into my program logic each time I do a delete?
可以使用约束来完成吗?或者我每次删除时都必须手动将其编码到我的程序逻辑中?
1 个解决方案
#1
0
You cannot implement what you're looking for using inbuilt implementation of CONSTRAINTS.
您无法使用内置的CONSTRAINTS实现来实现您正在寻找的内容。
The ON DELETE *reference_option*
clause accepts 4 different values for reference option -
ON DELETE * reference_option *子句接受4个不同的值作为参考选项 -
RESTRICT | CASCADE | SET NULL | NO ACTION
RESTRICT will not let you delete the record
RESTRICT不允许您删除记录
CASCADE will delete the record from parent table as well as those from child tables (reference records).
CASCADE将从父表以及子表(引用记录)中删除记录。
SET NULL will let you delete the parent record and set the column value in child tables as NULL.
SET NULL将允许您删除父记录并将子表中的列值设置为NULL。
NO ACTION is equivalent to RESTRICT.
NO ACTION相当于RESTRICT。
You will have to write custom code to get what you want.
您必须编写自定义代码才能获得所需内容。
#1
0
You cannot implement what you're looking for using inbuilt implementation of CONSTRAINTS.
您无法使用内置的CONSTRAINTS实现来实现您正在寻找的内容。
The ON DELETE *reference_option*
clause accepts 4 different values for reference option -
ON DELETE * reference_option *子句接受4个不同的值作为参考选项 -
RESTRICT | CASCADE | SET NULL | NO ACTION
RESTRICT will not let you delete the record
RESTRICT不允许您删除记录
CASCADE will delete the record from parent table as well as those from child tables (reference records).
CASCADE将从父表以及子表(引用记录)中删除记录。
SET NULL will let you delete the parent record and set the column value in child tables as NULL.
SET NULL将允许您删除父记录并将子表中的列值设置为NULL。
NO ACTION is equivalent to RESTRICT.
NO ACTION相当于RESTRICT。
You will have to write custom code to get what you want.
您必须编写自定义代码才能获得所需内容。