我有两张桌子t1(alternativeRoute)& t2(alternativeRoute_addressInformation)。
挑战在于,如果在T2中删除所有具有相同ID的条目(All in one delete query或者连续),则还应删除T1中的条目。
为此,我创建了一个SQL查询,如果我在删除t2中具有相同ID的条目后手动运行它,则该查询非常有效。
DELETE FROM traffic.traffic_alternativeRoute WHERE altRoute_id NOT IN (
SELECT distinct traffic.traffic_alternativeRoute_addressInformation.altRoute_id
FROM traffic.traffic_alternativeRoute_addressInformation);
但我想自动执行该操作,因此我决定创建一个Trigger:
DELIMITER $$
CREATE TRIGGER clear_traffic_alternativeRoute
AFTER DELETE ON traffic.traffic_alternativeRoute_addressInformation
FOR EACH ROW
BEGIN
DELETE FROM traffic.traffic_alternativeRoute WHERE altRoute_id NOT IN (
SELECT distinct 'traffic.traffic_alternativeRoute_addressInformation.altRoute_id'
FROM traffic.traffic_alternativeRoute_addressInformation);
END; $$
DELIMITER ;
现在问题: 触发器被触发,但不是仅删除t1中的条目,而是删除两个表中的所有条目。
有没有人知道为什么这个代码在使用触发器时不起作用?
测试数据:
T1:
INSERT INTO `traffic_alternativeRoute` (`info_id`, `altRoute_id`, `altRoute_desc`) VALUES
(66, 63, '');
INSERT INTO `traffic_alternativeRoute` (`info_id`, `altRoute_id`, `altRoute_desc`) VALUES
(66, 64, '');
T2:
INSERT INTO `traffic_alternativeRoute_addressInformation` (`altRoute_id`, altRouteAddress_id`, `altRoute_address`, `altRoute_address_houseNumber`, `altRoute_locality`, `altRoute_postal_code`, `altRoute_order_number`, `altRoute_pos_lat`, `altRoute_pos_lng`) VALUES
(63, 249, 'Kölner Straße', 445, 'Kürten', '51515', 1, 51.03150342683682, 7.209327220916748),
(63, 250, 'Kölner Straße', 488, 'Kürten', '51515', 2, 51.02742788461119, 7.205550670623779),
(64, 251, 'Engelsgasse', 14, 'Kürten', '51515', 3, 51.02900686236984, 7.199242115020752),
(64, 252, 'Cliev', 5, 'Kürten', '51515', 4, 51.03193525137229, 7.20728874206543);
预期结果:
删除ID为64的两个条目后,T1应仅包含ID为63的条目。
编辑:
我还没说过的一件事,也就是我已经不记得了,就是我在T1上有一个ON CASCADE。这可能是问题的一部分吗?
CREATE TABLE IF NOT EXISTS `traffic`.`traffic_alternativeRoute` (
`info_id` INT NULL COMMENT '',
`altRoute_id` INT NOT NULL AUTO_INCREMENT COMMENT '',
`altRoute_desc` VARCHAR(256) NULL COMMENT '',
PRIMARY KEY (`altRoute_id`) COMMENT '',
INDEX `fk_info_id_idx` (`info_id` ASC) COMMENT '',
CONSTRAINT `fk_info_id`
FOREIGN KEY (`info_id`)
REFERENCES `traffic`.`traffic_info` (`info_id`)
ON DELETE CASCADE
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;
CREATE TABLE IF NOT EXISTS `traffic`.`traffic_alternativeRoute_addressInformation` (
`altRoute_id` INT NULL COMMENT '',
`altRouteAddress_id` INT NOT NULL AUTO_INCREMENT COMMENT '',
`altRoute_address` VARCHAR(128) NOT NULL COMMENT '',
`altRoute_address_houseNumber` INT NOT NULL COMMENT '',
`altRoute_locality` VARCHAR(45) NOT NULL COMMENT '',
`altRoute_postal_code` VARCHAR(10) NOT NULL COMMENT '',
`altRoute_order_number` INT NOT NULL COMMENT '',
`altRoute_pos_lat` DOUBLE NOT NULL COMMENT '',
`altRoute_pos_lng` DOUBLE NOT NULL COMMENT '',
PRIMARY KEY (`altRouteAddress_id`) COMMENT '',
INDEX `fk_altRoute_id_idx` (`altRoute_id` ASC) COMMENT '',
CONSTRAINT `fk_altRoute_id`
FOREIGN KEY (`altRoute_id`)
REFERENCES `traffic`.`traffic_alternativeRoute` (`altRoute_id`)
ON DELETE CASCADE
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;
这里的entrie DB - ERD,希望有所帮助:
答案 0 :(得分:0)
为什么使用子查询?您可以使用触发器完成所需操作,如下所示:
DELIMITER //
CREATE TRIGGER `clear_traffic_alternativeRoute`
AFTER DELETE ON `traffic_alternativeRoute_addressInformation`
FOR EACH ROW
BEGIN
/*
DELETE FROM traffic.traffic_alternativeRoute WHERE altRoute_id NOT IN (
SELECT distinct 'traffic.traffic_alternativeRoute_addressInformation.altRoute_id'
FROM traffic.traffic_alternativeRoute_addressInformation);
*/
DELETE FROM `traffic_alternativeRoute`
WHERE `altRoute_id` NOT IN (OLD.`altRoute_id`);
END//
DELIMITER ;