1 -- source include/have_ndb.inc
2 -- source include/not_embedded.inc
5 drop table if exists t1, t2, t3, t4, t5, t6, t7, t8;
9 # Simple test to show use of UNIQUE indexes
13 a int NOT NULL PRIMARY KEY,
19 insert t1 values(1, 2, 3), (2, 3, 5), (3, 4, 6), (4, 5, 8), (5,6, 2), (6,7, 2);
20 select * from t1 order by b;
21 select * from t1 where b = 4 order by b;
22 insert into t1 values(7,8,3);
23 select * from t1 where b = 4 order by a;
26 insert into t1 values(8, 2, 3);
27 select * from t1 order by a;
28 delete from t1 where a = 1;
29 insert into t1 values(8, 2, 3);
30 select * from t1 order by a;
32 alter table t1 drop index ib;
33 insert into t1 values(1, 2, 3);
36 create unique index ib on t1(b);
41 # Indexing NULL values
45 a int unsigned NOT NULL PRIMARY KEY,
51 insert into t1 values(1,1,1),(2,NULL,2),(3,NULL,NULL),(4,4,NULL);
52 select * from t1 use index (bc) where b IS NULL order by a;
54 select * from t1 use index (bc)order by a;
55 select * from t1 use index (bc) order by a;
56 select * from t1 use index (PRIMARY) where b IS NULL order by a;
57 select * from t1 use index (bc) where b IS NULL order by a;
58 select * from t1 use index (bc) where b IS NULL and c IS NULL order by a;
59 select * from t1 use index (bc) where b IS NULL and c = 2 order by a;
60 select * from t1 use index (bc) where b < 4 order by a;
61 select * from t1 use index (bc) where b IS NOT NULL order by a;
63 insert into t1 values(5,1,1);
68 # Show use of UNIQUE USING HASH indexes
72 a int unsigned NOT NULL PRIMARY KEY,
73 b int unsigned not null,
74 c int unsigned not null,
75 UNIQUE (b, c) USING HASH
78 insert t2 values(1, 2, 3), (2, 3, 5), (3, 4, 6), (4, 5, 8), (5,6, 2), (6,7, 2);
79 select * from t2 where a = 3;
80 select * from t2 where b = 4;
81 select * from t2 where c = 6;
82 insert into t2 values(7,8,3);
83 select * from t2 where b = 4 order by a;
86 insert into t2 values(8, 2, 3);
87 select * from t2 order by a;
88 delete from t2 where a = 1;
89 insert into t2 values(8, 2, 3);
90 select * from t2 order by a;
92 # Bug #24818 CREATE UNIQUE INDEX (...) USING HASH on a NDB table crashes mysqld
93 create unique index bi using hash on t2(b);
95 insert into t2 values(9, 3, 1);
96 alter table t2 drop index bi;
97 insert into t2 values(9, 3, 1);
98 select * from t2 order by a;
103 a int unsigned NOT NULL PRIMARY KEY,
104 b int unsigned not null,
106 UNIQUE (b, c) USING HASH
110 insert t2 values(1,1,NULL),(2,2,2),(3,3,NULL),(4,4,4),(5,5,NULL),(6,6,6),(7,7,NULL),(8,3,NULL),(9,3,NULL);
112 select * from t2 where c IS NULL order by a;
113 select * from t2 where b = 3 AND c IS NULL order by a;
114 select * from t2 where (b = 3 OR b = 5) AND c IS NULL order by a;
115 set @old_ecpd = @@session.engine_condition_pushdown;
116 set engine_condition_pushdown = true;
117 explain select * from t2 where (b = 3 OR b = 5) AND c IS NULL AND a < 9 order by a;
118 select * from t2 where (b = 3 OR b = 5) AND c IS NULL AND a < 9 order by a;
119 set engine_condition_pushdown = @old_ecpd;
124 # Show use of PRIMARY KEY USING HASH indexes
128 a int unsigned NOT NULL,
129 b int unsigned not null,
131 PRIMARY KEY (a, b) USING HASH
134 insert t3 values(1, 2, 3), (2, 3, 5), (3, 4, 6), (4, 5, 8), (5,6, 2), (6,7, 2);
135 select * from t3 where a = 3;
136 select * from t3 where b = 4;
137 select * from t3 where c = 6;
138 insert into t3 values(7,8,3);
139 select * from t3 where b = 4 order by a;
144 # Indexes on NULL-able columns
148 pk int NOT NULL PRIMARY KEY,
153 insert into t1 values (-1,NULL), (0,0), (1,NULL),(2,2),(3,NULL),(4,4);
155 select * from t1 order by pk;
158 insert into t1 values (5,0);
159 select * from t1 order by pk;
160 delete from t1 where a = 0;
161 insert into t1 values (5,0);
162 select * from t1 order by pk;
165 pk int NOT NULL PRIMARY KEY,
172 insert into t2 values (-1,1,17,NULL),(0,NULL,18,NULL),(1,3,19,'abc');
174 select * from t2 order by pk;
177 insert into t2 values(2,3,19,'abc');
178 select * from t2 order by pk;
179 delete from t2 where c IS NOT NULL;
180 insert into t2 values(2,3,19,'abc');
181 select * from t2 order by pk;
186 # More complex tables
190 cid smallint(5) unsigned NOT NULL default '0',
191 cv varchar(250) NOT NULL default '',
195 INSERT INTO t1 VALUES (8,'dummy');
197 cid bigint(20) unsigned NOT NULL auto_increment,
198 cap varchar(255) NOT NULL default '',
200 UNIQUE KEY (cid, cap)
202 INSERT INTO t2 VALUES (NULL,'another dummy');
204 gid bigint(20) unsigned NOT NULL auto_increment,
205 gn varchar(255) NOT NULL default '',
206 must tinyint(4) default NULL,
209 INSERT INTO t3 VALUES (1,'V1',NULL);
211 uid bigint(20) unsigned NOT NULL default '0',
212 gid bigint(20) unsigned NOT NULL,
213 rid bigint(20) unsigned NOT NULL,
214 cid bigint(20) unsigned NOT NULL,
215 UNIQUE KEY m (uid,gid,rid,cid)
217 INSERT INTO t4 VALUES (1,1,2,4);
218 INSERT INTO t4 VALUES (1,1,2,3);
219 INSERT INTO t4 VALUES (1,1,5,7);
220 INSERT INTO t4 VALUES (1,1,10,8);
222 rid bigint(20) unsigned NOT NULL auto_increment,
223 rl varchar(255) NOT NULL default '',
227 uid bigint(20) unsigned NOT NULL auto_increment,
228 un varchar(250) NOT NULL default '',
229 uc smallint(5) unsigned NOT NULL default '0',
231 UNIQUE KEY nc (un,uc)
233 INSERT INTO t6 VALUES (1,'test',8);
234 INSERT INTO t6 VALUES (2,'test2',9);
235 INSERT INTO t6 VALUES (3,'tre',3);
237 mid bigint(20) unsigned NOT NULL PRIMARY KEY,
238 uid bigint(20) unsigned NOT NULL default '0',
239 gid bigint(20) unsigned NOT NULL,
240 rid bigint(20) unsigned NOT NULL,
241 cid bigint(20) unsigned NOT NULL,
242 UNIQUE KEY m (uid,gid,rid,cid)
244 INSERT INTO t7 VALUES(1, 1, 1, 1, 1);
245 INSERT INTO t7 VALUES(2, 2, 1, 1, 1);
246 INSERT INTO t7 VALUES(3, 3, 1, 1, 1);
247 INSERT INTO t7 VALUES(4, 4, 1, 1, 1);
248 INSERT INTO t7 VALUES(5, 5, 1, 1, 1);
249 INSERT INTO t7 VALUES(6, 1, 1, 1, 6);
250 INSERT INTO t7 VALUES(7, 2, 1, 1, 7);
251 INSERT INTO t7 VALUES(8, 3, 1, 1, 8);
252 INSERT INTO t7 VALUES(9, 4, 1, 1, 9);
253 INSERT INTO t7 VALUES(10, 5, 1, 1, 10);
255 select * from t1 where cv = 'dummy';
256 select * from t1 where cv = 'test';
257 select * from t2 where cap = 'another dummy';
258 select * from t4 where uid = 1 and gid=1 and rid=2 and cid=4;
259 select * from t4 where uid = 1 and gid=1 and rid=1 and cid=4;
260 select * from t4 where uid = 1 order by cid;
261 select * from t4 where rid = 2 order by cid;
262 select * from t6 where un='test' and uc=8;
263 select * from t6 where un='test' and uc=7;
264 select * from t6 where un='test';
265 select * from t7 where mid = 8;
266 select * from t7 where uid = 8;
267 select * from t7 where uid = 1 order by mid;
268 select * from t7 where uid = 4 order by mid;
269 select * from t7 where gid = 4;
270 select * from t7 where gid = 1 order by mid;
271 select * from t7 where cid = 4;
272 select * from t7 where cid = 8;
275 # insert more records into t4
281 eval insert into t4 values(1, $1, 5, 12);
282 eval insert into t4 values($1, 3, 9, 11);
287 select * from t4 where uid = 1 and gid=1 and rid=2 and cid=4;
288 select * from t4 where uid = 1 and gid=1 and rid=1 and cid=4;
289 select * from t4 where uid = 1 order by gid,cid;
290 select * from t4 where uid = 1 order by gid,cid;
291 select * from t4 where rid = 2 order by cid;
294 drop table t1,t2,t3,t4,t5,t6,t7;
296 # test null in indexes
298 a int unsigned NOT NULL PRIMARY KEY,
301 UNIQUE bc(b,c) ) engine = ndb;
303 insert into t1 values(1,1,1),(2,NULL,2),(3,NULL,NULL),(4,4,NULL);
304 select * from t1 where b=1 and c=1;
305 select * from t1 where b is null and c is null;
306 select * from t1 where b is null and c = 2;
307 select * from t1 where b = 4 and c is null;
309 select * from t1 where (b = 1 and c = 1)
310 or (b is null and c is null)
311 or (b is null and c = 2)
312 or (b = 4 and c is null);
313 select * from t8 order by a;
314 select * from t1 order by a;
317 ###############################
320 # Unique index not specified in the same order as in table
324 id integer not null auto_increment,
325 month integer not null,
326 year integer not null,
327 code varchar( 2) not null,
329 unique idx_t1( month, code, year)
332 INSERT INTO t1 (month, year, code) VALUES (4,2004,'12');
333 INSERT INTO t1 (month, year, code) VALUES (5,2004,'12');
335 select * from t1 where code = '12' and month = 4 and year = 2004 ;
339 # bug#15918 Unique Key Limit in NDB Engine
341 create table t1 (a int primary key, b varchar(1000) not null, unique key (b))
342 engine=ndb charset=utf8;
344 insert into t1 values (1, repeat(_utf8 0xe288ab6474, 200));
346 insert into t1 values (2, repeat(_utf8 0xe288ab6474, 200));
347 select a, sha1(b) from t1;
349 # perl -e 'print pack("H2000","e288ab6474"x200)' | sha1sum
353 # bug#21873 MySQLD Crash on ALTER...ADD..UNIQUE..USING HASH statement for NDB backed table
355 create table t1(id int not null) engine = NDB;
357 alter table t1 add constraint uk_test unique (id) using hash;