我想在带有日期和信息列的表中插入行。如果已经有一行具有相同的信息值且日期与今天的距离> = 1天,则不应发生插入。
假设当前日期:2017-01-03
id, info, date
1, a, 2017-01-01
2, b, 2017-01-02
要插入的新行:
info
c
a
b
预期运营:
c: gets inserted because no other row has the same info value
a: gets inserted because existing a has a date more than 1 day in the past
b: no insert because the time difference is not big enough
预期结果:
id, info, date
1, a, 2017-01-01
2, b, 2017-01-02
3, c, 2017-03-03
4, a, 2017-03-03
答案 0 :(得分:0)
对于该约束,您必须触发以避免插入,这是一个未经测试的触发器示例,您可以实现所需的任何逻辑:
CREATE OR REPLACE TRIGGER tg_triggername
BEFORE INSERT OR UPDATE ON table_name
FOR EACH ROW
BEGIN
IF( your_logic )
THEN
RAISE_APPLICATION_ERROR(
-20002,
'can't in sert already a row with the same info value and a date that is >= 1 day difference to today' );
END IF;
END;