Check SQL的预订限制

时间:2019-03-18 16:09:08

标签: mysql

我想设置一个约束,不允许某人预订某辆汽车(如果已经预订)。我是一个初学者,所以我想知道检查/触发是否可以解决这个问题。例如。

ALTER TABLE bookings
ADD CHECK (vehicle_id = NOT NULL) 

表是

CREATE TABLE  customers  (
   customer_id  int(50)  NOT NULL AUTO_INCREMENT,
   fname  varchar(20) NOT NULL,
   lname  varchar(20) NOT NULL,
   dateOfBirth date NOT NULL,
   address  varchar(30) NOT NULL,
   city  varchar(20) NOT NULL,
   county  ENUM('Armagh','Carlow','Cavan','Clare','Cork','Derry','Donegal','Down','Dublin','Fermanagh','Galway','Kerry','Kildare','Kilkenny','Laois','Leitrim','Limerick','Longford','Louth','Mayo','Meath','Monaghan','Offaly','Roscommon','Sligo','Tipperary','Tyrone','Waterford','Westmeath','Wexford','Wicklow') NOT NULL,
   phone  int(10) NOT NULL,
   email  varchar(20) NOT NULL,
   payment_method  ENUM ('Visa', 'Amex', 'Cash', 'American Express', 'Mastercard') NOT NULL,
   valid_licence varchar(5) NOT NULL,
   status  varchar(10),
   PRIMARY KEY (customer_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

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)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


CREATE TABLE  vehicle_collections  (
   collection_id  int(10)NOT NULL AUTO_INCREMENT,
   vehicle_id  int(10)  NOT NULL ,
   collection_date  date NOT NULL,
   return_date  date NOT NULL,
   mileage_before  int(10) NOT NULL,
   mileage_after  int(10) NOT NULL,
   duration  int(5) NOT NULL,
   PRIMARY KEY (collection_id),
   KEY vehicle_idfk1 (vehicle_id),
   CONSTRAINT vehicle_idfk1 FOREIGN KEY (vehicle_id) REFERENCES vehicles (vehicle_id) 
   ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 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)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

这是我的表结构,如果有帮助的话。我想使用支票,但主要问题之一是-您如何用MySQL语言说“如果预定了车?” 。触发吗?

0 个答案:

没有答案