在较高的层面上,我已经意识到您不能将新行插入到同一个表中,并且应该考虑使用 SPROC。不过,这是用例。我有一个第 3 方 Web 应用程序和这个 MySQL 数据库,所以我无法控制应用程序流程。已收到简化数据输入的请求。我能控制的是数据库。该应用程序就像一个 CRM,有一个联系人表,还有第二个 contact_relationships 表,我在其中放置触发器。基本上 contact_relationships 需要三个字段。联系人表中的两个 contactID (INT) 和一个关系类型 varchar(45),如(配偶、 sibling 、外部家庭等)。
这里的目标是当新行(TRIGGER)添加到 contact_relationships 表时,我们还将第二行写入同一个表,以反转 contactID 并保留关系类型。这确保了还为同一单个条目中的其他联系人建立了记录关系。 (理想情况下应该在应用程序中完成)。
我对这个本来应该是愚蠢的简单操作感到束手无策。我什至尝试过这种创造性的实现。
我创建了一个新的_temp表
CREATE TABLE `_temp` (
`id_temp` int(11) NOT NULL AUTO_INCREMENT,
`Contact_id` int(11) DEFAULT NULL,
`Relation_id` int(11) DEFAULT NULL,
`Relationship_Type` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id_temp`)
我在 contact_relationships 表上有两个触发器
#1
CREATE DEFINER=`root`@`localhost` TRIGGER `contact_relationships_BEFORE_INSERT` BEFORE INSERT ON `contact_relationships` FOR EACH ROW BEGIN
insert into _temp (Contact_id,Relation_id,Relationship_Type)
values(NEW.Contact_id,NEW.Relation_id,NEW.Relationship_Type);
END
#2
CREATE DEFINER=`root`@`localhost` TRIGGER `contact_relationships_AFTER_INSERT` AFTER INSERT ON `contact_relationships` FOR EACH ROW BEGIN
delete from _temp;
END
在 _temp 表上我有这个触发器
CREATE DEFINER=`root`@`localhost` TRIGGER `_temp_AFTER_DELETE` AFTER DELETE ON `_temp` FOR EACH ROW BEGIN
#DO Sleep(2);
# Sleep commented out as it did not work and only delayed the error
insert into contact_relationships (Relation_id,Contact_id,Relationship_Type) values(OLD.Contact_id,OLD.Relation_id,OLD.Relationship_Type);
END
所以我在伪代码中的想法是这样的
When a row is inserted into the contact_relationships table write the inverted row to a temp table as I can not write it to the **same table from inside the trigger**.
AFTER the triggered inserted row is complete lets delete the row in the temp table so we can create a DELETE trigger on that other table to write the desired row into the contact_relationship.
At this stage I believe the TRIGGER and TRANSACTIONS on the contact_relationships table are DONE
On the temp Table a trigger and transaction firing AFTER any contact_relationships transactions
结果令人抓狂,而且无论是内部还是外部,结果总是一样的。我尝试过创造性的函数和存储过程,但结果都一样令人恼火。
Contact Relationships : Add New
Can't update table 'contact_relationships' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Query:
insert into `Contact_Relationships` set `Contact_id`='2', `Relation_id`='3', `Relationship_Type`='Extended Family'
我完全不知道如何实现这一目标 - 有人有办法实现这一目标吗?
最佳答案
dsoden。
我要做的是:
在触发器定义中,您可以获取已插入关系的“inserted_id”(您可以使用LAST_INSERT_ID()获取它) > ).
一旦获得它,您就可以从该记录中选择到临时变量中:
SELECT `Contact_id`,`Relation_id`,`Relationship_Type` INTO @cont_id, @rel_id, @rel_type
FROM contact_relationships
WHERE id = LAST_INSERT_ID()
然后以相反的顺序插入新关系,具有相同的关系类型:
INSERT INTO contact_relationships (`Contact_id`,`Relation_id`,`Relationship_Type`) VALUES ( @rel_id, @cont_id, @rel_type);
如果这就是你的目标..
类似于:
当插入 Anne 和 John 之间的 sibling 关系时, 还插入 John、Anne、Siblings 关系
关于mysql - 需要替代方案来从同一个表上的插入触发器插入第二个数据行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54749097/