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 the CREATE UNIQUE INDEX statement,
13 # and primary keys, and the UNIQUE constraint on table columns
15 # $Id: unique.test,v 1.7 2003/08/05 13:13:39 drh Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
20 # Try to create a table with two primary keys.
21 # (This is allowed in SQLite even that it is not valid SQL)
31 } {1 {table "t1" has more than one primary key}}
43 INSERT INTO t1(a,b,c) VALUES(1,2,3)
48 INSERT INTO t1(a,b,c) VALUES(1,3,4)
50 } {1 {column a is not unique}}
53 SELECT * FROM t1 ORDER BY a;
58 INSERT INTO t1(a,b,c) VALUES(3,2,4)
60 } {1 {column b is not unique}}
63 SELECT * FROM t1 ORDER BY a;
68 INSERT INTO t1(a,b,c) VALUES(3,4,5)
73 SELECT * FROM t1 ORDER BY a;
76 integrity_check unique-1.9
81 CREATE TABLE t2(a int, b int);
82 INSERT INTO t2(a,b) VALUES(1,2);
83 INSERT INTO t2(a,b) VALUES(3,4);
84 SELECT * FROM t2 ORDER BY a;
89 CREATE UNIQUE INDEX i2 ON t2(a)
94 SELECT * FROM t2 ORDER BY a
99 INSERT INTO t2 VALUES(1,5);
101 } {1 {column a is not unique}}
104 SELECT * FROM t2 ORDER BY a
110 SELECT * FROM t2 ORDER BY a;
115 INSERT INTO t2 VALUES(1,5)
120 SELECT * FROM t2 ORDER BY a, b;
125 CREATE UNIQUE INDEX i2 ON t2(a);
127 } {1 {indexed columns are not unique}}
130 CREATE INDEX i2 ON t2(a);
133 integrity_check unique-2.10
135 # Test the UNIQUE keyword as used on two or more fields.
150 INSERT INTO t3(a,b,c,d) VALUES(1,2,3,4);
151 SELECT * FROM t3 ORDER BY a,b,c,d;
156 INSERT INTO t3(a,b,c,d) VALUES(1,2,3,5);
157 SELECT * FROM t3 ORDER BY a,b,c,d;
159 } {0 {1 2 3 4 1 2 3 5}}
162 INSERT INTO t3(a,b,c,d) VALUES(1,4,3,5);
163 SELECT * FROM t3 ORDER BY a,b,c,d;
165 } {1 {columns a, c, d are not unique}}
166 integrity_check unique-3.5
168 # Make sure NULLs are distinct as far as the UNIQUE tests are
173 CREATE TABLE t4(a UNIQUE, b, c, UNIQUE(b,c));
174 INSERT INTO t4 VALUES(1,2,3);
175 INSERT INTO t4 VALUES(NULL, 2, NULL);
181 INSERT INTO t4 VALUES(NULL, 3, 4);
188 } {1 2 3 {} 2 {} {} 3 4}
191 INSERT INTO t4 VALUES(2, 2, NULL);
198 } {1 2 3 {} 2 {} {} 3 4 2 2 {}}
199 integrity_check unique-4.6
201 # Test the error message generation logic. In particular, make sure we
202 # do not overflow the static buffer used to generate the error message.
207 first_column_with_long_name,
208 second_column_with_long_name,
209 third_column_with_long_name,
210 fourth_column_with_long_name,
211 fifth_column_with_long_name,
212 sixth_column_with_long_name,
214 first_column_with_long_name,
215 second_column_with_long_name,
216 third_column_with_long_name,
217 fourth_column_with_long_name,
218 fifth_column_with_long_name,
219 sixth_column_with_long_name
222 INSERT INTO t5 VALUES(1,2,3,4,5,6);
228 INSERT INTO t5 VALUES(1,2,3,4,5,6);
230 } {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}}