1*c5c4113dSnw141292 2*c5c4113dSnw141292#pragma ident "%Z%%M% %I% %E% SMI" 3*c5c4113dSnw141292 4*c5c4113dSnw141292# 2001 September 15 5*c5c4113dSnw141292# 6*c5c4113dSnw141292# The author disclaims copyright to this source code. In place of 7*c5c4113dSnw141292# a legal notice, here is a blessing: 8*c5c4113dSnw141292# 9*c5c4113dSnw141292# May you do good and not evil. 10*c5c4113dSnw141292# May you find forgiveness for yourself and forgive others. 11*c5c4113dSnw141292# May you share freely, never taking more than you give. 12*c5c4113dSnw141292# 13*c5c4113dSnw141292#*********************************************************************** 14*c5c4113dSnw141292# This file implements regression tests for SQLite library. The 15*c5c4113dSnw141292# focus of this file is testing the CREATE TABLE statement. 16*c5c4113dSnw141292# 17*c5c4113dSnw141292# $Id: table.test,v 1.22 2003/01/29 18:46:54 drh Exp $ 18*c5c4113dSnw141292 19*c5c4113dSnw141292set testdir [file dirname $argv0] 20*c5c4113dSnw141292source $testdir/tester.tcl 21*c5c4113dSnw141292 22*c5c4113dSnw141292# Create a basic table and verify it is added to sqlite_master 23*c5c4113dSnw141292# 24*c5c4113dSnw141292do_test table-1.1 { 25*c5c4113dSnw141292 execsql { 26*c5c4113dSnw141292 CREATE TABLE test1 ( 27*c5c4113dSnw141292 one varchar(10), 28*c5c4113dSnw141292 two text 29*c5c4113dSnw141292 ) 30*c5c4113dSnw141292 } 31*c5c4113dSnw141292 execsql { 32*c5c4113dSnw141292 SELECT sql FROM sqlite_master WHERE type!='meta' 33*c5c4113dSnw141292 } 34*c5c4113dSnw141292} {{CREATE TABLE test1 ( 35*c5c4113dSnw141292 one varchar(10), 36*c5c4113dSnw141292 two text 37*c5c4113dSnw141292 )}} 38*c5c4113dSnw141292 39*c5c4113dSnw141292 40*c5c4113dSnw141292# Verify the other fields of the sqlite_master file. 41*c5c4113dSnw141292# 42*c5c4113dSnw141292do_test table-1.3 { 43*c5c4113dSnw141292 execsql {SELECT name, tbl_name, type FROM sqlite_master WHERE type!='meta'} 44*c5c4113dSnw141292} {test1 test1 table} 45*c5c4113dSnw141292 46*c5c4113dSnw141292# Close and reopen the database. Verify that everything is 47*c5c4113dSnw141292# still the same. 48*c5c4113dSnw141292# 49*c5c4113dSnw141292do_test table-1.4 { 50*c5c4113dSnw141292 db close 51*c5c4113dSnw141292 sqlite db test.db 52*c5c4113dSnw141292 execsql {SELECT name, tbl_name, type from sqlite_master WHERE type!='meta'} 53*c5c4113dSnw141292} {test1 test1 table} 54*c5c4113dSnw141292 55*c5c4113dSnw141292# Drop the database and make sure it disappears. 56*c5c4113dSnw141292# 57*c5c4113dSnw141292do_test table-1.5 { 58*c5c4113dSnw141292 execsql {DROP TABLE test1} 59*c5c4113dSnw141292 execsql {SELECT * FROM sqlite_master WHERE type!='meta'} 60*c5c4113dSnw141292} {} 61*c5c4113dSnw141292 62*c5c4113dSnw141292# Close and reopen the database. Verify that the table is 63*c5c4113dSnw141292# still gone. 64*c5c4113dSnw141292# 65*c5c4113dSnw141292do_test table-1.6 { 66*c5c4113dSnw141292 db close 67*c5c4113dSnw141292 sqlite db test.db 68*c5c4113dSnw141292 execsql {SELECT name FROM sqlite_master WHERE type!='meta'} 69*c5c4113dSnw141292} {} 70*c5c4113dSnw141292 71*c5c4113dSnw141292# Repeat the above steps, but this time quote the table name. 72*c5c4113dSnw141292# 73*c5c4113dSnw141292do_test table-1.10 { 74*c5c4113dSnw141292 execsql {CREATE TABLE "create" (f1 int)} 75*c5c4113dSnw141292 execsql {SELECT name FROM sqlite_master WHERE type!='meta'} 76*c5c4113dSnw141292} {create} 77*c5c4113dSnw141292do_test table-1.11 { 78*c5c4113dSnw141292 execsql {DROP TABLE "create"} 79*c5c4113dSnw141292 execsql {SELECT name FROM "sqlite_master" WHERE type!='meta'} 80*c5c4113dSnw141292} {} 81*c5c4113dSnw141292do_test table-1.12 { 82*c5c4113dSnw141292 execsql {CREATE TABLE test1("f1 ho" int)} 83*c5c4113dSnw141292 execsql {SELECT name as "X" FROM sqlite_master WHERE type!='meta'} 84*c5c4113dSnw141292} {test1} 85*c5c4113dSnw141292do_test table-1.13 { 86*c5c4113dSnw141292 execsql {DROP TABLE "TEST1"} 87*c5c4113dSnw141292 execsql {SELECT name FROM "sqlite_master" WHERE type!='meta'} 88*c5c4113dSnw141292} {} 89*c5c4113dSnw141292 90*c5c4113dSnw141292 91*c5c4113dSnw141292 92*c5c4113dSnw141292# Verify that we cannot make two tables with the same name 93*c5c4113dSnw141292# 94*c5c4113dSnw141292do_test table-2.1 { 95*c5c4113dSnw141292 execsql {CREATE TABLE TEST2(one text)} 96*c5c4113dSnw141292 set v [catch {execsql {CREATE TABLE test2(two text)}} msg] 97*c5c4113dSnw141292 lappend v $msg 98*c5c4113dSnw141292} {1 {table test2 already exists}} 99*c5c4113dSnw141292do_test table-2.1b { 100*c5c4113dSnw141292 set v [catch {execsql {CREATE TABLE sqlite_master(two text)}} msg] 101*c5c4113dSnw141292 lappend v $msg 102*c5c4113dSnw141292} {1 {table sqlite_master already exists}} 103*c5c4113dSnw141292do_test table-2.1c { 104*c5c4113dSnw141292 db close 105*c5c4113dSnw141292 sqlite db test.db 106*c5c4113dSnw141292 set v [catch {execsql {CREATE TABLE sqlite_master(two text)}} msg] 107*c5c4113dSnw141292 lappend v $msg 108*c5c4113dSnw141292} {1 {table sqlite_master already exists}} 109*c5c4113dSnw141292do_test table-2.1d { 110*c5c4113dSnw141292 execsql {DROP TABLE test2; SELECT name FROM sqlite_master WHERE type!='meta'} 111*c5c4113dSnw141292} {} 112*c5c4113dSnw141292 113*c5c4113dSnw141292# Verify that we cannot make a table with the same name as an index 114*c5c4113dSnw141292# 115*c5c4113dSnw141292do_test table-2.2a { 116*c5c4113dSnw141292 execsql {CREATE TABLE test2(one text); CREATE INDEX test3 ON test2(one)} 117*c5c4113dSnw141292 set v [catch {execsql {CREATE TABLE test3(two text)}} msg] 118*c5c4113dSnw141292 lappend v $msg 119*c5c4113dSnw141292} {1 {there is already an index named test3}} 120*c5c4113dSnw141292do_test table-2.2b { 121*c5c4113dSnw141292 db close 122*c5c4113dSnw141292 sqlite db test.db 123*c5c4113dSnw141292 set v [catch {execsql {CREATE TABLE test3(two text)}} msg] 124*c5c4113dSnw141292 lappend v $msg 125*c5c4113dSnw141292} {1 {there is already an index named test3}} 126*c5c4113dSnw141292do_test table-2.2c { 127*c5c4113dSnw141292 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name} 128*c5c4113dSnw141292} {test2 test3} 129*c5c4113dSnw141292do_test table-2.2d { 130*c5c4113dSnw141292 execsql {DROP INDEX test3} 131*c5c4113dSnw141292 set v [catch {execsql {CREATE TABLE test3(two text)}} msg] 132*c5c4113dSnw141292 lappend v $msg 133*c5c4113dSnw141292} {0 {}} 134*c5c4113dSnw141292do_test table-2.2e { 135*c5c4113dSnw141292 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name} 136*c5c4113dSnw141292} {test2 test3} 137*c5c4113dSnw141292do_test table-2.2f { 138*c5c4113dSnw141292 execsql {DROP TABLE test2; DROP TABLE test3} 139*c5c4113dSnw141292 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name} 140*c5c4113dSnw141292} {} 141*c5c4113dSnw141292 142*c5c4113dSnw141292# Create a table with many field names 143*c5c4113dSnw141292# 144*c5c4113dSnw141292set big_table \ 145*c5c4113dSnw141292{CREATE TABLE big( 146*c5c4113dSnw141292 f1 varchar(20), 147*c5c4113dSnw141292 f2 char(10), 148*c5c4113dSnw141292 f3 varchar(30) primary key, 149*c5c4113dSnw141292 f4 text, 150*c5c4113dSnw141292 f5 text, 151*c5c4113dSnw141292 f6 text, 152*c5c4113dSnw141292 f7 text, 153*c5c4113dSnw141292 f8 text, 154*c5c4113dSnw141292 f9 text, 155*c5c4113dSnw141292 f10 text, 156*c5c4113dSnw141292 f11 text, 157*c5c4113dSnw141292 f12 text, 158*c5c4113dSnw141292 f13 text, 159*c5c4113dSnw141292 f14 text, 160*c5c4113dSnw141292 f15 text, 161*c5c4113dSnw141292 f16 text, 162*c5c4113dSnw141292 f17 text, 163*c5c4113dSnw141292 f18 text, 164*c5c4113dSnw141292 f19 text, 165*c5c4113dSnw141292 f20 text 166*c5c4113dSnw141292)} 167*c5c4113dSnw141292do_test table-3.1 { 168*c5c4113dSnw141292 execsql $big_table 169*c5c4113dSnw141292 execsql {SELECT sql FROM sqlite_master WHERE type=='table'} 170*c5c4113dSnw141292} \{$big_table\} 171*c5c4113dSnw141292do_test table-3.2 { 172*c5c4113dSnw141292 set v [catch {execsql {CREATE TABLE BIG(xyz foo)}} msg] 173*c5c4113dSnw141292 lappend v $msg 174*c5c4113dSnw141292} {1 {table BIG already exists}} 175*c5c4113dSnw141292do_test table-3.3 { 176*c5c4113dSnw141292 set v [catch {execsql {CREATE TABLE biG(xyz foo)}} msg] 177*c5c4113dSnw141292 lappend v $msg 178*c5c4113dSnw141292} {1 {table biG already exists}} 179*c5c4113dSnw141292do_test table-3.4 { 180*c5c4113dSnw141292 set v [catch {execsql {CREATE TABLE bIg(xyz foo)}} msg] 181*c5c4113dSnw141292 lappend v $msg 182*c5c4113dSnw141292} {1 {table bIg already exists}} 183*c5c4113dSnw141292do_test table-3.5 { 184*c5c4113dSnw141292 db close 185*c5c4113dSnw141292 sqlite db test.db 186*c5c4113dSnw141292 set v [catch {execsql {CREATE TABLE Big(xyz foo)}} msg] 187*c5c4113dSnw141292 lappend v $msg 188*c5c4113dSnw141292} {1 {table Big already exists}} 189*c5c4113dSnw141292do_test table-3.6 { 190*c5c4113dSnw141292 execsql {DROP TABLE big} 191*c5c4113dSnw141292 execsql {SELECT name FROM sqlite_master WHERE type!='meta'} 192*c5c4113dSnw141292} {} 193*c5c4113dSnw141292 194*c5c4113dSnw141292# Try creating large numbers of tables 195*c5c4113dSnw141292# 196*c5c4113dSnw141292set r {} 197*c5c4113dSnw141292for {set i 1} {$i<=100} {incr i} { 198*c5c4113dSnw141292 lappend r [format test%03d $i] 199*c5c4113dSnw141292} 200*c5c4113dSnw141292do_test table-4.1 { 201*c5c4113dSnw141292 for {set i 1} {$i<=100} {incr i} { 202*c5c4113dSnw141292 set sql "CREATE TABLE [format test%03d $i] (" 203*c5c4113dSnw141292 for {set k 1} {$k<$i} {incr k} { 204*c5c4113dSnw141292 append sql "field$k text," 205*c5c4113dSnw141292 } 206*c5c4113dSnw141292 append sql "last_field text)" 207*c5c4113dSnw141292 execsql $sql 208*c5c4113dSnw141292 } 209*c5c4113dSnw141292 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name} 210*c5c4113dSnw141292} $r 211*c5c4113dSnw141292do_test table-4.1b { 212*c5c4113dSnw141292 db close 213*c5c4113dSnw141292 sqlite db test.db 214*c5c4113dSnw141292 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name} 215*c5c4113dSnw141292} $r 216*c5c4113dSnw141292 217*c5c4113dSnw141292# Drop the even numbered tables 218*c5c4113dSnw141292# 219*c5c4113dSnw141292set r {} 220*c5c4113dSnw141292for {set i 1} {$i<=100} {incr i 2} { 221*c5c4113dSnw141292 lappend r [format test%03d $i] 222*c5c4113dSnw141292} 223*c5c4113dSnw141292do_test table-4.2 { 224*c5c4113dSnw141292 for {set i 2} {$i<=100} {incr i 2} { 225*c5c4113dSnw141292 # if {$i==38} {execsql {pragma vdbe_trace=on}} 226*c5c4113dSnw141292 set sql "DROP TABLE [format TEST%03d $i]" 227*c5c4113dSnw141292 execsql $sql 228*c5c4113dSnw141292 } 229*c5c4113dSnw141292 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name} 230*c5c4113dSnw141292} $r 231*c5c4113dSnw141292#exit 232*c5c4113dSnw141292 233*c5c4113dSnw141292# Drop the odd number tables 234*c5c4113dSnw141292# 235*c5c4113dSnw141292do_test table-4.3 { 236*c5c4113dSnw141292 for {set i 1} {$i<=100} {incr i 2} { 237*c5c4113dSnw141292 set sql "DROP TABLE [format test%03d $i]" 238*c5c4113dSnw141292 execsql $sql 239*c5c4113dSnw141292 } 240*c5c4113dSnw141292 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name} 241*c5c4113dSnw141292} {} 242*c5c4113dSnw141292 243*c5c4113dSnw141292# Try to drop a table that does not exist 244*c5c4113dSnw141292# 245*c5c4113dSnw141292do_test table-5.1 { 246*c5c4113dSnw141292 set v [catch {execsql {DROP TABLE test009}} msg] 247*c5c4113dSnw141292 lappend v $msg 248*c5c4113dSnw141292} {1 {no such table: test009}} 249*c5c4113dSnw141292 250*c5c4113dSnw141292# Try to drop sqlite_master 251*c5c4113dSnw141292# 252*c5c4113dSnw141292do_test table-5.2 { 253*c5c4113dSnw141292 set v [catch {execsql {DROP TABLE sqlite_master}} msg] 254*c5c4113dSnw141292 lappend v $msg 255*c5c4113dSnw141292} {1 {table sqlite_master may not be dropped}} 256*c5c4113dSnw141292 257*c5c4113dSnw141292# Make sure an EXPLAIN does not really create a new table 258*c5c4113dSnw141292# 259*c5c4113dSnw141292do_test table-5.3 { 260*c5c4113dSnw141292 execsql {EXPLAIN CREATE TABLE test1(f1 int)} 261*c5c4113dSnw141292 execsql {SELECT name FROM sqlite_master WHERE type!='meta'} 262*c5c4113dSnw141292} {} 263*c5c4113dSnw141292 264*c5c4113dSnw141292# Make sure an EXPLAIN does not really drop an existing table 265*c5c4113dSnw141292# 266*c5c4113dSnw141292do_test table-5.4 { 267*c5c4113dSnw141292 execsql {CREATE TABLE test1(f1 int)} 268*c5c4113dSnw141292 execsql {EXPLAIN DROP TABLE test1} 269*c5c4113dSnw141292 execsql {SELECT name FROM sqlite_master WHERE type!='meta'} 270*c5c4113dSnw141292} {test1} 271*c5c4113dSnw141292 272*c5c4113dSnw141292# Create a table with a goofy name 273*c5c4113dSnw141292# 274*c5c4113dSnw141292#do_test table-6.1 { 275*c5c4113dSnw141292# execsql {CREATE TABLE 'Spaces In This Name!'(x int)} 276*c5c4113dSnw141292# execsql {INSERT INTO 'spaces in this name!' VALUES(1)} 277*c5c4113dSnw141292# set list [glob -nocomplain testdb/spaces*.tbl] 278*c5c4113dSnw141292#} {testdb/spaces+in+this+name+.tbl} 279*c5c4113dSnw141292 280*c5c4113dSnw141292# Try using keywords as table names or column names. 281*c5c4113dSnw141292# 282*c5c4113dSnw141292do_test table-7.1 { 283*c5c4113dSnw141292 set v [catch {execsql { 284*c5c4113dSnw141292 CREATE TABLE weird( 285*c5c4113dSnw141292 desc text, 286*c5c4113dSnw141292 asc text, 287*c5c4113dSnw141292 explain int, 288*c5c4113dSnw141292 [14_vac] boolean, 289*c5c4113dSnw141292 fuzzy_dog_12 varchar(10), 290*c5c4113dSnw141292 begin blob, 291*c5c4113dSnw141292 end clob 292*c5c4113dSnw141292 ) 293*c5c4113dSnw141292 }} msg] 294*c5c4113dSnw141292 lappend v $msg 295*c5c4113dSnw141292} {0 {}} 296*c5c4113dSnw141292do_test table-7.2 { 297*c5c4113dSnw141292 execsql { 298*c5c4113dSnw141292 INSERT INTO weird VALUES('a','b',9,0,'xyz','hi','y''all'); 299*c5c4113dSnw141292 SELECT * FROM weird; 300*c5c4113dSnw141292 } 301*c5c4113dSnw141292} {a b 9 0 xyz hi y'all} 302*c5c4113dSnw141292do_test table-7.3 { 303*c5c4113dSnw141292 execsql2 { 304*c5c4113dSnw141292 SELECT * FROM weird; 305*c5c4113dSnw141292 } 306*c5c4113dSnw141292} {desc a asc b explain 9 14_vac 0 fuzzy_dog_12 xyz begin hi end y'all} 307*c5c4113dSnw141292 308*c5c4113dSnw141292# Try out the CREATE TABLE AS syntax 309*c5c4113dSnw141292# 310*c5c4113dSnw141292do_test table-8.1 { 311*c5c4113dSnw141292 execsql2 { 312*c5c4113dSnw141292 CREATE TABLE t2 AS SELECT * FROM weird; 313*c5c4113dSnw141292 SELECT * FROM t2; 314*c5c4113dSnw141292 } 315*c5c4113dSnw141292} {desc a asc b explain 9 14_vac 0 fuzzy_dog_12 xyz begin hi end y'all} 316*c5c4113dSnw141292do_test table-8.1.1 { 317*c5c4113dSnw141292 execsql { 318*c5c4113dSnw141292 SELECT sql FROM sqlite_master WHERE name='t2'; 319*c5c4113dSnw141292 } 320*c5c4113dSnw141292} {{CREATE TABLE t2( 321*c5c4113dSnw141292 'desc', 322*c5c4113dSnw141292 'asc', 323*c5c4113dSnw141292 'explain', 324*c5c4113dSnw141292 '14_vac', 325*c5c4113dSnw141292 fuzzy_dog_12, 326*c5c4113dSnw141292 'begin', 327*c5c4113dSnw141292 'end' 328*c5c4113dSnw141292)}} 329*c5c4113dSnw141292do_test table-8.2 { 330*c5c4113dSnw141292 execsql { 331*c5c4113dSnw141292 CREATE TABLE 't3''xyz'(a,b,c); 332*c5c4113dSnw141292 INSERT INTO [t3'xyz] VALUES(1,2,3); 333*c5c4113dSnw141292 SELECT * FROM [t3'xyz]; 334*c5c4113dSnw141292 } 335*c5c4113dSnw141292} {1 2 3} 336*c5c4113dSnw141292do_test table-8.3 { 337*c5c4113dSnw141292 execsql2 { 338*c5c4113dSnw141292 CREATE TABLE [t4'abc] AS SELECT count(*) as cnt, max(b+c) FROM [t3'xyz]; 339*c5c4113dSnw141292 SELECT * FROM [t4'abc]; 340*c5c4113dSnw141292 } 341*c5c4113dSnw141292} {cnt 1 max(b+c) 5} 342*c5c4113dSnw141292do_test table-8.3.1 { 343*c5c4113dSnw141292 execsql { 344*c5c4113dSnw141292 SELECT sql FROM sqlite_master WHERE name='t4''abc' 345*c5c4113dSnw141292 } 346*c5c4113dSnw141292} {{CREATE TABLE 't4''abc'(cnt,'max(b+c)')}} 347*c5c4113dSnw141292do_test table-8.4 { 348*c5c4113dSnw141292 execsql2 { 349*c5c4113dSnw141292 CREATE TEMPORARY TABLE t5 AS SELECT count(*) AS [y'all] FROM [t3'xyz]; 350*c5c4113dSnw141292 SELECT * FROM t5; 351*c5c4113dSnw141292 } 352*c5c4113dSnw141292} {y'all 1} 353*c5c4113dSnw141292do_test table-8.5 { 354*c5c4113dSnw141292 db close 355*c5c4113dSnw141292 sqlite db test.db 356*c5c4113dSnw141292 execsql2 { 357*c5c4113dSnw141292 SELECT * FROM [t4'abc]; 358*c5c4113dSnw141292 } 359*c5c4113dSnw141292} {cnt 1 max(b+c) 5} 360*c5c4113dSnw141292do_test table-8.6 { 361*c5c4113dSnw141292 execsql2 { 362*c5c4113dSnw141292 SELECT * FROM t2; 363*c5c4113dSnw141292 } 364*c5c4113dSnw141292} {desc a asc b explain 9 14_vac 0 fuzzy_dog_12 xyz begin hi end y'all} 365*c5c4113dSnw141292do_test table-8.7 { 366*c5c4113dSnw141292 catchsql { 367*c5c4113dSnw141292 SELECT * FROM t5; 368*c5c4113dSnw141292 } 369*c5c4113dSnw141292} {1 {no such table: t5}} 370*c5c4113dSnw141292do_test table-8.8 { 371*c5c4113dSnw141292 catchsql { 372*c5c4113dSnw141292 CREATE TABLE t5 AS SELECT * FROM no_such_table; 373*c5c4113dSnw141292 } 374*c5c4113dSnw141292} {1 {no such table: no_such_table}} 375*c5c4113dSnw141292 376*c5c4113dSnw141292# Make sure we cannot have duplicate column names within a table. 377*c5c4113dSnw141292# 378*c5c4113dSnw141292do_test table-9.1 { 379*c5c4113dSnw141292 catchsql { 380*c5c4113dSnw141292 CREATE TABLE t6(a,b,a); 381*c5c4113dSnw141292 } 382*c5c4113dSnw141292} {1 {duplicate column name: a}} 383*c5c4113dSnw141292 384*c5c4113dSnw141292# Check the foreign key syntax. 385*c5c4113dSnw141292# 386*c5c4113dSnw141292do_test table-10.1 { 387*c5c4113dSnw141292 catchsql { 388*c5c4113dSnw141292 CREATE TABLE t6(a REFERENCES t4(a) NOT NULL); 389*c5c4113dSnw141292 INSERT INTO t6 VALUES(NULL); 390*c5c4113dSnw141292 } 391*c5c4113dSnw141292} {1 {t6.a may not be NULL}} 392*c5c4113dSnw141292do_test table-10.2 { 393*c5c4113dSnw141292 catchsql { 394*c5c4113dSnw141292 DROP TABLE t6; 395*c5c4113dSnw141292 CREATE TABLE t6(a REFERENCES t4(a) MATCH PARTIAL); 396*c5c4113dSnw141292 } 397*c5c4113dSnw141292} {0 {}} 398*c5c4113dSnw141292do_test table-10.3 { 399*c5c4113dSnw141292 catchsql { 400*c5c4113dSnw141292 DROP TABLE t6; 401*c5c4113dSnw141292 CREATE TABLE t6(a REFERENCES t4 MATCH FULL ON DELETE SET NULL NOT NULL); 402*c5c4113dSnw141292 } 403*c5c4113dSnw141292} {0 {}} 404*c5c4113dSnw141292do_test table-10.4 { 405*c5c4113dSnw141292 catchsql { 406*c5c4113dSnw141292 DROP TABLE t6; 407*c5c4113dSnw141292 CREATE TABLE t6(a REFERENCES t4 MATCH FULL ON UPDATE SET DEFAULT DEFAULT 1); 408*c5c4113dSnw141292 } 409*c5c4113dSnw141292} {0 {}} 410*c5c4113dSnw141292do_test table-10.5 { 411*c5c4113dSnw141292 catchsql { 412*c5c4113dSnw141292 DROP TABLE t6; 413*c5c4113dSnw141292 CREATE TABLE t6(a NOT NULL NOT DEFERRABLE INITIALLY IMMEDIATE); 414*c5c4113dSnw141292 } 415*c5c4113dSnw141292} {0 {}} 416*c5c4113dSnw141292do_test table-10.6 { 417*c5c4113dSnw141292 catchsql { 418*c5c4113dSnw141292 DROP TABLE t6; 419*c5c4113dSnw141292 CREATE TABLE t6(a NOT NULL DEFERRABLE INITIALLY DEFERRED); 420*c5c4113dSnw141292 } 421*c5c4113dSnw141292} {0 {}} 422*c5c4113dSnw141292do_test table-10.7 { 423*c5c4113dSnw141292 catchsql { 424*c5c4113dSnw141292 DROP TABLE t6; 425*c5c4113dSnw141292 CREATE TABLE t6(a, 426*c5c4113dSnw141292 FOREIGN KEY (a) REFERENCES t4(b) DEFERRABLE INITIALLY DEFERRED 427*c5c4113dSnw141292 ); 428*c5c4113dSnw141292 } 429*c5c4113dSnw141292} {0 {}} 430*c5c4113dSnw141292do_test table-10.8 { 431*c5c4113dSnw141292 catchsql { 432*c5c4113dSnw141292 DROP TABLE t6; 433*c5c4113dSnw141292 CREATE TABLE t6(a,b,c, 434*c5c4113dSnw141292 FOREIGN KEY (b,c) REFERENCES t4(x,y) MATCH PARTIAL 435*c5c4113dSnw141292 ON UPDATE SET NULL ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED 436*c5c4113dSnw141292 ); 437*c5c4113dSnw141292 } 438*c5c4113dSnw141292} {0 {}} 439*c5c4113dSnw141292do_test table-10.9 { 440*c5c4113dSnw141292 catchsql { 441*c5c4113dSnw141292 DROP TABLE t6; 442*c5c4113dSnw141292 CREATE TABLE t6(a,b,c, 443*c5c4113dSnw141292 FOREIGN KEY (b,c) REFERENCES t4(x) 444*c5c4113dSnw141292 ); 445*c5c4113dSnw141292 } 446*c5c4113dSnw141292} {1 {number of columns in foreign key does not match the number of columns in the referenced table}} 447*c5c4113dSnw141292do_test table-10.10 { 448*c5c4113dSnw141292 catchsql {DROP TABLE t6} 449*c5c4113dSnw141292 catchsql { 450*c5c4113dSnw141292 CREATE TABLE t6(a,b,c, 451*c5c4113dSnw141292 FOREIGN KEY (b,c) REFERENCES t4(x,y,z) 452*c5c4113dSnw141292 ); 453*c5c4113dSnw141292 } 454*c5c4113dSnw141292} {1 {number of columns in foreign key does not match the number of columns in the referenced table}} 455*c5c4113dSnw141292do_test table-10.11 { 456*c5c4113dSnw141292 catchsql {DROP TABLE t6} 457*c5c4113dSnw141292 catchsql { 458*c5c4113dSnw141292 CREATE TABLE t6(a,b, c REFERENCES t4(x,y)); 459*c5c4113dSnw141292 } 460*c5c4113dSnw141292} {1 {foreign key on c should reference only one column of table t4}} 461*c5c4113dSnw141292do_test table-10.12 { 462*c5c4113dSnw141292 catchsql {DROP TABLE t6} 463*c5c4113dSnw141292 catchsql { 464*c5c4113dSnw141292 CREATE TABLE t6(a,b,c, 465*c5c4113dSnw141292 FOREIGN KEY (b,x) REFERENCES t4(x,y) 466*c5c4113dSnw141292 ); 467*c5c4113dSnw141292 } 468*c5c4113dSnw141292} {1 {unknown column "x" in foreign key definition}} 469*c5c4113dSnw141292do_test table-10.13 { 470*c5c4113dSnw141292 catchsql {DROP TABLE t6} 471*c5c4113dSnw141292 catchsql { 472*c5c4113dSnw141292 CREATE TABLE t6(a,b,c, 473*c5c4113dSnw141292 FOREIGN KEY (x,b) REFERENCES t4(x,y) 474*c5c4113dSnw141292 ); 475*c5c4113dSnw141292 } 476*c5c4113dSnw141292} {1 {unknown column "x" in foreign key definition}} 477*c5c4113dSnw141292 478*c5c4113dSnw141292 479*c5c4113dSnw141292# Test for the "typeof" function. 480*c5c4113dSnw141292# 481*c5c4113dSnw141292do_test table-11.1 { 482*c5c4113dSnw141292 execsql { 483*c5c4113dSnw141292 CREATE TABLE t7( 484*c5c4113dSnw141292 a integer primary key, 485*c5c4113dSnw141292 b number(5,10), 486*c5c4113dSnw141292 c character varying (8), 487*c5c4113dSnw141292 d VARCHAR(9), 488*c5c4113dSnw141292 e clob, 489*c5c4113dSnw141292 f BLOB, 490*c5c4113dSnw141292 g Text, 491*c5c4113dSnw141292 h 492*c5c4113dSnw141292 ); 493*c5c4113dSnw141292 INSERT INTO t7(a) VALUES(1); 494*c5c4113dSnw141292 SELECT typeof(a), typeof(b), typeof(c), typeof(d), 495*c5c4113dSnw141292 typeof(e), typeof(f), typeof(g), typeof(h) 496*c5c4113dSnw141292 FROM t7 LIMIT 1; 497*c5c4113dSnw141292 } 498*c5c4113dSnw141292} {numeric numeric text text text text text numeric} 499*c5c4113dSnw141292do_test table-11.2 { 500*c5c4113dSnw141292 execsql { 501*c5c4113dSnw141292 SELECT typeof(a+b), typeof(a||b), typeof(c+d), typeof(c||d) 502*c5c4113dSnw141292 FROM t7 LIMIT 1; 503*c5c4113dSnw141292 } 504*c5c4113dSnw141292} {numeric text numeric text} 505*c5c4113dSnw141292 506*c5c4113dSnw141292finish_test 507