需要你的帮助
我有 2 个连接的实体:图书馆读者(订阅者)和图书问题。由于违反图书馆规则,阅读器可能会在几天内被锁定一段时间:
我需要添加检查,以便锁定期尚未结束的读者无法在图书馆借书(换句话说,Issue.Taken > Subscriber.Lock_Date+Subscriber.Lock_Period)
请帮忙,我该怎么做?
这实际上应该在业务逻辑中处理,而不是在表级别。
但是,您可以使用物化视图来做到这一点:
CREATE TABLE subscriber (
id INT PRIMARY KEY,
lock_date DATE,
lock_period INTERVAL DAY(5) TO SECOND
);
CREATE TABLE issue (
id INT PRIMARY KEY,
subscr_id INT NOT NULL REFERENCES subscriber( id ),
book_id INT,
taken DATE,
returned DATE
);
CREATE MATERIALIZED VIEW LOG ON subscriber
WITH SEQUENCE, ROWID( id, lock_date, lock_period )
INCLUDING NEW VALUES;
CREATE MATERIALIZED VIEW LOG ON issue
WITH SEQUENCE, ROWID( subscr_id, taken )
INCLUDING NEW VALUES;
CREATE MATERIALIZED VIEW subscriber_issue_MV
BUILD IMMEDIATE
REFRESH FAST ON COMMIT
AS SELECT s.id,
s.lock_date,
s.lock_period,
i.taken
FROM subscriber s
INNER JOIN
issue i
ON ( i.subscr_id = s.id );
ALTER TABLE subscriber_issue_MV ADD CONSTRAINT subscriber_issue__mv__chk
CHECK ( lock_date IS NULL
OR lock_period IS NULL
OR NOT taken BETWEEN lock_date AND lock_date + lock_period );
我同意 MTO 的观点,即此类验证应由应用程序代码(通过存储过程)处理。但是,如果您坚持要通过数据库执行此验证,那么以下触发器将很有帮助。同样,我不推荐这种解决方案,最好的方法是使用应用程序逻辑来处理它。
CREATE OR REPLACE TRIGGER trg_val_lock_dt
BEFORE INSERT ON issue
FOR EACH ROW
DECLARE
v_is_valid CHAR(1);
BEGIN
v_is_valid := 'Y';
SELECT 'N' INTO v_is_valid
FROM subscriber s
WHERE :NEW.subscr_id = s.subscr_id
AND :NEW.taken BETWEEN s.lock_date AND (s.lock_date + lock_period);
RAISE_APPLICATION_ERROR(-20001,'The subscriber is locked');
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
END;
上述触发器将在问题表中的每个插入之前触发。它将检查拍摄日期是否介于锁定日期和锁定日期 + 锁定期(这将是锁定结束日期)之间。如果找到这样的记录,则会抛出以下错误并且不会插入该行。
ORA-20001: The subscriber is locked
ORA-06512: at "RETAIL_1.TRG_VAL_LOCK_DT", line 12
如果不满足条件,则将引发 no data found 异常,触发器将不执行任何操作并插入行。