使用此触发器代码时:
CREATE TRIGGER Verify_deathDate
BEFORE INSERT Or UPDATE of deathDate, birthDate on Actor
FOR EACH ROW
BEGIN
IF (:NEW.deathDate < :NEW.birthDate) THEN
Raise_application_error(-20000, 'birthDate should be before deathDate');
END IF;
END;
/
然后是此更新查询:
UPDATE Actor SET deathDate = '28-Dec-1937' , birthDate = '29-Dec-1938'
WHERE actorID = '00069';
我得到以下代码(只有一个是正确的):
ORA-20000: birthDate should be before deathDate
ORA-06512: at "OPS$U0853885.CHECK_DEATHDATE", line 3
ORA-04088: error during execution of trigger 'OPS$xxxxxxxx.CHECK_DEATHDATE'
答案 0 :(得分:4)
Oracle recommends你使用触发器来限制数据只能在你不能使用(除其他外)CHECK约束的地方。由于这实际上是更简单,你应该在这种情况下使用它;
CREATE TABLE Actor (
id int,
name varchar(32),
birthdate date,
deathdate date,
CONSTRAINT birth_death CHECK (deathdate > birthdate)
);
这将强制执行您的业务规则,而无需额外的触发器。
演示here。