2
# Test of auto_increment; The test for BDB tables is in bdb.test
5
drop table if exists t1;
6
drop table if exists t2;
10
create table t1 (a int not null auto_increment,b int, primary key (a)) engine=myisam auto_increment=3;
11
insert into t1 values (1,1),(NULL,3),(NULL,4);
12
delete from t1 where a=4;
13
insert into t1 values (NULL,5),(NULL,6);
15
delete from t1 where a=6;
16
#show table status like "t1";
17
replace t1 values (3,1);
18
ALTER TABLE t1 add c int;
19
replace t1 values (3,3,3);
20
insert into t1 values (NULL,7,7);
21
update t1 set a=8,b=b+1,c=c+1 where a=7;
22
insert into t1 values (NULL,9,9);
27
skey tinyint unsigned NOT NULL auto_increment PRIMARY KEY,
30
insert into t1 values (NULL, "hello");
31
insert into t1 values (NULL, "hey");
33
select _rowid,t1._rowid,skey,sval from t1;
37
# Test auto_increment on sub key
39
create table t1 (a char(10) not null, b int not null auto_increment, primary key(a,b));
40
insert into t1 values ("a",1),("b",2),("a",2),("c",1);
41
insert into t1 values ("a",NULL),("b",NULL),("c",NULL),("e",NULL);
42
insert into t1 (a) values ("a"),("b"),("c"),("d");
43
insert into t1 (a) values ('k'),('d');
44
insert into t1 (a) values ("a");
45
insert into t1 values ("d",last_insert_id());
49
create table t1 (ordid int(8) not null auto_increment, ord varchar(50) not null, primary key (ordid), index(ord,ordid));
50
insert into t1 (ordid,ord) values (NULL,'sdj'),(NULL,'sdj');
54
create table t1 (ordid int(8) not null auto_increment, ord varchar(50) not null, primary key (ord,ordid));
55
insert into t1 values (NULL,'sdj'),(NULL,'sdj'),(NULL,"abc"),(NULL,'abc'),(NULL,'zzz'),(NULL,'sdj'),(NULL,'abc');
59
create table t1 (sid char(5), id int(2) NOT NULL auto_increment, key(sid, id));
60
create table t2 (sid char(20), id int(2));
61
insert into t2 values ('skr',NULL),('skr',NULL),('test',NULL);
62
insert into t1 select * from t2;
67
# Test of auto_increment columns when they are set to 0
70
create table t1 (a int not null primary key auto_increment);
71
insert into t1 values (0);
78
# Test negative values (Bug #1366)
81
create table t1 (a int not null auto_increment primary key);
82
insert into t1 values (NULL);
83
insert into t1 values (-1);
84
select last_insert_id();
85
insert into t1 values (NULL);
89
create table t1 (a int not null auto_increment primary key) /*!40102 engine=heap */;
90
insert into t1 values (NULL);
91
insert into t1 values (-1);
92
select last_insert_id();
93
insert into t1 values (NULL);
97
# last_insert_id() madness
99
create table t1 (i tinyint unsigned not null auto_increment primary key);
100
insert into t1 set i = 254;
101
insert into t1 set i = null;
102
select last_insert_id();
103
explain extended select last_insert_id();
105
insert into t1 set i = 254;
106
select last_insert_id();
108
insert into t1 set i = null;
109
select last_insert_id();
112
create table t1 (i tinyint unsigned not null auto_increment, key (i));
113
insert into t1 set i = 254;
114
insert into t1 set i = null;
115
select last_insert_id();
116
insert into t1 set i = null;
117
select last_insert_id();
120
create table t1 (i tinyint unsigned not null auto_increment primary key, b int, unique (b));
121
insert into t1 values (NULL, 10);
122
select last_insert_id();
123
insert into t1 values (NULL, 15);
124
select last_insert_id();
126
insert into t1 values (NULL, 10);
127
select last_insert_id();
131
create table t1(a int auto_increment,b int null,primary key(a));
132
SET SQL_MODE=NO_AUTO_VALUE_ON_ZERO;
133
insert into t1(a,b)values(NULL,1);
134
insert into t1(a,b)values(200,2);
135
insert into t1(a,b)values(0,3);
136
insert into t1(b)values(4);
137
insert into t1(b)values(5);
138
insert into t1(b)values(6);
139
insert into t1(b)values(7);
140
select * from t1 order by b;
141
alter table t1 modify b mediumint;
142
select * from t1 order by b;
143
create table t2 (a int);
144
insert t2 values (1),(2);
145
alter table t2 add b int auto_increment primary key;
148
delete from t1 where a=0;
149
update t1 set a=0 where b=5;
150
select * from t1 order by b;
151
delete from t1 where a=0;
153
update t1 set a=NULL where b=6;
154
update t1 set a=300 where b=7;
156
insert into t1(a,b)values(NULL,8);
157
insert into t1(a,b)values(400,9);
158
insert into t1(a,b)values(0,10);
159
insert into t1(b)values(11);
160
insert into t1(b)values(12);
161
insert into t1(b)values(13);
162
insert into t1(b)values(14);
163
select * from t1 order by b;
164
delete from t1 where a=0;
165
update t1 set a=0 where b=12;
166
select * from t1 order by b;
167
delete from t1 where a=0;
169
update t1 set a=NULL where b=13;
170
update t1 set a=500 where b=14;
171
select * from t1 order by b;
175
# Test of behavior of ALTER TABLE when coulmn containing NULL or zeroes is
176
# converted to AUTO_INCREMENT column
178
create table t1 (a bigint);
179
insert into t1 values (1), (2), (3), (NULL), (NULL);
180
alter table t1 modify a bigint not null auto_increment primary key;
184
create table t1 (a bigint);
185
insert into t1 values (1), (2), (3), (0), (0);
186
alter table t1 modify a bigint not null auto_increment primary key;
190
# We still should be able to preserve zero in NO_AUTO_VALUE_ON_ZERO mode
191
create table t1 (a bigint);
192
insert into t1 values (0), (1), (2), (3);
193
set sql_mode=NO_AUTO_VALUE_ON_ZERO;
194
alter table t1 modify a bigint not null auto_increment primary key;
199
# It also sensible to preserve zeroes if we are converting auto_increment
200
# column to auto_increment column (or not touching it at all, which is more
201
# common case probably)
202
create table t1 (a int auto_increment primary key , b int null);
203
set sql_mode=NO_AUTO_VALUE_ON_ZERO;
204
insert into t1 values (0,1),(1,2),(2,3);
207
alter table t1 modify b varchar(255);
208
insert into t1 values (0,4);
213
# BUG #10045: Problem with composite AUTO_INCREMENT + BLOB key
215
CREATE TABLE t1 ( a INT AUTO_INCREMENT, b BLOB, PRIMARY KEY (a,b(10)));
216
INSERT INTO t1 (b) VALUES ('aaaa');
218
INSERT INTO t1 (b) VALUES ('');
220
INSERT INTO t1 (b) VALUES ('bbbb');
222
DROP TABLE IF EXISTS t1;
227
t1_name VARCHAR(255) DEFAULT NULL,
228
t1_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
231
) AUTO_INCREMENT = 1000;
233
INSERT INTO t1 (t1_name) VALUES('MySQL');
234
INSERT INTO t1 (t1_name) VALUES('MySQL');
235
INSERT INTO t1 (t1_name) VALUES('MySQL');
239
SHOW CREATE TABLE `t1`;
244
# Bug #6880: LAST_INSERT_ID() within a statement
247
create table t1(a int not null auto_increment primary key);
248
create table t2(a int not null auto_increment primary key, t1a int);
249
insert into t1 values(NULL);
250
insert into t2 values (NULL, LAST_INSERT_ID()), (NULL, LAST_INSERT_ID());
251
insert into t1 values (NULL);
252
insert into t2 values (NULL, LAST_INSERT_ID()), (NULL, LAST_INSERT_ID()),
253
(NULL, LAST_INSERT_ID());
254
insert into t1 values (NULL);
255
insert into t2 values (NULL, LAST_INSERT_ID()), (NULL, LAST_INSERT_ID()),
256
(NULL, LAST_INSERT_ID()), (NULL, LAST_INSERT_ID());
260
--echo End of 4.1 tests
263
# Bug #11080 & #11005 Multi-row REPLACE fails on a duplicate key error
266
CREATE TABLE t1 ( `a` int(11) NOT NULL auto_increment, `b` int(11) default NULL,PRIMARY KEY (`a`),UNIQUE KEY `b` (`b`));
267
insert into t1 (b) values (1);
268
replace into t1 (b) values (2), (1), (3);
271
insert into t1 (b) values (1);
272
replace into t1 (b) values (2);
273
replace into t1 (b) values (1);
274
replace into t1 (b) values (3);
278
create table t1 (rowid int not null auto_increment, val int not null,primary
279
key (rowid), unique(val));
280
replace into t1 (val) values ('1'),('2');
281
replace into t1 (val) values ('1'),('2');
283
insert into t1 (val) values ('1'),('2');
288
# Test that update changes internal auto-increment value
291
create table t1 (a int not null auto_increment primary key, val int);
292
insert into t1 (val) values (1);
293
update t1 set a=2 where a=1;
294
insert into t1 (val) values (1);
299
# Test key duplications with auto-increment in ALTER TABLE
302
CREATE TABLE t1 (t1 INT(10) PRIMARY KEY, t2 INT(10));
303
INSERT INTO t1 VALUES(0, 0);
304
INSERT INTO t1 VALUES(1, 1);
306
ALTER TABLE t1 CHANGE t1 t1 INT(10) auto_increment;
309
# Test of REPLACE when it does INSERT+DELETE and not UPDATE:
310
# see if it sets LAST_INSERT_ID() ok
311
create table t1 (a int primary key auto_increment, b int, c int, d timestamp default current_timestamp, unique(b),unique(c));
312
insert into t1 values(null,1,1,now());
313
insert into t1 values(null,0,0,null);
314
# this will delete two rows
315
replace into t1 values(null,1,0,null);
316
select last_insert_id();