1
-- source include/have_innodb.inc
3
create table t1(a int not null, b int, c char(10) not null, d varchar(20)) engine = innodb;
4
insert into t1 values (5,5,'oo','oo'),(4,4,'tr','tr'),(3,4,'ad','ad'),(2,3,'ak','ak');
7
alter table t1 add index b (b), add index b (b);
8
--error ER_DUP_FIELDNAME
9
alter table t1 add index (b,b);
10
alter table t1 add index d2 (d);
12
explain select * from t1 force index(d2) order by d;
13
select * from t1 force index (d2) order by d;
15
alter table t1 add unique index (b);
17
alter table t1 add index (b);
20
# Check how existing tables interfere with temporary tables.
21
CREATE TABLE `t1#1`(a INT PRIMARY KEY) ENGINE=InnoDB;
24
alter table t1 add unique index (c), add index (d);
25
rename table `t1#1` to `t1#2`;
27
alter table t1 add unique index (c), add index (d);
30
alter table t1 add unique index (c), add index (d);
32
explain select * from t1 force index(c) order by c;
33
alter table t1 add primary key (a), drop index c;
35
--error ER_MULTIPLE_PRI_KEY
36
alter table t1 add primary key (c);
38
alter table t1 drop primary key, add primary key (b);
39
create unique index c on t1 (c);
41
explain select * from t1 force index(c) order by c;
42
select * from t1 force index(c) order by c;
43
alter table t1 drop index b, add index (b);
45
insert into t1 values(6,1,'ggg','ggg');
47
select * from t1 force index(b) order by b;
48
select * from t1 force index(c) order by c;
49
select * from t1 force index(d) order by d;
50
explain select * from t1 force index(b) order by b;
51
explain select * from t1 force index(c) order by c;
52
explain select * from t1 force index(d) order by d;
56
create table t1(a int not null, b int, c char(10), d varchar(20), primary key (a)) engine = innodb;
57
insert into t1 values (1,1,'ab','ab'),(2,2,'ac','ac'),(3,3,'ad','ad'),(4,4,'afe','afe');
59
alter table t1 add index (c(2));
61
alter table t1 add unique index (d(10));
63
insert into t1 values(5,1,'ggg','ggg');
65
select * from t1 force index(c) order by c;
66
select * from t1 force index(d) order by d;
67
explain select * from t1 order by b;
68
explain select * from t1 force index(c) order by c;
69
explain select * from t1 force index(d) order by d;
71
alter table t1 drop index d;
72
insert into t1 values(8,9,'fff','fff');
74
select * from t1 force index(c) order by c;
75
explain select * from t1 order by b;
76
explain select * from t1 force index(c) order by c;
77
explain select * from t1 order by d;
81
create table t1(a int not null, b int, c char(10), d varchar(20), primary key (a)) engine = innodb;
82
insert into t1 values (1,1,'ab','ab'),(2,2,'ac','ac'),(3,2,'ad','ad'),(4,4,'afe','afe');
84
alter table t1 add unique index (b,c);
85
insert into t1 values(8,9,'fff','fff');
87
select * from t1 force index(b) order by b;
88
explain select * from t1 force index(b) order by b;
90
alter table t1 add index (b,c);
91
insert into t1 values(11,11,'kkk','kkk');
93
select * from t1 force index(b) order by b;
94
explain select * from t1 force index(b) order by b;
96
alter table t1 add unique index (c,d);
97
insert into t1 values(13,13,'yyy','aaa');
99
select * from t1 force index(b) order by b;
100
select * from t1 force index(c) order by c;
101
explain select * from t1 force index(b) order by b;
102
explain select * from t1 force index(c) order by c;
103
show create table t1;
106
create table t1(a int not null, b int not null, c int, primary key (a), key (b)) engine = innodb;
107
create table t3(a int not null, c int not null, d int, primary key (a), key (c)) engine = innodb;
108
create table t4(a int not null, d int not null, e int, primary key (a), key (d)) engine = innodb;
109
create table t2(a int not null, b int not null, c int not null, d int not null, e int,
110
foreign key (b) references t1(b) on delete cascade,
111
foreign key (c) references t3(c), foreign key (d) references t4(d))
113
--error ER_DROP_INDEX_FK
114
alter table t1 drop index b;
115
--error ER_DROP_INDEX_FK
116
alter table t3 drop index c;
117
--error ER_DROP_INDEX_FK
118
alter table t4 drop index d;
119
--error ER_DROP_INDEX_FK
120
alter table t2 drop index b;
121
--error ER_DROP_INDEX_FK
122
alter table t2 drop index b, drop index c, drop index d;
123
# Apparently, the following makes mysql_alter_table() drop index d.
124
create unique index dc on t2 (d,c);
125
create index dc on t1 (b,c);
126
# This should preserve the foreign key constraints.
127
alter table t2 add primary key (a);
128
insert into t1 values (1,1,1);
129
insert into t3 values (1,1,1);
130
insert into t4 values (1,1,1);
131
insert into t2 values (1,1,1,1,1);
133
alter table t4 add constraint dc foreign key (a) references t1(a);
134
show create table t4;
135
--replace_regex /'test\.#sql-[0-9a-f_]*'/'#sql-temporary'/
136
# a foreign key 'test/dc' already exists
137
--error ER_CANT_CREATE_TABLE
138
alter table t3 add constraint dc foreign key (a) references t1(a);
139
show create table t3;
140
alter table t2 drop index b, add index (b);
141
show create table t2;
142
--error ER_ROW_IS_REFERENCED_2
144
--error ER_CANT_DROP_FIELD_OR_KEY
146
# there is no foreign key dc on t3
147
--replace_regex /'\.\/test\/#sql2-[0-9a-f-]*'/'#sql2-temporary'/
148
--error ER_ERROR_ON_RENAME
149
alter table t3 drop foreign key dc;
150
alter table t4 drop foreign key dc;
155
drop table t2,t4,t3,t1;
157
-- let charset = utf8
158
-- source include/innodb-index.inc
160
create table t1(a int not null, b int) engine = innodb;
161
insert into t1 values (1,1),(1,1),(1,1),(1,1);
163
alter table t1 add unique index (a);
165
alter table t1 add unique index (b);
167
alter table t1 add unique index (a), add unique index(b);
168
show create table t1;
171
create table t1(a int not null, c int not null,b int, primary key(a), unique key(c), key(b)) engine = innodb;
172
alter table t1 drop index c, drop index b;
173
show create table t1;
176
create table t1(a int not null, b int, primary key(a)) engine = innodb;
177
alter table t1 add index (b);
178
show create table t1;
181
create table t1(a int not null, b int, c char(10), d varchar(20), primary key (a)) engine = innodb;
182
insert into t1 values (1,1,'ab','ab'),(2,2,'ac','ac'),(3,3,'ac','ac'),(4,4,'afe','afe'),(5,4,'affe','affe');
184
alter table t1 add unique index (b), add unique index (c), add unique index (d);
186
alter table t1 add unique index (c), add unique index (b), add index (d);
187
show create table t1;
190
create table t1(a int not null, b int not null, c int, primary key (a), key(c)) engine=innodb;
191
insert into t1 values (5,1,5),(4,2,4),(3,3,3),(2,4,2),(1,5,1);
192
alter table t1 add unique index (b);
193
insert into t1 values (10,20,20),(11,19,19),(12,18,18),(13,17,17);
194
show create table t1;
196
explain select * from t1 force index(c) order by c;
197
explain select * from t1 order by a;
198
explain select * from t1 force index(b) order by b;
199
select * from t1 order by a;
200
select * from t1 force index(b) order by b;
201
select * from t1 force index(c) order by c;
204
create table t1(a int not null, b int not null) engine=innodb;
205
insert into t1 values (1,1);
206
alter table t1 add primary key(b);
207
insert into t1 values (2,2);
208
show create table t1;
211
explain select * from t1;
212
explain select * from t1 order by a;
213
explain select * from t1 order by b;
217
create table t1(a int not null) engine=innodb;
218
insert into t1 values (1);
219
alter table t1 add primary key(a);
220
insert into t1 values (2);
221
show create table t1;
225
explain select * from t1;
226
explain select * from t1 order by a;
229
create table t2(d varchar(17) primary key) engine=innodb default charset=utf8;
230
create table t3(a int primary key) engine=innodb;
232
insert into t3 values(22),(44),(33),(55),(66);
234
insert into t2 values ('jejdkrun87'),('adfd72nh9k'),
235
('adfdpplkeock'),('adfdijnmnb78k'),('adfdijn0loKNHJik');
237
create table t1(a int, b blob, c text, d text not null)
238
engine=innodb default charset = utf8;
240
# r2667 The following test is disabled because MySQL behavior changed.
241
# r2667 The test was added with this comment:
243
# r2667 ------------------------------------------------------------------------
244
# r2667 r1699 | marko | 2007-08-10 19:53:19 +0300 (Fri, 10 Aug 2007) | 5 lines
246
# r2667 branches/zip: Add changes that accidentally omitted from r1698:
248
# r2667 innodb-index.test, innodb-index.result: Add a test for creating
249
# r2667 a PRIMARY KEY on a column that contains a NULL value.
250
# r2667 ------------------------------------------------------------------------
252
# r2667 but in BZR-r2667:
253
# r2667 http://bazaar.launchpad.net/~mysql/mysql-server/mysql-5.1/revision/davi%40mysql.com-20080617141221-8yre8ys9j4uw3xx5?start_revid=joerg%40mysql.com-20080630105418-7qoe5ehomgrcdb89
254
# r2667 MySQL changed the behavior to do full table copy when creating PRIMARY INDEX
255
# r2667 on a non-NULL column instead of calling ::add_index() which would fail (and
256
# r2667 this is what we were testing here). Before r2667 the code execution path was
257
# r2667 like this (when adding PRIMARY INDEX on a non-NULL column with ALTER TABLE):
259
# r2667 mysql_alter_table()
260
# r2667 compare_tables() // would return ALTER_TABLE_INDEX_CHANGED
261
# r2667 ::add_index() // would fail with "primary index cannot contain NULL"
263
# r2667 after r2667 the code execution path is the following:
265
# r2667 mysql_alter_table()
266
# r2667 compare_tables() // returns ALTER_TABLE_DATA_CHANGED
267
# r2667 full copy is done, without calling ::add_index()
269
# r2667 To enable, remove "# r2667: " below.
271
# r2667: insert into t1 values (null,null,null,'null');
273
select a,left(repeat(d,100*a),65535),repeat(d,20*a),d from t2,t3;
275
select count(*) from t1 where a=44;
277
length(b),b=left(repeat(d,100*a),65535),length(c),c=repeat(d,20*a),d from t1;
278
# r2667: --error ER_PRIMARY_CANT_HAVE_NULL
279
# r2667: alter table t1 add primary key (a), add key (b(20));
280
# r2667: delete from t1 where d='null';
282
alter table t1 add primary key (a), add key (b(20));
283
delete from t1 where a%2;
285
alter table t1 add primary key (a,b(255),c(255)), add key (b(767));
286
select count(*) from t1 where a=44;
288
length(b),b=left(repeat(d,100*a),65535),length(c),c=repeat(d,20*a),d from t1;
289
show create table t1;
291
explain select * from t1 where b like 'adfd%';
297
create table t2(a int, b varchar(255), primary key(a,b)) engine=innodb;
298
insert into t2 select a,left(b,255) from t1;
300
rename table t2 to t1;
302
connect (a,localhost,root,,);
303
connect (b,localhost,root,,);
305
set innodb_lock_wait_timeout=1;
307
# Obtain an IX lock on the table
308
select a from t1 limit 1 for update;
310
set innodb_lock_wait_timeout=1;
311
# This would require an S lock on the table, conflicting with the IX lock.
312
--error ER_LOCK_WAIT_TIMEOUT
313
create index t1ba on t1 (b,a);
317
# Obtain an IS lock on the table
318
select a from t1 limit 1 lock in share mode;
320
# This will require an S lock on the table. No conflict with the IS lock.
321
create index t1ba on t1 (b,a);
322
# This would require an X lock on the table, conflicting with the IS lock.
323
--error ER_LOCK_WAIT_TIMEOUT
324
drop index t1ba on t1;
327
explain select a from t1 order by b;
329
select a,sleep(2+a/100) from t1 order by b limit 3;
331
# The following DROP INDEX will succeed, altough the SELECT above has
332
# opened a read view. However, during the execution of the SELECT,
333
# MySQL should hold a table lock that should block the execution
334
# of the DROP INDEX below.
338
drop index t1ba on t1;
340
# After the index was dropped, subsequent SELECTs will use the same
341
# read view, but they should not be accessing the dropped index any more.
345
explain select a from t1 order by b;
346
select a from t1 order by b limit 3;
355
let $per_table=`select @@innodb_file_per_table`;
356
let $format=`select @@innodb_file_format`;
357
set global innodb_file_per_table=on;
358
set global innodb_file_format='Barracuda';
359
# Test creating a table that could lead to undo log overflow.
360
# In the undo log, we write a 768-byte prefix (REC_MAX_INDEX_COL_LEN)
361
# of each externally stored column that appears as a column prefix in an index.
362
# For this test case, it would suffice to write 1 byte, though.
363
create table t1(a blob,b blob,c blob,d blob,e blob,f blob,g blob,h blob,
364
i blob,j blob,k blob,l blob,m blob,n blob,o blob,p blob,
365
q blob,r blob,s blob,t blob,u blob)
366
engine=innodb row_format=dynamic;
367
create index t1a on t1 (a(1));
368
create index t1b on t1 (b(1));
369
create index t1c on t1 (c(1));
370
create index t1d on t1 (d(1));
371
create index t1e on t1 (e(1));
372
create index t1f on t1 (f(1));
373
create index t1g on t1 (g(1));
374
create index t1h on t1 (h(1));
375
create index t1i on t1 (i(1));
376
create index t1j on t1 (j(1));
377
create index t1k on t1 (k(1));
378
create index t1l on t1 (l(1));
379
create index t1m on t1 (m(1));
380
create index t1n on t1 (n(1));
381
create index t1o on t1 (o(1));
382
create index t1p on t1 (p(1));
383
create index t1q on t1 (q(1));
384
create index t1r on t1 (r(1));
385
create index t1s on t1 (s(1));
386
create index t1t on t1 (t(1));
388
create index t1u on t1 (u(1));
390
create index t1ut on t1 (u(1), t(1));
391
create index t1st on t1 (s(1), t(1));
392
show create table t1;
394
create index t1u on t1 (u(1));
395
alter table t1 row_format=compact;
396
create index t1u on t1 (u(1));
399
eval set global innodb_file_per_table=$per_table;
400
eval set global innodb_file_format=$format;
403
# Test to check whether CREATE INDEX handles implicit foreign key
404
# constraint modifications (Issue #70, Bug #38786)
406
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
407
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
410
c1 BIGINT(12) NOT NULL,
412
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
415
c1 BIGINT(16) NOT NULL,
416
c2 BIGINT(12) NOT NULL,
417
c3 BIGINT(12) NOT NULL,
419
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
421
ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
422
FOREIGN KEY (c3) REFERENCES t1(c1);
424
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
425
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
427
SHOW CREATE TABLE t2;
429
CREATE INDEX i_t2_c3_c2 ON t2(c3, c2);
431
SHOW CREATE TABLE t2;
433
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
434
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
436
--error ER_NO_REFERENCED_ROW_2
437
INSERT INTO t2 VALUES(0,0,0);
438
INSERT INTO t1 VALUES(0);
439
INSERT INTO t2 VALUES(0,0,0);
444
c1 BIGINT(16) NOT NULL,
445
c2 BIGINT(12) NOT NULL,
446
c3 BIGINT(12) NOT NULL,
447
PRIMARY KEY (c1,c2,c3)
448
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
450
ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
451
FOREIGN KEY (c3) REFERENCES t1(c1);
453
SHOW CREATE TABLE t2;
455
CREATE INDEX i_t2_c3_c2 ON t2(c3, c2);
457
SHOW CREATE TABLE t2;
458
--error ER_NO_REFERENCED_ROW_2
459
INSERT INTO t2 VALUES(0,0,1);
460
INSERT INTO t2 VALUES(0,0,0);
461
--error ER_ROW_IS_REFERENCED_2
469
c1 BIGINT(12) NOT NULL,
472
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
475
c1 BIGINT(16) NOT NULL,
476
c2 BIGINT(12) NOT NULL,
477
c3 BIGINT(12) NOT NULL,
479
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
481
--replace_regex /'test\.#sql-[0-9_a-f-]*'/'#sql-temporary'/
482
--error ER_CANT_CREATE_TABLE
483
ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
484
FOREIGN KEY (c3,c2) REFERENCES t1(c1,c1);
485
--replace_regex /'test\.#sql-[0-9_a-f-]*'/'#sql-temporary'/
486
--error ER_CANT_CREATE_TABLE
487
ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
488
FOREIGN KEY (c3,c2) REFERENCES t1(c1,c2);
489
--replace_regex /'test\.#sql-[0-9_a-f-]*'/'#sql-temporary'/
490
--error ER_CANT_CREATE_TABLE
491
ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
492
FOREIGN KEY (c3,c2) REFERENCES t1(c2,c1);
493
ALTER TABLE t1 MODIFY COLUMN c2 BIGINT(12) NOT NULL;
494
--replace_regex /'test\.#sql-[0-9_a-f-]*'/'#sql-temporary'/
495
--error ER_CANT_CREATE_TABLE
496
ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
497
FOREIGN KEY (c3,c2) REFERENCES t1(c1,c2);
499
ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
500
FOREIGN KEY (c3,c2) REFERENCES t1(c2,c1);
501
SHOW CREATE TABLE t1;
502
SHOW CREATE TABLE t2;
503
CREATE INDEX i_t2_c2_c1 ON t2(c2, c1);
504
SHOW CREATE TABLE t2;
505
CREATE INDEX i_t2_c3_c1_c2 ON t2(c3, c1, c2);
506
SHOW CREATE TABLE t2;
507
CREATE INDEX i_t2_c3_c2 ON t2(c3, c2);
508
SHOW CREATE TABLE t2;