drop table c;
drop table b;
drop table a;
drop sequence seq;

-- create table a (a1 integer primary key , a2 integer);
-- create table b (b1 integer primary key , b2 integer references a);
-- create table c (c1 integer references b, c2 integer references a);

create table a (a1 integer, a2 integer);
create table b (b1 integer, b2 integer);
create table c (c1 integer, c2 integer);

create unique index ind_a_a1 on a(a1);
create unique index ind_b_b1 on b(b1);

alter table a add primary key (a1) using index ind_a_a1;
alter table b add primary key (b1) using index ind_b_b1;

alter table b add foreign key (b2) references a;
alter table c add foreign key (c1) references b;
alter table c add foreign key (c2) references a;

create sequence seq start with 54;

insert into a values (19, 31);
insert into a values (11, 42);
insert into a values (14, 34);
insert into a values (23, 45);
insert into a values (27, 39);
insert into a values (21, 47);

insert into a 
  select a2, a1 from a;

insert into a
  select a1 * 10, a2 * 3 - 4 from a;

insert into a
  select a1 * 100, a2 * 5 - 41 from a;

insert into a 
  select a1 + 1, a2 * 3 - 5 from a;

insert into b
  select seq.nextval, a1
    from a;

insert into b
  select seq.nextval * 2, a1
    from a
    where mod(a1, 2) = 0;


insert into b
  select seq.nextval * 3, a1
    from a
    where mod(a1, 3) = 0;

insert into c
  select b1, a1
    from a, b
    where a1 < b1;

commit;
