Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>While waiting for next Oracle 12c version, which supports <a href="http://docs.oracle.com/cd/E16655_01/appdev.121/e17620/adfns_design.htm#ADFNS967" rel="nofollow">Temporal Validity</a>, I still use next approach:</p> <pre><code>create table item ( title varchar2(32) primary key ); create table price ( price_id number primary key, item varchar2(32) not null references item (title), price number(9,2), effective_from date not null, effective_to date not null, effective_prev_to date, constraint price_from_to_ck check ( effective_to &gt; effective_from ), constraint price_to_prev_ck check ( effective_from = effective_prev_to + 1 ), constraint price_from_uq unique ( item, effective_to ), constraint price_dates_chain_fk foreign key ( item, effective_prev_to ) references price ( item, effective_to ) ); insert into item values ('LETTUCE'); insert into item values ('WHISKY'); insert into price values ( 1, 'LETTUCE', 1.05, date '2013-01-01', date '2013-03-31', null ); insert into price values ( 2, 'LETTUCE', 1.08, date '2013-04-01', date '2013-06-30', date '2013-03-31' ); insert into price values ( 3, 'WHISKY', 33.99, date '2013-01-01', date '2013-05-31', null ); insert into price values ( 4, 'WHISKY', 31.15, date '2013-06-01', date '2013-07-31', date '2013-05-31' ); </code></pre> <p>Let's <a href="http://sqlfiddle.com/#!4/e7df1/1" rel="nofollow">try</a>:</p> <pre><code>insert into price values ( 5, 'WHISKY', 30.55, date '2013-05-15', date '2013-06-05', date '2013-05-14' ); ORA-02291: integrity constraint (USER_4_E7DF1.PRICE_DATES_CHAIN_FK) violated - parent key not found : insert into price values ( 'WHISKY', 30.55, date '2013-05-15', date '2013-06-05', date '2013-05-14' ) </code></pre> <p>But now updating and deleting dates in the middle of the chain is pain in the ass. It needs to change preceding and following rows in one statement with <code>MERGE</code>. That's why I've added <code>price_id</code> column, because you can't update a key in <code>MERGE</code> -- so, you need another key instead of (item, effective_%).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload