summaryrefslogtreecommitdiff
path: root/mysql-test/suite/versioning/r/foreign.result
blob: ca2459f784a9177152964a2b2830c51e146fc44b (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
#################
# 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 (`test`.`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 (`test`.`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 from timestamp '1-1-1' to timestamp now(6);
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 (`test`.`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);
## FIXME: #415 update of foreign constraints is disabled
call mtr.add_suppression("foreign key constraints in timestamp-based temporal table");
delete from parent where id = 1;
ERROR 42000: Table 'parent' uses an extension that doesn't exist in this MariaDB version
delete from child where parent_id = 1;
## FIXME END
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
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,
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);
## FIXME: #415 update of foreign constraints is disabled
delete from child;
## FIXME END
delete from parent;
select * from child;
id	parent_id
select * from child for system_time all;
id	parent_id
3	3
drop table child;
drop table parent;
#################
# Test SET NULL #
#################
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 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);
## FIXME: #415 update of foreign constraints is disabled
delete from child where parent_id = 1;
## FIXME END
delete from parent where id = 1;
select * from child;
parent_id
select * from child for system_time from timestamp '1-1-1' to timestamp now(6);
parent_id
1
1
delete from child;
insert into parent values(1);
insert into child values(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 (`test`.`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 (`test`.`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 (`test`.`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 (`test`.`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 (`test`.`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 (`test`.`b`, CONSTRAINT `v_cola_fk` FOREIGN KEY (`v_cola`) REFERENCES `a` (`v_cola`))
drop table b, a;