有没有一种方法可以创建主键并将其级联到其他表中,而无需将数据重新输入到新表中?

Is there a way to create a primary key and have it cascade into other tables without re-entering data into the new tables?

我正在努力建立一个数据库,并创建了 table 级联,就像在外键更新时使用级联一样。我正在使用两个 table 来尝试解决这个问题。我的 table 是这样的:

create table Item(Item int(4) not null, EquipName varchar(20), Equip int(4) 
not null, primary key(Item, Equip))

create table Cross(Time timestamp default now(), Cross varchar(10) default 
'null', Item int(4) not null, Equip int(4) not null, Stop varchar(20) default 
'null', foreign key(Item, Equip) references Item(Item, Equip) on update 
cascade);

所以我希望能够将数据输入到项目 table 中,并自动将其放入交叉 table 中,这些值会级联到其中。这就是 Cross 和 StopCount 的默认值为 null 的原因。

例如insert into Item values(5, fan, 4);我希望 Cross 自动填充到 FK 点中,以在 Item 中输入什么信息。

您可以使用 trigger 自动填充另一个 table。

DELIMITER $$
CREATE TRIGGER init_cross AFTER INSERT ON item
  FOR EACH ROW
  BEGIN
    INSERT INTO `cross`(item,equip) VALUES( NEW.item, NEW.equip );
  END;
$$
DELIMITER ;