对此的需求来自这样一个事实,即我们现在有许多更新敏感双时态表的来源,我们有点警觉并且想要掩护我们的背部。
我使用 Richard Snodgrass 的“Developing Time-Oriented Database Applications in SQL”来帮助我解决这个问题。
我一直在尝试提出一个触发器,该触发器断言在每次更新或插入后都会保留双时间合同。更具体地说,合约确保主键是有效时间和交易时间排序的,以及非排序的有效时间连续性断言。分解后断言如下:
VALID_TIME
断言“活动”时间线中没有重叠。断言时间线没有重叠
TRANSACTION_TIME
。VALID_TIME
断言时间轴上没有间隙。
“活跃”是指TRANSACTION_END
价值为“永远”的记录(9999-12-31)
。
这是我到目前为止所拥有的:
CREATE OR REPLACE TRIGGER TRIGGER_NAME
AFTER INSERT OR UPDATE
ON SOME_TABLE
DECLARE
ROWCOUNT INTEGER;
BEGIN
SELECT COUNT(*) INTO ROWCOUNT
FROM SOME_TABLE T1,SOME_TABLE T2
WHERE T1.PK_COLUMN2 = T2.PK_COLUMN2
AND T1.PK_COLUMN1 = T2.PK_COLUMN1
AND T1.TRANSACTION_START < T2.TRANSACTION_END
AND T2.TRANSACTION_START < T1.TRANSACTION_END
AND T1.ROWID != T2.ROWID;
IF (ROWCOUNT>0) THEN
RAISE_APPLICATION_ERROR(-20001, 'BITEMPORAL INTEGRITY TRIGGER CHECK : AUDIT_TIME OVERLAP');
END IF;
SELECT COUNT(*) INTO ROWCOUNT
FROM SOME_TABLE T1,SOME_TABLE T2
WHERE T1.PK_COLUMN2 = T2.PK_COLUMN2
AND T1.PK_COLUMN1 = T2.PK_COLUMN1
AND T1.VALID_START < T2.VALID_END
AND T2.VALID_START < T1.VALID_END
AND T1.TRANSACTION_END = DATE '9999-12-31'
AND T2.TRANSACTION_END = DATE '9999-12-31'
AND T1.ROWID != T2.ROWID;
IF (ROWCOUNT>0) THEN
RAISE_APPLICATION_ERROR(-20001, 'BITEMPORAL INTEGRITY TRIGGER CHECK : ACTIVE VALID_TIME OVERLAP');
END IF;
SELECT COUNT(*) INTO ROWCOUNT
FROM SOME_TABLE S, SOME_TABLE T2
WHERE S.VALID_END < T2.VALID_START
AND S.PK_COLUMN1 = T2.PK_COLUMN1
AND S.PK_COLUMN2 = T2.PK_COLUMN2
AND S.TRANSACTION_END = DATE '9999-12-31'
AND T2.TRANSACTION_END = DATE '9999-12-31'
AND NOT EXISTS (
SELECT *
FROM SOME_TABLE T3
WHERE T3.PK_COLUMN1 = S.PK_COLUMN1
AND T3.PK_COLUMN2 = S.PK_COLUMN2
AND (((T3.VALID_START <= S.VALID_END)
AND (S.VALID_END < T3.VALID_END))
OR ((T3.VALID_START < T2.VALID_START)
AND (T2.VALID_START <= T3.VALID_END)))
AND T3.TRANSACTION_END = DATE '9999-12-31');
IF (ROWCOUNT>0) THEN
RAISE_APPLICATION_ERROR(-20001, 'BITEMPORAL TRIGGER CHECK : ACTIVE VALID_TIME CONTINUITY VIOLATED (GAPS)');
END IF;
END;
PK_COLUMN(s)
是天然钥匙的气质,休息应该是显而易见的。
问题如下:
我是否包括了所有可能的情况?有没有我忘记核对的附加合同?
额外的问题,你能推荐任何其他关于双时态数据架构的可靠书籍/资源吗?
//添加了更多标签,以扩大覆盖范围...
欢迎任何意见、建议、建设性批评。
提前致谢,
马特。