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.9 2009/05/02 15:46:47 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 {}}
200 # Ticket #1301. Any NULL value in a set of unique columns should
201 # cause the rows to be distinct.
205 INSERT INTO t4 VALUES(NULL, 2, NULL);
209 execsql {SELECT * FROM t4}
210 } {1 2 3 {} 2 {} {} 3 4 2 2 {} {} 2 {}}
212 catchsql {CREATE UNIQUE INDEX i4a ON t4(a,b)}
215 catchsql {CREATE UNIQUE INDEX i4b ON t4(a,b,c)}
217 do_test unique-4.10 {
218 catchsql {CREATE UNIQUE INDEX i4c ON t4(b)}
219 } {1 {indexed columns are not unique}}
220 integrity_check unique-4.99
222 # Test the error message generation logic. In particular, make sure we
223 # do not overflow the static buffer used to generate the error message.
228 first_column_with_long_name,
229 second_column_with_long_name,
230 third_column_with_long_name,
231 fourth_column_with_long_name,
232 fifth_column_with_long_name,
233 sixth_column_with_long_name,
235 first_column_with_long_name,
236 second_column_with_long_name,
237 third_column_with_long_name,
238 fourth_column_with_long_name,
239 fifth_column_with_long_name,
240 sixth_column_with_long_name
243 INSERT INTO t5 VALUES(1,2,3,4,5,6);
249 INSERT INTO t5 VALUES(1,2,3,4,5,6);
251 } {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, sixth_column_with_long_name are not unique}}