请查看以下两个表t_father
和t_child
:
create table t_father (
father_id int unsigned AUTO_INCREMENT PRIMARY key,
name varchar(100),
date_a date,
is_trash int(1),
key(father_id, date_a, is_trash)
);
create table t_child(
child_id int unsigned AUTO_INCREMENT PRIMARY key,
father_id int unsigned,
name varchar(100),
date_b date,
is_trash int(1),
foreign key(father_id, date_b, is_trash)
references t_father(father_id, date_a, is_trash) on update cascade
)
请注意::date_a
可以是重复值,并且date_b
中的date_a
引用必须基于father_id
现在,我在t_father
表中有以下两条记录:https://i.stack.imgur.com/lMr55.png
如果我要在t_child
表中插入一条记录并选择1
作为father_id
,则date_b
列必须为2020-05-31
。当前,如果我从date_a
中选择另一个日期,它将被插入!因此,t_child.date_b
不得在其他日期接受,因为t_father.date_a
具有2020-05-31
并且t_child.is_trash
列不能接受0
,因为t_fathar.is_trash
具有1
。
那么,我该怎么做? 预先感谢。