1# 2# 2001 September 27 3# 4# The author disclaims copyright to this source code. In place of 5# a legal notice, here is a blessing: 6# 7# May you do good and not evil. 8# May you find forgiveness for yourself and forgive others. 9# May you share freely, never taking more than you give. 10# 11#*********************************************************************** 12# This file implements regression tests for SQLite library. The 13# focus of this file is testing the CREATE UNIQUE INDEX statement, 14# and primary keys, and the UNIQUE constraint on table columns 15# 16# $Id: unique.test,v 1.7 2003/08/05 13:13:39 drh Exp $ 17 18set testdir [file dirname $argv0] 19source $testdir/tester.tcl 20 21# Try to create a table with two primary keys. 22# (This is allowed in SQLite even that it is not valid SQL) 23# 24do_test unique-1.1 { 25 catchsql { 26 CREATE TABLE t1( 27 a int PRIMARY KEY, 28 b int PRIMARY KEY, 29 c text 30 ); 31 } 32} {1 {table "t1" has more than one primary key}} 33do_test unique-1.1b { 34 catchsql { 35 CREATE TABLE t1( 36 a int PRIMARY KEY, 37 b int UNIQUE, 38 c text 39 ); 40 } 41} {0 {}} 42do_test unique-1.2 { 43 catchsql { 44 INSERT INTO t1(a,b,c) VALUES(1,2,3) 45 } 46} {0 {}} 47do_test unique-1.3 { 48 catchsql { 49 INSERT INTO t1(a,b,c) VALUES(1,3,4) 50 } 51} {1 {column a is not unique}} 52do_test unique-1.4 { 53 execsql { 54 SELECT * FROM t1 ORDER BY a; 55 } 56} {1 2 3} 57do_test unique-1.5 { 58 catchsql { 59 INSERT INTO t1(a,b,c) VALUES(3,2,4) 60 } 61} {1 {column b is not unique}} 62do_test unique-1.6 { 63 execsql { 64 SELECT * FROM t1 ORDER BY a; 65 } 66} {1 2 3} 67do_test unique-1.7 { 68 catchsql { 69 INSERT INTO t1(a,b,c) VALUES(3,4,5) 70 } 71} {0 {}} 72do_test unique-1.8 { 73 execsql { 74 SELECT * FROM t1 ORDER BY a; 75 } 76} {1 2 3 3 4 5} 77integrity_check unique-1.9 78 79do_test unique-2.0 { 80 execsql { 81 DROP TABLE t1; 82 CREATE TABLE t2(a int, b int); 83 INSERT INTO t2(a,b) VALUES(1,2); 84 INSERT INTO t2(a,b) VALUES(3,4); 85 SELECT * FROM t2 ORDER BY a; 86 } 87} {1 2 3 4} 88do_test unique-2.1 { 89 catchsql { 90 CREATE UNIQUE INDEX i2 ON t2(a) 91 } 92} {0 {}} 93do_test unique-2.2 { 94 catchsql { 95 SELECT * FROM t2 ORDER BY a 96 } 97} {0 {1 2 3 4}} 98do_test unique-2.3 { 99 catchsql { 100 INSERT INTO t2 VALUES(1,5); 101 } 102} {1 {column a is not unique}} 103do_test unique-2.4 { 104 catchsql { 105 SELECT * FROM t2 ORDER BY a 106 } 107} {0 {1 2 3 4}} 108do_test unique-2.5 { 109 catchsql { 110 DROP INDEX i2; 111 SELECT * FROM t2 ORDER BY a; 112 } 113} {0 {1 2 3 4}} 114do_test unique-2.6 { 115 catchsql { 116 INSERT INTO t2 VALUES(1,5) 117 } 118} {0 {}} 119do_test unique-2.7 { 120 catchsql { 121 SELECT * FROM t2 ORDER BY a, b; 122 } 123} {0 {1 2 1 5 3 4}} 124do_test unique-2.8 { 125 catchsql { 126 CREATE UNIQUE INDEX i2 ON t2(a); 127 } 128} {1 {indexed columns are not unique}} 129do_test unique-2.9 { 130 catchsql { 131 CREATE INDEX i2 ON t2(a); 132 } 133} {0 {}} 134integrity_check unique-2.10 135 136# Test the UNIQUE keyword as used on two or more fields. 137# 138do_test unique-3.1 { 139 catchsql { 140 CREATE TABLE t3( 141 a int, 142 b int, 143 c int, 144 d int, 145 unique(a,c,d) 146 ); 147 } 148} {0 {}} 149do_test unique-3.2 { 150 catchsql { 151 INSERT INTO t3(a,b,c,d) VALUES(1,2,3,4); 152 SELECT * FROM t3 ORDER BY a,b,c,d; 153 } 154} {0 {1 2 3 4}} 155do_test unique-3.3 { 156 catchsql { 157 INSERT INTO t3(a,b,c,d) VALUES(1,2,3,5); 158 SELECT * FROM t3 ORDER BY a,b,c,d; 159 } 160} {0 {1 2 3 4 1 2 3 5}} 161do_test unique-3.4 { 162 catchsql { 163 INSERT INTO t3(a,b,c,d) VALUES(1,4,3,5); 164 SELECT * FROM t3 ORDER BY a,b,c,d; 165 } 166} {1 {columns a, c, d are not unique}} 167integrity_check unique-3.5 168 169# Make sure NULLs are distinct as far as the UNIQUE tests are 170# concerned. 171# 172do_test unique-4.1 { 173 execsql { 174 CREATE TABLE t4(a UNIQUE, b, c, UNIQUE(b,c)); 175 INSERT INTO t4 VALUES(1,2,3); 176 INSERT INTO t4 VALUES(NULL, 2, NULL); 177 SELECT * FROM t4; 178 } 179} {1 2 3 {} 2 {}} 180do_test unique-4.2 { 181 catchsql { 182 INSERT INTO t4 VALUES(NULL, 3, 4); 183 } 184} {0 {}} 185do_test unique-4.3 { 186 execsql { 187 SELECT * FROM t4 188 } 189} {1 2 3 {} 2 {} {} 3 4} 190do_test unique-4.4 { 191 catchsql { 192 INSERT INTO t4 VALUES(2, 2, NULL); 193 } 194} {0 {}} 195do_test unique-4.5 { 196 execsql { 197 SELECT * FROM t4 198 } 199} {1 2 3 {} 2 {} {} 3 4 2 2 {}} 200integrity_check unique-4.6 201 202# Test the error message generation logic. In particular, make sure we 203# do not overflow the static buffer used to generate the error message. 204# 205do_test unique-5.1 { 206 execsql { 207 CREATE TABLE t5( 208 first_column_with_long_name, 209 second_column_with_long_name, 210 third_column_with_long_name, 211 fourth_column_with_long_name, 212 fifth_column_with_long_name, 213 sixth_column_with_long_name, 214 UNIQUE( 215 first_column_with_long_name, 216 second_column_with_long_name, 217 third_column_with_long_name, 218 fourth_column_with_long_name, 219 fifth_column_with_long_name, 220 sixth_column_with_long_name 221 ) 222 ); 223 INSERT INTO t5 VALUES(1,2,3,4,5,6); 224 SELECT * FROM t5; 225 } 226} {1 2 3 4 5 6} 227do_test unique-5.2 { 228 catchsql { 229 INSERT INTO t5 VALUES(1,2,3,4,5,6); 230 } 231} {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}} 232 233finish_test 234