@@ -12,6 +12,21 @@ create or replace table t2 (id int, s date, e date, period for apptime(s,e));
12
12
insert t2 select * from t;
13
13
create or replace table t3 (id int, s date, e date, period for apptime(s,e));
14
14
insert t3 select * from t;
15
+ create or replace table log_tbl(id int auto_increment primary key, log text) engine=myisam;
16
+ create or replace procedure log(s text)
17
+ insert into log_tbl(log) values(s);
18
+ create trigger tr1upd_t1 before update on t1
19
+ for each row call log(CONCAT('>UPD: ', old.id, ", ", old.s, ", ", old.e, ' -> ', new.id, ", ", new.s, ", ", new.e));
20
+ create trigger tr2upd_t1 after update on t1
21
+ for each row call log(CONCAT('<UPD: ', old.id, ", ", old.s, ", ", old.e, ' -> ', new.id, ", ", new.s, ", ", new.e));
22
+ create trigger tr1del_t1 before delete on t1
23
+ for each row call log(CONCAT('>DEL: ', old.id, ", ", old.s, ", ", old.e));
24
+ create trigger tr2del_t1 after delete on t1
25
+ for each row call log(CONCAT('<DEL: ', old.id, ", ", old.s, ", ", old.e));
26
+ create trigger tr1ins_t1 before insert on t1
27
+ for each row call log(CONCAT('>INS: ', new.id, ", ", new.s, ", ", new.e));
28
+ create trigger tr2ins_t1 after insert on t1
29
+ for each row call log(CONCAT('<INS: ', new.id, ", ", new.s, ", ", new.e));
15
30
delete from t for portion of apptime from '2000-01-01' to '2018-01-01';
16
31
delete from t1 for portion of APPTIME from '2000-01-01' to '2018-01-01';
17
32
select * from t;
@@ -67,6 +82,21 @@ id log
67
82
29 >DEL: 5, 2010-01-01, 2015-01-01
68
83
30 <DEL: 5, 2010-01-01, 2015-01-01
69
84
# INSERT trigger only also works
85
+ create or replace table log_tbl(id int auto_increment primary key, log text) engine=myisam;
86
+ create or replace procedure log(s text)
87
+ insert into log_tbl(log) values(s);
88
+ create trigger tr1upd_t2 before update on t2
89
+ for each row call log(CONCAT('>UPD: ', old.id, ", ", old.s, ", ", old.e, ' -> ', new.id, ", ", new.s, ", ", new.e));
90
+ create trigger tr2upd_t2 after update on t2
91
+ for each row call log(CONCAT('<UPD: ', old.id, ", ", old.s, ", ", old.e, ' -> ', new.id, ", ", new.s, ", ", new.e));
92
+ create trigger tr1del_t2 before delete on t2
93
+ for each row call log(CONCAT('>DEL: ', old.id, ", ", old.s, ", ", old.e));
94
+ create trigger tr2del_t2 after delete on t2
95
+ for each row call log(CONCAT('<DEL: ', old.id, ", ", old.s, ", ", old.e));
96
+ create trigger tr1ins_t2 before insert on t2
97
+ for each row call log(CONCAT('>INS: ', new.id, ", ", new.s, ", ", new.e));
98
+ create trigger tr2ins_t2 after insert on t2
99
+ for each row call log(CONCAT('<INS: ', new.id, ", ", new.s, ", ", new.e));
70
100
drop trigger tr1del_t2;
71
101
drop trigger tr2del_t2;
72
102
delete from t2 for portion of APPTIME from '2000-01-01' to '2018-01-01';
@@ -91,6 +121,21 @@ id log
91
121
# removing BEFORE INSERT trigger enables internal substitution
92
122
# DELETE+INSERT -> UPDATE, but without any side effects.
93
123
# The optimization is disabled for non-transactional engines
124
+ create or replace table log_tbl(id int auto_increment primary key, log text) engine=myisam;
125
+ create or replace procedure log(s text)
126
+ insert into log_tbl(log) values(s);
127
+ create trigger tr1upd_t3 before update on t3
128
+ for each row call log(CONCAT('>UPD: ', old.s, ", ", old.e, ' -> ', new.s, ", ", new.e));
129
+ create trigger tr2upd_t3 after update on t3
130
+ for each row call log(CONCAT('<UPD: ', old.s, ", ", old.e, ' -> ', new.s, ", ", new.e));
131
+ create trigger tr1del_t3 before delete on t3
132
+ for each row call log(CONCAT('>DEL: ', old.s, ", ", old.e));
133
+ create trigger tr2del_t3 after delete on t3
134
+ for each row call log(CONCAT('<DEL: ', old.s, ", ", old.e));
135
+ create trigger tr1ins_t3 before insert on t3
136
+ for each row call log(CONCAT('>INS: ', new.s, ", ", new.e));
137
+ create trigger tr2ins_t3 after insert on t3
138
+ for each row call log(CONCAT('<INS: ', new.s, ", ", new.e));
94
139
drop trigger tr1ins_t3;
95
140
delete from t3 for portion of APPTIME from '2000-01-01' to '2018-01-01';
96
141
select * from log_tbl order by id;
@@ -167,6 +212,21 @@ id s e
167
212
truncate t;
168
213
# same for trigger case
169
214
insert into t values (default, '1999-01-01', '2018-12-12');
215
+ create or replace table log_tbl(id int auto_increment primary key, log text) engine=myisam;
216
+ create or replace procedure log(s text)
217
+ insert into log_tbl(log) values(s);
218
+ create trigger tr1upd_t before update on t
219
+ for each row call log(CONCAT('>UPD: ', old.s, ", ", old.e, ' -> ', new.s, ", ", new.e));
220
+ create trigger tr2upd_t after update on t
221
+ for each row call log(CONCAT('<UPD: ', old.s, ", ", old.e, ' -> ', new.s, ", ", new.e));
222
+ create trigger tr1del_t before delete on t
223
+ for each row call log(CONCAT('>DEL: ', old.s, ", ", old.e));
224
+ create trigger tr2del_t after delete on t
225
+ for each row call log(CONCAT('<DEL: ', old.s, ", ", old.e));
226
+ create trigger tr1ins_t before insert on t
227
+ for each row call log(CONCAT('>INS: ', new.s, ", ", new.e));
228
+ create trigger tr2ins_t after insert on t
229
+ for each row call log(CONCAT('<INS: ', new.s, ", ", new.e));
170
230
delete from t for portion of apptime from '2000-01-01' to '2018-01-01';
171
231
select * from t;
172
232
id s e
@@ -196,6 +256,21 @@ s e xs xe
196
256
truncate t;
197
257
# same for trigger case
198
258
insert into t values('1999-01-01', '2018-12-12', default, default);
259
+ create or replace table log_tbl(id int auto_increment primary key, log text) engine=myisam;
260
+ create or replace procedure log(s text)
261
+ insert into log_tbl(log) values(s);
262
+ create trigger tr1upd_t before update on t
263
+ for each row call log(CONCAT('>UPD: ', old.s, ", ", old.e, ' -> ', new.s, ", ", new.e));
264
+ create trigger tr2upd_t after update on t
265
+ for each row call log(CONCAT('<UPD: ', old.s, ", ", old.e, ' -> ', new.s, ", ", new.e));
266
+ create trigger tr1del_t before delete on t
267
+ for each row call log(CONCAT('>DEL: ', old.s, ", ", old.e));
268
+ create trigger tr2del_t after delete on t
269
+ for each row call log(CONCAT('<DEL: ', old.s, ", ", old.e));
270
+ create trigger tr1ins_t before insert on t
271
+ for each row call log(CONCAT('>INS: ', new.s, ", ", new.e));
272
+ create trigger tr2ins_t after insert on t
273
+ for each row call log(CONCAT('<INS: ', new.s, ", ", new.e));
199
274
delete from t for portion of apptime from '2000-01-01' to '2018-01-01';
200
275
select * from t;
201
276
s e xs xe
@@ -219,11 +294,39 @@ s date, e date, period for apptime(s,e));
219
294
insert into t values(127, '1999-01-01', '2018-12-12');
220
295
delete from t for portion of apptime from '2000-01-01' to '2018-01-01';
221
296
ERROR 22003: Out of range value for column 'id' at row 1
297
+ select * from t;
298
+ id s e
299
+ 127 1999-01-01 2018-12-12
222
300
# same for trigger case
301
+ create or replace table log_tbl(id int auto_increment primary key, log text) engine=myisam;
302
+ create or replace procedure log(s text)
303
+ insert into log_tbl(log) values(s);
304
+ create trigger tr1upd_t before update on t
305
+ for each row call log(CONCAT('>UPD: ', old.s, ", ", old.e, ' -> ', new.s, ", ", new.e));
306
+ create trigger tr2upd_t after update on t
307
+ for each row call log(CONCAT('<UPD: ', old.s, ", ", old.e, ' -> ', new.s, ", ", new.e));
308
+ create trigger tr1del_t before delete on t
309
+ for each row call log(CONCAT('>DEL: ', old.s, ", ", old.e));
310
+ create trigger tr2del_t after delete on t
311
+ for each row call log(CONCAT('<DEL: ', old.s, ", ", old.e));
312
+ create trigger tr1ins_t before insert on t
313
+ for each row call log(CONCAT('>INS: ', new.s, ", ", new.e));
314
+ create trigger tr2ins_t after insert on t
315
+ for each row call log(CONCAT('<INS: ', new.s, ", ", new.e));
223
316
# negotiate side effects of non-transactional MyISAM engine
224
317
replace into t values(127, '1999-01-01', '2018-12-12');
318
+ select * from t;
319
+ id s e
320
+ 127 1999-01-01 2018-12-12
321
+ truncate table log_tbl;
225
322
delete from t for portion of apptime from '2000-01-01' to '2018-01-01';
226
323
ERROR 22003: Out of range value for column 'id' at row 1
324
+ select * from t;
325
+ id s e
326
+ 127 1999-01-01 2018-12-12
327
+ select * from log_tbl order by id;
328
+ id log
329
+ 1 >DEL: 1999-01-01, 2018-12-12
227
330
# custom constraint for period fields
228
331
create or replace table t(id int, s date, e date, period for apptime(s,e),
229
332
constraint dist2days check (datediff(e, s) >= 2));
@@ -246,54 +349,4 @@ id s e datediff(e, s)
246
349
1 1999-01-01 1999-01-03 2
247
350
1 2018-12-10 2018-12-12 2
248
351
2 1999-01-01 1999-01-03 2
249
- # system_time columns are updated
250
- create or replace table t (
251
- s date, e date,
252
- row_start SYS_TYPE as row start invisible,
253
- row_end SYS_TYPE as row end invisible,
254
- period for apptime(s, e),
255
- period for system_time (row_start, row_end)) with system versioning;
256
- insert into t values('1999-01-01', '2018-12-12'),
257
- ('1999-01-01', '1999-12-12');
258
- select row_start into @ins_time from t limit 1;
259
- select * from t order by s, e;
260
- s e
261
- 1999-01-01 1999-12-12
262
- 1999-01-01 2018-12-12
263
- delete from t for portion of apptime from '2000-01-01' to '2018-01-01';
264
- select *, if(row_start = @ins_time, "OLD", "NEW"), check_row(row_start, row_end)
265
- from t for system_time all
266
- order by s, e, row_start;
267
- s e if(row_start = @ins_time, "OLD", "NEW") check_row(row_start, row_end)
268
- 1999-01-01 1999-12-12 OLD CURRENT ROW
269
- 1999-01-01 2000-01-01 NEW CURRENT ROW
270
- 1999-01-01 2018-12-12 OLD HISTORICAL ROW
271
- 2018-01-01 2018-12-12 NEW CURRENT ROW
272
- # same for trigger case
273
- delete from t;
274
- delete history from t;
275
- insert into t values('1999-01-01', '2018-12-12'),
276
- ('1999-01-01', '1999-12-12');
277
- select row_start into @ins_time from t limit 1;
278
- select * from t order by s, e;
279
- s e
280
- 1999-01-01 1999-12-12
281
- 1999-01-01 2018-12-12
282
- delete from t for portion of apptime from '2000-01-01' to '2018-01-01';
283
- select *, if(row_start = @ins_time, "OLD", "NEW"), check_row(row_start, row_end)
284
- from t for system_time all
285
- order by s, e, row_start;
286
- s e if(row_start = @ins_time, "OLD", "NEW") check_row(row_start, row_end)
287
- 1999-01-01 1999-12-12 OLD CURRENT ROW
288
- 1999-01-01 2000-01-01 NEW CURRENT ROW
289
- 1999-01-01 2018-12-12 OLD HISTORICAL ROW
290
- 2018-01-01 2018-12-12 NEW CURRENT ROW
291
- select * from log_tbl order by id;
292
- id log
293
- 1 >DEL: 1999-01-01, 2018-12-12
294
- 2 >INS: 1999-01-01, 2000-01-01
295
- 3 <INS: 1999-01-01, 2000-01-01
296
- 4 >INS: 2018-01-01, 2018-12-12
297
- 5 <INS: 2018-01-01, 2018-12-12
298
- 6 <DEL: 1999-01-01, 2018-12-12
299
352
create or replace database test;
0 commit comments