MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
null_key.inc
1 #
2 # Check null keys
3 
4 --disable_warnings
5 drop table if exists t1,t2;
6 --enable_warnings
7 
8 create table t1 (a int, b int not null,unique key (a,b),index(b)) engine=myisam;
9 insert ignore into t1 values (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(null,7),(9,9),(8,8),(7,7),(null,9),(null,9),(6,6);
10 explain select * from t1 where a is null;
11 explain select * from t1 where a is null and b = 2;
12 explain select * from t1 where a is null and b = 7;
13 explain select * from t1 where a=2 and b = 2;
14 explain select * from t1 where a<=>b limit 2;
15 explain select * from t1 where (a is null or a > 0 and a < 3) and b < 5 limit 3;
16 explain select * from t1 where (a is null or a = 7) and b=7;
17 explain select * from t1 where (a is null or a = 7) and b=7 order by a;
18 explain select * from t1 where (a is null and b>a) or a is null and b=7 limit 2;
19 explain select * from t1 where a is null and b=9 or a is null and b=7 limit 3;
20 explain select * from t1 where a > 1 and a < 3 limit 1;
21 explain select * from t1 where a > 8 and a < 9;
22 select * from t1 where a is null;
23 select * from t1 where a is null and b = 7;
24 select * from t1 where a<=>b limit 2;
25 select * from t1 where (a is null or a > 0 and a < 3) and b < 5 limit 3;
26 select * from t1 where (a is null or a > 0 and a < 3) and b > 7 limit 3;
27 select * from t1 where (a is null or a = 7) and b=7;
28 select * from t1 where a is null and b=9 or a is null and b=7 limit 3;
29 select * from t1 where a > 1 and a < 3 limit 1;
30 select * from t1 where a > 8 and a < 9;
31 create table t2 like t1;
32 insert into t2 select * from t1;
33 alter table t1 modify b blob not null, add c int not null, drop key a, add unique key (a,b(20),c), drop key b, add key (b(10));
34 explain select * from t1 where a is null and b = 2;
35 explain select * from t1 where a is null and b = 2 and c=0;
36 explain select * from t1 where a is null and b = 7 and c=0;
37 explain select * from t1 where a=2 and b = 2;
38 explain select * from t1 where a<=>b limit 2;
39 explain select * from t1 where (a is null or a > 0 and a < 3) and b < 5 and c=0 limit 3;
40 explain select * from t1 where (a is null or a = 7) and b=7 and c=0;
41 explain select * from t1 where (a is null and b>a) or a is null and b=7 limit 2;
42 explain select * from t1 where a is null and b=9 or a is null and b=7 limit 3;
43 explain select * from t1 where a > 1 and a < 3 limit 1;
44 explain select * from t1 where a is null and b=7 or a > 1 and a < 3 limit 1;
45 explain select * from t1 where a > 8 and a < 9;
46 explain select * from t1 where b like "6%";
47 select * from t1 where a is null;
48 select * from t1 where a is null and b = 7 and c=0;
49 select * from t1 where a<=>b limit 2;
50 select * from t1 where (a is null or a > 0 and a < 3) and b < 5 limit 3;
51 select * from t1 where (a is null or a > 0 and a < 3) and b > 7 limit 3;
52 select * from t1 where (a is null or a = 7) and b=7 and c=0;
53 select * from t1 where a is null and b=9 or a is null and b=7 limit 3;
54 select * from t1 where b like "6%";
55 
56 #
57 # Test ref_or_null optimization
58 #
59 drop table t1;
60 rename table t2 to t1;
61 alter table t1 modify b int null;
62 insert into t1 values (7,null), (8,null), (8,7);
63 explain select * from t1 where a = 7 and (b=7 or b is null);
64 select * from t1 where a = 7 and (b=7 or b is null);
65 explain select * from t1 where (a = 7 or a is null) and (b=7 or b is null);
66 select * from t1 where (a = 7 or a is null) and (b=7 or b is null);
67 explain select * from t1 where (a = 7 or a is null) and (a = 7 or a is null);
68 select * from t1 where (a = 7 or a is null) and (a = 7 or a is null);
69 create table t2 (a int);
70 insert into t2 values (7),(8);
71 explain select * from t2 straight_join t1 where t1.a=t2.a and b is null;
72 drop index b on t1;
73 explain select * from t2,t1 where t1.a=t2.a and b is null;
74 select * from t2,t1 where t1.a=t2.a and b is null;
75 explain select * from t2,t1 where t1.a=t2.a and (b= 7 or b is null);
76 select * from t2,t1 where t1.a=t2.a and (b= 7 or b is null);
77 explain select * from t2,t1 where (t1.a=t2.a or t1.a is null) and b= 7;
78 select * from t2,t1 where (t1.a=t2.a or t1.a is null) and b= 7;
79 explain select * from t2,t1 where (t1.a=t2.a or t1.a is null) and (b= 7 or b is null);
80 select * from t2,t1 where (t1.a=t2.a or t1.a is null) and (b= 7 or b is null);
81 insert into t2 values (null),(6);
82 delete from t1 where a=8;
83 explain select * from t2,t1 where t1.a=t2.a or t1.a is null;
84 explain select * from t2,t1 where t1.a<=>t2.a or (t1.a is null and t1.b <> 9);
85 select * from t2,t1 where t1.a<=>t2.a or (t1.a is null and t1.b <> 9);
86 drop table t1,t2;
87 
88 #
89 # The following failed for Matt Loschert
90 #
91 
92 CREATE TABLE t1 (
93  id int(10) unsigned NOT NULL auto_increment,
94  uniq_id int(10) unsigned default NULL,
95  PRIMARY KEY (id),
96  UNIQUE KEY idx1 (uniq_id)
97 ) ENGINE=MyISAM;
98 
99 CREATE TABLE t2 (
100  id int(10) unsigned NOT NULL auto_increment,
101  uniq_id int(10) unsigned default NULL,
102  PRIMARY KEY (id)
103 ) ENGINE=MyISAM;
104 
105 INSERT INTO t1 VALUES (1,NULL),(2,NULL),(3,1),(4,2),(5,NULL),(6,NULL),(7,3),(8,4),(9,NULL),(10,NULL);
106 INSERT INTO t2 VALUES (1,NULL),(2,NULL),(3,1),(4,2),(5,NULL),(6,NULL),(7,3),(8,4),(9,NULL),(10,NULL);
107 
108 #
109 # Check IS NULL optimization
110 #
111 explain select id from t1 where uniq_id is null;
112 explain select id from t1 where uniq_id =1;
113 #
114 # Check updates
115 #
116 UPDATE t1 SET id=id+100 where uniq_id is null;
117 UPDATE t2 SET id=id+100 where uniq_id is null;
118 select id from t1 where uniq_id is null;
119 select id from t2 where uniq_id is null;
120 #
121 # Delete all records from each table where the uniq_id field is null
122 #
123 DELETE FROM t1 WHERE uniq_id IS NULL;
124 DELETE FROM t2 WHERE uniq_id IS NULL;
125 #
126 # Select what is left -- notice the difference
127 #
128 SELECT * FROM t1 ORDER BY uniq_id, id;
129 SELECT * FROM t2 ORDER BY uniq_id, id;
130 DROP table t1,t2;
131 
132 #
133 # This crashed MySQL 3.23.47
134 #
135 
136 CREATE TABLE `t1` (
137  `order_id` char(32) NOT NULL default '',
138  `product_id` char(32) NOT NULL default '',
139  `product_type` int(11) NOT NULL default '0',
140  PRIMARY KEY (`order_id`,`product_id`,`product_type`)
141 ) ENGINE=MyISAM;
142 CREATE TABLE `t2` (
143  `order_id` char(32) NOT NULL default '',
144  `product_id` char(32) NOT NULL default '',
145  `product_type` int(11) NOT NULL default '0',
146  PRIMARY KEY (`order_id`,`product_id`,`product_type`)
147 ) ENGINE=MyISAM;
148 INSERT INTO t1 (order_id, product_id, product_type) VALUES
149 ('3d7ce39b5d4b3e3d22aaafe9b633de51',1206029, 3),
150 ('3d7ce39b5d4b3e3d22aaafe9b633de51',5880836, 3),
151 ('9d9aad7764b5b2c53004348ef8d34500',2315652, 3);
152 INSERT INTO t2 (order_id, product_id, product_type) VALUES
153 ('9d9aad7764b5b2c53004348ef8d34500',2315652, 3);
154 
155 select t1.* from t1
156 left join t2 using(order_id, product_id, product_type)
157 where t2.order_id=NULL;
158 select t1.* from t1
159 left join t2 using(order_id, product_id, product_type)
160 where t2.order_id is NULL;
161 drop table t1,t2;
162 
163 #
164 # The last select returned wrong results in 3.23.52
165 #
166 
167 create table t1 (id int);
168 insert into t1 values (null), (0);
169 create table t2 (id int);
170 insert into t2 values (null);
171 select * from t1, t2 where t1.id = t2.id;
172 alter table t1 add key id (id);
173 select * from t1, t2 where t1.id = t2.id;
174 drop table t1,t2;
175 
176 #
177 # Check bug when doing <=> NULL on an indexed null field
178 #
179 
180 create table t1 (
181  id integer,
182  id2 integer not null,
183  index (id),
184  index (id2)
185 );
186 insert into t1 values(null,null),(1,1);
187 select * from t1;
188 select * from t1 where id <=> null;
189 select * from t1 where id <=> null or id > 0;
190 select * from t1 where id is null or id > 0;
191 select * from t1 where id2 <=> null or id2 > 0;
192 select * from t1 where id2 is null or id2 > 0;
193 delete from t1 where id <=> NULL;
194 select * from t1;
195 drop table t1;
196 
197 #
198 # Test for bug #12144: optimizations for key access with null keys
199 # used for outer joins
200 #
201 
202 CREATE TABLE t1 (a int);
203 CREATE TABLE t2 (a int, b int, INDEX idx(a));
204 CREATE TABLE t3 (b int, INDEX idx(b));
205 CREATE TABLE t4 (b int, INDEX idx(b));
206 INSERT INTO t1 VALUES (1), (2), (3), (4);
207 INSERT INTO t2 VALUES (1, 1), (3, 1);
208 INSERT INTO t3 VALUES
209  (NULL), (NULL), (NULL), (NULL), (NULL),
210  (NULL), (NULL), (NULL), (NULL), (NULL);
211 INSERT INTO t4 SELECT * FROM t3;
212 INSERT INTO t3 SELECT * FROM t4;
213 INSERT INTO t4 SELECT * FROM t3;
214 INSERT INTO t3 SELECT * FROM t4;
215 INSERT INTO t4 SELECT * FROM t3;
216 INSERT INTO t3 SELECT * FROM t4;
217 INSERT INTO t4 SELECT * FROM t3;
218 INSERT INTO t3 SELECT * FROM t4;
219 INSERT INTO t4 SELECT * FROM t3;
220 INSERT INTO t3 SELECT * FROM t4;
221 INSERT INTO t4 SELECT * FROM t3;
222 INSERT INTO t3 SELECT * FROM t4;
223 INSERT INTO t4 SELECT * FROM t3;
224 INSERT INTO t3 SELECT * FROM t4;
225 INSERT INTO t4 SELECT * FROM t3;
226 INSERT INTO t3 SELECT * FROM t4;
227 INSERT INTO t3 VALUES (2), (3);
228 
229 ANALYZE table t1, t2, t3;
230 
231 SELECT COUNT(*) FROM t3;
232 
233 EXPLAIN SELECT SQL_CALC_FOUND_ROWS * FROM t1 LEFT JOIN t2 ON t1.a=t2.a
234  LEFT JOIN t3 ON t2.b=t3.b;
235 FLUSH STATUS ;
236 SELECT SQL_CALC_FOUND_ROWS * FROM t1 LEFT JOIN t2 ON t1.a=t2.a
237  LEFT JOIN t3 ON t2.b=t3.b;
238 SELECT FOUND_ROWS();
239 SHOW STATUS LIKE "handler_read%";
240 
241 DROP TABLE t1,t2,t3,t4;
242 # End of 4.1 tests
243 
244 #
245 # BUG#34945 "ref_or_null queries that are null_rejecting and have a null value crash mysql"
246 #
247 CREATE TABLE t1 (
248  a int(11) default NULL,
249  b int(11) default NULL,
250  KEY a (a,b)
251 );
252 INSERT INTO t1 VALUES (0,10),(0,11),(0,12);
253 
254 CREATE TABLE t2 (
255  a int(11) default NULL,
256  b int(11) default NULL,
257  KEY a (a)
258 );
259 INSERT INTO t2 VALUES (3,NULL),(3,11),(3,12);
260 
261 SELECT * FROM t2 inner join t1 WHERE ( t1.a = 0 OR t1.a IS NULL) AND t2.a = 3 AND t2.b = t1.b;
262 
263 drop table t1, t2;
264 -- echo End of 5.0 tests
265 
266 --echo #
267 --echo # Bug#54608 Query using IN + OR + IS TRUE and IS NULL returns
268 --echo # NULL when should be empty
269 --echo #
270 
271 CREATE TABLE t1 (a INT, KEY (a));
272 INSERT INTO t1 VALUES (1), (2), (NULL);
273 explain SELECT a FROM t1 WHERE a IN (42) OR (a IS TRUE AND a IS NULL);
274 SELECT a FROM t1 WHERE a IN (42) OR (a IS TRUE AND a IS NULL);
275 explain SELECT a FROM t1 WHERE a IN (42) OR (a=NULL);
276 SELECT a FROM t1 WHERE a IN (42) OR (a=NULL);
277 drop table t1;