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