3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library.
13 # This file implements tests for the NOT NULL constraint.
15 # $Id: notnull.test,v 1.4 2006/01/17 09:35:02 danielk1977 Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
30 c NOT NULL ON CONFLICT REPLACE DEFAULT 6,
31 d NOT NULL ON CONFLICT IGNORE DEFAULT 7,
32 e NOT NULL ON CONFLICT ABORT DEFAULT 8
40 INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5);
41 SELECT * FROM t1 order by a;
47 INSERT INTO t1(b,c,d,e) VALUES(2,3,4,5);
48 SELECT * FROM t1 order by a;
50 } {1 {NOT NULL constraint failed: t1.a}}
51 verify_ex_errcode notnull-1.2b SQLITE_CONSTRAINT_NOTNULL
55 INSERT OR IGNORE INTO t1(b,c,d,e) VALUES(2,3,4,5);
56 SELECT * FROM t1 order by a;
62 INSERT OR REPLACE INTO t1(b,c,d,e) VALUES(2,3,4,5);
63 SELECT * FROM t1 order by a;
65 } {1 {NOT NULL constraint failed: t1.a}}
66 verify_ex_errcode notnull-1.4b SQLITE_CONSTRAINT_NOTNULL
70 INSERT OR ABORT INTO t1(b,c,d,e) VALUES(2,3,4,5);
71 SELECT * FROM t1 order by a;
73 } {1 {NOT NULL constraint failed: t1.a}}
74 verify_ex_errcode notnull-1.5b SQLITE_CONSTRAINT_NOTNULL
78 INSERT INTO t1(a,c,d,e) VALUES(1,3,4,5);
79 SELECT * FROM t1 order by a;
85 INSERT OR IGNORE INTO t1(a,c,d,e) VALUES(1,3,4,5);
86 SELECT * FROM t1 order by a;
92 INSERT OR REPLACE INTO t1(a,c,d,e) VALUES(1,3,4,5);
93 SELECT * FROM t1 order by a;
99 INSERT OR ABORT INTO t1(a,c,d,e) VALUES(1,3,4,5);
100 SELECT * FROM t1 order by a;
103 do_test notnull-1.10 {
106 INSERT INTO t1(a,b,c,d,e) VALUES(1,null,3,4,5);
107 SELECT * FROM t1 order by a;
109 } {1 {NOT NULL constraint failed: t1.b}}
110 verify_ex_errcode notnull-1.10b SQLITE_CONSTRAINT_NOTNULL
111 do_test notnull-1.11 {
114 INSERT OR IGNORE INTO t1(a,b,c,d,e) VALUES(1,null,3,4,5);
115 SELECT * FROM t1 order by a;
118 do_test notnull-1.12 {
121 INSERT OR REPLACE INTO t1(a,b,c,d,e) VALUES(1,null,3,4,5);
122 SELECT * FROM t1 order by a;
125 do_test notnull-1.13 {
128 INSERT INTO t1(a,b,c,d,e) VALUES(1,2,null,4,5);
129 SELECT * FROM t1 order by a;
132 do_test notnull-1.14 {
135 INSERT OR IGNORE INTO t1(a,b,c,d,e) VALUES(1,2,null,4,5);
136 SELECT * FROM t1 order by a;
139 do_test notnull-1.15 {
142 INSERT OR REPLACE INTO t1(a,b,c,d,e) VALUES(1,2,null,4,5);
143 SELECT * FROM t1 order by a;
146 do_test notnull-1.16 {
149 INSERT OR ABORT INTO t1(a,b,c,d,e) VALUES(1,2,null,4,5);
150 SELECT * FROM t1 order by a;
152 } {1 {NOT NULL constraint failed: t1.c}}
153 verify_ex_errcode notnull-1.16b SQLITE_CONSTRAINT_NOTNULL
154 do_test notnull-1.17 {
157 INSERT OR ABORT INTO t1(a,b,c,d,e) VALUES(1,2,3,null,5);
158 SELECT * FROM t1 order by a;
160 } {1 {NOT NULL constraint failed: t1.d}}
161 verify_ex_errcode notnull-1.17b SQLITE_CONSTRAINT_NOTNULL
162 do_test notnull-1.18 {
165 INSERT OR ABORT INTO t1(a,b,c,e) VALUES(1,2,3,5);
166 SELECT * FROM t1 order by a;
169 do_test notnull-1.19 {
172 INSERT INTO t1(a,b,c,d) VALUES(1,2,3,4);
173 SELECT * FROM t1 order by a;
176 do_test notnull-1.20 {
179 INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,null);
180 SELECT * FROM t1 order by a;
182 } {1 {NOT NULL constraint failed: t1.e}}
183 verify_ex_errcode notnull-1.20b SQLITE_CONSTRAINT_NOTNULL
184 do_test notnull-1.21 {
187 INSERT OR REPLACE INTO t1(e,d,c,b,a) VALUES(1,2,3,null,5);
188 SELECT * FROM t1 order by a;
192 do_test notnull-2.1 {
195 INSERT INTO t1 VALUES(1,2,3,4,5);
196 UPDATE t1 SET a=null;
197 SELECT * FROM t1 ORDER BY a;
199 } {1 {NOT NULL constraint failed: t1.a}}
200 verify_ex_errcode notnull-2.1b SQLITE_CONSTRAINT_NOTNULL
201 do_test notnull-2.2 {
204 INSERT INTO t1 VALUES(1,2,3,4,5);
205 UPDATE OR REPLACE t1 SET a=null;
206 SELECT * FROM t1 ORDER BY a;
208 } {1 {NOT NULL constraint failed: t1.a}}
209 verify_ex_errcode notnull-2.2b SQLITE_CONSTRAINT_NOTNULL
210 do_test notnull-2.3 {
213 INSERT INTO t1 VALUES(1,2,3,4,5);
214 UPDATE OR IGNORE t1 SET a=null;
215 SELECT * FROM t1 ORDER BY a;
218 do_test notnull-2.4 {
221 INSERT INTO t1 VALUES(1,2,3,4,5);
222 UPDATE OR ABORT t1 SET a=null;
223 SELECT * FROM t1 ORDER BY a;
225 } {1 {NOT NULL constraint failed: t1.a}}
226 verify_ex_errcode notnull-2.4b SQLITE_CONSTRAINT_NOTNULL
227 do_test notnull-2.5 {
230 INSERT INTO t1 VALUES(1,2,3,4,5);
231 UPDATE t1 SET b=null;
232 SELECT * FROM t1 ORDER BY a;
234 } {1 {NOT NULL constraint failed: t1.b}}
235 verify_ex_errcode notnull-2.6b SQLITE_CONSTRAINT_NOTNULL
236 do_test notnull-2.6 {
239 INSERT INTO t1 VALUES(1,2,3,4,5);
240 UPDATE OR REPLACE t1 SET b=null, d=e, e=d;
241 SELECT * FROM t1 ORDER BY a;
244 do_test notnull-2.7 {
247 INSERT INTO t1 VALUES(1,2,3,4,5);
248 UPDATE OR IGNORE t1 SET b=null, d=e, e=d;
249 SELECT * FROM t1 ORDER BY a;
252 do_test notnull-2.8 {
255 INSERT INTO t1 VALUES(1,2,3,4,5);
256 UPDATE t1 SET c=null, d=e, e=d;
257 SELECT * FROM t1 ORDER BY a;
260 do_test notnull-2.9 {
263 INSERT INTO t1 VALUES(1,2,3,4,5);
264 UPDATE t1 SET d=null, a=b, b=a;
265 SELECT * FROM t1 ORDER BY a;
268 do_test notnull-2.10 {
271 INSERT INTO t1 VALUES(1,2,3,4,5);
272 UPDATE t1 SET e=null, a=b, b=a;
273 SELECT * FROM t1 ORDER BY a;
275 } {1 {NOT NULL constraint failed: t1.e}}
276 verify_ex_errcode notnull-2.10b SQLITE_CONSTRAINT_NOTNULL
278 do_test notnull-3.0 {
280 CREATE INDEX t1a ON t1(a);
281 CREATE INDEX t1b ON t1(b);
282 CREATE INDEX t1c ON t1(c);
283 CREATE INDEX t1d ON t1(d);
284 CREATE INDEX t1e ON t1(e);
285 CREATE INDEX t1abc ON t1(a,b,c);
288 do_test notnull-3.1 {
291 INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5);
292 SELECT * FROM t1 order by a;
295 do_test notnull-3.2 {
298 INSERT INTO t1(b,c,d,e) VALUES(2,3,4,5);
299 SELECT * FROM t1 order by a;
301 } {1 {NOT NULL constraint failed: t1.a}}
302 verify_ex_errcode notnull-3.2b SQLITE_CONSTRAINT_NOTNULL
303 do_test notnull-3.3 {
306 INSERT OR IGNORE INTO t1(b,c,d,e) VALUES(2,3,4,5);
307 SELECT * FROM t1 order by a;
310 do_test notnull-3.4 {
313 INSERT OR REPLACE INTO t1(b,c,d,e) VALUES(2,3,4,5);
314 SELECT * FROM t1 order by a;
316 } {1 {NOT NULL constraint failed: t1.a}}
317 verify_ex_errcode notnull-3.4b SQLITE_CONSTRAINT_NOTNULL
318 do_test notnull-3.5 {
321 INSERT OR ABORT INTO t1(b,c,d,e) VALUES(2,3,4,5);
322 SELECT * FROM t1 order by a;
324 } {1 {NOT NULL constraint failed: t1.a}}
325 verify_ex_errcode notnull-3.5b SQLITE_CONSTRAINT_NOTNULL
326 do_test notnull-3.6 {
329 INSERT INTO t1(a,c,d,e) VALUES(1,3,4,5);
330 SELECT * FROM t1 order by a;
333 do_test notnull-3.7 {
336 INSERT OR IGNORE INTO t1(a,c,d,e) VALUES(1,3,4,5);
337 SELECT * FROM t1 order by a;
340 do_test notnull-3.8 {
343 INSERT OR REPLACE INTO t1(a,c,d,e) VALUES(1,3,4,5);
344 SELECT * FROM t1 order by a;
347 do_test notnull-3.9 {
350 INSERT OR ABORT INTO t1(a,c,d,e) VALUES(1,3,4,5);
351 SELECT * FROM t1 order by a;
354 do_test notnull-3.10 {
357 INSERT INTO t1(a,b,c,d,e) VALUES(1,null,3,4,5);
358 SELECT * FROM t1 order by a;
360 } {1 {NOT NULL constraint failed: t1.b}}
361 verify_ex_errcode notnull-3.10b SQLITE_CONSTRAINT_NOTNULL
362 do_test notnull-3.11 {
365 INSERT OR IGNORE INTO t1(a,b,c,d,e) VALUES(1,null,3,4,5);
366 SELECT * FROM t1 order by a;
369 do_test notnull-3.12 {
372 INSERT OR REPLACE INTO t1(a,b,c,d,e) VALUES(1,null,3,4,5);
373 SELECT * FROM t1 order by a;
376 do_test notnull-3.13 {
379 INSERT INTO t1(a,b,c,d,e) VALUES(1,2,null,4,5);
380 SELECT * FROM t1 order by a;
383 do_test notnull-3.14 {
386 INSERT OR IGNORE INTO t1(a,b,c,d,e) VALUES(1,2,null,4,5);
387 SELECT * FROM t1 order by a;
390 do_test notnull-3.15 {
393 INSERT OR REPLACE INTO t1(a,b,c,d,e) VALUES(1,2,null,4,5);
394 SELECT * FROM t1 order by a;
397 do_test notnull-3.16 {
400 INSERT OR ABORT INTO t1(a,b,c,d,e) VALUES(1,2,null,4,5);
401 SELECT * FROM t1 order by a;
403 } {1 {NOT NULL constraint failed: t1.c}}
404 verify_ex_errcode notnull-3.16b SQLITE_CONSTRAINT_NOTNULL
405 do_test notnull-3.17 {
408 INSERT OR ABORT INTO t1(a,b,c,d,e) VALUES(1,2,3,null,5);
409 SELECT * FROM t1 order by a;
411 } {1 {NOT NULL constraint failed: t1.d}}
412 verify_ex_errcode notnull-3.17b SQLITE_CONSTRAINT_NOTNULL
413 do_test notnull-3.18 {
416 INSERT OR ABORT INTO t1(a,b,c,e) VALUES(1,2,3,5);
417 SELECT * FROM t1 order by a;
420 do_test notnull-3.19 {
423 INSERT INTO t1(a,b,c,d) VALUES(1,2,3,4);
424 SELECT * FROM t1 order by a;
427 do_test notnull-3.20 {
430 INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,null);
431 SELECT * FROM t1 order by a;
433 } {1 {NOT NULL constraint failed: t1.e}}
434 verify_ex_errcode notnull-3.20b SQLITE_CONSTRAINT_NOTNULL
435 do_test notnull-3.21 {
438 INSERT OR REPLACE INTO t1(e,d,c,b,a) VALUES(1,2,3,null,5);
439 SELECT * FROM t1 order by a;
443 do_test notnull-4.1 {
446 INSERT INTO t1 VALUES(1,2,3,4,5);
447 UPDATE t1 SET a=null;
448 SELECT * FROM t1 ORDER BY a;
450 } {1 {NOT NULL constraint failed: t1.a}}
451 verify_ex_errcode notnull-4.1b SQLITE_CONSTRAINT_NOTNULL
452 do_test notnull-4.2 {
455 INSERT INTO t1 VALUES(1,2,3,4,5);
456 UPDATE OR REPLACE t1 SET a=null;
457 SELECT * FROM t1 ORDER BY a;
459 } {1 {NOT NULL constraint failed: t1.a}}
460 verify_ex_errcode notnull-4.2b SQLITE_CONSTRAINT_NOTNULL
461 do_test notnull-4.3 {
464 INSERT INTO t1 VALUES(1,2,3,4,5);
465 UPDATE OR IGNORE t1 SET a=null;
466 SELECT * FROM t1 ORDER BY a;
469 do_test notnull-4.4 {
472 INSERT INTO t1 VALUES(1,2,3,4,5);
473 UPDATE OR ABORT t1 SET a=null;
474 SELECT * FROM t1 ORDER BY a;
476 } {1 {NOT NULL constraint failed: t1.a}}
477 verify_ex_errcode notnull-4.4b SQLITE_CONSTRAINT_NOTNULL
478 do_test notnull-4.5 {
481 INSERT INTO t1 VALUES(1,2,3,4,5);
482 UPDATE t1 SET b=null;
483 SELECT * FROM t1 ORDER BY a;
485 } {1 {NOT NULL constraint failed: t1.b}}
486 verify_ex_errcode notnull-4.5b SQLITE_CONSTRAINT_NOTNULL
487 do_test notnull-4.6 {
490 INSERT INTO t1 VALUES(1,2,3,4,5);
491 UPDATE OR REPLACE t1 SET b=null, d=e, e=d;
492 SELECT * FROM t1 ORDER BY a;
495 do_test notnull-4.7 {
498 INSERT INTO t1 VALUES(1,2,3,4,5);
499 UPDATE OR IGNORE t1 SET b=null, d=e, e=d;
500 SELECT * FROM t1 ORDER BY a;
503 do_test notnull-4.8 {
506 INSERT INTO t1 VALUES(1,2,3,4,5);
507 UPDATE t1 SET c=null, d=e, e=d;
508 SELECT * FROM t1 ORDER BY a;
511 do_test notnull-4.9 {
514 INSERT INTO t1 VALUES(1,2,3,4,5);
515 UPDATE t1 SET d=null, a=b, b=a;
516 SELECT * FROM t1 ORDER BY a;
519 do_test notnull-4.10 {
522 INSERT INTO t1 VALUES(1,2,3,4,5);
523 UPDATE t1 SET e=null, a=b, b=a;
524 SELECT * FROM t1 ORDER BY a;
526 } {1 {NOT NULL constraint failed: t1.e}}
527 verify_ex_errcode notnull-4.10b SQLITE_CONSTRAINT_NOTNULL
529 # Test that bug 29ab7be99f is fixed.
531 do_test notnull-5.1 {
533 DROP TABLE IF EXISTS t1;
534 CREATE TABLE t1(a, b NOT NULL);
535 CREATE TABLE t2(c, d);
536 INSERT INTO t2 VALUES(3, 4);
537 INSERT INTO t2 VALUES(5, NULL);
540 do_test notnull-5.2 {
542 INSERT INTO t1 VALUES(1, 2);
543 INSERT INTO t1 SELECT * FROM t2;
545 } {1 {NOT NULL constraint failed: t1.b}}
546 verify_ex_errcode notnull-5.2b SQLITE_CONSTRAINT_NOTNULL
547 do_test notnull-5.3 {
548 execsql { SELECT * FROM t1 }
550 do_test notnull-5.4 {
554 INSERT INTO t1 VALUES(1, 2);
555 INSERT INTO t1 SELECT * FROM t2;
558 } {1 {NOT NULL constraint failed: t1.b}}
559 verify_ex_errcode notnull-5.4b SQLITE_CONSTRAINT_NOTNULL
560 do_test notnull-5.5 {
561 execsql { SELECT * FROM t1 }
564 #-------------------------------------------------------------------------
565 # Check that UNIQUE NOT NULL indexes are always recognized as such.
567 proc uses_op_next {sql} {
568 db eval "EXPLAIN $sql" a {
569 if {$a(opcode)=="Next"} { return 1 }
574 proc do_uses_op_next_test {tn sql res} {
575 uplevel [list do_test $tn [list uses_op_next $sql] $res]
579 do_execsql_test notnull-6.0 {
580 CREATE TABLE t1(a UNIQUE);
581 CREATE TABLE t2(a NOT NULL UNIQUE);
582 CREATE TABLE t3(a UNIQUE NOT NULL);
583 CREATE TABLE t4(a NOT NULL);
584 CREATE UNIQUE INDEX t4a ON t4(a);
586 CREATE TABLE t5(a PRIMARY KEY);
587 CREATE TABLE t6(a PRIMARY KEY NOT NULL);
588 CREATE TABLE t7(a NOT NULL PRIMARY KEY);
589 CREATE TABLE t8(a PRIMARY KEY) WITHOUT ROWID;
591 CREATE TABLE t9(a PRIMARY KEY UNIQUE NOT NULL);
592 CREATE TABLE t10(a UNIQUE PRIMARY KEY NOT NULL);
595 do_uses_op_next_test notnull-6.1 "SELECT * FROM t1 WHERE a IS ?" 1
596 do_uses_op_next_test notnull-6.2 "SELECT * FROM t2 WHERE a IS ?" 0
597 do_uses_op_next_test notnull-6.3 "SELECT * FROM t3 WHERE a IS ?" 0
598 do_uses_op_next_test notnull-6.4 "SELECT * FROM t4 WHERE a IS ?" 0
600 do_uses_op_next_test notnull-6.5 "SELECT * FROM t5 WHERE a IS ?" 1
601 do_uses_op_next_test notnull-6.6 "SELECT * FROM t6 WHERE a IS ?" 0
602 do_uses_op_next_test notnull-6.7 "SELECT * FROM t7 WHERE a IS ?" 0
603 do_uses_op_next_test notnull-6.8 "SELECT * FROM t8 WHERE a IS ?" 0
605 do_uses_op_next_test notnull-6.9 "SELECT * FROM t8 WHERE a IS ?" 0
606 do_uses_op_next_test notnull-6.10 "SELECT * FROM t8 WHERE a IS ?" 0