summaryrefslogtreecommitdiff
path: root/mysql-test/suite/period/t/fk.test
blob: 7d9352494c96ed7ee25509d92f4574c0f604ac6e (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
--source include/have_innodb.inc
set default_storage_engine=innodb;

create or replace table t (id int, x int, s date, e date, period for p(s,e),
                           unique(id, x, p without overlaps));

create or replace table s (id int, x int, s date, e date, period for fp(s,e),
                           foreign key(id, x, period fp)
                             references t(id, x, period p)
                             on delete restrict);

show create table s;

flush tables;

insert into t values(1, 1, '2017-01-03', '2017-01-20');
insert into t values(1, 1, '2017-01-20', '2017-01-25');
insert into t values(1, 1, '2017-01-25', '2017-01-26');
insert into t values(1, 1, '2017-01-26', '2017-01-30');

insert into s values(1, 1, '2017-01-03', '2017-01-20');
select * from t;
select * from s;

--error ER_ROW_IS_REFERENCED_2
delete from t;

select * from t;
select * from s;

--error ER_ROW_IS_REFERENCED_2
delete from t where s = '2017-01-03' and e = '2017-01-20';

--echo # no error
delete from t where s = '2017-01-20' and e = '2017-01-25';
insert into t values(1, 1, '2017-01-20', '2017-01-25');

insert into s values (1, 1, '2017-01-27', '2017-01-30');
--error ER_ROW_IS_REFERENCED_2
delete from t where s = '2017-01-26' and e = '2017-01-30';

delete from t where s = '2017-01-25' and e = '2017-01-26';
--error ER_NO_REFERENCED_ROW_2
insert into s values (1, 1, '2017-01-22', '2017-01-28');
insert into t values (1, 1, '2017-01-25', '2017-01-26');

select * from t;
select * from s;

insert into s values (1, 1, '2017-01-03', '2017-01-15');
insert into s values (1, 1, '2017-01-07', '2017-01-15');
insert into s values (1, 1, '2017-01-07', '2017-01-20');
insert into s values (1, 1, '2017-01-07', '2017-01-26');
insert into s values (1, 1, '2017-01-07', '2017-01-27');
--error ER_NO_REFERENCED_ROW_2
insert into s values (1, 1, '2017-01-01', '2017-02-28');
--error ER_NO_REFERENCED_ROW_2
insert into s values (1, 1, '2017-01-01', '2017-01-30');

select * from t;
select * from s;

--error ER_ROW_IS_REFERENCED_2
update t set x= 2 where s='2017-01-03' and e='2017-01-20';

--error ER_NO_REFERENCED_ROW_2
update s set x= 2 where s = '2017-01-03' and e = '2017-01-20';

update s set s= '2017-01-05' where s < '2017-01-05' and e > '2017-01-05';

--error ER_NO_REFERENCED_ROW_2
update s set s= '2017-01-01' where s < '2017-01-26' and e > '2017-01-25';

--echo # Free period ('2017-01-25', '2017-01-26') from references
update s set s= '2017-01-26', e= '2017-01-30' where s < '2017-01-26'
                                                    and e > '2017-01-25';


update t set x= 2 where s = '2017-01-25' and e = '2017-01-26';
update t set s= '2017-01-26', e= '2017-01-30' where s = '2017-01-25'
                                                    and e = '2017-01-26';

update s set x= 2 where s = '2017-01-26' and e = '2017-01-30';
update s set s= '2017-01-28', e = '2017-01-29' where x = 2;

select * from t;
select * from s;

--error ER_ROW_IS_REFERENCED_2
update t set x= 2 where s='2017-01-03' and e='2017-01-20';


--echo # Expand left
update t set s= '2017-01-01' where s = '2017-01-03' and e = '2017-01-20';

--echo # Shrink left
update t set s= '2017-01-05' where e = '2017-01-20';

--echo # Expand right
update t set e= '2017-02-10' where s = '2017-01-26' and e = '2017-01-30';

--echo # Shrink right
--error ER_ROW_IS_REFERENCED_2
update t set e= '2017-01-29' where s = '2017-01-26';

delete from s where s = '2017-01-27' and e = '2017-01-30';
update t set e= '2017-01-29' where s = '2017-01-26';

--echo # Shrink both
--error ER_ROW_IS_REFERENCED_2
update t set s= '2017-01-27', e= '2017-01-28' where x = 2;

update t set s= '2017-01-28', e= '2017-01-29' where x = 2;

--echo # Expand both
update t set s= '2017-01-20', e= '2017-02-05' where x = 2;

--echo # Move right
--error ER_ROW_IS_REFERENCED_2
update t set s= '2017-02-02', e= '2017-02-25' where x = 2;

update t set s= '2017-01-28', e= '2017-02-25' where x = 2;

--echo # Move left
--error ER_ROW_IS_REFERENCED_2
update t set s= '2017-01-20', e= '2017-01-27' where x = 2;

update t set s= '2017-01-20', e= '2017-01-29' where x = 2;


--error ER_PERIOD_NOT_FOUND
create or replace table s (id int, x int, s date, e date, period for fp(s,e),
                           foreign key(id, x, period no_such_p)
                             references t(id, x, period p)
                             on delete restrict);

--error ER_PERIOD_FK_NOT_FOUND
create or replace table s (id int, x int, s date, e date, period for fp(s,e),
                           foreign key(id, x, period fp)
                             references t(id, x, period no_such_p)
                             on delete restrict);


create or replace table t1 (id int, x int, s timestamp, e timestamp, period for p(s,e),
                           unique(id, x, p without overlaps));

--error ER_PERIOD_FK_TYPES_MISMATCH
create or replace table s (id int, x int, s date, e date, period for fp(s,e),
                           foreign key(id, x, period fp)
                             references t1(id, x, period p)
                             on delete restrict);

--error ER_PERIOD_FK_TYPES_MISMATCH
create or replace table s (id int, x int, s timestamp(6), e timestamp(6),
                           period for fp(s,e),
                           foreign key(id, x, period fp)
                             references t1(id, x, period p)
                             on delete restrict);

create or replace table s (id int, x int, s date, e date);
--error ER_PERIOD_FK_NOT_FOUND
alter table s add period for fp(s,e),
              add foreign key(id, x, period fp)
               references t1(id, x, period no_such_p)
               on delete restrict;

--error ER_PERIOD_FK_TYPES_MISMATCH
alter table s add period for fp(s,e),
              add foreign key(id, x, period fp)
               references t1(id, x, period p)
               on delete restrict;

--error ER_PERIOD_FK_TYPES_MISMATCH
alter table s change s s timestamp(6), change e e timestamp(6),
              add period for fp(s, e),
              add foreign key(id, x, period fp)
               references t1(id, x, period p)
               on delete restrict;

alter table s change s s timestamp(6), change e e timestamp(6),
              add period for fp(s, e);
--error ER_PERIOD_FK_TYPES_MISMATCH
alter table s add foreign key(id, x, period fp)
               references t1(id, x, period p)
               on delete restrict;

drop database test;
create database test;