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. The
12 # focus of this file is testing CHECK constraints
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
17 set ::testprefix check
19 # Only run these tests if the build includes support for CHECK constraints
28 x INTEGER CHECK( x<5 ),
35 INSERT INTO t1 VALUES(3,4);
41 INSERT INTO t1 VALUES(6,7);
43 } {1 {CHECK constraint failed: x<5}}
51 INSERT INTO t1 VALUES(4,3);
53 } {1 {CHECK constraint failed: y>x}}
61 INSERT INTO t1 VALUES(NULL,6);
71 INSERT INTO t1 VALUES(2,NULL);
81 DELETE FROM t1 WHERE x IS NULL OR x!=3;
82 UPDATE t1 SET x=2 WHERE x==3;
88 UPDATE t1 SET x=7 WHERE x==2
90 } {1 {CHECK constraint failed: x<5}}
98 UPDATE t1 SET x=5 WHERE x==2
100 } {1 {CHECK constraint failed: x<5}}
108 UPDATE t1 SET x=4, y=11 WHERE x==2
119 PRAGMA writable_schema = 1;
121 x INTEGER CONSTRAINT one CHECK( typeof(coalesce(x,0))=="integer" ),
122 y REAL CONSTRAINT two CHECK( typeof(coalesce(y,0.1))=='real' ),
123 z TEXT CONSTRAINT three CHECK( typeof(coalesce(z,''))=='text' )
126 x INTEGER CONSTRAINT one CHECK( typeof(coalesce(x,0))=="integer" ),
127 y NUMERIC CONSTRAINT two CHECK( typeof(coalesce(y,0.1))=='real' ),
128 z TEXT CONSTRAINT three CHECK( typeof(coalesce(z,''))=='text' )
130 PRAGMA writable_schema = 0;
135 INSERT INTO t2 VALUES(1,2.2,'three');
143 INSERT INTO t2 VALUES(NULL, NULL, NULL);
146 } {1 2.2 three {} {} {}}
149 INSERT INTO t2 VALUES(1.1, NULL, NULL);
151 } {1 {CHECK constraint failed: one}}
153 # The 5 gets automatically promoted to 5.0 because the column type is REAL
155 INSERT INTO t2 VALUES(NULL, 5, NULL);
159 # This time the column type is NUMERIC, so not automatic promption to REAL
160 # occurs and the constraint fails.
162 INSERT INTO t2n VALUES(NULL, 5, NULL);
164 } {1 {CHECK constraint failed: two}}
167 INSERT INTO t2 VALUES(NULL, NULL, 3.14159);
171 # Undocumented behavior: The CONSTRAINT name clause can follow a constraint.
172 # Such a clause is ignored. But the parser must accept it for backwards
178 x INTEGER CHECK( typeof(coalesce(x,0))=='integer' ) CONSTRAINT one,
179 y TEXT PRIMARY KEY constraint two,
181 UNIQUE(x,z) constraint three
187 INSERT INTO t2b VALUES('xyzzy','hi',5);
189 } {1 {CHECK constraint failed: typeof(coalesce(x,0))=='integer'}}
193 x INTEGER CONSTRAINT x_one CONSTRAINT x_two
194 CHECK( typeof(coalesce(x,0))=='integer' )
195 CONSTRAINT x_two CONSTRAINT x_three,
196 y INTEGER, z INTEGER,
197 CONSTRAINT u_one UNIQUE(x,y,z) CONSTRAINT u_two
203 INSERT INTO t2c VALUES('xyzzy',7,8);
205 } {1 {CHECK constraint failed: x_two}}
206 do_test check-2.cleanup {
208 DROP TABLE IF EXISTS t2b;
209 DROP TABLE IF EXISTS t2c;
210 DROP TABLE IF EXISTS t2n;
219 CHECK( x<(SELECT min(x) FROM t1) )
222 } {1 {subqueries prohibited in CHECK constraints}}
227 SELECT name FROM sqlite_master ORDER BY name
237 } {1 {no such column: q}}
240 SELECT name FROM sqlite_master ORDER BY name
250 } {1 {no such column: t2.x}}
253 SELECT name FROM sqlite_master ORDER BY name
266 INSERT INTO t3 VALUES(1,2,3);
272 INSERT INTO t3 VALUES(111,222,333);
274 } {1 {CHECK constraint failed: t3.x<25}}
278 CREATE TABLE t4(x, y,
282 OR x/y BETWEEN 5 AND 8
290 INSERT INTO t4 VALUES(1,10);
296 UPDATE t4 SET x=4, y=3;
302 UPDATE t4 SET x=12, y=2;
308 UPDATE t4 SET x=12, y=-22;
314 UPDATE t4 SET x=0, y=1;
316 } {1 {CHECK constraint failed: x+y==11
318 OR x/y BETWEEN 5 AND 8
327 PRAGMA ignore_check_constraints=ON;
328 UPDATE t4 SET x=0, y=1;
330 PRAGMA integrity_check;
333 do_execsql_test check-4.8.1 {
334 PRAGMA ignore_check_constraints=OFF;
335 PRAGMA integrity_check;
336 } {{CHECK constraint failed in t4}}
339 UPDATE t4 SET x=0, y=2;
341 } {1 {CHECK constraint failed: x+y==11
343 OR x/y BETWEEN 5 AND 8
355 CREATE TABLE t5(x, y,
359 } {1 {parameters prohibited in CHECK constraints}}
362 CREATE TABLE t5(x, y,
366 } {1 {parameters prohibited in CHECK constraints}}
371 execsql {SELECT * FROM t1}
375 UPDATE OR IGNORE t1 SET x=5;
381 INSERT OR IGNORE INTO t1 VALUES(5,4.0);
387 INSERT OR IGNORE INTO t1 VALUES(2,20.0);
393 UPDATE OR FAIL t1 SET x=7-x, y=y+1;
395 } {1 {CHECK constraint failed: x<5}}
404 INSERT INTO t1 VALUES(1,30.0);
405 INSERT OR ROLLBACK INTO t1 VALUES(8,40.0);
407 } {1 {CHECK constraint failed: x<5}}
412 } {1 {cannot commit - no transaction is active}}
420 execsql {SELECT * FROM t1}
424 REPLACE INTO t1 VALUES(6,7);
426 } {1 {CHECK constraint failed: x<5}}
428 execsql {SELECT * FROM t1}
432 INSERT OR IGNORE INTO t1 VALUES(6,7);
436 execsql {SELECT * FROM t1}
442 #--------------------------------------------------------------------------
443 # If a connection opens a database that contains a CHECK constraint that
444 # uses an unknown UDF, the schema should not be considered malformed.
445 # Attempting to modify the table should fail (since the CHECK constraint
449 proc myfunc {x} {expr $x < 10}
450 db func myfunc -deterministic myfunc
452 do_execsql_test 7.1 { CREATE TABLE t6(a CHECK (myfunc(a))) }
453 do_execsql_test 7.2 { INSERT INTO t6 VALUES(9) }
454 do_catchsql_test 7.3 { INSERT INTO t6 VALUES(11) } \
455 {1 {CHECK constraint failed: myfunc(a)}}
459 execsql { SELECT * FROM t6 } db2
463 catchsql { INSERT INTO t6 VALUES(8) } db2
464 } {1 {unknown function: myfunc()}}
467 catchsql { CREATE TABLE t7(a CHECK (myfunc(a))) } db2
468 } {1 {no such function: myfunc}}
471 db2 func myfunc myfunc
472 execsql { INSERT INTO t6 VALUES(8) } db2
476 db2 func myfunc myfunc
477 catchsql { INSERT INTO t6 VALUES(12) } db2
478 } {1 {CHECK constraint failed: myfunc(a)}}
480 # 2013-08-02: Silently ignore database name qualifiers in CHECK constraints.
482 do_execsql_test 8.1 {
483 CREATE TABLE t810(a, CHECK( main.t810.a>0 ));
484 CREATE TABLE t811(b, CHECK( xyzzy.t811.b BETWEEN 5 AND 10 ));
487 # Make sure check constraints involving the ROWID are not ignored
489 do_execsql_test 9.1 {
491 a INTEGER PRIMARY KEY,
492 b INTEGER NOT NULL CONSTRAINT 'b-check' CHECK( b>a ),
493 c INTEGER NOT NULL CONSTRAINT 'c-check' CHECK( c>rowid*2 ),
494 d INTEGER NOT NULL CONSTRAINT 'd-check' CHECK( d BETWEEN b AND c )
496 INSERT INTO t1(a,b,c,d) VALUES(1,2,4,3),(2,4,6,5),(3,10,30,20);
498 do_catchsql_test 9.2 {
499 UPDATE t1 SET b=0 WHERE a=1;
500 } {1 {CHECK constraint failed: b-check}}
501 do_catchsql_test 9.3 {
502 UPDATE t1 SET c=a*2 WHERE a=1;
503 } {1 {CHECK constraint failed: c-check}}
505 # Integrity check on a VIEW with columns.
511 do_execsql_test 10.1 {
513 CREATE VIEW v1(y) AS SELECT x FROM t1;
514 PRAGMA integrity_check;
517 #-------------------------------------------------------------------------
519 do_execsql_test 11.0 {
520 CREATE TABLE t1 (Col0 CHECK(1 COLLATE BINARY BETWEEN 1 AND 1) ) ;
522 do_execsql_test 11.1 {
523 INSERT INTO t1 VALUES (NULL);
525 do_execsql_test 11.2 {
526 INSERT INTO t1 VALUES (NULL);
529 do_execsql_test 11.3 {
530 CREATE TABLE t2(b, a CHECK(
531 CASE 'abc' COLLATE nocase WHEN a THEN 1 ELSE 0 END)
534 do_execsql_test 11.4 {
535 INSERT INTO t2(a) VALUES('abc');
537 do_execsql_test 11.5 {
538 INSERT INTO t2(b, a) VALUES(1, 'abc'||'');
540 do_execsql_test 11.6 {
541 INSERT INTO t2(b, a) VALUES(2, 'abc');
544 # 2019-12-24 ticket b383b90278186263
547 do_execsql_test 12.10 {
548 CREATE TABLE t1(a TEXT, CHECK(a=+a));
549 INSERT INTO t1(a) VALUES(NULL),('xyz'),(5),(x'303132'),(4.75);
550 SELECT quote(a) FROM t1 ORDER BY rowid;
551 } {NULL 'xyz' '5' X'303132' '4.75'}
552 do_execsql_test 12.20 {
554 CREATE TABLE t1(a TEXT, CHECK(a<>+a));
555 INSERT INTO t1(a) VALUES(NULL);
557 do_catchsql_test 12.21 {
558 INSERT INTO t1(a) VALUES('xyz');
559 } {1 {CHECK constraint failed: a<>+a}}
560 do_catchsql_test 12.22 {
561 INSERT INTO t1(a) VALUES(123);
562 } {1 {CHECK constraint failed: a<>+a}}
563 do_execsql_test 12.30 {
565 CREATE TABLE t1(a TEXT, CHECK(NOT(a=+a)));
566 INSERT INTO t1(a) VALUES(NULL);
568 do_catchsql_test 12.31 {
569 INSERT INTO t1(a) VALUES('xyz');
570 } {1 {CHECK constraint failed: NOT(a=+a)}}
571 do_catchsql_test 12.32 {
572 INSERT INTO t1(a) VALUES(123);
573 } {1 {CHECK constraint failed: NOT(a=+a)}}
574 do_execsql_test 12.40 {
576 CREATE TABLE t1(a TEXT, CHECK(NOT(a<>+a)));
577 INSERT INTO t1(a) VALUES(NULL),('xyz'),(5),(x'303132'),(4.75);
578 SELECT quote(a) FROM t1 ORDER BY rowid;
579 } {NULL 'xyz' '5' X'303132' '4.75'}
580 do_execsql_test 12.50 {
582 CREATE TABLE t1(a TEXT, CHECK(a BETWEEN 0 AND +a));
583 INSERT INTO t1(a) VALUES(NULL),('xyz'),(5),(x'303132'),(4.75);
584 SELECT quote(a) FROM t1 ORDER BY rowid;
585 } {NULL 'xyz' '5' X'303132' '4.75'}
586 do_execsql_test 12.60 {
588 CREATE TABLE t1(a TEXT, CHECK(a NOT BETWEEN 0 AND +a));
589 INSERT INTO t1(a) VALUES(NULL);
590 SELECT quote(a) FROM t1 ORDER BY rowid;
592 do_catchsql_test 12.61 {
593 INSERT INTO t1(a) VALUES(456);
594 } {1 {CHECK constraint failed: a NOT BETWEEN 0 AND +a}}
595 do_execsql_test 12.70 {
597 CREATE TABLE t1(a TEXT, CHECK(a BETWEEN +a AND 999999));
598 INSERT INTO t1(a) VALUES(NULL),(5);
599 SELECT quote(a) FROM t1 ORDER BY rowid;
601 do_execsql_test 12.80 {
603 CREATE TABLE t1(a TEXT, CHECK(a NOT BETWEEN +a AND 999999));
604 INSERT INTO t1(a) VALUES(NULL);
605 SELECT quote(a) FROM t1 ORDER BY rowid;
607 do_catchsql_test 12.81 {
608 INSERT INTO t1(a) VALUES(456);
609 } {1 {CHECK constraint failed: a NOT BETWEEN +a AND 999999}}