summaryrefslogtreecommitdiff
path: root/mysql-test/suite/versioning/t/foreign.test
blob: 831ba518337cd5f51f4fed12e6e16b87cf04f5f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
--source suite/versioning/key_type.inc
--source suite/versioning/common.inc

--echo #################
--echo # Test RESTRICT #
--echo #################

--replace_result "$KEY_TYPE" KEY_TYPE
eval create table parent(
  id int,
  $KEY_TYPE (id)
) engine innodb;

--replace_result $sys_datatype_expl SYS_DATATYPE
eval create table child(
  parent_id int,
  sys_start $sys_datatype_expl as row start invisible,
  sys_end $sys_datatype_expl 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);

-- error ER_ROW_IS_REFERENCED_2
delete from parent where id = 1;
delete from child where parent_id = 1;
delete from parent where id = 1;

insert into parent values(1);
insert into child values(1);
-- error ER_ROW_IS_REFERENCED_2
update parent set id=id+1;
delete from child;
update parent set id=id+1;
select * from child for system_time all;

drop table child;
drop table parent;

--echo ##############################################
--echo # Test when clustered index is a foreign key #
--echo ##############################################

--replace_result "$KEY_TYPE" KEY_TYPE
eval create table parent(
  id int(10) unsigned,
  $KEY_TYPE (id)
) engine innodb;

--replace_result $sys_datatype_expl SYS_DATATYPE
eval create table child(
  parent_id int(10) unsigned primary key,
  sys_start $sys_datatype_expl as row start invisible,
  sys_end $sys_datatype_expl 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);

-- error ER_ROW_IS_REFERENCED_2
delete from parent where id = 1;

drop table child;
drop table parent;

--echo ################
--echo # Test CASCADE #
--echo ################

--replace_result "$KEY_TYPE" KEY_TYPE
eval create table parent(
  id int,
  $KEY_TYPE (id)
) engine innodb;

--replace_result $sys_datatype_expl SYS_DATATYPE
eval create table child(
  parent_id int,
  sys_start $sys_datatype_expl as row start invisible,
  sys_end $sys_datatype_expl 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;
select * from child for system_time all;

insert into parent values(1);
insert into child values(1);
update parent set id = id + 1;
select * from child;
select * from child for system_time all;

drop table child;
drop table parent;

--replace_result $sys_datatype_expl SYS_DATATYPE "$KEY_TYPE" KEY_TYPE
eval create or replace table parent (
  id int,
  $KEY_TYPE(id),
  sys_start $sys_datatype_expl as row start invisible,
  sys_end $sys_datatype_expl 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;

drop table child;
drop table parent;

--replace_result "$KEY_TYPE" KEY_TYPE
eval create or replace table parent (
  id int,
  $KEY_TYPE(id)
)
engine innodb;

--replace_result $sys_datatype_expl SYS_DATATYPE
eval create or replace table child (
  id int primary key,
  parent_id int not null,
  row_start $sys_datatype_expl as row start invisible,
  row_end $sys_datatype_expl 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;
select *, check_row(row_start, row_end) from child for system_time all;

drop table child;
drop table parent;

--echo #################
--echo # Test SET NULL #
--echo #################

--replace_result "$KEY_TYPE" KEY_TYPE
eval create table parent(
  id int,
  $KEY_TYPE (id)
) engine innodb;

--replace_result $sys_datatype_expl SYS_DATATYPE
eval create or replace table child(
  parent_id int,
  sys_start $sys_datatype_expl as row start invisible,
  sys_end $sys_datatype_expl 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;
select *, current_row(sys_end) as current_row from child for system_time all order by sys_end;
delete from child;

insert into parent values(1);
insert into child values(1);
update parent set id= id + 1;
select * from child;
select *, current_row(sys_end) as current_row from child for system_time all order by sys_end;

drop table child;
drop table parent;

--echo ###########################
--echo # Parent table is foreign #
--echo ###########################

--replace_result $sys_datatype_expl SYS_DATATYPE "$KEY_TYPE" KEY_TYPE
eval create or replace table parent(
  id int,
  $KEY_TYPE (id),
  sys_start $sys_datatype_expl as row start invisible,
  sys_end $sys_datatype_expl 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);
-- error ER_ROW_IS_REFERENCED_2
delete from parent;
-- error ER_ROW_IS_REFERENCED_2
update parent set id=2;

delete from child;
delete from parent;

-- error ER_NO_REFERENCED_ROW_2
insert into child values(1);

insert into parent values(1);
insert into child values(1);
-- error ER_ROW_IS_REFERENCED_2
delete from parent;
-- error ER_ROW_IS_REFERENCED_2
update parent set id=2;

drop table child;
drop table parent;

--echo ###################
--echo # crash on DELETE #
--echo ###################

--replace_result $sys_datatype_expl SYS_DATATYPE "$KEY_TYPE" KEY_TYPE
eval create or replace table a (
  cola int(10),
  $KEY_TYPE (cola),
  v_cola int(10) as (cola mod 10) virtual,
  sys_start $sys_datatype_expl as row start invisible,
  sys_end $sys_datatype_expl 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);

--replace_result $sys_datatype_expl SYS_DATATYPE
eval create or replace table b(
  cola int(10),
  v_cola int(10),
  sys_start $sys_datatype_expl as row start invisible,
  sys_end $sys_datatype_expl 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);
--error ER_ROW_IS_REFERENCED_2
delete from a;

drop table b, a;

--echo ###############################################
--echo # CASCADE UPDATE foreign not system versioned #
--echo ###############################################
create or replace table parent (
	id smallint unsigned not null auto_increment,
	value int unsigned not null,
	primary key (id, value)
) engine = innodb;

--replace_result $sys_datatype_expl SYS_DATATYPE
eval 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_expl as row start invisible,
  sys_end $sys_datatype_expl 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;
update parent set id = 11, value = value + 1;
select parent_id from subchild;
select * from child;

delete from parent;
select count(*) from child;
select * from child for system_time all;
select count(*) from subchild;

drop table subchild, child, parent;

--echo #
--echo # MDEV-18057 Assertion `(node->state == 5) || (node->state == 6)' failed in row_upd_sec_step upon DELETE after UPDATE failed due to FK violation
--echo #
create or replace table t1 (f1 int, key(f1)) engine=innodb;
create or replace 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;
--error ER_NO_REFERENCED_ROW_2
update t2 set f2= 2;
delete from t2;

drop table t2, t1;

--echo #
--echo # MDEV-18879 Corrupted record inserted by FOREIGN KEY operation
--echo #
SET timestamp = 1;
SET time_zone='+02:00';
SELECT now();
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 * INTO OUTFILE 't1.data' FROM t1;
SET timestamp = 3;
UPDATE t1 SET f13 = 'q';
SET timestamp = 4;
LOAD DATA INFILE 't1.data' REPLACE INTO TABLE t1;
SELECT * INTO OUTFILE 't1.data.2' FROM t1;
SET timestamp = 5;
LOAD DATA INFILE 't1.data.2' REPLACE INTO TABLE t1;
SELECT * INTO OUTFILE 't2.data' FROM t2;
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;
SET FOREIGN_KEY_CHECKS = ON;

SET SESSION SQL_MODE= 'NO_BACKSLASH_ESCAPES';
SET timestamp = 8;
LOAD DATA INFILE 't1.data' REPLACE INTO TABLE t1;
SET timestamp = 9;
REPLACE INTO t2 SELECT * FROM t2;

# Cleanup
DROP TABLE t1, t2;
--let $datadir= `select @@datadir`
--remove_file $datadir/test/t1.data
--remove_file $datadir/test/t1.data.2
--remove_file $datadir/test/t2.data

--echo #
--echo # MDEV-16210 FK constraints on versioned tables use historical rows, which may cause constraint violation
--echo #
create or replace table t1 (a int, key(a)) engine innodb with system versioning;
create or replace table t2 (b int, foreign key (b) references t1(a)) engine innodb;
insert into t1 values (1),(2);
insert into t2 values (1);
--echo # DELETE from referenced table is not allowed
--error ER_ROW_IS_REFERENCED_2
delete from t1 where a = 1;
drop tables t2, t1;

--echo #
--echo # MDEV-20812 Unexpected ER_ROW_IS_REFERENCED_2 or server crash in row_ins_foreign_report_err upon DELETE from versioned table with FK
--echo #
create or replace table t1 (x int primary key) engine innodb;
create or replace table t2 (x int, foreign key (x) references t1(x)) engine innodb with system versioning;
set foreign_key_checks= off;
insert into t2 values (1), (1);
set foreign_key_checks= on;
--echo # DELETE from foreign table is allowed
delete from t2;
drop tables t2, t1;

create or replace table t1 (a int, key(a)) engine innodb;
insert into t1 values (1);
create or replace table t2 (b int, foreign key (b) references t1(a)) engine innodb with system versioning;
insert into t2 values (1), (1);
--echo # DELETE from foreign table is allowed
delete from t2;
drop tables t2, t1;

--echo #
--echo # MDEV-20729 Fix REFERENCES constraint in column definition
--echo #
create or replace table t1(
  id int
);
--echo # system fields can't be foreign keys:
--replace_result $sys_datatype_expl SYS_DATATYPE
--error ER_PARSE_ERROR,ER_PARSE_ERROR
eval create or replace table t2(
  x int,
  sys_start $sys_datatype_expl as row start references t1(id),
  sys_end $sys_datatype_expl as row end,
  period for system_time(sys_start, sys_end)
) engine innodb with system versioning;
--replace_result $sys_datatype_expl SYS_DATATYPE
--error ER_PARSE_ERROR,ER_PARSE_ERROR
eval create or replace table t2(
  x int,
  sys_start $sys_datatype_expl as row start,
  sys_end $sys_datatype_expl as row end references t1(id),
  period for system_time(sys_start, sys_end)
) engine innodb with system versioning;
--replace_result $sys_datatype_expl SYS_DATATYPE
--error ER_CANT_CREATE_TABLE
eval create or replace table t2(
  x int,
  sys_start $sys_datatype_expl as row start,
  sys_end $sys_datatype_expl as row end,
  period for system_time(sys_start, sys_end),
  foreign key (sys_start) references t1(id)
) engine innodb with system versioning;
--replace_result $sys_datatype_expl SYS_DATATYPE
--error ER_CANT_CREATE_TABLE
eval create or replace table t2(
  x int,
  sys_start $sys_datatype_expl as row start,
  sys_end $sys_datatype_expl as row end,
  period for system_time(sys_start, sys_end),
  foreign key (sys_end) references t1(id)
) engine innodb with system versioning;
drop table t1;

--source suite/versioning/common_finish.inc