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
|
DROP TABLE IF EXISTS t1_select;
DROP TABLE IF EXISTS t1_aux;
DROP TABLE IF EXISTS t1_not_null;
DROP VIEW IF EXISTS v1_not_null;
DROP VIEW IF EXISTS v1_func;
DROP TABLE IF EXISTS t1_fail;
DROP FUNCTION IF EXISTS f1_simple_insert;
DROP FUNCTION IF EXISTS f1_two_inserts;
DROP FUNCTION IF EXISTS f1_insert_select;
SET SESSION AUTOCOMMIT=0;
SET SESSION sql_mode = '';
CREATE TABLE t1_select (f1 BIGINT, f2 BIGINT) ENGINE = MEMORY;
INSERT INTO t1_select(f1,f2) VALUES (1,-1),(2,NULL),(3,0),(4,1),(5,2);
SELECT * FROM t1_select;
f1 f2
1 -1
2 NULL
3 0
4 1
5 2
CREATE TABLE t1_not_null (f1 BIGINT, f2 BIGINT NOT NULL)
ENGINE = <transactional_engine>;
SELECT * FROM t1_not_null;
f1 f2
CREATE TABLE t1_aux (f1 BIGINT, f2 BIGINT)
ENGINE = <transactional_engine>;
SELECT * FROM t1_aux;
f1 f2
COMMIT;
CREATE FUNCTION f1_simple_insert(my_f1 INTEGER) RETURNS INTEGER
BEGIN
INSERT INTO t1_not_null SET f1 = 10, f2 = my_f1;
RETURN 1;
END//
# One f1_simple_insert execution per row, no NOT NULL violation
SELECT f1_simple_insert(1);
f1_simple_insert(1)
1
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
10 1
ROLLBACK;
SELECT * FROM t1_not_null;
f1 f2
SELECT f1_simple_insert(1) FROM t1_select;
f1_simple_insert(1)
1
1
1
1
1
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
10 1
10 1
10 1
10 1
10 1
ROLLBACK;
SELECT * FROM t1_not_null;
f1 f2
# One f1_simple_insert execution per row, NOT NULL violation when the
# SELECT processes the first row.
SELECT f1_simple_insert(NULL);
ERROR 23000: Column 'f2' cannot be null
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
ROLLBACK;
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
SELECT f1_simple_insert(NULL) FROM t1_select;
ERROR 23000: Column 'f2' cannot be null
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
ROLLBACK;
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
SELECT 1 FROM (SELECT 1 AS f1,1 AS f2 UNION ALL SELECT 1,NULL) AS t1 WHERE f1_simple_insert(NULL) = 1;
ERROR 23000: Column 'f2' cannot be null
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
ROLLBACK;
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
# One f1_simple_insert execution per row, NOT NULL violation when the
# SELECT processes the non first row
SELECT f1_simple_insert(f2) FROM (SELECT 1 AS f1,1 AS f2 UNION ALL SELECT 1,NULL) AS t1;
ERROR 23000: Column 'f2' cannot be null
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
ROLLBACK;
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
SELECT f1_simple_insert(f2) FROM t1_select;
ERROR 23000: Column 'f2' cannot be null
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
ROLLBACK;
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
# Two f1_simple_insert executions per row, NOT NULL violation when the
# SELECT processes the first row.
SELECT f1_simple_insert(1),f1_simple_insert(NULL);
ERROR 23000: Column 'f2' cannot be null
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
ROLLBACK;
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
SELECT f1_simple_insert(NULL),f1_simple_insert(1);
ERROR 23000: Column 'f2' cannot be null
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
ROLLBACK;
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
# Two f1_simple_insert executions per row, NOT NULL violation when the
# SELECT processes the non first row
SELECT f1_simple_insert(f1),f1_simple_insert(f2) FROM (SELECT 1 AS f1,1 AS f2 UNION ALL SELECT 1,NULL) AS t1;
ERROR 23000: Column 'f2' cannot be null
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
ROLLBACK;
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
SELECT f1_simple_insert(f2),f1_simple_insert(f1) FROM (SELECT 1 AS f1,1 AS f2 UNION ALL SELECT 1,NULL) AS t1;
ERROR 23000: Column 'f2' cannot be null
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
ROLLBACK;
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
SELECT f1_simple_insert(f1),f1_simple_insert(f2) FROM t1_select;
ERROR 23000: Column 'f2' cannot be null
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
ROLLBACK;
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
SELECT f1_simple_insert(f2),f1_simple_insert(f1) FROM t1_select;
ERROR 23000: Column 'f2' cannot be null
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
ROLLBACK;
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
SELECT 1 FROM (SELECT 1 AS f1,1 AS f2 UNION ALL SELECT 1,NULL) AS t1
WHERE 1 = f1_simple_insert(f2) AND 1 = f1_simple_insert(f1);
ERROR 23000: Column 'f2' cannot be null
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
ROLLBACK;
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
# Nested functions, the inner fails
SELECT f1_simple_insert(f1_simple_insert(NULL)) FROM t1_select;
ERROR 23000: Column 'f2' cannot be null
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
ROLLBACK;
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
# Nested functions, the outer fails
SELECT f1_simple_insert(f1_simple_insert(1) + NULL) FROM t1_select;
ERROR 23000: Column 'f2' cannot be null
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
ROLLBACK;
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
DROP FUNCTION f1_simple_insert;
CREATE FUNCTION f1_insert_select(my_f1 INTEGER) RETURNS INTEGER
BEGIN
INSERT INTO t1_not_null SELECT * FROM t1_select WHERE f1 = my_f1;
RETURN 1;
END;
# f1_insert_select(2), tries to INSERT SELECT one row containing NULL
# The fact that
# - SELECT f1_insert_select(2); gives any result set and
# - t1_not_null gets a row inserted
# is covered by the manual.
SELECT f1_insert_select(2);
f1_insert_select(2)
1
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
2 0
ROLLBACK;
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
DROP FUNCTION f1_insert_select;
SET SESSION sql_mode = 'traditional';
CREATE FUNCTION f1_insert_select(my_f1 INTEGER) RETURNS INTEGER
BEGIN
INSERT INTO t1_not_null SELECT * FROM t1_select WHERE f1 = my_f1;
RETURN 1;
END;
SELECT f1_insert_select(2);
ERROR 23000: Column 'f2' cannot be null
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
ROLLBACK;
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
DROP FUNCTION f1_insert_select;
SET SESSION sql_mode = '';
# Function tries to
# 1. INSERT statement: Insert one row with NULL -> NOT NULL violation
# 2. INSERT statement: Insert one row without NULL
CREATE FUNCTION f1_two_inserts() RETURNS INTEGER
BEGIN
INSERT INTO t1_not_null SET f1 = 10, f2 = NULL;
INSERT INTO t1_not_null SET f1 = 10, f2 = 10;
RETURN 1;
END//
SELECT f1_two_inserts();
ERROR 23000: Column 'f2' cannot be null
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
ROLLBACK;
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
DROP FUNCTION f1_two_inserts;
# Function tries to
# 1. INSERT statement: Insert one row without NULL
# 2. INSERT statement: Insert one row with NULL -> NOT NULL violation
CREATE FUNCTION f1_two_inserts() RETURNS INTEGER
BEGIN
INSERT INTO t1_not_null SET f1 = 10, f2 = 10;
INSERT INTO t1_not_null SET f1 = 10, f2 = NULL;
RETURN 1;
END//
SELECT f1_two_inserts();
ERROR 23000: Column 'f2' cannot be null
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
ROLLBACK;
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
# Function tries to
# INSERT statement: Insert two rows
# first row without NULL
# second row with NULL -> NOT NULL violation
# -> NOT NULL violation
CREATE FUNCTION f1_insert_with_two_rows() RETURNS INTEGER
BEGIN
INSERT INTO t1_not_null(f1,f2) VALUES (10,10),(10,NULL);
RETURN 1;
END;
# The fact that
# - SELECT f1_insert_with_two_rows(); gives any result set and
# - t1_not_null gets a row inserted
# is covered by the manual.
SELECT f1_insert_with_two_rows();
f1_insert_with_two_rows()
1
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
10 0
10 10
ROLLBACK;
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
DROP FUNCTION f1_insert_with_two_rows;
SET SESSION sql_mode = 'traditional';
CREATE FUNCTION f1_insert_with_two_rows() RETURNS INTEGER
BEGIN
INSERT INTO t1_not_null(f1,f2) VALUES (10,10),(10,NULL);
RETURN 1;
END;
SELECT f1_insert_with_two_rows();
ERROR 23000: Column 'f2' cannot be null
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
ROLLBACK;
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
SET SESSION sql_mode = '';
# FUNCTION in Correlated Subquery
SELECT 1 FROM t1_select t1
WHERE 1 = (SELECT f1_insert_with_two_rows() FROM t1_select t2
WHERE t2.f1 = t1.f1);
ERROR 23000: Column 'f2' cannot be null
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
ROLLBACK;
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
# FUNCTION in JOIN
SELECT 1 FROM t1_select t1, t1_select t2
WHERE t1.f1 = t2.f1 AND t2.f1 = f1_insert_with_two_rows();
ERROR 23000: Column 'f2' cannot be null
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
ROLLBACK;
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
SELECT STRAIGHT_JOIN * FROM t1_select t2 RIGHT JOIN t1_select t1
ON t1.f1 = t1.f1 WHERE 1 = f1_insert_with_two_rows();
ERROR 23000: Column 'f2' cannot be null
DROP FUNCTION f1_insert_with_two_rows;
# FUNCTION in UNION
SELECT 1
UNION ALL
SELECT f1_two_inserts();
ERROR 23000: Column 'f2' cannot be null
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
ROLLBACK;
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
# FUNCTION in INSERT
INSERT INTO t1_aux SET f1 = 1, f2 = f1_two_inserts();
ERROR 23000: Column 'f2' cannot be null
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
ROLLBACK;
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
INSERT INTO t1_aux SELECT 1, f1_two_inserts();
ERROR 23000: Column 'f2' cannot be null
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
ROLLBACK;
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
SELECT * FROM t1_aux ORDER BY f1,f2;
f1 f2
INSERT INTO t1_aux VALUES(1,f1_two_inserts());
ERROR 23000: Column 'f2' cannot be null
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
SELECT * FROM t1_aux ORDER BY f1,f2;
f1 f2
# FUNCTION in DELETE
INSERT INTO t1_aux VALUES (1,1);
COMMIT;
DELETE FROM t1_aux WHERE f1 = f1_two_inserts();
ERROR 23000: Column 'f2' cannot be null
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
ROLLBACK;
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
SELECT * FROM t1_aux ORDER BY f1,f2;
f1 f2
1 1
# FUNCTION in UPDATE SET
UPDATE t1_aux SET f2 = f1_two_inserts() + 1;
ERROR 23000: Column 'f2' cannot be null
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
ROLLBACK;
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
SELECT * FROM t1_aux ORDER BY f1,f2;
f1 f2
1 1
# FUNCTION in VIEW definition
CREATE VIEW v1_func AS SELECT f1_two_inserts() FROM t1_select;
SELECT * FROM v1_func;
ERROR 23000: Column 'f2' cannot be null
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
ROLLBACK;
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
DROP VIEW v1_func;
# FUNCTION in CREATE TABLE ... AS SELECT
CREATE TABLE t1_fail AS SELECT f1_two_inserts() FROM t1_select;
ERROR 23000: Column 'f2' cannot be null
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
CREATE TABLE t1_fail AS SELECT * FROM t1_select WHERE 1 = f1_two_inserts();
ERROR 23000: Column 'f2' cannot be null
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
# FUNCTION in ORDER BY
SELECT * FROM t1_select ORDER BY f1,f1_two_inserts();
ERROR 23000: Column 'f2' cannot be null
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
# FUNCTION in aggregate function
SELECT AVG(f1_two_inserts()) FROM t1_select;
ERROR 23000: Column 'f2' cannot be null
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
# FUNCTION in HAVING
SELECT 1 FROM t1_select HAVING AVG(f1) = f1_two_inserts() + 2;
ERROR 23000: Column 'f2' cannot be null
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
DROP FUNCTION f1_two_inserts;
# FUNCTION modifies Updatable VIEW
CREATE VIEW v1_not_null AS SELECT f1,f2 FROM t1_not_null WITH CHECK OPTION;
CREATE FUNCTION f1_two_inserts_v1() RETURNS INTEGER
BEGIN
INSERT INTO v1_not_null SET f1 = 10, f2 = 10;
INSERT INTO v1_not_null SET f1 = 10, f2 = NULL;
RETURN 1;
END//
SELECT f1_two_inserts_v1();
ERROR 23000: Column 'f2' cannot be null
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
ROLLBACK;
SELECT * FROM t1_not_null ORDER BY f1,f2;
f1 f2
DROP FUNCTION f1_two_inserts_v1;
DROP VIEW v1_not_null;
# FUNCTION causes FOREIGN KEY constraint violation
CREATE TABLE t1_parent (f1 BIGINT, f2 BIGINT, PRIMARY KEY(f1))
ENGINE = InnoDB;
INSERT INTO t1_parent VALUES (1,1);
CREATE TABLE t1_child (f1 BIGINT, f2 BIGINT, PRIMARY KEY(f1),
FOREIGN KEY (f1) REFERENCES t1_parent(f1))
ENGINE = InnoDB;
CREATE FUNCTION f1_two_inserts() RETURNS INTEGER
BEGIN
INSERT INTO t1_child SET f1 = 1, f2 = 1;
INSERT INTO t1_child SET f1 = 2, f2 = 2;
RETURN 1;
END//
SELECT f1_two_inserts();
ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`test`.`t1_child`, CONSTRAINT `t1_child_ibfk_1` FOREIGN KEY (`f1`) REFERENCES `t1_parent` (`f1`))
SELECT * FROM t1_child;
f1 f2
DROP TABLE t1_child;
DROP TABLE t1_parent;
DROP FUNCTION f1_two_inserts;
DROP TABLE t1_select;
DROP TABLE t1_aux;
DROP TABLE t1_not_null;
|