使用“插入前”触发器计算发票

时间:2019-04-04 15:54:14

标签: mysql phpmyadmin

我的问题如下-我想要一个字段,该字段计算每个客户的租车欠款。下面,我提供一些表格结构以及已经完成的工作。如果有人可以透露更多信息,我将不胜感激。

DELIMITER //

CREATE TRIGGER check_repair
BEFORE INSERT ON bookings 
FOR EACH ROW BEGIN 

SET NEW.AMOUNT_DUE  = DATEDIFF(NEW.end_date, NEW.start_date) * 200;

END;
//
DELIMITER ;

表结构是

CREATE TABLE  vehicles (
   vehicle_id  int(10)  NOT NULL AUTO_INCREMENT,
   category ENUM('Sedan','Hatchback','SUV', 'Coupe', 'Crossover') NOT NULL,
   no_of_seats  int(11) NOT NULL,
   brand  varchar(20) NOT NULL,
   model  varchar(20) NOT NULL,
   product_year  int(5) NOT NULL,
   rate_per_day  int(11) NOT NULL,
   PRIMARY KEY (vehicle_id)

 CREATE TABLE  bookings  (
   booking_id  int(50)  NOT NULL AUTO_INCREMENT,
   booking_date  date NOT NULL,
   start_date  date NOT NULL,
   end_date  date NOT NULL,
   invoice_no int(10)  NOT NULL ,
   chauffeur_id int(10) NULL,
   vehicle_id int(10)  NOT NULL ,
   customer_id int(50) NOT NULL ,
   chauffeur_req ENUM('Yes','No') NOT NULL,
   special_instructions varchar(255) NOT NULL,
   PRIMARY KEY (booking_id),
   KEY invoice_nofk2 (invoice_no),
   KEY chauffeur_idfk1 (chauffeur_id),
   KEY customer_idfk2 (customer_id),
   KEY vehicle_idfk2 (vehicle_id),
   CONSTRAINT invoice_nofk1 FOREIGN KEY (invoice_no) REFERENCES invoice (invoice_no),
   CONSTRAINT chauffeur_idfk2 FOREIGN KEY (chauffeur_id) REFERENCES chauffeurs (chauffeur_id),
   CONSTRAINT customer_idfk3 FOREIGN KEY (customer_id) REFERENCES customers (customer_id), 
   CONSTRAINT vehicle_idfk3 FOREIGN KEY (vehicle_id) REFERENCES vehicles (vehicle_id)

我也有

ALTER TABLE bookings
ADD COLUMN TheDuration varchar(10) NOT NULL;
ALTER TABLE bookings
ADD COLUMN AMOUNT_DUE varchar(16) NOT NULL; 

最后确定我的问题。如何创建一个使用rate_per_day和持续时间来计算Amount_Due的触发器?

0 个答案:

没有答案