ndb_index_unique.test 8.63 KB
Newer Older
unknown's avatar
unknown committed
1
-- source include/have_ndb.inc
unknown's avatar
unknown committed
2
-- source include/not_embedded.inc
unknown's avatar
unknown committed
3 4

--disable_warnings
5
drop table if exists t1, t2, t3, t4, t5, t6, t7, t8;
unknown's avatar
unknown committed
6 7 8 9 10 11 12
--enable_warnings

#
# Simple test to show use of UNIQUE indexes 
#

CREATE TABLE t1 (
13 14 15 16
  a int NOT NULL PRIMARY KEY,
  b int not null,
  c int,
  UNIQUE ib(b)
unknown's avatar
unknown committed
17 18 19 20 21 22 23 24
) engine=ndbcluster;

insert t1 values(1, 2, 3), (2, 3, 5), (3, 4, 6), (4, 5, 8), (5,6, 2), (6,7, 2);
select * from t1 order by b;
select * from t1 where b = 4 order by b;
insert into t1 values(7,8,3);
select * from t1 where b = 4 order by a;

25
-- error 1169
26 27 28 29 30 31
insert into t1 values(8, 2, 3);
select * from t1 order by a;
delete from t1 where a = 1;
insert into t1 values(8, 2, 3);
select * from t1 order by a;

32 33 34 35 36 37
alter table t1 drop index ib;
sleep 10;
insert into t1 values(1, 2, 3);
--error 1169
create unique index ib on t1(b);

unknown's avatar
unknown committed
38 39
drop table t1;

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#
# Indexing NULL values
#

CREATE TABLE t1 (
  a int unsigned NOT NULL PRIMARY KEY,
  b int unsigned,
  c int unsigned,
  UNIQUE bc(b,c)
) engine = ndb;

insert into t1 values(1,1,1),(2,NULL,2),(3,NULL,NULL),(4,4,NULL);
select * from t1 use index (bc) where b IS NULL order by a;

select * from t1 use index (bc)order by a;
select * from t1 use index (bc) order by a;
select * from t1 use index (PRIMARY) where b IS NULL order by a;
select * from t1 use index (bc) where b IS NULL order by a;
select * from t1 use index (bc) where b IS NULL and c IS NULL order by a;
select * from t1 use index (bc) where b IS NULL and c = 2 order by a;
select * from t1 use index (bc) where b < 4 order by a;
select * from t1 use index (bc) where b IS NOT NULL order by a;
62
-- error 1169
63 64 65
insert into t1 values(5,1,1);
drop table t1;

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

#
# Show use of UNIQUE USING HASH indexes 
#

CREATE TABLE t2 (
  a int unsigned NOT NULL PRIMARY KEY,
  b int unsigned not null,
  c int unsigned not null,
  UNIQUE USING HASH (b, c)	
) engine=ndbcluster;

insert t2 values(1, 2, 3), (2, 3, 5), (3, 4, 6), (4, 5, 8), (5,6, 2), (6,7, 2);
select * from t2 where a = 3;
select * from t2 where b = 4;
select * from t2 where c = 6;
insert into t2 values(7,8,3);
select * from t2 where b = 4 order by a;

85
-- error 1169
86 87 88 89 90 91
insert into t2 values(8, 2, 3);
select * from t2 order by a;
delete from t2 where a = 1;
insert into t2 values(8, 2, 3);
select * from t2 order by a;

92 93
drop table t2;

94 95 96 97 98 99 100 101
-- error 1121
CREATE TABLE t2 (
  a int unsigned NOT NULL PRIMARY KEY,
  b int unsigned not null,
  c int unsigned,
  UNIQUE USING HASH (b, c)	
) engine=ndbcluster;

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
#
# Show use of PRIMARY KEY USING HASH indexes 
#

CREATE TABLE t3 (
  a int unsigned NOT NULL,
  b int unsigned not null,
  c int unsigned,
  PRIMARY KEY USING HASH (a, b)	
) engine=ndbcluster;

insert t3 values(1, 2, 3), (2, 3, 5), (3, 4, 6), (4, 5, 8), (5,6, 2), (6,7, 2);
select * from t3 where a = 3;
select * from t3 where b = 4;
select * from t3 where c = 6;
insert into t3 values(7,8,3);
select * from t3 where b = 4 order by a;

drop table t3;

122 123 124 125
#
# Indexes on NULL-able columns 
#

unknown's avatar
unknown committed
126 127 128 129 130
CREATE TABLE t1 (
  pk int NOT NULL PRIMARY KEY,
  a int unsigned,
  UNIQUE KEY (a)
) engine=ndbcluster;
131
		 
unknown's avatar
unknown committed
132 133 134 135
insert into t1 values (-1,NULL), (0,0), (1,NULL),(2,2),(3,NULL),(4,4);

select * from t1 order by pk;

136
--error 1169
unknown's avatar
unknown committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
insert into t1 values (5,0);
select * from t1 order by pk;
delete from t1 where a = 0;
insert into t1 values (5,0);
select * from t1 order by pk;

CREATE TABLE t2 (
  pk int NOT NULL PRIMARY KEY,
  a int unsigned,
  b tinyint NOT NULL,
  c VARCHAR(10),
  UNIQUE KEY si(a, c)
) engine=ndbcluster;

insert into t2 values (-1,1,17,NULL),(0,NULL,18,NULL),(1,3,19,'abc');

select * from t2 order by pk;

155
--error 1169
unknown's avatar
unknown committed
156 157 158 159 160
insert into t2 values(2,3,19,'abc');
select * from t2 order by pk;
delete from t2 where c IS NOT NULL;
insert into t2 values(2,3,19,'abc');
select * from t2 order by pk;
161
		 
unknown's avatar
unknown committed
162
drop table t1, t2;
163

unknown's avatar
unknown committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177
#
# More complex tables
#

CREATE TABLE t1 (
  cid smallint(5) unsigned NOT NULL default '0',
  cv varchar(250) NOT NULL default '',
  PRIMARY KEY  (cid),
  UNIQUE KEY cv (cv)
) engine=ndbcluster;
INSERT INTO t1 VALUES (8,'dummy');
CREATE TABLE t2 (
  cid bigint(20) unsigned NOT NULL auto_increment,
  cap varchar(255) NOT NULL default '',
178 179
  PRIMARY KEY  (cid),
  UNIQUE KEY (cid, cap)
unknown's avatar
unknown committed
180
) engine=ndbcluster;
181
INSERT INTO t2 VALUES (NULL,'another dummy');
unknown's avatar
unknown committed
182 183 184 185
CREATE TABLE t3 (
  gid bigint(20) unsigned NOT NULL auto_increment,
  gn varchar(255) NOT NULL default '',
  must tinyint(4) default NULL,
unknown's avatar
unknown committed
186
  PRIMARY KEY  (gid)
unknown's avatar
unknown committed
187 188 189 190 191
) engine=ndbcluster;
INSERT INTO t3 VALUES (1,'V1',NULL);
CREATE TABLE t4 (
  uid bigint(20) unsigned NOT NULL default '0',
  gid bigint(20) unsigned NOT NULL,
192 193
  rid bigint(20) unsigned NOT NULL,
  cid bigint(20) unsigned NOT NULL,
unknown's avatar
unknown committed
194
  UNIQUE KEY m (uid,gid,rid,cid)
unknown's avatar
unknown committed
195 196 197 198 199 200 201 202
) engine=ndbcluster;
INSERT INTO t4 VALUES (1,1,2,4);
INSERT INTO t4 VALUES (1,1,2,3);
INSERT INTO t4 VALUES (1,1,5,7);
INSERT INTO t4 VALUES (1,1,10,8);
CREATE TABLE t5 (
  rid bigint(20) unsigned NOT NULL auto_increment,
  rl varchar(255) NOT NULL default '',
unknown's avatar
unknown committed
203
  PRIMARY KEY  (rid)
unknown's avatar
unknown committed
204 205 206 207 208 209
) engine=ndbcluster;
CREATE TABLE t6 (
  uid bigint(20) unsigned NOT NULL auto_increment,
  un varchar(250) NOT NULL default '',
  uc smallint(5) unsigned NOT NULL default '0',
  PRIMARY KEY  (uid),
unknown's avatar
unknown committed
210
  UNIQUE KEY nc (un,uc)
unknown's avatar
unknown committed
211 212 213 214 215 216 217 218
) engine=ndbcluster;
INSERT INTO t6 VALUES (1,'test',8);
INSERT INTO t6 VALUES (2,'test2',9);
INSERT INTO t6 VALUES (3,'tre',3);
CREATE TABLE t7 (
  mid bigint(20) unsigned NOT NULL PRIMARY KEY,
  uid bigint(20) unsigned NOT NULL default '0',
  gid bigint(20) unsigned NOT NULL,
219 220
  rid bigint(20) unsigned NOT NULL,
  cid bigint(20) unsigned NOT NULL,
unknown's avatar
unknown committed
221
  UNIQUE KEY m (uid,gid,rid,cid)
unknown's avatar
unknown committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235
) engine=ndbcluster;
INSERT INTO t7 VALUES(1, 1, 1, 1, 1);
INSERT INTO t7 VALUES(2, 2, 1, 1, 1);
INSERT INTO t7 VALUES(3, 3, 1, 1, 1);
INSERT INTO t7 VALUES(4, 4, 1, 1, 1);
INSERT INTO t7 VALUES(5, 5, 1, 1, 1);
INSERT INTO t7 VALUES(6, 1, 1, 1, 6);
INSERT INTO t7 VALUES(7, 2, 1, 1, 7);
INSERT INTO t7 VALUES(8, 3, 1, 1, 8);
INSERT INTO t7 VALUES(9, 4, 1, 1, 9);
INSERT INTO t7 VALUES(10, 5, 1, 1, 10);

select * from t1 where cv = 'dummy';
select * from t1 where cv = 'test';
236
select * from t2 where cap = 'another dummy';
unknown's avatar
unknown committed
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
select * from t4 where uid = 1 and gid=1 and rid=2 and cid=4;
select * from t4 where uid = 1 and gid=1 and rid=1 and cid=4;
select * from t4 where uid = 1 order by cid;
select * from t4 where rid = 2 order by cid;
select * from t6 where un='test' and uc=8;
select * from t6 where un='test' and uc=7;
select * from t6 where un='test';
select * from t7 where mid = 8; 
select * from t7 where uid = 8; 
select * from t7 where uid = 1 order by mid; 
select * from t7 where uid = 4 order by mid; 
select * from t7 where gid = 4; 
select * from t7 where gid = 1 order by mid; 
select * from t7 where cid = 4; 
select * from t7 where cid = 8; 

#
# insert more records into t4
#
let $1=100;
disable_query_log;
while ($1)
{
 eval insert into t4 values(1, $1, 5, 12);
 eval insert into t4 values($1, 3, 9, 11);
 dec $1;
}
enable_query_log;

select * from t4 where uid = 1 and gid=1 and rid=2 and cid=4;
select * from t4 where uid = 1 and gid=1 and rid=1 and cid=4;
select * from t4 where uid = 1 order by gid,cid;
select * from t4 where uid = 1 order by gid,cid;
select * from t4 where rid = 2 order by cid;


drop table t1,t2,t3,t4,t5,t6,t7;

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
# test null in indexes
CREATE TABLE t1 (   
   a int unsigned NOT NULL PRIMARY KEY,
   b int unsigned,   
   c int unsigned, 
   UNIQUE bc(b,c) ) engine = ndb;
 
insert into t1 values(1,1,1),(2,NULL,2),(3,NULL,NULL),(4,4,NULL);
select * from t1 where b=1 and c=1;
select * from t1 where b is null and c is null;
select * from t1 where b is null and c = 2;
select * from t1 where b = 4 and c is null;
create table t8 as 
select * from t1 where (b = 1 and c = 1)
                    or (b is null and c is null) 
                    or (b is null and c = 2)
                    or (b = 4 and c is null);
select * from t8 order by a;
select * from t1 order by a;
drop table t1, t8;
unknown's avatar
unknown committed
295

296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
###############################
# Bug 8101
#
# Unique index not specified in the same order as in table
#

create table t1(
        id integer not null auto_increment,
        month integer not null,
        year integer not null,
        code varchar( 2) not null,
        primary key ( id),
	unique idx_t1( month, code, year)
) engine=ndb;

INSERT INTO t1 (month, year, code) VALUES (4,2004,'12');
INSERT INTO t1 (month, year, code) VALUES (5,2004,'12');

select * from t1 where code = '12' and month = 4 and year = 2004 ;

drop table t1;
317

unknown's avatar
unknown committed
318 319 320 321 322 323
# bug#15918 Unique Key Limit in NDB Engine

create table t1 (a int primary key, b varchar(1000) not null, unique key (b))
engine=ndb charset=utf8;

insert into t1 values (1, repeat(_utf8 0xe288ab6474, 200));
324
--error 1169
unknown's avatar
unknown committed
325 326 327 328 329 330 331
insert into t1 values (2, repeat(_utf8 0xe288ab6474, 200));
select a, sha1(b) from t1;

# perl -e 'print pack("H2000","e288ab6474"x200)' | sha1sum

drop table t1;

332
# End of 4.1 tests