我有兩個表t1(alternativeRoute)& t2(alternativeRoute_addressInformation)。MySQL觸發刪除整個表而不是一個條目
問題是,如果在T2中刪除了所有具有相同ID的條目(全部在一個刪除查詢中或連續刪除),那麼T1中的條目也應該被刪除。
爲此,我創建了一個SQL查詢,如果我使用相同的ID從t2中刪除條目後手動運行它,它將很好地工作。
DELETE FROM traffic.traffic_alternativeRoute WHERE altRoute_id NOT IN (
SELECT distinct traffic.traffic_alternativeRoute_addressInformation.altRoute_id
FROM traffic.traffic_alternativeRoute_addressInformation);
但我要自動執行該操作,因此,我決定創建一個觸發器:
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上有一個級聯。這可能是問題的一部分嗎?
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,希望幫助:
你可以張貼一些試驗數據和結果,你期待什麼呢? – wchiquito
單獨運行(單獨)時,您的DELETE功能是否正常工作? 或'DISTINCT'可能有衝突。 –
嘗試通過此程序首先刪除單個ID 0 –