触发允许每个客户 1 辆车,并且不允许其他客户在租赁和 return 日期之间租用已经租用的车辆

Trigger to allow 1 car per customer and not let another customer rent an already rented vehicle between rental and return date

我有一个基于数据库 (Oracle) 的项目来创建汽车租赁店。到目前为止一切顺利,但我被困在一个触发器上,该触发器必须检查客户以及租赁之间的汽车以及 return 日期,以便已经租用车辆的客户无法租用另一辆车并且另一个客户无法租用已经租用的车辆。

租金Table:

CREATE TABLE RentAuto
(
auto_id INT,
customer_id INT,
employee_id INT,
date_rent DATE,
date_return DATE,
rent_days VARCHAR2(5)
);

汽车 Table:

CREATE TABLE Automobile 
(
auto_id INTEGER NOT NULL,
year_of_production VARCHAR(10),
auto_current_km VARCHAR(20),
price_per_day VARCHAR(20),
color_id INT,
is_auto_av INT,
model_id INT,
PRIMARY KEY(auto_id)
);

插入 Table:

INSERT INTO RentAuto (auto_id,customer_id,employee_id,date_rent,date_return, rent_days)
VALUES(14,13,3,TO_DATE(sysdate, 'dd-mm-yyyy'), '29-03-2022','2');

我在下面写的触发器在 IF 语句上给我一个错误,我不知道如何修复它。 (PLS-00201:必须声明标识符'NEW.CUSTOMER_ID')

create or replace TRIGGER RENTINGTRIGGER
BEFORE INSERT OR UPDATE OF AUTO_ID,CUSTOMER_ID,DATE_RENT,DATE_RETURN ON RENTAUTO
FOR EACH ROW
BEGIN
IF RENTAUTO.CUSTOMER_ID = :NEW.CUSTOMER_ID
and ((new.DATE_RENT >= RentAuto.DATE_RETURN and new.DATE_RENT < RentAuto.DATE_RETURN)
        or (new.DATE_RETURN > RentAuto.DATE_RENT and new.DATE_RETURN < RentAuto.DATE_RETURN))
THEN
RAISE_APPLICATION_ERROR(-2099, 'You can only book one car per single customer a day');
IF RentAuto.AUTO_ID = :NEW.AUTO_ID
and 
 ((new.date_rent >= RentAuto.date_return and new.date_rent < RentAuto.date_return)
    or (new.date_return > RentAuto.date_rent and new.date_return < RentAuto.date_return))
THEN
RAISE_APPLICATION_ERROR(-2099, 'Car has already been rented by another customer!');
END IF;
END IF;
END;

这是一个简单的 after 语句触发器,当插入或更新导致客户每天预订超过一辆车或一天预订超过一次汽车时抛出异常。

如果插入或更新语句在多行中,触发器不会告诉我们哪个预订导致失败。此外,每次发生插入或更新时,我们都必须扫描整个 table 以查找冲突。如果您只想对插入或更新的行做出反应,则需要一个复合触发器,您可以在其中记住“每行之后”部分中的相关行,然后将它们与 [= 中的其他行进行比较15=] 在“后语句”部分。

CREATE OR REPLACE TRIGGER rentingtrigger
AFTER INSERT OR UPDATE OF auto_id, customer_id, date_rent, date_return ON rentauto
  v_count INTEGER;
BEGIN
  select count(*)
  into v_count
  from rentauto
  where exists
  (
    select null
    from rentauto other
    where other.customer_id = rentauto.customer_id
    and other.date_rent <= rentauto.date_return
    and other.date_return >= rentauto.date_rent
    and other.rowid <> rentauto.rowid
  );

  IF v_count > 0 THEN
    RAISE_APPLICATION_ERROR(-20099, 'You can only book one car per single customer a day');
  END IF;

  select count(*)
  into v_count
  from rentauto
  where exists
  (
    select null
    from rentauto other
    where other.auto_id = rentauto.auto_id
    and other.date_rent <= rentauto.date_return
    and other.date_return >= rentauto.date_rent
    and other.rowid <> rentauto.rowid
  );

  IF v_count > 0 THEN
    RAISE_APPLICATION_ERROR(-20099, 'Car has already been rented by another customer!');
  END IF;
END rentingtrigger;