sl@0: # 2001 September 27 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # This file implements regression tests for SQLite library. The sl@0: # focus of this file is testing the CREATE UNIQUE INDEX statement, sl@0: # and primary keys, and the UNIQUE constraint on table columns sl@0: # sl@0: # $Id: unique.test,v 1.8 2005/06/24 03:53:06 drh Exp $ sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: # Try to create a table with two primary keys. sl@0: # (This is allowed in SQLite even that it is not valid SQL) sl@0: # sl@0: do_test unique-1.1 { sl@0: catchsql { sl@0: CREATE TABLE t1( sl@0: a int PRIMARY KEY, sl@0: b int PRIMARY KEY, sl@0: c text sl@0: ); sl@0: } sl@0: } {1 {table "t1" has more than one primary key}} sl@0: do_test unique-1.1b { sl@0: catchsql { sl@0: CREATE TABLE t1( sl@0: a int PRIMARY KEY, sl@0: b int UNIQUE, sl@0: c text sl@0: ); sl@0: } sl@0: } {0 {}} sl@0: do_test unique-1.2 { sl@0: catchsql { sl@0: INSERT INTO t1(a,b,c) VALUES(1,2,3) sl@0: } sl@0: } {0 {}} sl@0: do_test unique-1.3 { sl@0: catchsql { sl@0: INSERT INTO t1(a,b,c) VALUES(1,3,4) sl@0: } sl@0: } {1 {column a is not unique}} sl@0: do_test unique-1.4 { sl@0: execsql { sl@0: SELECT * FROM t1 ORDER BY a; sl@0: } sl@0: } {1 2 3} sl@0: do_test unique-1.5 { sl@0: catchsql { sl@0: INSERT INTO t1(a,b,c) VALUES(3,2,4) sl@0: } sl@0: } {1 {column b is not unique}} sl@0: do_test unique-1.6 { sl@0: execsql { sl@0: SELECT * FROM t1 ORDER BY a; sl@0: } sl@0: } {1 2 3} sl@0: do_test unique-1.7 { sl@0: catchsql { sl@0: INSERT INTO t1(a,b,c) VALUES(3,4,5) sl@0: } sl@0: } {0 {}} sl@0: do_test unique-1.8 { sl@0: execsql { sl@0: SELECT * FROM t1 ORDER BY a; sl@0: } sl@0: } {1 2 3 3 4 5} sl@0: integrity_check unique-1.9 sl@0: sl@0: do_test unique-2.0 { sl@0: execsql { sl@0: DROP TABLE t1; sl@0: CREATE TABLE t2(a int, b int); sl@0: INSERT INTO t2(a,b) VALUES(1,2); sl@0: INSERT INTO t2(a,b) VALUES(3,4); sl@0: SELECT * FROM t2 ORDER BY a; sl@0: } sl@0: } {1 2 3 4} sl@0: do_test unique-2.1 { sl@0: catchsql { sl@0: CREATE UNIQUE INDEX i2 ON t2(a) sl@0: } sl@0: } {0 {}} sl@0: do_test unique-2.2 { sl@0: catchsql { sl@0: SELECT * FROM t2 ORDER BY a sl@0: } sl@0: } {0 {1 2 3 4}} sl@0: do_test unique-2.3 { sl@0: catchsql { sl@0: INSERT INTO t2 VALUES(1,5); sl@0: } sl@0: } {1 {column a is not unique}} sl@0: do_test unique-2.4 { sl@0: catchsql { sl@0: SELECT * FROM t2 ORDER BY a sl@0: } sl@0: } {0 {1 2 3 4}} sl@0: do_test unique-2.5 { sl@0: catchsql { sl@0: DROP INDEX i2; sl@0: SELECT * FROM t2 ORDER BY a; sl@0: } sl@0: } {0 {1 2 3 4}} sl@0: do_test unique-2.6 { sl@0: catchsql { sl@0: INSERT INTO t2 VALUES(1,5) sl@0: } sl@0: } {0 {}} sl@0: do_test unique-2.7 { sl@0: catchsql { sl@0: SELECT * FROM t2 ORDER BY a, b; sl@0: } sl@0: } {0 {1 2 1 5 3 4}} sl@0: do_test unique-2.8 { sl@0: catchsql { sl@0: CREATE UNIQUE INDEX i2 ON t2(a); sl@0: } sl@0: } {1 {indexed columns are not unique}} sl@0: do_test unique-2.9 { sl@0: catchsql { sl@0: CREATE INDEX i2 ON t2(a); sl@0: } sl@0: } {0 {}} sl@0: integrity_check unique-2.10 sl@0: sl@0: # Test the UNIQUE keyword as used on two or more fields. sl@0: # sl@0: do_test unique-3.1 { sl@0: catchsql { sl@0: CREATE TABLE t3( sl@0: a int, sl@0: b int, sl@0: c int, sl@0: d int, sl@0: unique(a,c,d) sl@0: ); sl@0: } sl@0: } {0 {}} sl@0: do_test unique-3.2 { sl@0: catchsql { sl@0: INSERT INTO t3(a,b,c,d) VALUES(1,2,3,4); sl@0: SELECT * FROM t3 ORDER BY a,b,c,d; sl@0: } sl@0: } {0 {1 2 3 4}} sl@0: do_test unique-3.3 { sl@0: catchsql { sl@0: INSERT INTO t3(a,b,c,d) VALUES(1,2,3,5); sl@0: SELECT * FROM t3 ORDER BY a,b,c,d; sl@0: } sl@0: } {0 {1 2 3 4 1 2 3 5}} sl@0: do_test unique-3.4 { sl@0: catchsql { sl@0: INSERT INTO t3(a,b,c,d) VALUES(1,4,3,5); sl@0: SELECT * FROM t3 ORDER BY a,b,c,d; sl@0: } sl@0: } {1 {columns a, c, d are not unique}} sl@0: integrity_check unique-3.5 sl@0: sl@0: # Make sure NULLs are distinct as far as the UNIQUE tests are sl@0: # concerned. sl@0: # sl@0: do_test unique-4.1 { sl@0: execsql { sl@0: CREATE TABLE t4(a UNIQUE, b, c, UNIQUE(b,c)); sl@0: INSERT INTO t4 VALUES(1,2,3); sl@0: INSERT INTO t4 VALUES(NULL, 2, NULL); sl@0: SELECT * FROM t4; sl@0: } sl@0: } {1 2 3 {} 2 {}} sl@0: do_test unique-4.2 { sl@0: catchsql { sl@0: INSERT INTO t4 VALUES(NULL, 3, 4); sl@0: } sl@0: } {0 {}} sl@0: do_test unique-4.3 { sl@0: execsql { sl@0: SELECT * FROM t4 sl@0: } sl@0: } {1 2 3 {} 2 {} {} 3 4} sl@0: do_test unique-4.4 { sl@0: catchsql { sl@0: INSERT INTO t4 VALUES(2, 2, NULL); sl@0: } sl@0: } {0 {}} sl@0: do_test unique-4.5 { sl@0: execsql { sl@0: SELECT * FROM t4 sl@0: } sl@0: } {1 2 3 {} 2 {} {} 3 4 2 2 {}} sl@0: sl@0: # Ticket #1301. Any NULL value in a set of unique columns should sl@0: # cause the rows to be distinct. sl@0: # sl@0: do_test unique-4.6 { sl@0: catchsql { sl@0: INSERT INTO t4 VALUES(NULL, 2, NULL); sl@0: } sl@0: } {0 {}} sl@0: do_test unique-4.7 { sl@0: execsql {SELECT * FROM t4} sl@0: } {1 2 3 {} 2 {} {} 3 4 2 2 {} {} 2 {}} sl@0: do_test unique-4.8 { sl@0: catchsql {CREATE UNIQUE INDEX i4a ON t4(a,b)} sl@0: } {0 {}} sl@0: do_test unique-4.9 { sl@0: catchsql {CREATE UNIQUE INDEX i4b ON t4(a,b,c)} sl@0: } {0 {}} sl@0: do_test unique-4.10 { sl@0: catchsql {CREATE UNIQUE INDEX i4c ON t4(b)} sl@0: } {1 {indexed columns are not unique}} sl@0: integrity_check unique-4.99 sl@0: sl@0: # Test the error message generation logic. In particular, make sure we sl@0: # do not overflow the static buffer used to generate the error message. sl@0: # sl@0: do_test unique-5.1 { sl@0: execsql { sl@0: CREATE TABLE t5( sl@0: first_column_with_long_name, sl@0: second_column_with_long_name, sl@0: third_column_with_long_name, sl@0: fourth_column_with_long_name, sl@0: fifth_column_with_long_name, sl@0: sixth_column_with_long_name, sl@0: UNIQUE( sl@0: first_column_with_long_name, sl@0: second_column_with_long_name, sl@0: third_column_with_long_name, sl@0: fourth_column_with_long_name, sl@0: fifth_column_with_long_name, sl@0: sixth_column_with_long_name sl@0: ) sl@0: ); sl@0: INSERT INTO t5 VALUES(1,2,3,4,5,6); sl@0: SELECT * FROM t5; sl@0: } sl@0: } {1 2 3 4 5 6} sl@0: do_test unique-5.2 { sl@0: catchsql { sl@0: INSERT INTO t5 VALUES(1,2,3,4,5,6); sl@0: } sl@0: } {1 {columns first_column_with_long_name, second_column_with_long_name, third_column_with_long_name, fourth_column_with_long_name, fifth_column_with_long_name, ... are not unique}} sl@0: sl@0: finish_test