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