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