blob: 151f122edc3e908ea543a26205ab9da287702414 [file] [log] [blame]
drh348784e2000-05-29 20:41:49 +00001# Copyright (c) 1999, 2000 D. Richard Hipp
2#
3# This program is free software; you can redistribute it and/or
4# modify it under the terms of the GNU General Public
5# License as published by the Free Software Foundation; either
6# version 2 of the License, or (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11# General Public License for more details.
12#
13# You should have received a copy of the GNU General Public
14# License along with this library; if not, write to the
15# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16# Boston, MA 02111-1307, USA.
17#
18# Author contact information:
19# drh@hwaci.com
20# http://www.hwaci.com/drh/
21#
22#***********************************************************************
23# This file implements regression tests for SQLite library. The
24# focus of this file is testing the CREATE TABLE statement.
25#
drh3fc190c2001-09-14 03:24:23 +000026# $Id: table.test,v 1.11 2001/09/14 03:24:25 drh Exp $
drh348784e2000-05-29 20:41:49 +000027
28set testdir [file dirname $argv0]
29source $testdir/tester.tcl
30
31# Create a basic table and verify it is added to sqlite_master
32#
drh1b6a71f2000-05-29 23:58:11 +000033do_test table-1.1 {
drh348784e2000-05-29 20:41:49 +000034 execsql {
35 CREATE TABLE test1 (
36 one varchar(10),
37 two text
38 )
39 }
40 execsql {
drh28037572000-08-02 13:47:41 +000041 SELECT sql FROM sqlite_master WHERE type!='meta'
drh348784e2000-05-29 20:41:49 +000042 }
43} {{CREATE TABLE test1 (
44 one varchar(10),
45 two text
46 )}}
47
drh348784e2000-05-29 20:41:49 +000048
49# Verify the other fields of the sqlite_master file.
50#
drh1b6a71f2000-05-29 23:58:11 +000051do_test table-1.3 {
drh28037572000-08-02 13:47:41 +000052 execsql {SELECT name, tbl_name, type FROM sqlite_master WHERE type!='meta'}
drh348784e2000-05-29 20:41:49 +000053} {test1 test1 table}
54
55# Close and reopen the database. Verify that everything is
56# still the same.
57#
drh1b6a71f2000-05-29 23:58:11 +000058do_test table-1.4 {
drh348784e2000-05-29 20:41:49 +000059 db close
drh3fc190c2001-09-14 03:24:23 +000060 sqlite db test.db
drh28037572000-08-02 13:47:41 +000061 execsql {SELECT name, tbl_name, type from sqlite_master WHERE type!='meta'}
drh348784e2000-05-29 20:41:49 +000062} {test1 test1 table}
63
64# Drop the database and make sure it disappears.
65#
drh1b6a71f2000-05-29 23:58:11 +000066do_test table-1.5 {
drh348784e2000-05-29 20:41:49 +000067 execsql {DROP TABLE test1}
drh28037572000-08-02 13:47:41 +000068 execsql {SELECT * FROM sqlite_master WHERE type!='meta'}
drh348784e2000-05-29 20:41:49 +000069} {}
70
drh348784e2000-05-29 20:41:49 +000071# Close and reopen the database. Verify that the table is
72# still gone.
73#
drh1b6a71f2000-05-29 23:58:11 +000074do_test table-1.6 {
drh348784e2000-05-29 20:41:49 +000075 db close
drh3fc190c2001-09-14 03:24:23 +000076 sqlite db test.db
drh28037572000-08-02 13:47:41 +000077 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
drh348784e2000-05-29 20:41:49 +000078} {}
79
drh982cef72000-05-30 16:27:03 +000080# Repeat the above steps, but this time quote the table name.
81#
82do_test table-1.10 {
83 execsql {CREATE TABLE "create" (f1 int)}
drh28037572000-08-02 13:47:41 +000084 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
drh982cef72000-05-30 16:27:03 +000085} {create}
86do_test table-1.11 {
87 execsql {DROP TABLE "create"}
drh28037572000-08-02 13:47:41 +000088 execsql {SELECT name FROM "sqlite_master" WHERE type!='meta'}
drh982cef72000-05-30 16:27:03 +000089} {}
90do_test table-1.12 {
91 execsql {CREATE TABLE test1("f1 ho" int)}
drh28037572000-08-02 13:47:41 +000092 execsql {SELECT name as "X" FROM sqlite_master WHERE type!='meta'}
drh982cef72000-05-30 16:27:03 +000093} {test1}
94do_test table-1.13 {
95 execsql {DROP TABLE "TEST1"}
drh28037572000-08-02 13:47:41 +000096 execsql {SELECT name FROM "sqlite_master" WHERE type!='meta'}
drh982cef72000-05-30 16:27:03 +000097} {}
98
99
drhb24fcbe2000-05-29 23:30:50 +0000100
101# Verify that we cannot make two tables with the same name
102#
drh1b6a71f2000-05-29 23:58:11 +0000103do_test table-2.1 {
drh4cfa7932000-06-08 15:10:46 +0000104 execsql {CREATE TABLE TEST2(one text)}
drhb24fcbe2000-05-29 23:30:50 +0000105 set v [catch {execsql {CREATE TABLE test2(two text)}} msg]
106 lappend v $msg
drh1d37e282000-05-30 03:12:21 +0000107} {1 {table test2 already exists}}
drh1b6a71f2000-05-29 23:58:11 +0000108do_test table-2.1b {
drhb24fcbe2000-05-29 23:30:50 +0000109 set v [catch {execsql {CREATE TABLE sqlite_master(two text)}} msg]
110 lappend v $msg
drh1d37e282000-05-30 03:12:21 +0000111} {1 {table sqlite_master already exists}}
drh1b6a71f2000-05-29 23:58:11 +0000112do_test table-2.1c {
drhb24fcbe2000-05-29 23:30:50 +0000113 db close
drh3fc190c2001-09-14 03:24:23 +0000114 sqlite db test.db
drhb24fcbe2000-05-29 23:30:50 +0000115 set v [catch {execsql {CREATE TABLE sqlite_master(two text)}} msg]
116 lappend v $msg
drh1d37e282000-05-30 03:12:21 +0000117} {1 {table sqlite_master already exists}}
drh1b6a71f2000-05-29 23:58:11 +0000118do_test table-2.1d {
drh28037572000-08-02 13:47:41 +0000119 execsql {DROP TABLE test2; SELECT name FROM sqlite_master WHERE type!='meta'}
drhb24fcbe2000-05-29 23:30:50 +0000120} {}
121
122# Verify that we cannot make a table with the same name as an index
123#
drhdcc581c2000-05-30 13:44:19 +0000124do_test table-2.2a {
drhb24fcbe2000-05-29 23:30:50 +0000125 execsql {CREATE TABLE test2(one text); CREATE INDEX test3 ON test2(one)}
126 set v [catch {execsql {CREATE TABLE test3(two text)}} msg]
127 lappend v $msg
drh1d37e282000-05-30 03:12:21 +0000128} {1 {there is already an index named test3}}
drh1b6a71f2000-05-29 23:58:11 +0000129do_test table-2.2b {
drhb24fcbe2000-05-29 23:30:50 +0000130 db close
drh3fc190c2001-09-14 03:24:23 +0000131 sqlite db test.db
drhb24fcbe2000-05-29 23:30:50 +0000132 set v [catch {execsql {CREATE TABLE test3(two text)}} msg]
133 lappend v $msg
drh1d37e282000-05-30 03:12:21 +0000134} {1 {there is already an index named test3}}
drh1b6a71f2000-05-29 23:58:11 +0000135do_test table-2.2c {
drh3fc190c2001-09-14 03:24:23 +0000136 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
137} {test2 test3}
138do_test table-2.2d {
drhb24fcbe2000-05-29 23:30:50 +0000139 execsql {DROP INDEX test3}
140 set v [catch {execsql {CREATE TABLE test3(two text)}} msg]
141 lappend v $msg
142} {0 {}}
drh3fc190c2001-09-14 03:24:23 +0000143do_test table-2.2e {
drh28037572000-08-02 13:47:41 +0000144 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
drhb24fcbe2000-05-29 23:30:50 +0000145} {test2 test3}
drh3fc190c2001-09-14 03:24:23 +0000146do_test table-2.2f {
drhb24fcbe2000-05-29 23:30:50 +0000147 execsql {DROP TABLE test2; DROP TABLE test3}
drh28037572000-08-02 13:47:41 +0000148 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
drhb24fcbe2000-05-29 23:30:50 +0000149} {}
150
151# Create a table with many field names
152#
153set big_table \
154{CREATE TABLE big(
155 f1 varchar(20),
156 f2 char(10),
drhdcc581c2000-05-30 13:44:19 +0000157 f3 varchar(30) primary key,
drhb24fcbe2000-05-29 23:30:50 +0000158 f4 text,
159 f5 text,
160 f6 text,
161 f7 text,
162 f8 text,
163 f9 text,
164 f10 text,
165 f11 text,
166 f12 text,
167 f13 text,
168 f14 text,
169 f15 text,
170 f16 text,
171 f17 text,
172 f18 text,
173 f19 text,
174 f20 text
175)}
drh1b6a71f2000-05-29 23:58:11 +0000176do_test table-3.1 {
drhb24fcbe2000-05-29 23:30:50 +0000177 execsql $big_table
drh28037572000-08-02 13:47:41 +0000178 execsql {SELECT sql FROM sqlite_master WHERE type!='meta'}
drhb24fcbe2000-05-29 23:30:50 +0000179} \{$big_table\}
drh1b6a71f2000-05-29 23:58:11 +0000180do_test table-3.2 {
drhb24fcbe2000-05-29 23:30:50 +0000181 set v [catch {execsql {CREATE TABLE BIG(xyz foo)}} msg]
182 lappend v $msg
drh1d37e282000-05-30 03:12:21 +0000183} {1 {table BIG already exists}}
drh1b6a71f2000-05-29 23:58:11 +0000184do_test table-3.3 {
drhb24fcbe2000-05-29 23:30:50 +0000185 set v [catch {execsql {CREATE TABLE biG(xyz foo)}} msg]
186 lappend v $msg
drh1d37e282000-05-30 03:12:21 +0000187} {1 {table biG already exists}}
drh1b6a71f2000-05-29 23:58:11 +0000188do_test table-3.4 {
drhb24fcbe2000-05-29 23:30:50 +0000189 set v [catch {execsql {CREATE TABLE bIg(xyz foo)}} msg]
190 lappend v $msg
drh1d37e282000-05-30 03:12:21 +0000191} {1 {table bIg already exists}}
drh1b6a71f2000-05-29 23:58:11 +0000192do_test table-3.5 {
drhb24fcbe2000-05-29 23:30:50 +0000193 db close
drh3fc190c2001-09-14 03:24:23 +0000194 sqlite db test.db
drhb24fcbe2000-05-29 23:30:50 +0000195 set v [catch {execsql {CREATE TABLE Big(xyz foo)}} msg]
196 lappend v $msg
drh1d37e282000-05-30 03:12:21 +0000197} {1 {table Big already exists}}
drh1b6a71f2000-05-29 23:58:11 +0000198do_test table-3.6 {
drhb24fcbe2000-05-29 23:30:50 +0000199 execsql {DROP TABLE big}
drh28037572000-08-02 13:47:41 +0000200 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
drhb24fcbe2000-05-29 23:30:50 +0000201} {}
202
203# Try creating large numbers of tables
204#
205set r {}
206for {set i 1} {$i<=100} {incr i} {
207 lappend r test$i
208}
drh1b6a71f2000-05-29 23:58:11 +0000209do_test table-4.1 {
drhb24fcbe2000-05-29 23:30:50 +0000210 for {set i 1} {$i<=100} {incr i} {
211 set sql "CREATE TABLE test$i ("
212 for {set k 1} {$k<$i} {incr k} {
213 append sql "field$k text,"
214 }
215 append sql "last_field text)"
216 execsql $sql
217 }
drh28037572000-08-02 13:47:41 +0000218 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
drhb24fcbe2000-05-29 23:30:50 +0000219} $r
drh1b6a71f2000-05-29 23:58:11 +0000220do_test table-4.1b {
drhb24fcbe2000-05-29 23:30:50 +0000221 db close
drh3fc190c2001-09-14 03:24:23 +0000222 sqlite db test.db
drh28037572000-08-02 13:47:41 +0000223 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
drhb24fcbe2000-05-29 23:30:50 +0000224} $r
225
drhc4a3c772001-04-04 11:48:57 +0000226# Drop the even numbered tables
drhb24fcbe2000-05-29 23:30:50 +0000227#
228set r {}
229for {set i 1} {$i<=100} {incr i 2} {
230 lappend r test$i
231}
drh3fc190c2001-09-14 03:24:23 +0000232#execsql {--vdbe-trace-on--}
drh1b6a71f2000-05-29 23:58:11 +0000233do_test table-4.2 {
drhb24fcbe2000-05-29 23:30:50 +0000234 for {set i 2} {$i<=100} {incr i 2} {
235 set sql "DROP TABLE TEST$i"
236 execsql $sql
237 }
drh28037572000-08-02 13:47:41 +0000238 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
drhb24fcbe2000-05-29 23:30:50 +0000239} $r
drh3fc190c2001-09-14 03:24:23 +0000240#exit
drhb24fcbe2000-05-29 23:30:50 +0000241
242# Drop the odd number tables
243#
drh1b6a71f2000-05-29 23:58:11 +0000244do_test table-4.3 {
drhb24fcbe2000-05-29 23:30:50 +0000245 for {set i 1} {$i<=100} {incr i 2} {
246 set sql "DROP TABLE test$i"
247 execsql $sql
248 }
drh28037572000-08-02 13:47:41 +0000249 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
drhb24fcbe2000-05-29 23:30:50 +0000250} {}
251
drh1b6a71f2000-05-29 23:58:11 +0000252# Try to drop a table that does not exist
253#
254do_test table-5.1 {
255 set v [catch {execsql {DROP TABLE test9}} msg]
256 lappend v $msg
257} {1 {no such table: test9}}
258
259# Try to drop sqlite_master
260#
261do_test table-5.2 {
262 set v [catch {execsql {DROP TABLE sqlite_master}} msg]
263 lappend v $msg
drh1d37e282000-05-30 03:12:21 +0000264} {1 {table sqlite_master may not be dropped}}
drh1b6a71f2000-05-29 23:58:11 +0000265
266# Make sure an EXPLAIN does not really create a new table
267#
268do_test table-5.3 {
269 execsql {EXPLAIN CREATE TABLE test1(f1 int)}
drh28037572000-08-02 13:47:41 +0000270 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
drh1b6a71f2000-05-29 23:58:11 +0000271} {}
272
273# Make sure an EXPLAIN does not really drop an existing table
274#
275do_test table-5.4 {
276 execsql {CREATE TABLE test1(f1 int)}
277 execsql {EXPLAIN DROP TABLE test1}
drh28037572000-08-02 13:47:41 +0000278 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
drh1b6a71f2000-05-29 23:58:11 +0000279} {test1}
280
drh4cfa7932000-06-08 15:10:46 +0000281# Create a table with a goofy name
282#
drh3fc190c2001-09-14 03:24:23 +0000283#do_test table-6.1 {
284# execsql {CREATE TABLE 'Spaces In This Name!'(x int)}
285# execsql {INSERT INTO 'spaces in this name!' VALUES(1)}
286# set list [glob -nocomplain testdb/spaces*.tbl]
287#} {testdb/spaces+in+this+name+.tbl}
drh4cfa7932000-06-08 15:10:46 +0000288
drhc4a3c772001-04-04 11:48:57 +0000289# Try using keywords as table names or column names.
290#
291do_test table-7.1 {
292 set v [catch {execsql {
293 CREATE TABLE weird(
294 desc text,
295 asc text,
296 explain int,
297 vacuum boolean,
drhd4007282001-04-12 23:21:58 +0000298 delimiters varchar(10),
299 begin blob,
300 end clob
drhc4a3c772001-04-04 11:48:57 +0000301 )
302 }} msg]
303 lappend v $msg
304} {0 {}}
305do_test table-7.2 {
306 execsql {
drhd4007282001-04-12 23:21:58 +0000307 INSERT INTO weird VALUES('a','b',9,0,'xyz','hi','y''all');
drhc4a3c772001-04-04 11:48:57 +0000308 SELECT * FROM weird;
309 }
drhd4007282001-04-12 23:21:58 +0000310} {a b 9 0 xyz hi y'all}
drhc4a3c772001-04-04 11:48:57 +0000311do_test table-7.3 {
312 execsql2 {
313 SELECT * FROM weird;
314 }
drhd4007282001-04-12 23:21:58 +0000315} {desc a asc b explain 9 vacuum 0 delimiters xyz begin hi end y'all}
drhc4a3c772001-04-04 11:48:57 +0000316
drh348784e2000-05-29 20:41:49 +0000317finish_test