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 {UNIQUE constraint failed: t1.a}}
51 verify_ex_errcode unique-1.3b SQLITE_CONSTRAINT_PRIMARYKEY
54 SELECT * FROM t1 ORDER BY a;
59 INSERT INTO t1(a,b,c) VALUES(3,2,4)
61 } {1 {UNIQUE constraint failed: t1.b}}
62 verify_ex_errcode unique-1.5b SQLITE_CONSTRAINT_UNIQUE
65 SELECT * FROM t1 ORDER BY a;
70 INSERT INTO t1(a,b,c) VALUES(3,4,5)
75 SELECT * FROM t1 ORDER BY a;
78 integrity_check unique-1.9
83 CREATE TABLE t2(a int, b int);
84 INSERT INTO t2(a,b) VALUES(1,2);
85 INSERT INTO t2(a,b) VALUES(3,4);
86 SELECT * FROM t2 ORDER BY a;
91 CREATE UNIQUE INDEX i2 ON t2(a)
96 SELECT * FROM t2 ORDER BY a
101 INSERT INTO t2 VALUES(1,5);
103 } {1 {UNIQUE constraint failed: t2.a}}
104 verify_ex_errcode unique-2.3b SQLITE_CONSTRAINT_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 {UNIQUE constraint failed: t2.a}}
131 verify_ex_errcode unique-2.8b SQLITE_CONSTRAINT_UNIQUE
134 CREATE INDEX i2 ON t2(a);
137 integrity_check unique-2.10
139 # Test the UNIQUE keyword as used on two or more fields.
154 INSERT INTO t3(a,b,c,d) VALUES(1,2,3,4);
155 SELECT * FROM t3 ORDER BY a,b,c,d;
160 INSERT INTO t3(a,b,c,d) VALUES(1,2,3,5);
161 SELECT * FROM t3 ORDER BY a,b,c,d;
163 } {0 {1 2 3 4 1 2 3 5}}
166 INSERT INTO t3(a,b,c,d) VALUES(1,4,3,5);
167 SELECT * FROM t3 ORDER BY a,b,c,d;
169 } {1 {UNIQUE constraint failed: t3.a, t3.c, t3.d}}
170 verify_ex_errcode unique-3.4b SQLITE_CONSTRAINT_UNIQUE
171 integrity_check unique-3.5
173 # Make sure NULLs are distinct as far as the UNIQUE tests are
178 CREATE TABLE t4(a UNIQUE, b, c, UNIQUE(b,c));
179 INSERT INTO t4 VALUES(1,2,3);
180 INSERT INTO t4 VALUES(NULL, 2, NULL);
186 INSERT INTO t4 VALUES(NULL, 3, 4);
193 } {1 2 3 {} 2 {} {} 3 4}
196 INSERT INTO t4 VALUES(2, 2, NULL);
203 } {1 2 3 {} 2 {} {} 3 4 2 2 {}}
205 # Ticket #1301. Any NULL value in a set of unique columns should
206 # cause the rows to be distinct.
210 INSERT INTO t4 VALUES(NULL, 2, NULL);
214 execsql {SELECT * FROM t4}
215 } {1 2 3 {} 2 {} {} 3 4 2 2 {} {} 2 {}}
217 catchsql {CREATE UNIQUE INDEX i4a ON t4(a,b)}
220 catchsql {CREATE UNIQUE INDEX i4b ON t4(a,b,c)}
222 do_test unique-4.10 {
223 catchsql {CREATE UNIQUE INDEX i4c ON t4(b)}
224 } {1 {UNIQUE constraint failed: t4.b}}
225 verify_ex_errcode unique-4.10b SQLITE_CONSTRAINT_UNIQUE
226 integrity_check unique-4.99
228 # Test the error message generation logic. In particular, make sure we
229 # do not overflow the static buffer used to generate the error message.
234 first_column_with_long_name,
235 second_column_with_long_name,
236 third_column_with_long_name,
237 fourth_column_with_long_name,
238 fifth_column_with_long_name,
239 sixth_column_with_long_name,
241 first_column_with_long_name,
242 second_column_with_long_name,
243 third_column_with_long_name,
244 fourth_column_with_long_name,
245 fifth_column_with_long_name,
246 sixth_column_with_long_name
249 INSERT INTO t5 VALUES(1,2,3,4,5,6);
255 INSERT INTO t5 VALUES(1,2,3,4,5,6);
257 } {1 {UNIQUE constraint failed: t5.first_column_with_long_name, t5.second_column_with_long_name, t5.third_column_with_long_name, t5.fourth_column_with_long_name, t5.fifth_column_with_long_name, t5.sixth_column_with_long_name}}
258 verify_ex_errcode unique-5.2b SQLITE_CONSTRAINT_UNIQUE