################# # Test RESTRICT # ################# create table parent( id int unique key ) engine innodb; create table child( parent_id int, sys_start SYS_DATATYPE as row start invisible, sys_end SYS_DATATYPE as row end invisible, period for system_time(sys_start, sys_end), foreign key(parent_id) references parent(id) on delete restrict on update restrict ) engine innodb with system versioning; insert into parent values(1); insert into child values(1); delete from parent where id = 1; ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (`test2`.`child`, CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`)) delete from child where parent_id = 1; delete from parent where id = 1; insert into parent values(1); insert into child values(1); update parent set id=id+1; ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (`test2`.`child`, CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`)) delete from child; update parent set id=id+1; select * from child for system_time all; parent_id 1 1 drop table child; drop table parent; ############################################## # Test when clustered index is a foreign key # ############################################## create table parent( id int(10) unsigned unique key ) engine innodb; create table child( parent_id int(10) unsigned primary key, sys_start SYS_DATATYPE as row start invisible, sys_end SYS_DATATYPE as row end invisible, period for system_time(sys_start, sys_end), foreign key(parent_id) references parent(id) ) engine innodb with system versioning; insert into parent values(1); insert into child values(1); delete from parent where id = 1; ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (`test2`.`child`, CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`)) drop table child; drop table parent; ################ # Test CASCADE # ################ create table parent( id int unique key ) engine innodb; create table child( parent_id int, sys_start SYS_DATATYPE as row start invisible, sys_end SYS_DATATYPE as row end invisible, period for system_time(sys_start, sys_end), foreign key(parent_id) references parent(id) on delete cascade on update cascade ) engine innodb with system versioning; insert into parent values(1); insert into child values(1); delete from parent where id = 1; select * from child; parent_id select * from child for system_time all; parent_id 1 insert into parent values(1); insert into child values(1); update parent set id = id + 1; select * from child; parent_id 2 select * from child for system_time all; parent_id 1 1 2 drop table child; drop table parent; create or replace table parent ( id int primary key, sys_start SYS_DATATYPE as row start invisible, sys_end SYS_DATATYPE as row end invisible, period for system_time(sys_start, sys_end) ) with system versioning engine innodb; create or replace table child ( x int, parent_id int not null, constraint `parent-fk` foreign key (parent_id) references parent (id) on delete cascade on update restrict ) engine innodb; insert into parent (id) values (2); insert into child (x, parent_id) values (2, 2); delete from parent; select * from child; x parent_id drop table child; drop table parent; create or replace table parent ( id int primary key ) engine innodb; create or replace table child ( id int primary key, parent_id int not null, row_start SYS_DATATYPE as row start invisible, row_end SYS_DATATYPE as row end invisible, period for system_time(row_start, row_end), constraint `parent-fk` foreign key (parent_id) references parent (id) on delete cascade on update restrict ) with system versioning engine innodb; insert into parent (id) values (3); insert into child (id, parent_id) values (3, 3); delete from parent; select * from child; id parent_id select *, check_row(row_start, row_end) from child for system_time all; id parent_id check_row(row_start, row_end) 3 3 HISTORICAL ROW drop table child; drop table parent; ################# # Test SET NULL # ################# create or replace table parent( id int unique key ) engine innodb; create or replace table child( parent_id int, sys_start SYS_DATATYPE as row start invisible, sys_end SYS_DATATYPE as row end invisible, period for system_time(sys_start, sys_end), foreign key(parent_id) references parent(id) on delete set null on update set null ) engine innodb with system versioning; insert into parent values(1); insert into child values(1); delete from child; insert into child values(1); delete from parent where id = 1; select * from child; parent_id NULL select *, current_row(sys_end) as current_row from child for system_time all order by sys_end; parent_id current_row 1 0 1 0 NULL 1 delete from child; insert into parent values(1); insert into child values(1); update parent set id= id + 1; select * from child; parent_id NULL select *, current_row(sys_end) as current_row from child for system_time all order by sys_end; parent_id current_row 1 0 1 0 NULL 0 1 0 NULL 1 drop table child; drop table parent; ########################### # Parent table is foreign # ########################### create or replace table parent( id int unique key, sys_start SYS_DATATYPE as row start invisible, sys_end SYS_DATATYPE as row end invisible, period for system_time(sys_start, sys_end) ) engine innodb with system versioning; create or replace table child( parent_id int, foreign key(parent_id) references parent(id) ) engine innodb; insert into parent values(1); insert into child values(1); delete from parent; ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (`test2`.`child`, CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`)) update parent set id=2; ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (`test2`.`child`, CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`)) delete from child; delete from parent; insert into child values(1); ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`test2`.`child`, CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`)) insert into parent values(1); insert into child values(1); delete from parent; ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (`test2`.`child`, CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`)) update parent set id=2; ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (`test2`.`child`, CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`)) drop table child; drop table parent; ################### # crash on DELETE # ################### create or replace table a ( cola int(10) primary key, v_cola int(10) as (cola mod 10) virtual, sys_start SYS_DATATYPE as row start invisible, sys_end SYS_DATATYPE as row end invisible, period for system_time(sys_start, sys_end) ) engine=innodb with system versioning; create index v_cola on a (v_cola); create or replace table b( cola int(10), v_cola int(10), sys_start SYS_DATATYPE as row start invisible, sys_end SYS_DATATYPE as row end invisible, period for system_time(sys_start, sys_end) ) engine=innodb with system versioning; alter table b add constraint `v_cola_fk` foreign key (v_cola) references a (v_cola); insert into a(cola) values (12); insert into b(cola, v_cola) values (10,2); delete from a; ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (`test2`.`b`, CONSTRAINT `v_cola_fk` FOREIGN KEY (`v_cola`) REFERENCES `a` (`v_cola`)) drop table b, a; ############################################### # CASCADE UPDATE foreign not system versioned # ############################################### create or replace table parent ( id smallint unsigned not null auto_increment, value int unsigned not null, primary key (id, value) ) engine = innodb; create or replace table child ( id mediumint unsigned not null auto_increment primary key, parent_id smallint unsigned not null, parent_value int unsigned not null, sys_start SYS_DATATYPE as row start invisible, sys_end SYS_DATATYPE as row end invisible, period for system_time(sys_start, sys_end), constraint `fk_child_parent` foreign key (parent_id, parent_value) references parent (id, value) on delete cascade on update cascade ) engine = innodb with system versioning; create or replace table subchild ( id int not null auto_increment primary key, parent_id smallint unsigned not null, parent_value int unsigned not null, constraint `fk_subchild_child_parent` foreign key (parent_id, parent_value) references child (parent_id, parent_value) on delete cascade on update cascade ) engine=innodb; insert into parent (value) values (23); select id, value from parent into @id, @value; insert into child values (default, @id, @value); insert into subchild values (default, @id, @value); select parent_id from subchild; parent_id 1 update parent set id = 11, value = value + 1; select parent_id from subchild; parent_id 11 select * from child; id parent_id parent_value 1 11 24 delete from parent; select count(*) from child; count(*) 0 select * from child for system_time all; id parent_id parent_value 1 1 23 1 11 24 select count(*) from subchild; count(*) 0 drop table subchild, child, parent; CREATE TABLE t1 (f1 INT, KEY(f1)) ENGINE=InnoDB; CREATE TABLE t2 (f2 INT, FOREIGN KEY (f2) REFERENCES t1 (f1)) ENGINE=InnoDB WITH SYSTEM VERSIONING; SET FOREIGN_KEY_CHECKS= OFF; INSERT IGNORE INTO t2 VALUES (1); SET FOREIGN_KEY_CHECKS= ON; UPDATE t2 SET f2= 2; ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`test2`.`t2`, CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`f2`) REFERENCES `t1` (`f1`)) DELETE FROM t2; DROP TABLE t2, t1; # # MDEV-18879 Corrupted record inserted by FOREIGN KEY operation # SET timestamp = 1; SET time_zone='+02:00'; SELECT now(); now() 1970-01-01 02:00:01 CREATE TABLE t1 ( pk INT UNSIGNED PRIMARY KEY, f1 varchar(255) CHARACTER SET ucs2, f2 longtext CHARACTER SET ucs2, f3 varchar(255), f4 char(255), f5 longtext CHARACTER SET ucs2, f6 INT UNSIGNED, f7 INT UNSIGNED, f8 INT UNSIGNED, f9 INT UNSIGNED, f10 INT UNSIGNED, f11 INT UNSIGNED, f12 varchar(255) CHARACTER SET ucs2, f13 char(255) CHARACTER SET ucs2, f14 char(255) CHARACTER SET ucs2, f15 varchar(255), f16 longtext, f17 char(255) ) ENGINE=InnoDB WITH SYSTEM VERSIONING; INSERT INTO t1 VALUES (1, 'a', 'e', 'f', 'a', 'generate', 1, 2, 3, 4, 5, 6, 'main', 'against', 'b', 'u', 'explode', 'tomorrow'), (2, REPEAT('a',127), 'f', 'k', 'game', 'g', 2, 3, 4, 5, 6, 7, REPEAT('o',222), 'oven', 'flower', REPEAT('r',120), 'l', 'g'), (3, 'weekly', 'x', 'v', 'r', 'c', 3, 4, 5, 6, 7, 8, 'validity', 'y', 'h', 'oxygen', 'venture', 'uncertainty'), (4, 'r', 't', REPEAT('b',153), 'modern', 'h', 4, 5, 6, 7, 8, 9, REPEAT('g',128), 'a', 'w', 'f', 'b', 'b'), (5, 'h', 'y', REPEAT('v',107), 'knife', 'profession', 5, 6, 7, 8, 9, 0, 'infection', 'u', 'likelihood', REPEAT('n',149), 'folk', 'd'), (6, 'g', 'violent', REPEAT('o',28), 'capital', 'p', 6, 7, 8, 9, 0, 1, 'w', 'patron', 'd', 'y', 'originally', 'k'), (7, 'k', 'uncomfortable', REPEAT('v',248), 'y', 'link', 7, 8, 9, 0, 1, 2, REPEAT('j',204), 'j', 'statute', 'emphasis', 'u', 'water'), (8, 'preparation', 'water', 'suck', 'silver', 'a', 8, 9, 0, 1, 2, 3, 'h', 'q', 'o', 't', 'k', 'y'), (9, 'y', 'f', 'e', 'a', 'dawn', 9, 0, 1, 2, 3, 4, 'peak', 'parking', 'b', 't', 'timber', 'c'), (10, REPEAT('h',78), 'apologize', 'direct', 'u', 'frankly', 0, 1, 2, 3, 4, 5, 'h', 'exhibit', 'f', 'd', 'effective', 'c'), (11, 'i', 'h', 'a', 'y', 'u', 1, 2, 3, 4, 5, 6, 'l', 'b', 'm', 'respond', 'ideological', 'credibility'); CREATE TABLE t2 ( pk int primary key, f char(255) CHARACTER SET ucs2, key(f) ) ENGINE=InnoDB; INSERT INTO t2 VALUES (1,'against'),(2,'q'); SET SQL_MODE= ''; SET timestamp = 2; SELECT * FROM t1 INTO OUTFILE 't1.data'; SET timestamp = 3; UPDATE t1 SET f13 = 'q'; SET timestamp = 4; LOAD DATA INFILE 't1.data' REPLACE INTO TABLE t1; Warnings: Warning 1265 Data truncated for column 'f12' at row 2 Warning 1265 Data truncated for column 'f12' at row 4 Warning 1265 Data truncated for column 'f12' at row 7 SELECT * FROM t1 INTO OUTFILE 't1.data.2' ; SET timestamp = 5; LOAD DATA INFILE 't1.data.2' REPLACE INTO TABLE t1; Warnings: Warning 1265 Data truncated for column 'f1' at row 2 Warning 1265 Data truncated for column 'f12' at row 2 Warning 1265 Data truncated for column 'f12' at row 4 Warning 1265 Data truncated for column 'f12' at row 7 Warning 1265 Data truncated for column 'f1' at row 10 SELECT * FROM t2 INTO OUTFILE 't2.data'; SET timestamp = 6; LOAD DATA INFILE 't2.data' REPLACE INTO TABLE t2; SET FOREIGN_KEY_CHECKS = OFF; ALTER TABLE t1 ADD FOREIGN KEY (f13) REFERENCES t2 (f) ON DELETE SET NULL; SET timestamp = 7; LOAD DATA INFILE 't1.data' REPLACE INTO TABLE t1; Warnings: Warning 1265 Data truncated for column 'f12' at row 2 Warning 1265 Data truncated for column 'f12' at row 4 Warning 1265 Data truncated for column 'f12' at row 7 SET FOREIGN_KEY_CHECKS = ON; SET SESSION SQL_MODE= 'NO_BACKSLASH_ESCAPES'; SET timestamp = 8; LOAD DATA INFILE 't1.data' REPLACE INTO TABLE t1; Warnings: Warning 1265 Data truncated for column 'f12' at row 2 Warning 1265 Data truncated for column 'f12' at row 4 Warning 1265 Data truncated for column 'f12' at row 7 SET timestamp = 9; REPLACE INTO t2 SELECT * FROM t2; DROP TABLE t1, t2;