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