MySQL 在插入觸發器後更新另一個表的列
建立預訂請求時,需要根據以下資料更新BookingRequest 表中的狀態將相應的請求插入OccupiedRoom 表中。為此,需要一個觸發器。
首先,定義以下表格:
<br> -- BookingRequest 表:<br> CREATE TABLE BookingRequest (<pre class="brush:php;toolbar:false">idRequest INT NOT NULL AUTO_INCREMENT, roomClass INT NOT NULL, inDate DATE NOT NULL, outDate DATE NOT NULL, numOfBeds INT NOT NULL, status INT NOT NULL, idUser INT NOT NULL, PRIMARY KEY (idRequest), INDEX idx_status (status), INDEX idx_user (idUser)
);
););
);
idStatus INT NOT NULL AUTO_INCREMENT, nameStatus ENUM('underConsideration', 'approved', 'rejected'), PRIMARY KEY (idStatus)
);
););
);
idOccupation INT NOT NULL AUTO_INCREMENT, idRoom INT NOT NULL, idRequest INT NOT NULL, PRIMARY KEY (idOccupation), INDEX idx_id_room (idRoom), INDEX idx_id_request (idRequest)
--狀態表:
DELIMITER $$ CREATE TRIGGER occupy_trig AFTER INSERT ON `OccupiedRoom` FOR EACH ROW begin DECLARE id_exists Boolean; -- Check BookingRequest table SELECT 1 INTO @id_exists FROM BookingRequest WHERE BookingRequest.idRequest= NEW.idRequest; IF @id_exists = 1 THEN UPDATE BookingRequest SET status = '1' WHERE idRequest = NEW.idRequest; END IF; END; $$ DELIMITER ;
以上是如何使用 MySQL 觸發器插入 OccupiedRoom 後更新 BookingRequest 狀態?的詳細內容。更多資訊請關注PHP中文網其他相關文章!