如果对帐单不允许我预订任何汽车(MySQL)[已答复+已关闭]

时间:2019-04-04 14:03:30

标签: mysql

我正试图使修理中的汽车无法租用。因此,触发器可以在PhpMyAdmin中很好地加载,但是我选择预订的任何汽车(即使它不在维修中)也表示它正在维修。

我有以下触发器:

DELIMITER //

CREATE TRIGGER check_repair
BEFORE INSERT ON bookings 
FOR EACH ROW BEGIN 

                                                                            /*We need to save the "count"*/
DECLARE inspections_count INT DEFAULT 0;

SET NEW.TheDuration = DATEDIFF(NEW.end_date, NEW.start_date);
SET NEW.AMOUNT_DUE  = DATEDIFF(NEW.end_date, NEW.start_date) * 200;
                                                                        /*Check if where is a repair going which matches the vehicle_id*/
SELECT 1 
INTO inspections_count  /* store the "count" into the variable. */
FROM inspections
WHERE
    inspections.vehicle_id = NEW.vehicle_id
  AND
    repair_complete = 'Yes'
ORDER BY 
 inspections.inspection_date DESC


 LIMIT 1;

                                 /*if there is a "count" stop the insert.*/
IF inspections_count = 1 THEN 
  SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'vehicle is in repair';
END;
DELIMITER // 

表结构:

CREATE TABLE  inspections  (
   inspection_id  int(10)  NOT NULL AUTO_INCREMENT,
   inspection_date  date NOT NULL,
   vehicle_id  int(10) NOT NULL,
   problem  varchar(50) NOT NULL,
   repair_complete  ENUM('Yes','No') NOT NULL,
   mechanic_id  int(10)  NOT NULL,
   PRIMARY KEY(inspection_id),
   KEY mechanic_idfk1 (mechanic_id),
   KEY vehicle_idfk2 (vehicle_id),
   CONSTRAINT vehicle_idfk2 FOREIGN KEY (vehicle_id) REFERENCES vehicles (vehicle_id),
   CONSTRAINT mechanic_idfk1 FOREIGN KEY (mechanic_id) REFERENCES mechanics (mechanic_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

有人知道为什么这个触发条件不允许我租用工作车吗?

0 个答案:

没有答案