我有两张桌子
Try1(ID和ValueA)
和
try2(ID和valueb)
在表格中,Try1 ID是自动增量和主键,我想以某种方式将此TRY1.ID链接到try2.ID,这样当我insert into try1 (valueA)=XXX
时,自动生成的ID也会反映在Try2中
(解决方案是否基于第一个创建外键?)
我正在使用PHPMYADMIN和php来插入查询。任何可能的方式都会有所帮助,谢谢
答案 0 :(得分:4)
我认为你正在寻找触发器 - 就像这样:
DELIMITER $$
CREATE
/*[DEFINER = { user | CURRENT_USER }]*/
TRIGGER `CopyTry1RowToTry2` AFTER INSERT ON `try1`
FOR EACH ROW BEGIN
INSERT INTO `try2` (`id`,`value`) VALUES(NEW.`id`,NEW.`value`)
END;
$$
DELIMITER ;
答案 1 :(得分:2)
在Try2中提到Try1 ID作为外键。像:
CREATE TABLE Try2 (id INT, Try1_id INT, valueb INT,
INDEX par_ind (Try1_id),
FOREIGN KEY (Try1_id) REFERENCES Try1(id)
ON DELETE CASCADE
) ENGINE=INNODB;
有关foriegn密钥的更多信息,请参阅
http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html