一个表中基于主键的多个外键(已更新)

时间:2020-05-12 18:38:21

标签: mysql foreign-keys constraints

请查看以下两个表t_fathert_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

那么,我该怎么做? 预先感谢。

0 个答案:

没有答案