1 ###########################################################
2 # See if queries that use both auto_increment and LAST_INSERT_ID()
4 ############################################################
6 # Auto increment should work for a table with an auto_increment
7 # column and index but without primary key.
8 ##############################################################
17 --echo # See if queries that use both auto_increment and LAST_INSERT_ID()
18 --echo # are replicated well
20 --echo # We also check how the foreign_key_check variable is replicated
23 -- source include/master-slave.inc
24 #should work for both SBR and RBR
26 # If concurrent inserts are on, it is not guaranteed that the rows
27 # inserted by INSERT are immediately accessible by SELECT in another
28 # thread. This would cause problems near the line 'connection master1'
29 # below. So we turn off concurrent inserts.
31 SET @old_concurrent_insert= @@global.concurrent_insert;
32 SET @@global.concurrent_insert= 0;
35 eval create table t1(a int auto_increment, key(a)) engine=$engine_type;
36 eval create table t2(b int auto_increment, c int, key(b)) engine=$engine_type;
37 insert into t1 values (1),(2),(3);
38 insert into t1 values (null);
39 insert into t2 values (null,last_insert_id());
40 sync_slave_with_master;
41 select * from t1 ORDER BY a;
42 select * from t2 ORDER BY b;
44 #check if multi-line inserts,
45 #which set last_insert_id to the first id inserted,
46 #are replicated the same way
50 eval create table t1(a int auto_increment, key(a)) engine=$engine_type;
51 eval create table t2(b int auto_increment, c int, key(b), foreign key(b) references t1(a)) engine=$engine_type;
53 SET FOREIGN_KEY_CHECKS=0;
54 insert into t1 values (10);
55 insert into t1 values (null),(null),(null);
56 insert into t2 values (5,0);
57 insert into t2 values (null,last_insert_id());
58 SET FOREIGN_KEY_CHECKS=1;
59 sync_slave_with_master;
65 --echo # check if INSERT SELECT in auto_increment is well replicated (bug #490)
70 eval create table t1(a int auto_increment, key(a)) engine=$engine_type;
71 eval create table t2(b int auto_increment, c int, key(b)) engine=$engine_type;
72 insert into t1 values (10);
73 insert into t1 values (null),(null),(null);
74 insert into t2 values (5,0);
75 insert into t2 (c) select * from t1 ORDER BY a;
76 select * from t2 ORDER BY b;
77 sync_slave_with_master;
78 select * from t1 ORDER BY a;
79 select * from t2 ORDER BY b;
83 sync_slave_with_master;
86 --echo # Bug#8412: Error codes reported in binary log for CHARACTER SET,
87 --echo # FOREIGN_KEY_CHECKS
91 SET TIMESTAMP=1000000000;
92 eval CREATE TABLE t1 ( a INT UNIQUE ) engine=$engine_type;
93 SET FOREIGN_KEY_CHECKS=0;
94 # Duplicate Key Errors
95 --error 1022, ER_DUP_ENTRY
96 INSERT INTO t1 VALUES (1),(1);
97 sync_slave_with_master;
100 sync_slave_with_master;
103 --echo # Bug#14553: NULL in WHERE resets LAST_INSERT_ID
107 eval create table t1(a int auto_increment, key(a)) engine=$engine_type;
108 eval create table t2(a int) engine=$engine_type;
109 insert into t1 (a) values (null);
110 insert into t2 (a) select a from t1 where a is null;
111 insert into t2 (a) select a from t1 where a is null;
113 sync_slave_with_master;
121 --echo # End of 4.1 tests
125 --echo # BUG#15728: LAST_INSERT_ID function inside a stored function returns 0
127 --echo # The solution is not to reset last_insert_id on enter to sub-statement.
132 drop function if exists bug15728;
133 drop function if exists bug15728_insert;
134 drop table if exists t1, t2;
137 eval create table t1 (
138 id int not null auto_increment,
141 ) engine=$engine_type;
142 create function bug15728() returns int(11)
143 return last_insert_id();
145 insert into t1 (last_id) values (0);
146 insert into t1 (last_id) values (last_insert_id());
147 insert into t1 (last_id) values (bug15728());
149 # Check that nested call replicates too.
150 eval create table t2 (
151 id int not null auto_increment,
154 ) engine=$engine_type;
156 create function bug15728_insert() returns int(11) modifies sql data
158 insert into t2 (last_id) values (bug15728());
161 create trigger t1_bi before insert on t1 for each row
164 select bug15728_insert() into res;
165 set NEW.last_id = res;
169 insert into t1 (last_id) values (0);
173 # Check that nested call doesn't affect outer context.
174 select last_insert_id();
175 select bug15728_insert();
176 select last_insert_id();
177 insert into t1 (last_id) values (bug15728());
178 # This should be exactly one greater than in the previous call.
179 select last_insert_id();
181 # BUG#20339 - stored procedure using LAST_INSERT_ID() does not
182 # replicate statement-based
184 drop procedure if exists foo;
187 create procedure foo()
190 insert into t2 (last_id) values (bug15728());
191 insert into t1 (last_id) values (bug15728());
198 sync_slave_with_master;
203 drop function bug15728;
204 drop function bug15728_insert;
208 # test of BUG#20188 REPLACE or ON DUPLICATE KEY UPDATE in
209 # auto_increment breaks binlog
211 eval create table t1 (n int primary key auto_increment not null,
212 b int, unique(b)) engine=$engine_type;
214 # First, test that we do not call restore_auto_increment() too early
217 insert into t1 values(null,100);
218 replace into t1 values(null,50),(null,100),(null,150);
219 select * from t1 order by n;
223 insert into t1 values(null,100);
224 select * from t1 order by n;
225 sync_slave_with_master;
226 # make slave's table autoinc counter bigger
227 insert into t1 values(null,200),(null,300);
228 delete from t1 where b <> 100;
229 # check that slave's table content is identical to master
230 select * from t1 order by n;
231 # only the auto_inc counter differs.
234 replace into t1 values(null,100),(null,350);
235 select * from t1 order by n;
236 sync_slave_with_master;
237 select * from t1 order by n;
239 # Same test as for REPLACE, but for ON DUPLICATE KEY UPDATE
241 # We first check that if we update a row using a value larger than the
242 # table's counter, the counter for next row is bigger than the
243 # after-value of the updated row.
245 insert into t1 values (NULL,400),(3,500),(NULL,600) on duplicate key UPDATE n=1000;
246 select * from t1 order by n;
247 sync_slave_with_master;
248 select * from t1 order by n;
250 # and now test for the bug:
253 eval create table t1 (n int primary key auto_increment not null,
254 b int, unique(b)) engine=$engine_type;
255 insert into t1 values(null,100);
256 select * from t1 order by n;
257 sync_slave_with_master;
258 insert into t1 values(null,200),(null,300);
259 delete from t1 where b <> 100;
260 select * from t1 order by n;
263 insert into t1 values(null,100),(null,350) on duplicate key update n=2;
264 select * from t1 order by n;
265 sync_slave_with_master;
266 select * from t1 order by n;
270 sync_slave_with_master;
273 # BUG#24432 "INSERT... ON DUPLICATE KEY UPDATE skips auto_increment values"
277 # testcase with INSERT VALUES
278 eval CREATE TABLE t1 (a INT NOT NULL PRIMARY KEY AUTO_INCREMENT, b INT,
279 UNIQUE(b)) ENGINE=$engine_type;
280 INSERT INTO t1(b) VALUES(1),(1),(2) ON DUPLICATE KEY UPDATE t1.b=10;
281 SELECT * FROM t1 ORDER BY a;
282 sync_slave_with_master;
283 SELECT * FROM t1 ORDER BY a;
287 # tescase with INSERT SELECT
288 eval CREATE TABLE t1 (
289 id bigint(20) unsigned NOT NULL auto_increment,
290 field_1 int(10) unsigned NOT NULL,
291 field_2 varchar(255) NOT NULL,
292 field_3 varchar(255) NOT NULL,
294 UNIQUE KEY field_1 (field_1, field_2)
295 ) ENGINE=$engine_type;
296 eval CREATE TABLE t2 (
297 field_a int(10) unsigned NOT NULL,
298 field_b varchar(255) NOT NULL,
299 field_c varchar(255) NOT NULL
300 ) ENGINE=$engine_type;
301 INSERT INTO t2 (field_a, field_b, field_c) VALUES (1, 'a', '1a');
302 INSERT INTO t2 (field_a, field_b, field_c) VALUES (2, 'b', '2b');
303 INSERT INTO t2 (field_a, field_b, field_c) VALUES (3, 'c', '3c');
304 INSERT INTO t2 (field_a, field_b, field_c) VALUES (4, 'd', '4d');
305 INSERT INTO t2 (field_a, field_b, field_c) VALUES (5, 'e', '5e');
306 # Updating table t1 based on values from table t2
307 INSERT INTO t1 (field_1, field_2, field_3)
308 SELECT t2.field_a, t2.field_b, t2.field_c
310 ON DUPLICATE KEY UPDATE
311 t1.field_3 = t2.field_c;
312 # Inserting new record into t2
313 INSERT INTO t2 (field_a, field_b, field_c) VALUES (6, 'f', '6f');
315 INSERT INTO t1 (field_1, field_2, field_3)
316 SELECT t2.field_a, t2.field_b, t2.field_c
318 ON DUPLICATE KEY UPDATE
319 t1.field_3 = t2.field_c;
320 SELECT * FROM t1 ORDER BY id;
321 sync_slave_with_master;
322 SELECT * FROM t1 ORDER BY id;
327 # BUG#20339: stored procedure using LAST_INSERT_ID() does not
328 # replicate statement-based.
330 # There is another version of the test for bug#20339 above that is
331 # actually originates in 5.1, and this is the version that is merged
337 DROP PROCEDURE IF EXISTS p1;
338 DROP TABLE IF EXISTS t1, t2;
341 # Reset result of LAST_INSERT_ID().
342 SELECT LAST_INSERT_ID(0);
344 eval CREATE TABLE t1 (
345 id INT NOT NULL DEFAULT 0,
348 ) ENGINE=$engine_type;
350 eval CREATE TABLE t2 (
351 id INT NOT NULL AUTO_INCREMENT,
354 ) ENGINE=$engine_type;
357 CREATE PROCEDURE p1()
359 INSERT INTO t2 (last_id) VALUES (LAST_INSERT_ID());
360 INSERT INTO t1 (last_id) VALUES (LAST_INSERT_ID());
365 SELECT * FROM t1 ORDER BY id;
366 SELECT * FROM t2 ORDER BY id;
368 sync_slave_with_master;
369 SELECT * FROM t1 ORDER BY id;
370 SELECT * FROM t2 ORDER BY id;
379 # BUG#21726: Incorrect result with multiple invocations of
383 DROP PROCEDURE IF EXISTS p1;
384 DROP FUNCTION IF EXISTS f1;
385 DROP FUNCTION IF EXISTS f2;
386 DROP FUNCTION IF EXISTS f3;
387 DROP TABLE IF EXISTS t1, t2;
390 eval CREATE TABLE t1 (
391 i INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
393 ) ENGINE=$engine_type;
394 eval CREATE TABLE t2 (i INT) ENGINE=$engine_type;
397 CREATE PROCEDURE p1()
399 INSERT INTO t1 (i) VALUES (NULL);
400 INSERT INTO t2 (i) VALUES (LAST_INSERT_ID());
401 INSERT INTO t1 (i) VALUES (NULL), (NULL);
402 INSERT INTO t2 (i) VALUES (LAST_INSERT_ID());
405 CREATE FUNCTION f1() RETURNS INT MODIFIES SQL DATA
407 INSERT INTO t1 (i) VALUES (NULL);
408 INSERT INTO t2 (i) VALUES (LAST_INSERT_ID());
409 INSERT INTO t1 (i) VALUES (NULL), (NULL);
410 INSERT INTO t2 (i) VALUES (LAST_INSERT_ID());
414 CREATE FUNCTION f2() RETURNS INT NOT DETERMINISTIC
415 RETURN LAST_INSERT_ID() |
417 CREATE FUNCTION f3() RETURNS INT MODIFIES SQL DATA
419 INSERT INTO t2 (i) VALUES (LAST_INSERT_ID());
424 INSERT INTO t1 VALUES (NULL, -1);
427 INSERT INTO t1 VALUES (NULL, f2()), (NULL, LAST_INSERT_ID()),
428 (NULL, LAST_INSERT_ID()), (NULL, f2()), (NULL, f2());
429 INSERT INTO t1 VALUES (NULL, f2());
430 # Test replication of substitution "IS NULL" -> "= LAST_INSERT_ID".
431 INSERT INTO t1 VALUES (NULL, 0), (NULL, LAST_INSERT_ID());
432 UPDATE t1 SET j= -1 WHERE i IS NULL;
434 # Test statement-based replication of function calls.
435 INSERT INTO t1 (i) VALUES (NULL);
437 # Here, we rely on having set @@concurrent_insert= 0 (see comment at
438 # the top of this file).
440 INSERT INTO t1 (i) VALUES (NULL);
445 SELECT * FROM t1 ORDER BY i;
446 SELECT * FROM t2 ORDER BY i;
448 sync_slave_with_master;
460 sync_slave_with_master;
463 --echo # End of 5.0 tests
466 # Tests in this file are tightly bound together. Recreate t2.
468 eval create table t2 (
469 id int not null auto_increment,
472 ) engine=$engine_type;
475 # Test for BUG#20341 "stored function inserting into one
476 # auto_increment puts bad data in slave"
481 eval create table t1 (id tinyint primary key) engine=$engine_type;
484 create function insid() returns int
486 insert into t2 (last_id) values (0);
491 insert into t2 (id) values(1),(2),(3);
494 #inside SELECT, then inside INSERT
497 insert into t2 (id) values(5),(6),(7);
498 delete from t2 where id>=5;
500 insert into t1 select insid();
501 select * from t1 order by id;
502 select * from t2 order by id;
504 sync_slave_with_master;
505 select * from t1 order by id;
506 select * from t2 order by id;
513 eval create table t1 (n int primary key auto_increment not null,
514 b int, unique(b)) engine=$engine_type;
516 create procedure foo()
518 insert into t1 values(null,10);
519 insert ignore into t1 values(null,10);
520 insert ignore into t1 values(null,10);
521 insert into t2 values(null,3);
525 select * from t1 order by n;
526 select * from t2 order by id;
528 sync_slave_with_master;
529 select * from t1 order by n;
530 select * from t2 order by id;
535 SET @@global.concurrent_insert= @old_concurrent_insert;
537 --source include/rpl_end.inc