2 #pragma ident "%Z%%M% %I% %E% SMI"
6 # The author disclaims copyright to this source code. In place of
7 # a legal notice, here is a blessing:
9 # May you do good and not evil.
10 # May you find forgiveness for yourself and forgive others.
11 # May you share freely, never taking more than you give.
13 #***********************************************************************
14 # This file implements regression tests for SQLite library. The
15 # focus of this file is testing the CREATE UNIQUE INDEX statement,
16 # and primary keys, and the UNIQUE constraint on table columns
18 # $Id: unique.test,v 1.7 2003/08/05 13:13:39 drh Exp $
20 set testdir [file dirname $argv0]
21 source $testdir/tester.tcl
23 # Try to create a table with two primary keys.
24 # (This is allowed in SQLite even that it is not valid SQL)
34 } {1 {table "t1" has more than one primary key}}
46 INSERT INTO t1(a,b,c) VALUES(1,2,3)
51 INSERT INTO t1(a,b,c) VALUES(1,3,4)
53 } {1 {column a is not unique}}
56 SELECT * FROM t1 ORDER BY a;
61 INSERT INTO t1(a,b,c) VALUES(3,2,4)
63 } {1 {column b is not unique}}
66 SELECT * FROM t1 ORDER BY a;
71 INSERT INTO t1(a,b,c) VALUES(3,4,5)
76 SELECT * FROM t1 ORDER BY a;
79 integrity_check unique-1.9
84 CREATE TABLE t2(a int, b int);
85 INSERT INTO t2(a,b) VALUES(1,2);
86 INSERT INTO t2(a,b) VALUES(3,4);
87 SELECT * FROM t2 ORDER BY a;
92 CREATE UNIQUE INDEX i2 ON t2(a)
97 SELECT * FROM t2 ORDER BY a
102 INSERT INTO t2 VALUES(1,5);
104 } {1 {column a is not unique}}
107 SELECT * FROM t2 ORDER BY a
113 SELECT * FROM t2 ORDER BY a;
118 INSERT INTO t2 VALUES(1,5)
123 SELECT * FROM t2 ORDER BY a, b;
128 CREATE UNIQUE INDEX i2 ON t2(a);
130 } {1 {indexed columns are not unique}}
133 CREATE INDEX i2 ON t2(a);
136 integrity_check unique-2.10
138 # Test the UNIQUE keyword as used on two or more fields.
153 INSERT INTO t3(a,b,c,d) VALUES(1,2,3,4);
154 SELECT * FROM t3 ORDER BY a,b,c,d;
159 INSERT INTO t3(a,b,c,d) VALUES(1,2,3,5);
160 SELECT * FROM t3 ORDER BY a,b,c,d;
162 } {0 {1 2 3 4 1 2 3 5}}
165 INSERT INTO t3(a,b,c,d) VALUES(1,4,3,5);
166 SELECT * FROM t3 ORDER BY a,b,c,d;
168 } {1 {columns a, c, d are not unique}}
169 integrity_check unique-3.5
171 # Make sure NULLs are distinct as far as the UNIQUE tests are
176 CREATE TABLE t4(a UNIQUE, b, c, UNIQUE(b,c));
177 INSERT INTO t4 VALUES(1,2,3);
178 INSERT INTO t4 VALUES(NULL, 2, NULL);
184 INSERT INTO t4 VALUES(NULL, 3, 4);
191 } {1 2 3 {} 2 {} {} 3 4}
194 INSERT INTO t4 VALUES(2, 2, NULL);
201 } {1 2 3 {} 2 {} {} 3 4 2 2 {}}
202 integrity_check unique-4.6
204 # Test the error message generation logic. In particular, make sure we
205 # do not overflow the static buffer used to generate the error message.
210 first_column_with_long_name,
211 second_column_with_long_name,
212 third_column_with_long_name,
213 fourth_column_with_long_name,
214 fifth_column_with_long_name,
215 sixth_column_with_long_name,
217 first_column_with_long_name,
218 second_column_with_long_name,
219 third_column_with_long_name,
220 fourth_column_with_long_name,
221 fifth_column_with_long_name,
222 sixth_column_with_long_name
225 INSERT INTO t5 VALUES(1,2,3,4,5,6);
231 INSERT INTO t5 VALUES(1,2,3,4,5,6);
233 } {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}}