[英]MySQL Triggers - Implementation Issue
I am developing a MySQL database using Workbench. 我正在使用Workbench开发MySQL数据库。 I want two send two fields from a newly created record to another table.
我想两个将两个字段从新创建的记录发送到另一个表。 I would then like to update the original table with newly created data from the second table.
然后,我想用第二个表中的新创建的数据更新原始表。 I was looking to implement this with triggers, unless there is a better way of course :) My attempt was a fail when I went to upload it(see below)
我一直在尝试使用触发器来实现,除非有更好的方法:)我上载尝试失败(请参见下文)
Specifically, I would like tc_Event to send the ID & tc_EventTags_ID to tc_EventTags to fill in tc_Tag_ID & tc_Event_ID. 具体来说,我希望tc_Event将ID和tc_EventTags_ID发送到tc_EventTags,以填写tc_Tag_ID和tc_Event_ID。 Afterwards I want the ID of tc_EventTags sent back to tc_Event to the tc_EventTags_ID field.
之后,我希望将tc_EventTags的ID发送回tc_Event到tc_EventTags_ID字段。
Thanks for any help. 谢谢你的帮助。
-- -----------------------------------------------------
-- Table `mcontest`.`tc_EventTags`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mcontest`.`tc_EventTags` (
`ID` INT NOT NULL AUTO_INCREMENT ,
`tc_Tag_ID` INT NOT NULL ,
`tc_Event_ID` INT NOT NULL ,
PRIMARY KEY (`ID`) ,
INDEX `fk_tc_EventTags_tc_Tag1` (`tc_Tag_ID` ASC) ,
INDEX `fk_tc_EventTags_tc_Event1` (`tc_Event_ID` ASC) ,
CONSTRAINT `fk_tc_EventTags_tc_Tag1`
FOREIGN KEY (`tc_Tag_ID` )
REFERENCES `mcontest`.`tc_Tag` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tc_EventTags_tc_Event1`
FOREIGN KEY (`tc_Event_ID` )
REFERENCES `mcontest`.`tc_Event` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = MyISAM;
-- -----------------------------------------------------
-- Table `mcontest`.`tc_Event`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mcontest`.`tc_Event` (
`ID` INT NOT NULL AUTO_INCREMENT ,
`date` DATE NOT NULL ,
`time` TIME NOT NULL ,
`location` VARCHAR(45) NOT NULL ,
`description` VARCHAR(45) NOT NULL ,
`tc_EventTags_ID` INT NULL ,
`tc_Orgs_ID` INT NOT NULL ,
`tc_PersonEvent_ID` INT NOT NULL ,
PRIMARY KEY (`ID`) ,
INDEX `fk_tc_Event_tc_EventTags1` (`tc_EventTags_ID` ASC) ,
INDEX `fk_tc_Event_tc_Orgs1` (`tc_Orgs_ID` ASC) ,
INDEX `fk_tc_Event_tc_PersonEvent1` (`tc_PersonEvent_ID` ASC) ,
CONSTRAINT `fk_tc_Event_tc_EventTags1`
FOREIGN KEY (`tc_EventTags_ID` )
REFERENCES `mcontest`.`tc_EventTags` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tc_Event_tc_Orgs1`
FOREIGN KEY (`tc_Orgs_ID` )
REFERENCES `mcontest`.`tc_Orgs` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tc_Event_tc_PersonEvent1`
FOREIGN KEY (`tc_PersonEvent_ID` )
REFERENCES `mcontest`.`tc_PersonEvent` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = MyISAM;
USE `mcontest`;
DELIMITER $$
USE `mcontest`$$
CREATE TRIGGER eventTag_Trigger
AFTER insert ON tc_Event
FOR EACH ROW BEGIN
INSERT INTO tc_EventTags values('',NEW.tc_Event_ID);
END;
END$$
DELIMITER ;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
Why have put a tc_EventTags_ID
in table tc_Event
? 为什么将
tc_EventTags_ID
放在表tc_Event
? What is the logic behind that? 这背后的逻辑是什么?
I mean that the relationship between the 2 tables would be (I guess): 1 Event - many EventTags. 我的意思是2个表之间的关系为(我猜):1事件-许多EventTag。 This is already achieved by the
tc_EventTags.tc_Event_ID
which is a Foreign Key to tc_Event
. 这是已经被实现
tc_EventTags.tc_Event_ID
这是一个外键tc_Event
。
To answer your question: 要回答您的问题:
As it is now, the Trigger tries for every row inserted in table Event, to add a row in table EventTag. 现在,触发器会尝试插入表Event中的每一行,以在表EventTag中添加一行。 But it will fail for 2 reasons:
但是它会失败有两个原因:
tc_Event_ID
is ok but tc_Tag_ID
has to be NOT NULL and reference the Tag table but the value you supply, ''
, probably is not in table Tag. tc_Event_ID
可以,但是tc_Tag_ID
必须为NOT NULL并引用Tag表,但您提供的值''
可能不在表Tag中。 EDIT: the value you supply, ''
, is also a CHAR
while it should be INT
. 编辑:您提供的值
''
也是一个CHAR
而它应该是INT
。
tc_EventTags_ID
which references EventTag table. tc_EventTags_ID
它引用EventTag表。 But EventTag table is empty.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.