-- CIRCULAR SELF REFERENCING FK
-- ON DELETE CASCADE
drop table testB if exists;
create cached table testB(id integer, parent integer, ref integer,
 data varchar(200), unique(parent), primary key (id),foreign key (parent)
 references testB(id) on delete cascade, foreign key (id)
 references testB(parent) on delete cascade);
/*u1*/insert into testB values(100,100,1,'xxxx');
/*u1*/insert into testB values(200,200,1,'xxxx');
/*u1*/delete from testB where id=100;
/*r200,200,1,xxxx*/select * from testB
-- NORMAL SELF REFERENCING FK
-- ON DELETE CASCADE
drop table testB if exists;
create cached table testB(id integer, parent integer, ref integer,
 data varchar(200), unique (id), foreign key (parent)
 references testB(id) on delete cascade);
/*u1*/insert into testB values(100,100,1,'xxxx');
/*u1*/insert into testB values(101,100,1,'xxxx');
/*u1*/insert into testB values(102,100,1,'xxxx');
/*u1*/insert into testB values(200,200,1,'xxxx');
/*u1*/delete from testB where id=100;
/*r200,200,1,xxxx*/select * from testB
/*c1*/select * from testB
-- ON DELETE SET NULL
drop table testB if exists;
create cached table testB(id integer, parent integer, ref integer,
 data varchar(200), unique (id), foreign key (parent)
 references testB(id) on delete set null);
/*u1*/insert into testB values(100,100,1,'xxxx');
/*u1*/insert into testB values(101,100,1,'xxxx');
/*u1*/insert into testB values(102,100,1,'xxxx');
/*u1*/insert into testB values(200,200,1,'xxxx');
/*u1*/delete from testB where id=100;
/*r
 101,NULL,1,xxxx
 102,NULL,1,xxxx
 200,200,1,xxxx
*/select * from testB order by id
/*c2*/select * from testB where parent is null
-- ON DELETE SET DEFAULT

delete from testB;
/*u4*/insert into testB values(100,100,1,'xxxx'),
 (101,100,1,'xxxx'),
 (102,100,1,'xxxx'),
 (200,200,1,'xxxx')

drop table testB if exists;
create cached table testB(id integer, parent integer default 20, ref integer,
 data varchar(200), unique (id),foreign key (parent)
 references testB(id) on delete set default);
/*u1*/insert into testB values(20,20,1,'xxxx');
/*u1*/insert into testB values(100,100,1,'xxxx');
/*u1*/insert into testB values(101,100,1,'xxxx');
/*u1*/insert into testB values(200,200,1,'xxxx');
/*u1*/delete from testB where id=100;
/*r
 20,20,1,xxxx
 101,20,1,xxxx
 200,200,1,xxxx
*/select * from testB order by id
/*c2*/select * from testB where parent=20
-- CHAINED SELF REFERENCING FK
-- ON DELETE CASCADE
drop table testA if exists;
create cached table testA(a int primary key,b int,
    foreign key(b) references testA(a) on update cascade on delete cascade);
insert into testA(a,b) values(1,1);
insert into testA(a,b) values(2,1);
insert into testA(a,b) values(3,1);
insert into testA(a,b) values(4,2);
insert into testA(a,b) values(5,2);
insert into testA(a,b) values(6,2);
insert into testA(a,b) values(7,3);
insert into testA(a,b) values(8,3);
insert into testA(a,b) values(9,3);
/*u9*/update testA set a = a+1;
/*r3*/select count(*) from testA where b=4;
/*u9*/update testA set a = a-1;
/*r0*/select count(*) from testA where b=4;
/*r3*/select count(*) from testA where b=3;
/*u1*/delete from testA where a=1;
/*r0*/select count(*) from testA;
-- bug 870835
-- MIXED SELF AND FORWARD REFERENCE
-- UPDATE ISSUE
CREATE CACHED TABLE GroupSubject (
 description VARCHAR(10),
 parent BIGINT,
 admin BIGINT NOT NULL,
 id_ BIGINT,
 UNIQUE ( id_ ));
CREATE CACHED TABLE UserSubject (
 subjectName VARCHAR(10) NOT NULL,
 id_ BIGINT,
 UNIQUE ( id_ ),
 PRIMARY KEY ( subjectName ));
ALTER TABLE GroupSubject ADD CONSTRAINT
 GroupSubject_REF_parent
 FOREIGN KEY ( parent )
 REFERENCES GroupSubject ( id_ );
ALTER TABLE GroupSubject ADD CONSTRAINT
 GroupSubject_REF_admin
 FOREIGN KEY ( admin )
 REFERENCES UserSubject ( id_ );
insert into UserSubject values ('admin', 100);
insert into GroupSubject values (null, null, 100, 200);
/*u1*/update GroupSubject set description = null,
 parent = null, admin = 100 where id_ = 200;
/*r
 admin,100
*/select * from UserSubject
/*r
 NULL,NULL,100,200
*/select * from GroupSubject

-- MULTIPLE FK ISSUE
drop table testA if exists;
drop table testB if exists;
drop table testC if exists;
create cached table testA(id integer primary key);
create cached table testB(id integer, foreign key (id) references testA(id) on delete cascade);
create cached table testC(id integer, foreign key (id) references testA(id));
insert into testA values(1);
insert into testA values(2);
insert into testB values(1);
insert into testB values(2);
insert into testC values(1);
/*e*/delete from testA
/*c2*/select * from testB
/*c1*/select * from testC
/*u1*/delete from testA where id=2
/*c1*/select * from testA
/*c1*/select * from testB
/*c1*/select * from testC

-- INVALID SET DEFAULT
/*e*/create cached table testE(id integer, foreign key (id) references testA(id) on delete set default);
/*e*/create cached table testE(id integer primary key, idref integer, foreign key (idref) references teste(id) on delete set default);
create cached table testE(id integer primary key, idref integer);
/*e*/alter table testE add foreign key(idref) references testE(id) on delete set default;
alter table testE alter column idref set default 10;
alter table testE add foreign key(idref) references testE(id) on delete set default;
/*e*/alter table testE alter column idref drop default;

--
drop table testA if exists cascade;
drop table testB if exists;
drop table testC if exists;
create cached table testA(id integer primary key);
create cached table testB(id integer primary key, foreign key (id) references testA(id) on delete cascade);
create cached table testC(id integer primary key, foreign key (id) references testB(id) on delete cascade);
insert into testA values 10
insert into testB values 10
insert into testC values 10
alter table testA add foreign key (id) references testC(id) on delete cascade;
delete from testA
drop table testA if exists cascade;
drop table testB if exists cascade;
drop table testC if exists cascade;
--
create cached table testA(id integer primary key, ref integer, foreign key (ref) references testA(id));
insert into testA values 10, 10
insert into testA values 11, 10
insert into testA values 12, 10
insert into testA values 13, 12
delete from testA

CREATE TABLE user(
 USER_ID VARCHAR(15) NOT NULL
 ,PASSWD_TX VARCHAR(15) NOT NULL
 ,CONSTRAINT user_PK PRIMARY KEY (USER_ID)
 );

CREATE TABLE role(
 ROLE_NM VARCHAR(25) NOT NULL
 ,DESC VARCHAR(100) NOT NULL
 ,CONSTRAINT role_PK PRIMARY KEY (ROLE_NM)
 );

CREATE TABLE user_roles(
 USER_ID VARCHAR(15) NOT NULL
 ,ROLE_NM VARCHAR(25) NOT NULL
 ,CONSTRAINT user_roles_PK PRIMARY KEY (USER_ID,ROLE_NM)
 );

ALTER TABLE user_roles ADD CONSTRAINT FK_user_roles_user_0 FOREIGN KEY (USER_ID) REFERENCES user;
ALTER TABLE user_roles ADD CONSTRAINT FK_user_roles_role_1 FOREIGN KEY (ROLE_NM) REFERENCES role;

INSERT INTO user VALUES('JOE_USER', 'secret');
INSERT INTO role VALUES('SUPERSTAR', 'The best role');
INSERT INTO role VALUES('ACTUAL_TALENT', 'What there''s a difference?');

INSERT INTO user_roles VALUES('JOE_USER', 'SUPERSTAR');

UPDATE user SET USER_ID='JOE_USER', PASSWD_TX = 'eat@joes37' WHERE USER_ID = 'JOE_USER'

--

create table department (depid identity primary key,depname varchar(20),parentid int)
ALTER TABLE department ADD FOREIGN KEY (parentid) REFERENCES department(depid)
insert into department (depid,depname) values(1,'dep1')
insert into department values(2,'depchild',1)
delete from department

create table tfk(c char, id int, v varchar(10), primary key(c, id))
create table tfk2(c char, id int, v varchar(10),
 foreign key(id, c) references tfk(id, c))
insert into tfk values 'f' , 1, 'string 1'
insert into tfk values 'f' , 4, 'string 4'
insert into tfk2 values 'f' , 1, 'string 1'
insert into tfk2 values 'f' , 4, 'string 4'
insert into tfk2 values 'f' , 1, 'string 1'
/*e*/insert into tfk2 values 'e' , 4, 'string 4'

CREATE CACHED TABLE article_a (id INT PRIMARY KEY, val VARCHAR(20));
CREATE CACHED TABLE employee_a(id INT PRIMARY KEY, val VARCHAR(20));
CREATE CACHED TABLE move_a (
 id INTEGER IDENTITY PRIMARY KEY,
 employee_id INTEGER,
 article_id INTEGER,
 batch VARCHAR(10),
 source_address VARCHAR(15),
 dest_address VARCHAR(15),
 quantity DECIMAL(10,2),
 done DATETIME,
 CONSTRAINT fk_move_article FOREIGN KEY (article_id) REFERENCES article_a (id) ON DELETE CASCADE,
 CONSTRAINT fk_move_employee FOREIGN KEY (employee_id) REFERENCES employee_a (id) ON DELETE CASCADE
 );

CREATE INDEX idx_move_employee ON move_a (employee_id);
CREATE INDEX idx_move_article ON move_a (article_id);
CREATE INDEX idx_move_batch ON move_a (batch);

INSERT INTO article_a VALUES (10, 'TEN')
INSERT INTO employee_a VALUES (20, 'TWENTY')
INSERT INTO move_a VALUES default, 20, 10, 'test', 'test 1', 'test 2', 20.34, current_date
CREATE INDEX idx_move_done ON move_a (done);
