我想知道是否可以执行以下操作:
- 向表a插入数据后,表b中将自动创建一行,并且Note_Id(其主键)将存储在其中一个属性中(这是引用表中主键的外键) b) 表a中。
CREATE TABLE table_a ( D_Id int(5) NOT NULL AUTO_INCREMENT,
User_Id int(8) not null,
Note_Id int(5) not null, -- this is the foreign key that points to table b
PRIMARY KEY (D_Id) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE table_b ( Note_Id int(5) NOT NULL AUTO_INCREMENT,
Note_Description varchar(50) null,
PRIMARY KEY (Note_Id) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
谢谢!
delimiter $$
CREATE TRIGGER ins_Document
AFTER INSERT ON TABLE_A FOR EACH ROW
BEGIN
set @notenum=(Select max(Note_Id) from TABLE_B);
if(@notenum=0) then begin new.Note_Id=1;
end;
else
new.Note_Id=@notenum+1;
end if;
INSERT INTO TABLE_B (Note_Id) VALUES (NEW.Note_Id);
END$$
delimiter ;
最佳答案
查看触发器:Create Trigger 在这里,您可以对插入表等事件使用react,并为其定义相应的操作。
关于mysql - 在另一个表中保留/分配一个新行,并将每一行添加到一个表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22303227/