2
create table t1(a int not null, b int, c char(10) not null, d varchar(20)) engine = innodb;
3
insert into t1 values (5,5,'oo','oo'),(4,4,'tr','tr'),(3,4,'ad','ad'),(2,3,'ak','ak');
6
alter table t1 add index b (b), add index b (b);
7
--error ER_DUP_FIELDNAME
8
alter table t1 add index (b,b);
9
alter table t1 add index d2 (d);
11
explain select * from t1 force index(d2) order by d;
12
select * from t1 force index (d2) order by d;
14
alter table t1 add unique index (b);
16
alter table t1 add index (b);
19
# Check how existing tables interfere with temporary tables.
20
CREATE TABLE `t1#1`(a INT PRIMARY KEY) ENGINE=InnoDB;
23
alter table t1 add unique index (c), add index (d);
24
rename table `t1#1` to `t1#2`;
26
alter table t1 add unique index (c), add index (d);
29
alter table t1 add unique index (c), add index (d);
31
explain select * from t1 force index(c) order by c;
32
alter table t1 add primary key (a), drop index c;
34
--error ER_MULTIPLE_PRI_KEY
35
alter table t1 add primary key (c);
37
alter table t1 drop primary key, add primary key (b);
38
create unique index c on t1 (c);
40
explain select * from t1 force index(c) order by c;
41
select * from t1 force index(c) order by c;
42
alter table t1 drop index b, add index (b);
44
insert into t1 values(6,1,'ggg','ggg');
46
select * from t1 force index(b) order by b;
47
select * from t1 force index(c) order by c;
48
select * from t1 force index(d) order by d;
49
explain select * from t1 force index(b) order by b;
50
explain select * from t1 force index(c) order by c;
51
explain select * from t1 force index(d) order by d;
55
create table t1(a int not null, b int, c char(10), d varchar(20), primary key (a)) engine = innodb;
56
insert into t1 values (1,1,'ab','ab'),(2,2,'ac','ac'),(3,3,'ad','ad'),(4,4,'afe','afe');
58
alter table t1 add index (c(2));
60
alter table t1 add unique index (d(10));
62
insert into t1 values(5,1,'ggg','ggg');
64
select * from t1 force index(c) order by c;
65
select * from t1 force index(d) order by d;
66
explain select * from t1 order by b;
67
explain select * from t1 force index(c) order by c;
68
explain select * from t1 force index(d) order by d;
70
alter table t1 drop index d;
71
insert into t1 values(8,9,'fff','fff');
73
select * from t1 force index(c) order by c;
74
explain select * from t1 order by b;
75
explain select * from t1 force index(c) order by c;
76
explain select * from t1 order by d;
80
create table t1(a int not null, b int, c char(10), d varchar(20), primary key (a)) engine = innodb;
81
insert into t1 values (1,1,'ab','ab'),(2,2,'ac','ac'),(3,2,'ad','ad'),(4,4,'afe','afe');
83
alter table t1 add unique index (b,c);
84
insert into t1 values(8,9,'fff','fff');
86
select * from t1 force index(b) order by b;
87
explain select * from t1 force index(b) order by b;
89
alter table t1 add index (b,c);
90
insert into t1 values(11,11,'kkk','kkk');
92
select * from t1 force index(b) order by b;
93
explain select * from t1 force index(b) order by b;
95
alter table t1 add unique index (c,d);
96
insert into t1 values(13,13,'yyy','aaa');
98
select * from t1 force index(b) order by b;
99
select * from t1 force index(c) order by c;
100
explain select * from t1 force index(b) order by b;
101
explain select * from t1 force index(c) order by c;
102
show create table t1;
105
create table t1(a int not null, b int not null, c int, primary key (a), key (b)) engine = innodb;
106
create table t3(a int not null, c int not null, d int, primary key (a), key (c)) engine = innodb;
107
create table t4(a int not null, d int not null, e int, primary key (a), key (d)) engine = innodb;
108
create table t2(a int not null, b int not null, c int not null, d int not null, e int,
109
foreign key (b) references t1(b) on delete cascade,
110
foreign key (c) references t3(c), foreign key (d) references t4(d))
112
--error ER_DROP_INDEX_FK
113
alter table t1 drop index b;
114
--error ER_DROP_INDEX_FK
115
alter table t3 drop index c;
116
--error ER_DROP_INDEX_FK
117
alter table t4 drop index d;
118
--error ER_DROP_INDEX_FK
119
alter table t2 drop index b;
120
--error ER_DROP_INDEX_FK
121
alter table t2 drop index b, drop index c, drop index d;
122
# Apparently, the following makes mysql_alter_table() drop index d.
123
create unique index dc on t2 (d,c);
124
create index dc on t1 (b,c);
125
# This should preserve the foreign key constraints.
126
alter table t2 add primary key (a);
127
insert into t1 values (1,1,1);
128
insert into t3 values (1,1,1);
129
insert into t4 values (1,1,1);
130
insert into t2 values (1,1,1,1,1);
132
alter table t4 add constraint dc foreign key (a) references t1(a);
133
show create table t4;
134
--replace_regex /'test\.#sql-[0-9a-f_]*'/'#sql-temporary'/
135
# a foreign key 'test/dc' already exists
136
--error ER_CANT_CREATE_TABLE
137
alter table t3 add constraint dc foreign key (a) references t1(a);
138
show create table t3;
139
alter table t2 drop index b, add index (b);
140
show create table t2;
141
--error ER_ROW_IS_REFERENCED_2
143
--error ER_CANT_DROP_FIELD_OR_KEY
145
# there is no foreign key dc on t3
146
--replace_regex /'\.\/test\/#sql2-[0-9a-f-]*'/'#sql2-temporary'/
147
--error ER_ERROR_ON_RENAME
148
alter table t3 drop foreign key dc;
149
alter table t4 drop foreign key dc;
154
drop table t2,t4,t3,t1;
157
create table t1(a int not null, b int) engine = innodb;
158
insert into t1 values (1,1),(1,1),(1,1),(1,1);
160
alter table t1 add unique index (a);
162
alter table t1 add unique index (b);
164
alter table t1 add unique index (a), add unique index(b);
165
show create table t1;
168
create table t1(a int not null, c int not null,b int, primary key(a), unique key(c), key(b)) engine = innodb;
169
alter table t1 drop index c, drop index b;
170
show create table t1;
173
create table t1(a int not null, b int, primary key(a)) engine = innodb;
174
alter table t1 add index (b);
175
show create table t1;
178
create table t1(a int not null, b int, c char(10), d varchar(20), primary key (a)) engine = innodb;
179
insert into t1 values (1,1,'ab','ab'),(2,2,'ac','ac'),(3,3,'ac','ac'),(4,4,'afe','afe'),(5,4,'affe','affe');
181
alter table t1 add unique index (b), add unique index (c), add unique index (d);
183
alter table t1 add unique index (c), add unique index (b), add index (d);
184
show create table t1;
187
create table t1(a int not null, b int not null, c int, primary key (a), key(c)) engine=innodb;
188
insert into t1 values (5,1,5),(4,2,4),(3,3,3),(2,4,2),(1,5,1);
189
alter table t1 add unique index (b);
190
insert into t1 values (10,20,20),(11,19,19),(12,18,18),(13,17,17);
191
show create table t1;
193
explain select * from t1 force index(c) order by c;
194
explain select * from t1 order by a;
195
explain select * from t1 force index(b) order by b;
196
select * from t1 order by a;
197
select * from t1 force index(b) order by b;
198
select * from t1 force index(c) order by c;
201
create table t1(a int not null, b int not null) engine=innodb;
202
insert into t1 values (1,1);
203
alter table t1 add primary key(b);
204
insert into t1 values (2,2);
205
show create table t1;
208
explain select * from t1;
209
explain select * from t1 order by a;
210
explain select * from t1 order by b;
214
create table t1(a int not null) engine=innodb;
215
insert into t1 values (1);
216
alter table t1 add primary key(a);
217
insert into t1 values (2);
218
show create table t1;
222
explain select * from t1;
223
explain select * from t1 order by a;
226
create table t2(d varchar(17) primary key) engine=innodb default charset=utf8;
227
create table t3(a int primary key) engine=innodb;
229
insert into t3 values(22),(44),(33),(55),(66);
231
insert into t2 values ('jejdkrun87'),('adfd72nh9k'),
232
('adfdpplkeock'),('adfdijnmnb78k'),('adfdijn0loKNHJik');
234
create table t1(a int, b blob, c text, d text not null)
235
engine=innodb default charset = utf8;
237
# r2667 The following test is disabled because MySQL behavior changed.
238
# r2667 The test was added with this comment:
240
# r2667 ------------------------------------------------------------------------
241
# r2667 r1699 | marko | 2007-08-10 19:53:19 +0300 (Fri, 10 Aug 2007) | 5 lines
243
# r2667 branches/zip: Add changes that accidentally omitted from r1698:
245
# r2667 innodb-index.test, innodb-index.result: Add a test for creating
246
# r2667 a PRIMARY KEY on a column that contains a NULL value.
247
# r2667 ------------------------------------------------------------------------
249
# r2667 but in BZR-r2667:
250
# r2667 http://bazaar.launchpad.net/~mysql/mysql-server/mysql-5.1/revision/davi%40mysql.com-20080617141221-8yre8ys9j4uw3xx5?start_revid=joerg%40mysql.com-20080630105418-7qoe5ehomgrcdb89
251
# r2667 MySQL changed the behavior to do full table copy when creating PRIMARY INDEX
252
# r2667 on a non-NULL column instead of calling ::add_index() which would fail (and
253
# r2667 this is what we were testing here). Before r2667 the code execution path was
254
# r2667 like this (when adding PRIMARY INDEX on a non-NULL column with ALTER TABLE):
256
# r2667 mysql_alter_table()
257
# r2667 compare_tables() // would return ALTER_TABLE_INDEX_CHANGED
258
# r2667 ::add_index() // would fail with "primary index cannot contain NULL"
260
# r2667 after r2667 the code execution path is the following:
262
# r2667 mysql_alter_table()
263
# r2667 compare_tables() // returns ALTER_TABLE_DATA_CHANGED
264
# r2667 full copy is done, without calling ::add_index()
266
# r2667 To enable, remove "# r2667: " below.
268
# r2667: insert into t1 values (null,null,null,'null');
270
select a,left(repeat(d,100*a),65535),repeat(d,20*a),d from t2,t3;
272
select count(*) from t1 where a=44;
274
length(b),b=left(repeat(d,100*a),65535),length(c),c=repeat(d,20*a),d from t1;
275
# r2667: --error ER_PRIMARY_CANT_HAVE_NULL
276
# r2667: alter table t1 add primary key (a), add key (b(20));
277
# r2667: delete from t1 where d='null';
279
alter table t1 add primary key (a), add key (b(20));
280
delete from t1 where a%2;
282
alter table t1 add primary key (a,b(255),c(255)), add key (b(767));
283
select count(*) from t1 where a=44;
285
length(b),b=left(repeat(d,100*a),65535),length(c),c=repeat(d,20*a),d from t1;
286
show create table t1;
288
explain select * from t1 where b like 'adfd%';
294
create table t2(a int, b varchar(255), primary key(a,b)) engine=innodb;
295
insert into t2 select a,left(b,255) from t1;
297
rename table t2 to t1;
299
connect (a,localhost,root,,);
300
connect (b,localhost,root,,);
302
set innodb_lock_wait_timeout=1;
304
# Obtain an IX lock on the table
305
select a from t1 limit 1 for update;
307
set innodb_lock_wait_timeout=1;
308
# This would require an S lock on the table, conflicting with the IX lock.
309
--error ER_LOCK_WAIT_TIMEOUT
310
create index t1ba on t1 (b,a);
314
# Obtain an IS lock on the table
315
select a from t1 limit 1 lock in share mode;
317
# This will require an S lock on the table. No conflict with the IS lock.
318
create index t1ba on t1 (b,a);
319
# This would require an X lock on the table, conflicting with the IS lock.
320
--error ER_LOCK_WAIT_TIMEOUT
321
drop index t1ba on t1;
324
explain select a from t1 order by b;
326
select a,sleep(2+a/100) from t1 order by b limit 3;
328
# The following DROP INDEX will succeed, altough the SELECT above has
329
# opened a read view. However, during the execution of the SELECT,
330
# MySQL should hold a table lock that should block the execution
331
# of the DROP INDEX below.
335
drop index t1ba on t1;
337
# After the index was dropped, subsequent SELECTs will use the same
338
# read view, but they should not be accessing the dropped index any more.
342
explain select a from t1 order by b;
343
select a from t1 order by b limit 3;
352
let $per_table=`select @@innodb_file_per_table`;
353
let $format=`select @@innodb_file_format`;
354
set global innodb_file_per_table=on;
355
set global innodb_file_format='Barracuda';
356
# Test creating a table that could lead to undo log overflow.
357
# In the undo log, we write a 768-byte prefix (REC_MAX_INDEX_COL_LEN)
358
# of each externally stored column that appears as a column prefix in an index.
359
# For this test case, it would suffice to write 1 byte, though.
360
create table t1(a blob,b blob,c blob,d blob,e blob,f blob,g blob,h blob,
361
i blob,j blob,k blob,l blob,m blob,n blob,o blob,p blob,
362
q blob,r blob,s blob,t blob,u blob)
363
engine=innodb row_format=dynamic;
364
create index t1a on t1 (a(1));
365
create index t1b on t1 (b(1));
366
create index t1c on t1 (c(1));
367
create index t1d on t1 (d(1));
368
create index t1e on t1 (e(1));
369
create index t1f on t1 (f(1));
370
create index t1g on t1 (g(1));
371
create index t1h on t1 (h(1));
372
create index t1i on t1 (i(1));
373
create index t1j on t1 (j(1));
374
create index t1k on t1 (k(1));
375
create index t1l on t1 (l(1));
376
create index t1m on t1 (m(1));
377
create index t1n on t1 (n(1));
378
create index t1o on t1 (o(1));
379
create index t1p on t1 (p(1));
380
create index t1q on t1 (q(1));
381
create index t1r on t1 (r(1));
382
create index t1s on t1 (s(1));
383
create index t1t on t1 (t(1));
385
create index t1u on t1 (u(1));
387
create index t1ut on t1 (u(1), t(1));
388
create index t1st on t1 (s(1), t(1));
389
show create table t1;
391
create index t1u on t1 (u(1));
392
alter table t1 row_format=compact;
393
create index t1u on t1 (u(1));
396
eval set global innodb_file_per_table=$per_table;
397
eval set global innodb_file_format=$format;
400
# Test to check whether CREATE INDEX handles implicit foreign key
401
# constraint modifications (Issue #70, Bug #38786)
403
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
404
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
407
c1 BIGINT(12) NOT NULL,
409
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
412
c1 BIGINT(16) NOT NULL,
413
c2 BIGINT(12) NOT NULL,
414
c3 BIGINT(12) NOT NULL,
416
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
418
ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
419
FOREIGN KEY (c3) REFERENCES t1(c1);
421
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
422
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
424
SHOW CREATE TABLE t2;
426
CREATE INDEX i_t2_c3_c2 ON t2(c3, c2);
428
SHOW CREATE TABLE t2;
430
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
431
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
433
--error ER_NO_REFERENCED_ROW_2
434
INSERT INTO t2 VALUES(0,0,0);
435
INSERT INTO t1 VALUES(0);
436
INSERT INTO t2 VALUES(0,0,0);
441
c1 BIGINT(16) NOT NULL,
442
c2 BIGINT(12) NOT NULL,
443
c3 BIGINT(12) NOT NULL,
444
PRIMARY KEY (c1,c2,c3)
445
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
447
ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
448
FOREIGN KEY (c3) REFERENCES t1(c1);
450
SHOW CREATE TABLE t2;
452
CREATE INDEX i_t2_c3_c2 ON t2(c3, c2);
454
SHOW CREATE TABLE t2;
455
--error ER_NO_REFERENCED_ROW_2
456
INSERT INTO t2 VALUES(0,0,1);
457
INSERT INTO t2 VALUES(0,0,0);
458
--error ER_ROW_IS_REFERENCED_2
466
c1 BIGINT(12) NOT NULL,
469
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
472
c1 BIGINT(16) NOT NULL,
473
c2 BIGINT(12) NOT NULL,
474
c3 BIGINT(12) NOT NULL,
476
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
478
--replace_regex /'test\.#sql-[0-9_a-f-]*'/'#sql-temporary'/
479
--error ER_CANT_CREATE_TABLE
480
ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
481
FOREIGN KEY (c3,c2) REFERENCES t1(c1,c1);
482
--replace_regex /'test\.#sql-[0-9_a-f-]*'/'#sql-temporary'/
483
--error ER_CANT_CREATE_TABLE
484
ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
485
FOREIGN KEY (c3,c2) REFERENCES t1(c1,c2);
486
--replace_regex /'test\.#sql-[0-9_a-f-]*'/'#sql-temporary'/
487
--error ER_CANT_CREATE_TABLE
488
ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
489
FOREIGN KEY (c3,c2) REFERENCES t1(c2,c1);
490
ALTER TABLE t1 MODIFY COLUMN c2 BIGINT(12) NOT NULL;
491
--replace_regex /'test\.#sql-[0-9_a-f-]*'/'#sql-temporary'/
492
--error ER_CANT_CREATE_TABLE
493
ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
494
FOREIGN KEY (c3,c2) REFERENCES t1(c1,c2);
496
ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
497
FOREIGN KEY (c3,c2) REFERENCES t1(c2,c1);
498
SHOW CREATE TABLE t1;
499
SHOW CREATE TABLE t2;
500
CREATE INDEX i_t2_c2_c1 ON t2(c2, c1);
501
SHOW CREATE TABLE t2;
502
CREATE INDEX i_t2_c3_c1_c2 ON t2(c3, c1, c2);
503
SHOW CREATE TABLE t2;
504
CREATE INDEX i_t2_c3_c2 ON t2(c3, c2);
505
SHOW CREATE TABLE t2;