/***********************************************************/
/* Test case for the ensurance of referential integrity    */
/* Thomas Myrach, IWI Bern, 31-OCT-1995                    */
/***********************************************************/

/* Data definitions */
/* ---------------- */

/* Definition of bitemporal relation TMANAGER */

create table tmanager 
(m_name char(10) primary key, 
 e_rate integer, 
 rank char(10))
as valid and transaction;

/* Definition of bitemporal relation TACCOUNT */

create table taccount
(a_name char(10) primary key, 
 city char(10), 
 am_name char(10) valid references tmanager(m_name))
as valid and transaction;

/* Data Modifications */
/* ------------------ */

/* Year 1992 */
/*---------- */

set clock to timestamp '1992/10/25';

/* A new manager 'Elsa' is hired */

nonsequenced valid period '1992/11-forever'
insert into tmanager values ('Elsa',80,'junior');
commit;

set clock to timestamp '1992/11/01';

/* A new manager 'Robert' is hired */

nonsequenced valid period '1993-forever'
insert into tmanager values ('Robert',100,'senior');
commit;

set clock to timestamp '1992/11/05';

/* The account 'Roche' is opened */

nonsequenced valid period '1992/11/05-forever'
insert into taccount values ('Roche','Basel','Elsa');
commit;

set clock to timestamp '1992/12/15';

/* A new manager 'Joe' is hired */

nonsequenced valid period '1993-forever'
insert into tmanager values ('Joe',100,'assistant');
commit;

/* Year 1993 */
/* --------- */

/* Starting situation */
/* ------------------ */

set clock to timestamp '1993';

valid select * from tmanager as m
where timestamp 'now' precedes end(valid(m));

/*
             VALID m_name e_rate      rank
 ----------------- ------ ------ ---------
 [1992/11-forever)   Elsa     80    junior
    [1993-forever) Robert    100    senior
    [1993-forever)    Joe    100 assistant
*/

valid select * from taccount as a
where timestamp 'now' precedes end(valid(a));

/*
                VALID a_name  city am_name
 -------------------- ------ ----- -------
 [1992/11/05-forever)  Roche Basel    Elsa
*/

/* Events */
/* ------ */

set clock to timestamp '1993/01/04';

/* The account 'UBS' is opened */

nonsequenced valid period '1993-forever'
insert into taccount values ('UBS','Zurich','Robert');
commit;

set clock to timestamp '1993/01/05';

/* The account 'Ascom' is opened */

nonsequenced valid period '1993-forever'
insert into taccount values ('Ascom','Bern','Robert');
commit;

set clock to timestamp '1993/05/01';

/* Elsa leaves (temporarily) the company */

nonsequenced valid period '1993/08-1994/05'
delete from tmanager where m_name='Elsa';

/* Does that affect any accounts? */

valid 
  select a_name from taccount as a1
  where valid(a1) overlaps period '1993/08-1994/05'
  and not exists (select * from tmanager a2
                  where a1.am_name = a2.m_name);

/*
             VALID a_name
 ----------------- ------
 [1993/08-1994/05)  Roche
*/

/* The interruption of Elsa's employment is overlapped by
   the account 'Roche'. The execution of the deletion would 
   therefore lead to a violation of referential integrity */

commit;

/* To avoid RI violation, the account 'Roche' is reassigned */

nonsequenced valid period '1993/08-forever'
update taccount set am_name='Robert' where a_name='Roche';
commit;

/* Now, the deletion of Elsa should be unproblematic */

nonsequenced valid period '1993/08-1994/05'
delete from tmanager where m_name='Elsa';
commit;

/* Elsa will reduce her employment rate after the leave */

nonsequenced valid period '1994/05-forever'
update tmanager set e_rate=50 where m_name='Elsa';
commit;

set clock to timestamp '1993/09/07';

/* Joe is promoted to junior */

nonsequenced valid period '1993/09-forever'
update tmanager set rank='senior' where m_name='Joe';
commit;

set clock to timestamp '1993/09/12';

/* Correction of the incorrectly entered rank 'senior' */

nonsequenced valid period '1993/09-forever'
update tmanager set rank='junior' where m_name='Joe';
commit;

set clock to timestamp '1993/12/02';

/* A new manager 'Tony' is hired */

nonsequenced valid period '1994-forever'
insert into tmanager values ('Tony',80,'assistant');
commit;

/* The account 'Mobiliar' is opened */

nonsequenced valid period '1994-forever'
insert into taccount values ('Mobiliar','Bern','Tony');
commit;

set clock to timestamp '1993/12/20';

/* The account 'Ciba' is opened */

nonsequenced valid period '1994-forever'
insert into taccount values ('Ciba','Basel','Joe');
commit;

set clock to timestamp '1993/12/13';

/* Robert reduces his employment rate */

nonsequenced valid period '1994-forever'
update tmanager set e_rate=80 where m_name='Robert';
commit;

/* The account 'Roche' is reassigned */

nonsequenced valid period '1994-forever'
update taccount set am_name='Tony' where a_name='Roche';
commit;

/* Year 1994 */
/* --------- */

/* Starting situation */
/* ------------------ */

set clock to timestamp '1994';

valid select * from tmanager as m
where timestamp 'now' precedes end(valid(m));

/*
             VALID m_name e_rate      rank
 ----------------- ------ ------ ---------
 [1994/05-forever)   Elsa     50    junior
 [1993/09-forever)    Joe    100    junior
    [1994-forever)   Tony     80 assistant
    [1994-forever) Robert     80    senior
*/

valid select * from taccount as a
where timestamp 'now' precedes end(valid(a));

/*
          VALID   a_name   city am_name
 -------------- -------- ------ -------
 [1993-forever)      UBS Zurich  Robert
 [1993-forever)    Ascom   Bern  Robert
 [1994-forever) Mobiliar   Bern    Tony
 [1994-forever)     Ciba  Basel     Joe
 [1994-forever)    Roche  Basel    Tony
*/

/* Events */
/* ------ */

set clock to timestamp '1994/03/19';

/* Robert leaves the company */

nonsequenced valid period '1994/06-forever'
delete from tmanager where m_name='Robert';

/* Does that affect any accounts? */

valid
  select a_name from taccount as a1
  where am_name='Robert' 
  and valid(a1) overlaps period '1994/06-forever'
  and not exists (select * from tmanager a2
                  where a1.am_name = a2.m_name);

/*
             VALID a_name
 ----------------- ------
 [1994/06-forever)    UBS
 [1994/06-forever)  Ascom
*/

/* The two accounts 'UBS' and 'Ascom' are affected. Therefore: */

/* The account 'UBS' is reassigned */

nonsequenced valid period '1994/06-forever'
update taccount set am_name='Elsa' where a_name='UBS';

/* The account 'Ascom' is reassigned */

nonsequenced valid period '1994/06-forever'
update taccount set am_name='Joe' where a_name='Ascom';

commit;

set clock to timestamp '1994/05/01';

/* The account 'Nestle is opened */

nonsequenced valid period '1994/05/16-forever'
insert into taccount values ('Nestle','Vevey','Elsa');
commit;

set clock to timestamp '1994/12/02';

/* The account 'Mobiliar' is closed */

nonsequenced valid period '1995/03-forever'
delete from taccount where a_name='Mobiliar';
commit;

set clock to timestamp '1994/12/05';

/* Tony announces to leave the company */

nonsequenced valid period '1995/11-forever'
delete from tmanager where m_name='Tony';

/* Does that affect any accounts? */

valid
  select a_name from taccount as a1
  where am_name='Tony' 
  and valid(a1) overlaps period '1995/11-forever'
  and not exists (select * from tmanager a2
                  where a1.am_name = a2.m_name);

/*
             VALID a_name
 ----------------- ------
 [1995/11-forever)  Roche
*/

/* The account Roche would be affected
   Because the validity is in the future, the account shall be 
   noted to be unassigned, i.e., to have a NULL value */

/* This is currently [TimeDB V.1.0] not possible! 
   Instead we assign account Roche to Joe */

nonsequenced valid period '1995/11-forever'
update taccount set am_name='Joe' where a_name='Roche';

commit;

/* Joe is promoted */

nonsequenced valid period '1995-forever'
update tmanager set rank='senior' where m_name='Joe';
commit;

/* Year 1995 */
/* --------- */

/* Starting situation */
/* ------------------ */

set clock to timestamp '1995';

valid select * from tmanager as m
where timestamp 'now' precedes end(valid(m));

/*
             VALID m_name e_rate      rank
 ----------------- ------ ------ ---------
 [1994/05-forever)   Elsa     50    junior
    [1994-1995/11)   Tony     80 assistant
    [1995-forever)    Joe    100    senior
*/

valid select * from taccount as a
where timestamp 'now' precedes end(valid(a));

/*
                VALID   a_name   city am_name
 -------------------- -------- ------ -------
       [1994-forever)     Ciba  Basel     Joe
    [1994/06-forever)      UBS Zurich    Elsa
    [1994/06-forever)    Ascom   Bern     Joe
 [1994/05/16-forever)   Nestle  Vevey    Elsa
       [1994-1995/03) Mobiliar   Bern    Tony
       [1994-1995/11)    Roche  Basel    Tony
    [1995/11-forever)    Roche  Basel     Joe
*/

/* Events */
/* ------ */

set clock to timestamp '1995/01/04';

/* Tony decides to stay and gets promoted */

nonsequenced valid period '1995/11-forever'
insert into tmanager values ('Tony',100,'junior');
commit;

/* The temporarily unassigned account 'Roche' is reassigned */

/* This is only relevant if the NULL update works: */

nonsequenced valid period '1995/11-forever'
update taccount set am_name='Tony' where a_name='Roche';
commit;

set clock to timestamp '1995/01/10';

/* A new manager 'Anne' is hired */

nonsequenced valid period '1995/10-forever'
insert into tmanager values ('Anne',80,'assistant');
commit;

set clock to timestamp '1995/01/15';

/* The account 'Ciba' is reassigned */

nonsequenced valid period '1995/10-forever'
update taccount set am_name='Anne' where a_name='Ciba';
commit;

set clock to timestamp '1995/02/20';

/* A new account 'Wander' is opened */

nonsequenced valid period '1995/03-forever'
insert into taccount values ('Wander','Bern','Tony');
commit;

set clock to timestamp '1995/04/20';

/* The account 'UBS' is reassigned */

nonsequenced valid period '1995/08-forever'
update taccount set am_name='Joe' where a_name='UBS';
commit;

set clock to timestamp '1995/08/28';

/* Account 'Roche' is closed */

nonsequenced valid period '1995/10-forever'
delete from taccount where a_name='Roche';
commit;

set clock to timestamp '1995/09/18';

/* Two modification operations that would lead to RI violations: */

/* (a) Joe has left the company unexpectedly */

nonsequenced valid period '1995/07-forever'
delete from tmanager where m_name='Joe';
commit;

/* (b) Tonies employment interruption is retroactively documented */

nonsequenced valid period '1995/02/24-1995/03/18'
delete from tmanager where m_name='Tony';
commit;

/* End situation */
/* ------------- */

valid select * from tmanager as m
where timestamp 'now' precedes end(valid(m));

/*
             VALID m_name e_rate      rank
 ----------------- ------ ------ ---------
 [1994/05-forever)   Elsa     50    junior
    [1994-1995/11)   Tony     80 assistant
    [1995-forever)    Joe    100    senior
 [1995/11-forever)   Tony    100    junior
 [1995/10-forever)   Anne     80 assistant
*/

valid select * from taccount as a
where timestamp 'now' precedes end(valid(a));

/*
                VALID a_name   city am_name
 -------------------- ------ ------ -------
    [1994/06-forever)  Ascom   Bern     Joe
 [1994/05/16-forever) Nestle  Vevey    Elsa
       [1994-1995/10)   Ciba  Basel     Joe
    [1995/10-forever)   Ciba  Basel    Anne
    [1995/03-forever) Wander   Bern    Tony
    [1995/08-forever)    UBS Zurich     Joe
       [1994-1995/10)  Roche  Basel    Tony
*/

/* End of modifications */

remove clock;
