table.test 10 KB

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