/**************************************************************************************/
/* File:        demo14                                                                */
/* Project:     ATSQL2 : Referential integrity, column constraints, assertions        */
/* Author:      Andreas Steiner                                                       */
/* Date:        06-12-1995                                                            */
/* Results:                                                                           */
/**************************************************************************************/

create table s(i integer, s varchar(32));
create table v(i integer, s varchar(32)) as valid;
create table t(i integer, s varchar(32)) as transaction;
create table b(i integer, s varchar(32)) as valid and transaction;


/* Snapshot referential integrity : */
/* -------------------------------- */

create table t1(s varchar(20), k integer references s(i));
create table t2(s varchar(20), k integer references s(i)) as valid;
create table t3(s varchar(20), k integer references s(i)) as transaction;
create table t4(s varchar(20), k integer references s(i)) as valid and transaction;

drop table t1;
drop table t2;
drop table t3;
drop table t4;


create table t1(s varchar(20), k integer references v(i));
create table t2(s varchar(20), k integer references v(i)) as valid;
create table t3(s varchar(20), k integer references v(i)) as transaction;
create table t4(s varchar(20), k integer references v(i)) as valid and transaction;

drop table t1;
drop table t2;
drop table t3;
drop table t4;


create table t1(s varchar(20), k integer references t(i));
create table t2(s varchar(20), k integer references t(i)) as valid;
create table t3(s varchar(20), k integer references t(i)) as transaction;
create table t4(s varchar(20), k integer references t(i)) as valid and transaction;

drop table t1;
drop table t2;
drop table t3;
drop table t4;


create table t1(s varchar(20), k integer references b(i));
create table t2(s varchar(20), k integer references b(i)) as valid;
create table t3(s varchar(20), k integer references b(i)) as transaction;
create table t4(s varchar(20), k integer references b(i)) as valid and transaction;

drop table t1;
drop table t2;
drop table t3;
drop table t4;



/* Valid referential integrity : */
/* ----------------------------- */

create table t1(s varchar(20), k integer valid references s(i));
create table t2(s varchar(20), k integer valid references s(i)) as valid;
create table t3(s varchar(20), k integer valid references s(i)) as transaction;
create table t4(s varchar(20), k integer valid references s(i)) as valid and transaction;


create table t1(s varchar(20), k integer valid references v(i));
create table t2(s varchar(20), k integer valid references v(i)) as valid;
create table t3(s varchar(20), k integer valid references v(i)) as transaction;
create table t4(s varchar(20), k integer valid references v(i)) as valid and transaction;

drop table t2;
drop table t4;


create table t1(s varchar(20), k integer valid references t(i));
create table t2(s varchar(20), k integer valid references t(i)) as valid;
create table t3(s varchar(20), k integer valid references t(i)) as transaction;
create table t4(s varchar(20), k integer valid references t(i)) as valid and transaction;


create table t1(s varchar(20), k integer valid references b(i));
create table t2(s varchar(20), k integer valid references b(i)) as valid;
create table t3(s varchar(20), k integer valid references b(i)) as transaction;
create table t4(s varchar(20), k integer valid references b(i)) as valid and transaction;

drop table t2;
drop table t4;


drop table s;
drop table v;
drop table t;
drop table b;


/**************************************************************************************/

/* Temporal upward compatible referential integrity constraint : */
/* ============================================================= */

create table dep(d_name char(10) primary key) as valid;

create table emp(e_name char(10) primary key, 
                 ed_name char(10) references dep(d_name)) as valid;


set clock to timestamp '1995/12';

nonsequenced valid period '1989-1996'
insert into dep values ('AD');

nonsequenced valid period '1984-forever'
insert into dep values ('DM');

nonsequenced valid period '1995-1997'
insert into emp values ('Hinz','AD');

commit;


set clock to timestamp '1996/1';

commit;
/* Error(s) found checking referential integrity * emp.ed_name references dep.d_name * */

drop table emp;
drop table dep;


/**************************************************************************************/

/* Column Constraints : */
/* ==================== */

/* Snapshot table : */
/* ---------------- */

set clock to timestamp '1995/12/1';

create table test(i integer valid check (i > 5 AND i < 10));
/* Error: snapshot table/view test contains no valid time */

create table test(i integer check (i > 5 AND i < 10));
commit;

insert into test values(1);
commit;
/* Error(s) found checking table-/column-constraint of table * test * */


insert into test values(6);
commit;
/* Commit complete */


insert into test values(10);
commit;
/* Error(s) found checking table-/column-constraint of table * test * */

drop table test;


/* Transaction-time table : */
/* ------------------------ */

set clock to timestamp '1995/12/1';

create table test(i integer valid check (i > 5 AND i < 10)) as transaction;
/* Error: transaction table test contains no valid time */

create table test(i integer check (i > 5 AND i < 10)) as transaction;
commit;

insert into test values(1);
commit;
/* Error(s) found checking table-/column-constraint of table * test * */


insert into test values(6);
commit;
/* Commit complete */


insert into test values(10);
commit;
/* Error(s) found checking table-/column-constraint of table * test * */

drop table test;


/* Valid-time table : */
/* ------------------ */

set clock to timestamp '1995/12/1';

create table test(i integer valid check (i > 5 AND i < 10)) as valid;
commit;

nonsequenced valid period '1980-forever'
insert into test values(1);
commit;
/* Error(s) found checking table-/column-constraint of table * test * */


nonsequenced valid period '1980-forever'
insert into test values(6);
commit;
/* Commit complete */


nonsequenced valid period '1980-forever'
insert into test values(10);
commit;
/* Error(s) found checking table-/column-constraint of table * test * */


nonsequenced valid period '1997-forever'
delete from test where i = 6;
commit;
/* Commit complete */

drop table test;



create table test(i integer valid check (i > 5 AND 
                                         i < 10 AND
                                         interval(VALID(test)) > interval '10' year)
                 ) as valid;
commit;

insert into test values(1);
commit;
/* Error(s) found checking table-/column-constraint of table * test * */


insert into test values(6);
commit;
/* Commit complete */


insert into test values(10);
commit;
/* Error(s) found checking table-/column-constraint of table * test * */


nonsequenced valid period '1997-forever'
delete from test where i = 6;
commit;
/* Error(s) found checking table-/column-constraint of table * test * */

drop table test;



/* Bitemporal table : */
/* ------------------ */

set clock to timestamp '1995/12/1';

create table test(i integer valid check (i > 5 AND i < 10)) as valid and transaction;
commit;

nonsequenced valid period '1980-forever'
insert into test values(1);
commit;
/* Error(s) found checking table-/column-constraint of table * test * */


nonsequenced valid period '1980-forever'
insert into test values(6);
commit;
/* Commit complete */


nonsequenced valid period '1980-forever'
insert into test values(10);
commit;
/* Error(s) found checking table-/column-constraint of table * test * */


nonsequenced valid period '1997-forever'
delete from test where i = 6;
commit;
/* Commit complete */

drop table test;



create table test(i integer valid check (i > 5 AND 
                                         i < 10 AND
                                         interval(VALID(test)) > interval '10' year)
                 ) as valid and transaction;
commit;

insert into test values(1);
commit;
/* Error(s) found checking table-/column-constraint of table * test * */


insert into test values(6);
commit;
/* Commit complete */


insert into test values(10);
commit;
/* Error(s) found checking table-/column-constraint of table * test * */


nonsequenced valid period '1997-forever'
delete from test where i = 6;
commit;
/* Error(s) found checking table-/column-constraint of table * test * */

drop table test;



/* Snapshot column constraints with subqueries : */
/* --------------------------------------------- */

create table test(i integer check (not exists(select * from s where i = s.i)));
/* Error: table or view  * s * does not exist */

create table s(i integer);

create table test(i integer check (not exists(select * from s where i = s.i)));
/* Error: invalid or ambiguous column name * i * */

create table test(i integer valid check (not exists(select * from s where test.i = s.i)));
/* Error: snapshot table/view test contains no valid time */

create table test(i integer check (not exists(select * from s where test.i = s.i)));
/* Table created */
commit;

insert into test values (1);
check;
/* No integrity constraints violated */

insert into s values (1);
commit;
/* Error(s) found checking column constraint of table * test * */


insert into test values (1);
insert into test values (3);

insert into s values (2);
commit;

drop table test;


create table test(i integer check (not exists(select * from s where test.i = s.i)),
                  l integer check (not exists(select * from s where l = s.i)));
/* Table created */

drop table test;

drop table s;



/* Transaction table with column constraints with subqueries : */
/* ----------------------------------------------------------- */

create table test(i integer check (not exists(select * from s where i = s.i))) 
as transaction;
/* Error: table or view  * s * does not exist */


create table s(i integer);

create table test(i integer check (not exists(select * from s where i = s.i)))
as transaction;
/* Error: invalid or ambiguous column name * i * */

create table test(i integer valid check (not exists(select * from s where test.i = s.i)))
as transaction;
/* Error: transaction table test contains no valid time */

create table test(i integer check (not exists(select * from s where test.i = s.i)))
as transaction;
/* Table created */
commit;

insert into test values (1);
check;
/* No integrity constraints violated */

insert into s values (1);
commit;
/* Error(s) found checking column constraint of table * test * */


insert into test values (1);
insert into test values (3);

insert into s values (2);
commit;
/* Commit complete */

drop table test;



create table test(i integer check (not exists(select * from s where test.i = s.i)),
                  l integer check (not exists(select * from s where l = s.i)))
as transaction;
/* Table created */

drop table test;
drop table s;



/* Valid column constraints with subqueries : */
/* ------------------------------------------ */

create table test(i integer valid check (not exists(select * from v where test.i = v.i)))
as valid;
/* Error: table or view  * v * does not exist */


create table v(i integer) as valid;

create table test(i integer valid check (not exists(select * from v where test.i = v.i)))
as valid;
/* Table created */
commit;


nonsequenced valid period '1980-forever'
insert into test values (1);
commit;
/* Commit complete */

nonsequenced valid period '1980-1990'
insert into v values (1);
commit;
/* Error(s) found checking column constraint of table * test * */

nonsequenced valid period '1970-1980'
insert into v values (1);
commit;
/* Commit complete */

drop table test;
drop table v;


/* Bitemporal table column constraints with subqueries : */
/* ----------------------------------------------------- */

create table test(i integer valid check (not exists(select * from v where test.i = v.i)))
as valid and transaction;
/* Error: table or view  * v * does not exist */


create table v(i integer) as valid;

create table test(i integer valid check (not exists(select * from v where test.i = v.i)))
as valid and transaction;
/* Table created */
commit;


nonsequenced valid period '1980-forever'
insert into test values (1);
commit;
/* Commit complete */

nonsequenced valid period '1980-1990'
insert into v values (1);
commit;
/* Error(s) found checking column constraint of table * test * */

nonsequenced valid period '1970-1980'
insert into v values (1);
commit;
/* Commit complete */

 
drop table test;
drop table v;


/**************************************************************************************/

/* Table Constraints : */
/* =================== */

/* Snapshot table constraints with subqueries : */
/* -------------------------------------------- */

create table s(i integer);

create table test(k integer);
commit;

alter table test add valid check (not exists(select * from s where k = s.i));
/* Error: snapshot table/view test contains no valid time */

alter table test add check (not exists(select * from s where k = s.i));
commit;

insert into test values (1);
check;
/* No integrity constraints violated */

insert into s values (1);
commit;
/* Error(s) found checking column constraint of table * test * */


insert into test values (1);
insert into test values (3);

insert into s values (2);
commit;
/* Commit complete */

drop table test;
drop table s;


/* Transaction table constraints with subqueries : */
/* ----------------------------------------------- */

create table s(i integer);

create table test(k integer) as transaction;
commit;

alter table test add valid check (not exists(select * from s where k = s.i));
/* Error: transaction table test contains no valid time */

alter table test add check (not exists(select * from s where k = s.i));
commit;

insert into test values (1);
check;
/* No integrity constraints violated */

insert into s values (1);
commit;
/* Error(s) found checking column constraint of table * test * */


insert into test values (1);
insert into test values (3);

insert into s values (2);
commit;
/* Commit complete */

drop table test;
drop table s;



/* Valid table constraints with subqueries : */
/* ----------------------------------------- */

create table v(i integer) as valid;

create table test(i integer) as valid;
/* Table created */
commit;

alter table test add valid check (not exists(select * from v where test.i = v.i));
commit;

nonsequenced valid period '1980-forever'
insert into test values (1);
commit;
/* Commit complete */

nonsequenced valid period '1980-1990'
insert into v values (1);
commit;
/* Error(s) found checking table-/column-constraint of table * test * */

nonsequenced valid period '1970-1980'
insert into v values (1);
commit;
/* Commit complete */

 
drop table test;
drop table v;


/* Bitemporal table constraints with subqueries : */
/* ---------------------------------------------- */

create table v(i integer) as valid;

create table test(i integer) as valid and transaction;
/* Table created */
commit;

alter table test add valid check (not exists(select * from v where test.i = v.i));
commit;

nonsequenced valid period '1980-forever'
insert into test values (1);
commit;
/* Commit complete */

nonsequenced valid period '1980-1990'
insert into v values (1);
commit;
/* Error(s) found checking table-/column-constraint of table * test * */

nonsequenced valid period '1970-1980'
insert into v values (1);
commit;
/* Commit complete */

 
drop table test;
drop table v;


/**************************************************************************************/

/* Assertions : */
/* ============ */

/* Snapshot assertions with subqueries for snapshot tables : */
/* --------------------------------------------------------- */

create table s(i integer);

create table test(k integer);
commit;

create assertion check_test valid check (
  not exists(select * from test where exists(select * from s where k = s.i)));
/* Error: snapshot table/view test contains no valid time */

create assertion check_test check (
  not exists(select * from test where exists(select * from s where k = s.i)));
commit;

insert into test values (1);
check;
/* No integrity constraints violated */

insert into s values (1);
commit;
/* Error(s) found checking assertion * check_test * */


insert into test values (1);
insert into test values (3);

insert into s values (2);
commit;
/* Commit complete */

drop assertion check_test;


create assertion check_test check (
  exists(select * from test where exists(select * from s where k = s.i)));
check;

insert into test values (2);
check;

drop table test;
drop table s;

drop assertion check_test;



/* Snapshot assertions with subqueries for transaction tables : */
/* ------------------------------------------------------------ */

create table s(i integer);

create table test(k integer) as transaction;
commit;

create assertion check_test valid check (
  not exists(select * from test where exists(select * from s where k = s.i)));
/* Error: transaction table test contains no valid time */

create assertion check_test check (
  not exists(select * from test where exists(select * from s where k = s.i)));
commit;

insert into test values (1);
check;
/* No integrity constraints violated */

insert into s values (1);
commit;
/* Error(s) found checking assertion * check_test * */


insert into test values (1);
insert into test values (3);

insert into s values (2);
commit;
/* Commit complete */

drop assertion check_test;


create assertion check_test check (
  exists(select * from test where exists(select * from s where k = s.i)));
check;
/* Error(s) found checking assertion * check_test * */

insert into test values (2);
check;
/* No integrity constraints violated */

drop table test;
drop table s;

drop assertion check_test;



/* Valid table constraints with subqueries for valid tables : */
/* ---------------------------------------------------------- */

create table v(i integer) as valid;

create table test(i integer) as valid;
/* Table created */
commit;

create assertion check_test valid check (
  not exists(select * from test where exists(select * from v where test.i = v.i)));
commit;


nonsequenced valid period '1980-forever'
insert into test values (1);
commit;
/* Commit complete */

nonsequenced valid period '1980-1990'
insert into v values (1);
commit;
/* Error(s) found checking assertion * check_test * */

nonsequenced valid period '1970-1980'
insert into v values (1);
commit;
/* Commit complete */

drop assertion check_test;


create assertion check_test valid check (
  exists(select * from test where exists(select * from v where test.i = v.i)));
check;
/* Error(s) found checking assertion * check_test * */

nonsequenced valid period '1985-1990'
insert into v values (1);
check;
/* No integrity constraints violated */
 

drop table test;
drop table v;

drop assertion check_test;



/* Valid table constraints with subqueries for bitemporal tables : */
/* --------------------------------------------------------------- */

create table v(i integer) as valid;

create table test(i integer) as valid and transaction;
/* Table created */
commit;

create assertion check_test valid check (
  not exists(select * from test where exists(select * from v where test.i = v.i)));
commit;


nonsequenced valid period '1980-forever'
insert into test values (1);
commit;
/* Commit complete */

nonsequenced valid period '1980-1990'
insert into v values (1);
commit;
/* Error(s) found checking assertion * check_test * */

nonsequenced valid period '1970-1980'
insert into v values (1);
commit;
/* Commit complete */

drop assertion check_test;


create assertion check_test valid check (
  exists(select * from test where exists(select * from v where test.i = v.i)));
check;
/* Error(s) found checking assertion * check_test * */

nonsequenced valid period '1985-1990'
insert into v values (1);
check;
/* No integrity constraints violated */
 

drop table test;
drop table v;

drop assertion check_test;
