1
###########################################################
2
# 2006-02-01: By JBM: Added 1022, ORDER BY
3
###########################################################
4
# See if queries that use both auto_increment and LAST_INSERT_ID()
6
############################################################
8
# Auto increment should work for a table with an auto_increment
9
# column and index but without primary key.
10
##############################################################
18
drop table if exists t1, t2, t3;
22
--echo # See if queries that use both auto_increment and LAST_INSERT_ID()
23
--echo # are replicated well
25
--echo # We also check how the foreign_key_check variable is replicated
28
-- source include/master-slave.inc
29
#should work for both SBR and RBR
32
create table t1(a int auto_increment, key(a));
33
create table t2(b int auto_increment, c int, key(b));
34
insert into t1 values (1),(2),(3);
35
insert into t1 values (null);
36
insert into t2 values (null,last_insert_id());
40
select * from t1 ORDER BY a;
41
select * from t2 ORDER BY b;
43
#check if multi-line inserts,
44
#which set last_insert_id to the first id inserted,
45
#are replicated the same way
49
eval create table t1(a int auto_increment, key(a)) engine=$engine_type;
50
eval create table t2(b int auto_increment, c int, key(b), foreign key(b) references t1(a)) engine=$engine_type;
52
SET FOREIGN_KEY_CHECKS=0;
53
insert into t1 values (10);
54
insert into t1 values (null),(null),(null);
55
insert into t2 values (5,0);
56
insert into t2 values (null,last_insert_id());
57
SET FOREIGN_KEY_CHECKS=1;
66
--echo # check if INSERT SELECT in auto_increment is well replicated (bug #490)
71
create table t1(a int auto_increment, key(a));
72
create table t2(b int auto_increment, c int, key(b));
73
insert into t1 values (10);
74
insert into t1 values (null),(null),(null);
75
insert into t2 values (5,0);
76
insert into t2 (c) select * from t1 ORDER BY a;
77
select * from t2 ORDER BY b;
81
select * from t1 ORDER BY a;
82
select * from t2 ORDER BY b;
91
--echo # Bug#8412: Error codes reported in binary log for CHARACTER SET,
92
--echo # FOREIGN_KEY_CHECKS
96
SET TIMESTAMP=1000000000;
97
CREATE TABLE t1 ( a INT UNIQUE );
98
SET FOREIGN_KEY_CHECKS=0;
99
# Duplicate Key Errors
100
--error 1022, ER_DUP_ENTRY
101
INSERT INTO t1 VALUES (1),(1);
102
sync_slave_with_master;
105
sync_slave_with_master;
108
--echo # Bug#14553: NULL in WHERE resets LAST_INSERT_ID
112
create table t1(a int auto_increment, key(a));
113
create table t2(a int);
114
insert into t1 (a) values (null);
115
insert into t2 (a) select a from t1 where a is null;
116
insert into t2 (a) select a from t1 where a is null;
118
sync_slave_with_master;
126
--echo # End of 4.1 tests
130
--echo # BUG#15728: LAST_INSERT_ID function inside a stored function returns 0
132
--echo # The solution is not to reset last_insert_id on enter to sub-statement.
137
drop function if exists bug15728;
138
drop function if exists bug15728_insert;
139
drop table if exists t1, t2;
143
id int not null auto_increment,
147
create function bug15728() returns int(11)
148
return last_insert_id();
150
insert into t1 (last_id) values (0);
151
insert into t1 (last_id) values (last_insert_id());
152
insert into t1 (last_id) values (bug15728());
154
# Check that nested call replicates too.
156
id int not null auto_increment,
161
create function bug15728_insert() returns int(11) modifies sql data
163
insert into t2 (last_id) values (bug15728());
166
create trigger t1_bi before insert on t1 for each row
169
select bug15728_insert() into res;
170
set NEW.last_id = res;
174
insert into t1 (last_id) values (0);
178
# Check that nested call doesn't affect outer context.
179
select last_insert_id();
180
select bug15728_insert();
181
select last_insert_id();
182
insert into t1 (last_id) values (bug15728());
183
# This should be exactly one greater than in the previous call.
184
select last_insert_id();
186
# BUG#20339 - stored procedure using LAST_INSERT_ID() does not
187
# replicate statement-based
189
drop procedure if exists foo;
192
create procedure foo()
195
insert into t2 (last_id) values (bug15728());
196
insert into t1 (last_id) values (bug15728());
210
drop function bug15728;
211
drop function bug15728_insert;
215
# test of BUG#20188 REPLACE or ON DUPLICATE KEY UPDATE in
216
# auto_increment breaks binlog
218
create table t1 (n int primary key auto_increment not null,
221
# First, test that we do not call restore_auto_increment() too early
224
insert into t1 values(null,100);
225
replace into t1 values(null,50),(null,100),(null,150);
226
select * from t1 order by n;
230
insert into t1 values(null,100);
231
select * from t1 order by n;
232
sync_slave_with_master;
233
# make slave's table autoinc counter bigger
234
insert into t1 values(null,200),(null,300);
235
delete from t1 where b <> 100;
236
# check that slave's table content is identical to master
237
select * from t1 order by n;
238
# only the auto_inc counter differs.
241
replace into t1 values(null,100),(null,350);
242
select * from t1 order by n;
243
sync_slave_with_master;
244
select * from t1 order by n;
246
# Same test as for REPLACE, but for ON DUPLICATE KEY UPDATE
248
# We first check that if we update a row using a value larger than the
249
# table's counter, the counter for next row is bigger than the
250
# after-value of the updated row.
252
insert into t1 values (NULL,400),(3,500),(NULL,600) on duplicate key UPDATE n=1000;
253
select * from t1 order by n;
254
sync_slave_with_master;
255
select * from t1 order by n;
257
# and now test for the bug:
260
create table t1 (n int primary key auto_increment not null,
262
insert into t1 values(null,100);
263
select * from t1 order by n;
264
sync_slave_with_master;
265
insert into t1 values(null,200),(null,300);
266
delete from t1 where b <> 100;
267
select * from t1 order by n;
270
insert into t1 values(null,100),(null,350) on duplicate key update n=2;
271
select * from t1 order by n;
272
sync_slave_with_master;
273
select * from t1 order by n;
277
sync_slave_with_master;
280
# BUG#24432 "INSERT... ON DUPLICATE KEY UPDATE skips auto_increment values"
284
# testcase with INSERT VALUES
285
CREATE TABLE t1 (a INT NOT NULL PRIMARY KEY AUTO_INCREMENT, b INT,
287
INSERT INTO t1(b) VALUES(1),(1),(2) ON DUPLICATE KEY UPDATE t1.b=10;
289
sync_slave_with_master;
294
# tescase with INSERT SELECT
296
id bigint(20) unsigned NOT NULL auto_increment,
297
field_1 int(10) unsigned NOT NULL,
298
field_2 varchar(255) NOT NULL,
299
field_3 varchar(255) NOT NULL,
301
UNIQUE KEY field_1 (field_1, field_2)
304
field_a int(10) unsigned NOT NULL,
305
field_b varchar(255) NOT NULL,
306
field_c varchar(255) NOT NULL
308
INSERT INTO t2 (field_a, field_b, field_c) VALUES (1, 'a', '1a');
309
INSERT INTO t2 (field_a, field_b, field_c) VALUES (2, 'b', '2b');
310
INSERT INTO t2 (field_a, field_b, field_c) VALUES (3, 'c', '3c');
311
INSERT INTO t2 (field_a, field_b, field_c) VALUES (4, 'd', '4d');
312
INSERT INTO t2 (field_a, field_b, field_c) VALUES (5, 'e', '5e');
313
# Updating table t1 based on values from table t2
314
INSERT INTO t1 (field_1, field_2, field_3)
315
SELECT t2.field_a, t2.field_b, t2.field_c
317
ON DUPLICATE KEY UPDATE
318
t1.field_3 = t2.field_c;
319
# Inserting new record into t2
320
INSERT INTO t2 (field_a, field_b, field_c) VALUES (6, 'f', '6f');
322
INSERT INTO t1 (field_1, field_2, field_3)
323
SELECT t2.field_a, t2.field_b, t2.field_c
325
ON DUPLICATE KEY UPDATE
326
t1.field_3 = t2.field_c;
328
sync_slave_with_master;
334
# BUG#20339: stored procedure using LAST_INSERT_ID() does not
335
# replicate statement-based.
337
# There is another version of the test for bug#20339 above that is
338
# actually originates in 5.1, and this is the version that is merged
344
DROP PROCEDURE IF EXISTS p1;
345
DROP TABLE IF EXISTS t1, t2;
348
# Reset result of LAST_INSERT_ID().
349
SELECT LAST_INSERT_ID(0);
352
id INT NOT NULL DEFAULT 0,
358
id INT NOT NULL AUTO_INCREMENT,
364
CREATE PROCEDURE p1()
366
INSERT INTO t2 (last_id) VALUES (LAST_INSERT_ID());
367
INSERT INTO t1 (last_id) VALUES (LAST_INSERT_ID());
375
sync_slave_with_master;
386
# BUG#21726: Incorrect result with multiple invocations of
390
DROP PROCEDURE IF EXISTS p1;
391
DROP FUNCTION IF EXISTS f1;
392
DROP FUNCTION IF EXISTS f2;
393
DROP FUNCTION IF EXISTS f3;
394
DROP TABLE IF EXISTS t1, t2;
398
i INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
401
CREATE TABLE t2 (i INT);
404
CREATE PROCEDURE p1()
406
INSERT INTO t1 (i) VALUES (NULL);
407
INSERT INTO t2 (i) VALUES (LAST_INSERT_ID());
408
INSERT INTO t1 (i) VALUES (NULL), (NULL);
409
INSERT INTO t2 (i) VALUES (LAST_INSERT_ID());
412
CREATE FUNCTION f1() RETURNS INT MODIFIES SQL DATA
414
INSERT INTO t1 (i) VALUES (NULL);
415
INSERT INTO t2 (i) VALUES (LAST_INSERT_ID());
416
INSERT INTO t1 (i) VALUES (NULL), (NULL);
417
INSERT INTO t2 (i) VALUES (LAST_INSERT_ID());
421
CREATE FUNCTION f2() RETURNS INT NOT DETERMINISTIC
422
RETURN LAST_INSERT_ID() |
424
CREATE FUNCTION f3() RETURNS INT MODIFIES SQL DATA
426
INSERT INTO t2 (i) VALUES (LAST_INSERT_ID());
431
INSERT INTO t1 VALUES (NULL, -1);
434
INSERT INTO t1 VALUES (NULL, f2()), (NULL, LAST_INSERT_ID()),
435
(NULL, LAST_INSERT_ID()), (NULL, f2()), (NULL, f2());
436
INSERT INTO t1 VALUES (NULL, f2());
437
INSERT INTO t1 VALUES (NULL, LAST_INSERT_ID()), (NULL, LAST_INSERT_ID(5)),
438
(NULL, @@LAST_INSERT_ID);
439
# Test replication of substitution "IS NULL" -> "= LAST_INSERT_ID".
440
INSERT INTO t1 VALUES (NULL, 0), (NULL, LAST_INSERT_ID());
441
UPDATE t1 SET j= -1 WHERE i IS NULL;
443
# Test statement-based replication of function calls.
444
INSERT INTO t1 (i) VALUES (NULL);
447
INSERT INTO t1 (i) VALUES (NULL);
455
sync_slave_with_master;
467
sync_slave_with_master;
470
--echo # End of 5.0 tests
473
# Tests in this file are tightly bound together. Recreate t2.
476
id int not null auto_increment,
482
# Test for BUG#20341 "stored function inserting into one
483
# auto_increment puts bad data in slave"
487
create table t1 (id tinyint primary key); # no auto_increment
490
create function insid() returns int
492
insert into t2 (last_id) values (0);
497
insert into t2 (id) values(1),(2),(3);
500
#inside SELECT, then inside INSERT
503
insert into t2 (id) values(5),(6),(7);
504
delete from t2 where id>=5;
506
insert into t1 select insid();
510
sync_slave_with_master;
519
create table t1 (n int primary key auto_increment not null,
522
create procedure foo()
524
insert into t1 values(null,10);
525
insert ignore into t1 values(null,10);
526
insert ignore into t1 values(null,10);
527
insert into t2 values(null,3);
534
sync_slave_with_master;
541
sync_slave_with_master;