tableONE
tb1_id tbl_name tb2_id
1 ab 0
tableTOW
插入tableTOW(" tb2_id,tb2_name,tb1_id)值(' 10,'名称',1)
结果将是:
tb2_id tb2name tb1_id
10 name 1
现在tableONE应该更新列tb2_id = 12因为插入到tableTOW,其中tb1_id是(1)
如何在插入更新期间设置这样的触发器,其他表与where where(WHERE tableTOW.tb1_id = tableONE.tb1_id)..
问候
答案 0 :(得分:1)
使用过程将其设为原子,否则在应用程序逻辑中执行
# change the delimiter to $$, so you can use semicolon in create procedure
DELIMITER $$
USE database_name$$
DROP PROCEDURE IF EXISTS data_insert$$
CREATE PROCEDURE data_insert(IN tb1_id INT, IN tbl_name VARCHAR(50), IN tb2_id INT)
BEGIN
INSERT INTO tableONE (tb1_id, tbl_name, tb2_id) VALUES (tb1_id, tbl_name, tb2_id);
######## ######## ######## ######## #######
####### WRITE YOUR UPDATE QUERY HERE ########
######## ######## ######## ######## #######
END$$
# change the delimiter back to semicolon
DELIMITER ;
取自insert into in stored procedure with parameters MYSQL doesnt work
的程序代码答案 1 :(得分:0)
CREATE TRIGGER `ins_trig` AFTER INSERT ON `tableTOW`
FOR EACH ROW UPDATE tableONE
SET tb2_id= new.tb2_id
WHERE tb1_id = NEW.tb1_id
和我使用的更新
CREATE TRIGGER `ins_trig` AFTER INSERT ON `tableTOW`
FOR EACH ROW UPDATE tableONE
UPDATE tb2_id= new.tb2_id
WHERE tb1_id = NEW.tb1_id
感谢您的评论和答案..