xref: /sqlite-3.40.0/test/table.test (revision 78c2e6d8)
1# 2001 September 15
2#
3# The author disclaims copyright to this source code.  In place of
4# a legal notice, here is a blessing:
5#
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.
9#
10#***********************************************************************
11# This file implements regression tests for SQLite library.  The
12# focus of this file is testing the CREATE TABLE statement.
13#
14# $Id: table.test,v 1.49 2009/01/16 11:04:58 danielk1977 Exp $
15
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18
19# Create a basic table and verify it is added to sqlite_master
20#
21do_test table-1.1 {
22  execsql {
23    CREATE TABLE test1 (
24      one varchar(10),
25      two text
26    )
27  }
28  execsql {
29    SELECT sql FROM sqlite_master WHERE type!='meta'
30  }
31} {{CREATE TABLE test1 (
32      one varchar(10),
33      two text
34    )}}
35
36
37# Verify the other fields of the sqlite_master file.
38#
39do_test table-1.3 {
40  execsql {SELECT name, tbl_name, type FROM sqlite_master WHERE type!='meta'}
41} {test1 test1 table}
42
43# Close and reopen the database.  Verify that everything is
44# still the same.
45#
46do_test table-1.4 {
47  db close
48  sqlite3 db test.db
49  execsql {SELECT name, tbl_name, type from sqlite_master WHERE type!='meta'}
50} {test1 test1 table}
51
52# Drop the database and make sure it disappears.
53#
54do_test table-1.5 {
55  execsql {DROP TABLE test1}
56  execsql {SELECT * FROM sqlite_master WHERE type!='meta'}
57} {}
58
59# Close and reopen the database.  Verify that the table is
60# still gone.
61#
62do_test table-1.6 {
63  db close
64  sqlite3 db test.db
65  execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
66} {}
67
68# Repeat the above steps, but this time quote the table name.
69#
70do_test table-1.10 {
71  execsql {CREATE TABLE "create" (f1 int)}
72  execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
73} {create}
74do_test table-1.11 {
75  execsql {DROP TABLE "create"}
76  execsql {SELECT name FROM "sqlite_master" WHERE type!='meta'}
77} {}
78do_test table-1.12 {
79  execsql {CREATE TABLE test1("f1 ho" int)}
80  execsql {SELECT name as "X" FROM sqlite_master WHERE type!='meta'}
81} {test1}
82do_test table-1.13 {
83  execsql {DROP TABLE "TEST1"}
84  execsql {SELECT name FROM "sqlite_master" WHERE type!='meta'}
85} {}
86
87
88
89# Verify that we cannot make two tables with the same name
90#
91do_test table-2.1 {
92  execsql {CREATE TABLE TEST2(one text)}
93  catchsql {CREATE TABLE test2(two text default 'hi')}
94} {1 {table test2 already exists}}
95do_test table-2.1.1 {
96  catchsql {CREATE TABLE "test2" (two)}
97} {1 {table "test2" already exists}}
98do_test table-2.1b {
99  set v [catch {execsql {CREATE TABLE sqlite_master(two text)}} msg]
100  lappend v $msg
101} {1 {object name reserved for internal use: sqlite_master}}
102do_test table-2.1c {
103  db close
104  sqlite3 db test.db
105  set v [catch {execsql {CREATE TABLE sqlite_master(two text)}} msg]
106  lappend v $msg
107} {1 {object name reserved for internal use: sqlite_master}}
108do_test table-2.1d {
109  catchsql {CREATE TABLE IF NOT EXISTS test2(x,y)}
110} {0 {}}
111do_test table-2.1e {
112  catchsql {CREATE TABLE IF NOT EXISTS test2(x UNIQUE, y TEXT PRIMARY KEY)}
113} {0 {}}
114do_test table-2.1f {
115  execsql {DROP TABLE test2; SELECT name FROM sqlite_master WHERE type!='meta'}
116} {}
117
118# Verify that we cannot make a table with the same name as an index
119#
120do_test table-2.2a {
121  execsql {CREATE TABLE test2(one text); CREATE INDEX test3 ON test2(one)}
122  set v [catch {execsql {CREATE TABLE test3(two text)}} msg]
123  lappend v $msg
124} {1 {there is already an index named test3}}
125do_test table-2.2b {
126  db close
127  sqlite3 db test.db
128  set v [catch {execsql {CREATE TABLE test3(two text)}} msg]
129  lappend v $msg
130} {1 {there is already an index named test3}}
131do_test table-2.2c {
132  execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
133} {test2 test3}
134do_test table-2.2d {
135  execsql {DROP INDEX test3}
136  set v [catch {execsql {CREATE TABLE test3(two text)}} msg]
137  lappend v $msg
138} {0 {}}
139do_test table-2.2e {
140  execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
141} {test2 test3}
142do_test table-2.2f {
143  execsql {DROP TABLE test2; DROP TABLE test3}
144  execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
145} {}
146
147# Create a table with many field names
148#
149set big_table \
150{CREATE TABLE big(
151  f1 varchar(20),
152  f2 char(10),
153  f3 varchar(30) primary key,
154  f4 text,
155  f5 text,
156  f6 text,
157  f7 text,
158  f8 text,
159  f9 text,
160  f10 text,
161  f11 text,
162  f12 text,
163  f13 text,
164  f14 text,
165  f15 text,
166  f16 text,
167  f17 text,
168  f18 text,
169  f19 text,
170  f20 text
171)}
172do_test table-3.1 {
173  execsql $big_table
174  execsql {SELECT sql FROM sqlite_master WHERE type=='table'}
175} \{$big_table\}
176do_test table-3.2 {
177  set v [catch {execsql {CREATE TABLE BIG(xyz foo)}} msg]
178  lappend v $msg
179} {1 {table BIG already exists}}
180do_test table-3.3 {
181  set v [catch {execsql {CREATE TABLE biG(xyz foo)}} msg]
182  lappend v $msg
183} {1 {table biG already exists}}
184do_test table-3.4 {
185  set v [catch {execsql {CREATE TABLE bIg(xyz foo)}} msg]
186  lappend v $msg
187} {1 {table bIg already exists}}
188do_test table-3.5 {
189  db close
190  sqlite3 db test.db
191  set v [catch {execsql {CREATE TABLE Big(xyz foo)}} msg]
192  lappend v $msg
193} {1 {table Big already exists}}
194do_test table-3.6 {
195  execsql {DROP TABLE big}
196  execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
197} {}
198
199# Try creating large numbers of tables
200#
201set r {}
202for {set i 1} {$i<=100} {incr i} {
203  lappend r [format test%03d $i]
204}
205do_test table-4.1 {
206  for {set i 1} {$i<=100} {incr i} {
207    set sql "CREATE TABLE [format test%03d $i] ("
208    for {set k 1} {$k<$i} {incr k} {
209      append sql "field$k text,"
210    }
211    append sql "last_field text)"
212    execsql $sql
213  }
214  execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
215} $r
216do_test table-4.1b {
217  db close
218  sqlite3 db test.db
219  execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
220} $r
221
222# Drop the even numbered tables
223#
224set r {}
225for {set i 1} {$i<=100} {incr i 2} {
226  lappend r [format test%03d $i]
227}
228do_test table-4.2 {
229  for {set i 2} {$i<=100} {incr i 2} {
230    # if {$i==38} {execsql {pragma vdbe_trace=on}}
231    set sql "DROP TABLE [format TEST%03d $i]"
232    execsql $sql
233  }
234  execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
235} $r
236#exit
237
238# Drop the odd number tables
239#
240do_test table-4.3 {
241  for {set i 1} {$i<=100} {incr i 2} {
242    set sql "DROP TABLE [format test%03d $i]"
243    execsql $sql
244  }
245  execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
246} {}
247
248# Try to drop a table that does not exist
249#
250do_test table-5.1.1 {
251  catchsql {DROP TABLE test009}
252} {1 {no such table: test009}}
253do_test table-5.1.2 {
254  catchsql {DROP TABLE IF EXISTS test009}
255} {0 {}}
256
257# Try to drop sqlite_master
258#
259do_test table-5.2 {
260  catchsql {DROP TABLE IF EXISTS sqlite_master}
261} {1 {table sqlite_master may not be dropped}}
262
263# Make sure an EXPLAIN does not really create a new table
264#
265do_test table-5.3 {
266  ifcapable {explain} {
267    execsql {EXPLAIN CREATE TABLE test1(f1 int)}
268  }
269  execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
270} {}
271
272# Make sure an EXPLAIN does not really drop an existing table
273#
274do_test table-5.4 {
275  execsql {CREATE TABLE test1(f1 int)}
276  ifcapable {explain} {
277    execsql {EXPLAIN DROP TABLE test1}
278  }
279  execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
280} {test1}
281
282# Create a table with a goofy name
283#
284#do_test table-6.1 {
285#  execsql {CREATE TABLE 'Spaces In This Name!'(x int)}
286#  execsql {INSERT INTO 'spaces in this name!' VALUES(1)}
287#  set list [glob -nocomplain testdb/spaces*.tbl]
288#} {testdb/spaces+in+this+name+.tbl}
289
290# Try using keywords as table names or column names.
291#
292do_test table-7.1 {
293  set v [catch {execsql {
294    CREATE TABLE weird(
295      desc text,
296      asc text,
297      key int,
298      [14_vac] boolean,
299      fuzzy_dog_12 varchar(10),
300      begin blob,
301      end clob
302    )
303  }} msg]
304  lappend v $msg
305} {0 {}}
306do_test table-7.2 {
307  execsql {
308    INSERT INTO weird VALUES('a','b',9,0,'xyz','hi','y''all');
309    SELECT * FROM weird;
310  }
311} {a b 9 0 xyz hi y'all}
312do_test table-7.3 {
313  execsql2 {
314    SELECT * FROM weird;
315  }
316} {desc a asc b key 9 14_vac 0 fuzzy_dog_12 xyz begin hi end y'all}
317do_test table-7.3 {
318  execsql {
319    CREATE TABLE savepoint(release);
320    INSERT INTO savepoint(release) VALUES(10);
321    UPDATE savepoint SET release = 5;
322    SELECT release FROM savepoint;
323  }
324} {5}
325
326# Try out the CREATE TABLE AS syntax
327#
328do_test table-8.1 {
329  execsql2 {
330    CREATE TABLE t2 AS SELECT * FROM weird;
331    SELECT * FROM t2;
332  }
333} {desc a asc b key 9 14_vac 0 fuzzy_dog_12 xyz begin hi end y'all}
334do_test table-8.1.1 {
335  execsql {
336    SELECT sql FROM sqlite_master WHERE name='t2';
337  }
338} {{CREATE TABLE t2(
339  "desc" text,
340  "asc" text,
341  "key" int,
342  "14_vac" boolean,
343  fuzzy_dog_12 varchar(10),
344  "begin" blob,
345  "end" clob
346)}}
347do_test table-8.2 {
348  execsql {
349    CREATE TABLE "t3""xyz"(a,b,c);
350    INSERT INTO [t3"xyz] VALUES(1,2,3);
351    SELECT * FROM [t3"xyz];
352  }
353} {1 2 3}
354do_test table-8.3 {
355  execsql2 {
356    CREATE TABLE [t4"abc] AS SELECT count(*) as cnt, max(b+c) FROM [t3"xyz];
357    SELECT * FROM [t4"abc];
358  }
359} {cnt 1 max(b+c) 5}
360
361# Update for v3: The declaration type of anything except a column is now a
362# NULL pointer, so the created table has no column types. (Changed result
363# from {{CREATE TABLE 't4"abc'(cnt NUMERIC,"max(b+c)" NUMERIC)}}).
364do_test table-8.3.1 {
365  execsql {
366    SELECT sql FROM sqlite_master WHERE name='t4"abc'
367  }
368} {{CREATE TABLE "t4""abc"(cnt,"max(b+c)")}}
369
370ifcapable tempdb {
371  do_test table-8.4 {
372    execsql2 {
373      CREATE TEMPORARY TABLE t5 AS SELECT count(*) AS [y'all] FROM [t3"xyz];
374      SELECT * FROM t5;
375    }
376  } {y'all 1}
377}
378
379do_test table-8.5 {
380  db close
381  sqlite3 db test.db
382  execsql2 {
383    SELECT * FROM [t4"abc];
384  }
385} {cnt 1 max(b+c) 5}
386do_test table-8.6 {
387  execsql2 {
388    SELECT * FROM t2;
389  }
390} {desc a asc b key 9 14_vac 0 fuzzy_dog_12 xyz begin hi end y'all}
391do_test table-8.7 {
392  catchsql {
393    SELECT * FROM t5;
394  }
395} {1 {no such table: t5}}
396do_test table-8.8 {
397  catchsql {
398    CREATE TABLE t5 AS SELECT * FROM no_such_table;
399  }
400} {1 {no such table: no_such_table}}
401
402# Make sure we cannot have duplicate column names within a table.
403#
404do_test table-9.1 {
405  catchsql {
406    CREATE TABLE t6(a,b,a);
407  }
408} {1 {duplicate column name: a}}
409do_test table-9.2 {
410  catchsql {
411    CREATE TABLE t6(a varchar(100), b blob, a integer);
412  }
413} {1 {duplicate column name: a}}
414
415# Check the foreign key syntax.
416#
417ifcapable {foreignkey} {
418do_test table-10.1 {
419  catchsql {
420    CREATE TABLE t6(a REFERENCES t4(a) NOT NULL);
421    INSERT INTO t6 VALUES(NULL);
422  }
423} {1 {t6.a may not be NULL}}
424do_test table-10.2 {
425  catchsql {
426    DROP TABLE t6;
427    CREATE TABLE t6(a REFERENCES t4(a) MATCH PARTIAL);
428  }
429} {0 {}}
430do_test table-10.3 {
431  catchsql {
432    DROP TABLE t6;
433    CREATE TABLE t6(a REFERENCES t4 MATCH FULL ON DELETE SET NULL NOT NULL);
434  }
435} {0 {}}
436do_test table-10.4 {
437  catchsql {
438    DROP TABLE t6;
439    CREATE TABLE t6(a REFERENCES t4 MATCH FULL ON UPDATE SET DEFAULT DEFAULT 1);
440  }
441} {0 {}}
442do_test table-10.5 {
443  catchsql {
444    DROP TABLE t6;
445    CREATE TABLE t6(a NOT NULL NOT DEFERRABLE INITIALLY IMMEDIATE);
446  }
447} {0 {}}
448do_test table-10.6 {
449  catchsql {
450    DROP TABLE t6;
451    CREATE TABLE t6(a NOT NULL DEFERRABLE INITIALLY DEFERRED);
452  }
453} {0 {}}
454do_test table-10.7 {
455  catchsql {
456    DROP TABLE t6;
457    CREATE TABLE t6(a,
458      FOREIGN KEY (a) REFERENCES t4(b) DEFERRABLE INITIALLY DEFERRED
459    );
460  }
461} {0 {}}
462do_test table-10.8 {
463  catchsql {
464    DROP TABLE t6;
465    CREATE TABLE t6(a,b,c,
466      FOREIGN KEY (b,c) REFERENCES t4(x,y) MATCH PARTIAL
467        ON UPDATE SET NULL ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
468    );
469  }
470} {0 {}}
471do_test table-10.9 {
472  catchsql {
473    DROP TABLE t6;
474    CREATE TABLE t6(a,b,c,
475      FOREIGN KEY (b,c) REFERENCES t4(x)
476    );
477  }
478} {1 {number of columns in foreign key does not match the number of columns in the referenced table}}
479do_test table-10.10 {
480  catchsql {DROP TABLE t6}
481  catchsql {
482    CREATE TABLE t6(a,b,c,
483      FOREIGN KEY (b,c) REFERENCES t4(x,y,z)
484    );
485  }
486} {1 {number of columns in foreign key does not match the number of columns in the referenced table}}
487do_test table-10.11 {
488  catchsql {DROP TABLE t6}
489  catchsql {
490    CREATE TABLE t6(a,b, c REFERENCES t4(x,y));
491  }
492} {1 {foreign key on c should reference only one column of table t4}}
493do_test table-10.12 {
494  catchsql {DROP TABLE t6}
495  catchsql {
496    CREATE TABLE t6(a,b,c,
497      FOREIGN KEY (b,x) REFERENCES t4(x,y)
498    );
499  }
500} {1 {unknown column "x" in foreign key definition}}
501do_test table-10.13 {
502  catchsql {DROP TABLE t6}
503  catchsql {
504    CREATE TABLE t6(a,b,c,
505      FOREIGN KEY (x,b) REFERENCES t4(x,y)
506    );
507  }
508} {1 {unknown column "x" in foreign key definition}}
509} ;# endif foreignkey
510
511# Test for the "typeof" function. More tests for the
512# typeof() function are found in bind.test and types.test.
513#
514do_test table-11.1 {
515  execsql {
516    CREATE TABLE t7(
517       a integer primary key,
518       b number(5,10),
519       c character varying (8),
520       d VARCHAR(9),
521       e clob,
522       f BLOB,
523       g Text,
524       h
525    );
526    INSERT INTO t7(a) VALUES(1);
527    SELECT typeof(a), typeof(b), typeof(c), typeof(d),
528           typeof(e), typeof(f), typeof(g), typeof(h)
529    FROM t7 LIMIT 1;
530  }
531} {integer null null null null null null null}
532do_test table-11.2 {
533  execsql {
534    SELECT typeof(a+b), typeof(a||b), typeof(c+d), typeof(c||d)
535    FROM t7 LIMIT 1;
536  }
537} {null null null null}
538
539# Test that when creating a table using CREATE TABLE AS, column types are
540# assigned correctly for (SELECT ...) and 'x AS y' expressions.
541do_test table-12.1 {
542  ifcapable subquery {
543    execsql {
544      CREATE TABLE t8 AS SELECT b, h, a as i, (SELECT f FROM t7) as j FROM t7;
545    }
546  } else {
547    execsql {
548      CREATE TABLE t8 AS SELECT b, h, a as i, f as j FROM t7;
549    }
550  }
551} {}
552do_test table-12.2 {
553  execsql {
554    SELECT sql FROM sqlite_master WHERE tbl_name = 't8'
555  }
556} {{CREATE TABLE t8(b number(5,10),h,i integer,j BLOB)}}
557
558#--------------------------------------------------------------------
559# Test cases table-13.*
560#
561# Test the ability to have default values of CURRENT_TIME, CURRENT_DATE
562# and CURRENT_TIMESTAMP.
563#
564do_test table-13.1 {
565  execsql {
566    CREATE TABLE tablet8(
567       a integer primary key,
568       tm text DEFAULT CURRENT_TIME,
569       dt text DEFAULT CURRENT_DATE,
570       dttm text DEFAULT CURRENT_TIMESTAMP
571    );
572    SELECT * FROM tablet8;
573  }
574} {}
575set i 0
576foreach {date time seconds} {
577  1976-07-04 12:00:00 205329600
578  1994-04-16 14:00:00 766504800
579  2000-01-01 00:00:00 946684800
580  2003-12-31 12:34:56 1072874096
581} {
582  incr i
583  set sqlite_current_time $seconds
584  do_test table-13.2.$i {
585    execsql "
586      INSERT INTO tablet8(a) VALUES($i);
587      SELECT tm, dt, dttm FROM tablet8 WHERE a=$i;
588    "
589  } [list $time $date [list $date $time]]
590}
591set sqlite_current_time 0
592
593#--------------------------------------------------------------------
594# Test cases table-14.*
595#
596# Test that a table cannot be created or dropped while other virtual
597# machines are active. This is required because otherwise when in
598# auto-vacuum mode the btree-layer may need to move the root-pages of
599# a table for which there is an open cursor.
600#
601# 2007-05-02:  A open btree cursor no longer blocks CREATE TABLE.
602# But DROP TABLE is still prohibited because we do not want to
603# delete a table out from under a running query.
604#
605
606# db eval {
607#   pragma vdbe_trace = 0;
608# }
609# Try to create a table from within a callback:
610unset -nocomplain result
611do_test table-14.1 {
612  set rc [
613    catch {
614      db eval {SELECT * FROM tablet8 LIMIT 1} {} {
615        db eval {CREATE TABLE t9(a, b, c)}
616      }
617    } msg
618  ]
619  set result [list $rc $msg]
620} {0 {}}
621
622# Try to drop a table from within a callback:
623do_test table-14.2 {
624  set rc [
625    catch {
626      db eval {SELECT * FROM tablet8 LIMIT 1} {} {
627        db eval {DROP TABLE t9;}
628      }
629    } msg
630  ]
631  set result [list $rc $msg]
632} {1 {database table is locked}}
633
634ifcapable attach {
635  # Now attach a database and ensure that a table can be created in the
636  # attached database whilst in a callback from a query on the main database.
637  do_test table-14.3 {
638    file delete -force test2.db
639    file delete -force test2.db-journal
640    execsql {
641      ATTACH 'test2.db' as aux;
642    }
643    db eval {SELECT * FROM tablet8 LIMIT 1} {} {
644      db eval {CREATE TABLE aux.t1(a, b, c)}
645    }
646  } {}
647
648  # On the other hand, it should be impossible to drop a table when any VMs
649  # are active. This is because VerifyCookie instructions may have already
650  # been executed, and btree root-pages may not move after this (which a
651  # delete table might do).
652  do_test table-14.4 {
653    set rc [
654      catch {
655        db eval {SELECT * FROM tablet8 LIMIT 1} {} {
656          db eval {DROP TABLE aux.t1;}
657        }
658      } msg
659    ]
660    set result [list $rc $msg]
661  } {1 {database table is locked}}
662}
663
664# Create and drop 2000 tables. This is to check that the balance_shallow()
665# routine works correctly on the sqlite_master table. At one point it
666# contained a bug that would prevent the right-child pointer of the
667# child page from being copied to the root page.
668#
669do_test table-15.1 {
670  execsql {BEGIN}
671  for {set i 0} {$i<2000} {incr i} {
672    execsql "CREATE TABLE tbl$i (a, b, c)"
673  }
674  execsql {COMMIT}
675} {}
676do_test table-15.2 {
677  execsql {BEGIN}
678  for {set i 0} {$i<2000} {incr i} {
679    execsql "DROP TABLE tbl$i"
680  }
681  execsql {COMMIT}
682} {}
683
684finish_test
685