insert.test 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 INSERT statement.
  13. #
  14. # $Id: insert.test,v 1.31 2007/04/05 11:25:59 drh Exp $
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. # Try to insert into a non-existant table.
  18. #
  19. do_test insert-1.1 {
  20. set v [catch {execsql {INSERT INTO test1 VALUES(1,2,3)}} msg]
  21. lappend v $msg
  22. } {1 {no such table: test1}}
  23. # Try to insert into sqlite_master
  24. #
  25. do_test insert-1.2 {
  26. set v [catch {execsql {INSERT INTO sqlite_master VALUES(1,2,3,4)}} msg]
  27. lappend v $msg
  28. } {1 {table sqlite_master may not be modified}}
  29. # Try to insert the wrong number of entries.
  30. #
  31. do_test insert-1.3 {
  32. execsql {CREATE TABLE test1(one int, two int, three int)}
  33. set v [catch {execsql {INSERT INTO test1 VALUES(1,2)}} msg]
  34. lappend v $msg
  35. } {1 {table test1 has 3 columns but 2 values were supplied}}
  36. do_test insert-1.3b {
  37. set v [catch {execsql {INSERT INTO test1 VALUES(1,2,3,4)}} msg]
  38. lappend v $msg
  39. } {1 {table test1 has 3 columns but 4 values were supplied}}
  40. do_test insert-1.3c {
  41. set v [catch {execsql {INSERT INTO test1(one,two) VALUES(1,2,3,4)}} msg]
  42. lappend v $msg
  43. } {1 {4 values for 2 columns}}
  44. do_test insert-1.3d {
  45. set v [catch {execsql {INSERT INTO test1(one,two) VALUES(1)}} msg]
  46. lappend v $msg
  47. } {1 {1 values for 2 columns}}
  48. # Try to insert into a non-existant column of a table.
  49. #
  50. do_test insert-1.4 {
  51. set v [catch {execsql {INSERT INTO test1(one,four) VALUES(1,2)}} msg]
  52. lappend v $msg
  53. } {1 {table test1 has no column named four}}
  54. # Make sure the inserts actually happen
  55. #
  56. do_test insert-1.5 {
  57. execsql {INSERT INTO test1 VALUES(1,2,3)}
  58. execsql {SELECT * FROM test1}
  59. } {1 2 3}
  60. do_test insert-1.5b {
  61. execsql {INSERT INTO test1 VALUES(4,5,6)}
  62. execsql {SELECT * FROM test1 ORDER BY one}
  63. } {1 2 3 4 5 6}
  64. do_test insert-1.5c {
  65. execsql {INSERT INTO test1 VALUES(7,8,9)}
  66. execsql {SELECT * FROM test1 ORDER BY one}
  67. } {1 2 3 4 5 6 7 8 9}
  68. do_test insert-1.6 {
  69. execsql {DELETE FROM test1}
  70. execsql {INSERT INTO test1(one,two) VALUES(1,2)}
  71. execsql {SELECT * FROM test1 ORDER BY one}
  72. } {1 2 {}}
  73. do_test insert-1.6b {
  74. execsql {INSERT INTO test1(two,three) VALUES(5,6)}
  75. execsql {SELECT * FROM test1 ORDER BY one}
  76. } {{} 5 6 1 2 {}}
  77. do_test insert-1.6c {
  78. execsql {INSERT INTO test1(three,one) VALUES(7,8)}
  79. execsql {SELECT * FROM test1 ORDER BY one}
  80. } {{} 5 6 1 2 {} 8 {} 7}
  81. # A table to use for testing default values
  82. #
  83. do_test insert-2.1 {
  84. execsql {
  85. CREATE TABLE test2(
  86. f1 int default -111,
  87. f2 real default +4.32,
  88. f3 int default +222,
  89. f4 int default 7.89
  90. )
  91. }
  92. execsql {SELECT * from test2}
  93. } {}
  94. do_test insert-2.2 {
  95. execsql {INSERT INTO test2(f1,f3) VALUES(+10,-10)}
  96. execsql {SELECT * FROM test2}
  97. } {10 4.32 -10 7.89}
  98. do_test insert-2.3 {
  99. execsql {INSERT INTO test2(f2,f4) VALUES(1.23,-3.45)}
  100. execsql {SELECT * FROM test2 WHERE f1==-111}
  101. } {-111 1.23 222 -3.45}
  102. do_test insert-2.4 {
  103. execsql {INSERT INTO test2(f1,f2,f4) VALUES(77,+1.23,3.45)}
  104. execsql {SELECT * FROM test2 WHERE f1==77}
  105. } {77 1.23 222 3.45}
  106. do_test insert-2.10 {
  107. execsql {
  108. DROP TABLE test2;
  109. CREATE TABLE test2(
  110. f1 int default 111,
  111. f2 real default -4.32,
  112. f3 text default hi,
  113. f4 text default 'abc-123',
  114. f5 varchar(10)
  115. )
  116. }
  117. execsql {SELECT * from test2}
  118. } {}
  119. do_test insert-2.11 {
  120. execsql {INSERT INTO test2(f2,f4) VALUES(-2.22,'hi!')}
  121. execsql {SELECT * FROM test2}
  122. } {111 -2.22 hi hi! {}}
  123. do_test insert-2.12 {
  124. execsql {INSERT INTO test2(f1,f5) VALUES(1,'xyzzy')}
  125. execsql {SELECT * FROM test2 ORDER BY f1}
  126. } {1 -4.32 hi abc-123 xyzzy 111 -2.22 hi hi! {}}
  127. # Do additional inserts with default values, but this time
  128. # on a table that has indices. In particular we want to verify
  129. # that the correct default values are inserted into the indices.
  130. #
  131. do_test insert-3.1 {
  132. execsql {
  133. DELETE FROM test2;
  134. CREATE INDEX index9 ON test2(f1,f2);
  135. CREATE INDEX indext ON test2(f4,f5);
  136. SELECT * from test2;
  137. }
  138. } {}
  139. # Update for sqlite3 v3:
  140. # Change the 111 to '111' in the following two test cases, because
  141. # the default value is being inserted as a string. TODO: It shouldn't be.
  142. do_test insert-3.2 {
  143. execsql {INSERT INTO test2(f2,f4) VALUES(-3.33,'hum')}
  144. execsql {SELECT * FROM test2 WHERE f1='111' AND f2=-3.33}
  145. } {111 -3.33 hi hum {}}
  146. do_test insert-3.3 {
  147. execsql {INSERT INTO test2(f1,f2,f5) VALUES(22,-4.44,'wham')}
  148. execsql {SELECT * FROM test2 WHERE f1='111' AND f2=-3.33}
  149. } {111 -3.33 hi hum {}}
  150. do_test insert-3.4 {
  151. execsql {SELECT * FROM test2 WHERE f1=22 AND f2=-4.44}
  152. } {22 -4.44 hi abc-123 wham}
  153. ifcapable {reindex} {
  154. do_test insert-3.5 {
  155. execsql REINDEX
  156. } {}
  157. }
  158. integrity_check insert-3.5
  159. # Test of expressions in the VALUES clause
  160. #
  161. do_test insert-4.1 {
  162. execsql {
  163. CREATE TABLE t3(a,b,c);
  164. INSERT INTO t3 VALUES(1+2+3,4,5);
  165. SELECT * FROM t3;
  166. }
  167. } {6 4 5}
  168. do_test insert-4.2 {
  169. ifcapable subquery {
  170. execsql {INSERT INTO t3 VALUES((SELECT max(a) FROM t3)+1,5,6);}
  171. } else {
  172. set maxa [execsql {SELECT max(a) FROM t3}]
  173. execsql "INSERT INTO t3 VALUES($maxa+1,5,6);"
  174. }
  175. execsql {
  176. SELECT * FROM t3 ORDER BY a;
  177. }
  178. } {6 4 5 7 5 6}
  179. ifcapable subquery {
  180. do_test insert-4.3 {
  181. catchsql {
  182. INSERT INTO t3 VALUES((SELECT max(a) FROM t3)+1,t3.a,6);
  183. SELECT * FROM t3 ORDER BY a;
  184. }
  185. } {1 {no such column: t3.a}}
  186. }
  187. do_test insert-4.4 {
  188. ifcapable subquery {
  189. execsql {INSERT INTO t3 VALUES((SELECT b FROM t3 WHERE a=0),6,7);}
  190. } else {
  191. set b [execsql {SELECT b FROM t3 WHERE a = 0}]
  192. if {$b==""} {set b NULL}
  193. execsql "INSERT INTO t3 VALUES($b,6,7);"
  194. }
  195. execsql {
  196. SELECT * FROM t3 ORDER BY a;
  197. }
  198. } {{} 6 7 6 4 5 7 5 6}
  199. do_test insert-4.5 {
  200. execsql {
  201. SELECT b,c FROM t3 WHERE a IS NULL;
  202. }
  203. } {6 7}
  204. do_test insert-4.6 {
  205. catchsql {
  206. INSERT INTO t3 VALUES(notafunc(2,3),2,3);
  207. }
  208. } {1 {no such function: notafunc}}
  209. do_test insert-4.7 {
  210. execsql {
  211. INSERT INTO t3 VALUES(min(1,2,3),max(1,2,3),99);
  212. SELECT * FROM t3 WHERE c=99;
  213. }
  214. } {1 3 99}
  215. # Test the ability to insert from a temporary table into itself.
  216. # Ticket #275.
  217. #
  218. ifcapable tempdb {
  219. do_test insert-5.1 {
  220. execsql {
  221. CREATE TEMP TABLE t4(x);
  222. INSERT INTO t4 VALUES(1);
  223. SELECT * FROM t4;
  224. }
  225. } {1}
  226. do_test insert-5.2 {
  227. execsql {
  228. INSERT INTO t4 SELECT x+1 FROM t4;
  229. SELECT * FROM t4;
  230. }
  231. } {1 2}
  232. ifcapable {explain} {
  233. do_test insert-5.3 {
  234. # verify that a temporary table is used to copy t4 to t4
  235. set x [execsql {
  236. EXPLAIN INSERT INTO t4 SELECT x+2 FROM t4;
  237. }]
  238. expr {[lsearch $x OpenEphemeral]>0}
  239. } {1}
  240. }
  241. do_test insert-5.4 {
  242. # Verify that table "test1" begins on page 3. This should be the same
  243. # page number used by "t4" above.
  244. #
  245. # Update for v3 - the first table now begins on page 2 of each file, not 3.
  246. execsql {
  247. SELECT rootpage FROM sqlite_master WHERE name='test1';
  248. }
  249. } [expr $AUTOVACUUM?3:2]
  250. do_test insert-5.5 {
  251. # Verify that "t4" begins on page 3.
  252. #
  253. # Update for v3 - the first table now begins on page 2 of each file, not 3.
  254. execsql {
  255. SELECT rootpage FROM sqlite_temp_master WHERE name='t4';
  256. }
  257. } {2}
  258. do_test insert-5.6 {
  259. # This should not use an intermediate temporary table.
  260. execsql {
  261. INSERT INTO t4 SELECT one FROM test1 WHERE three=7;
  262. SELECT * FROM t4
  263. }
  264. } {1 2 8}
  265. ifcapable {explain} {
  266. do_test insert-5.7 {
  267. # verify that no temporary table is used to copy test1 to t4
  268. set x [execsql {
  269. EXPLAIN INSERT INTO t4 SELECT one FROM test1;
  270. }]
  271. expr {[lsearch $x OpenTemp]>0}
  272. } {0}
  273. }
  274. }
  275. # Ticket #334: REPLACE statement corrupting indices.
  276. #
  277. ifcapable conflict {
  278. # The REPLACE command is not available if SQLITE_OMIT_CONFLICT is
  279. # defined at compilation time.
  280. do_test insert-6.1 {
  281. execsql {
  282. CREATE TABLE t1(a INTEGER PRIMARY KEY, b UNIQUE);
  283. INSERT INTO t1 VALUES(1,2);
  284. INSERT INTO t1 VALUES(2,3);
  285. SELECT b FROM t1 WHERE b=2;
  286. }
  287. } {2}
  288. do_test insert-6.2 {
  289. execsql {
  290. REPLACE INTO t1 VALUES(1,4);
  291. SELECT b FROM t1 WHERE b=2;
  292. }
  293. } {}
  294. do_test insert-6.3 {
  295. execsql {
  296. UPDATE OR REPLACE t1 SET a=2 WHERE b=4;
  297. SELECT * FROM t1 WHERE b=4;
  298. }
  299. } {2 4}
  300. do_test insert-6.4 {
  301. execsql {
  302. SELECT * FROM t1 WHERE b=3;
  303. }
  304. } {}
  305. ifcapable {reindex} {
  306. do_test insert-6.5 {
  307. execsql REINDEX
  308. } {}
  309. }
  310. do_test insert-6.6 {
  311. execsql {
  312. DROP TABLE t1;
  313. }
  314. } {}
  315. }
  316. # Test that the special optimization for queries of the form
  317. # "SELECT max(x) FROM tbl" where there is an index on tbl(x) works with
  318. # INSERT statments.
  319. do_test insert-7.1 {
  320. execsql {
  321. CREATE TABLE t1(a);
  322. INSERT INTO t1 VALUES(1);
  323. INSERT INTO t1 VALUES(2);
  324. CREATE INDEX i1 ON t1(a);
  325. }
  326. } {}
  327. do_test insert-7.2 {
  328. execsql {
  329. INSERT INTO t1 SELECT max(a) FROM t1;
  330. }
  331. } {}
  332. do_test insert-7.3 {
  333. execsql {
  334. SELECT a FROM t1;
  335. }
  336. } {1 2 2}
  337. # Ticket #1140: Check for an infinite loop in the algorithm that tests
  338. # to see if the right-hand side of an INSERT...SELECT references the left-hand
  339. # side.
  340. #
  341. ifcapable subquery&&compound {
  342. do_test insert-8.1 {
  343. execsql {
  344. INSERT INTO t3 SELECT * FROM (SELECT * FROM t3 UNION ALL SELECT 1,2,3)
  345. }
  346. } {}
  347. }
  348. # Make sure the rowid cache in the VDBE is reset correctly when
  349. # an explicit rowid is given.
  350. #
  351. do_test insert-9.1 {
  352. execsql {
  353. CREATE TABLE t5(x);
  354. INSERT INTO t5 VALUES(1);
  355. INSERT INTO t5 VALUES(2);
  356. INSERT INTO t5 VALUES(3);
  357. INSERT INTO t5(rowid, x) SELECT nullif(x*2+10,14), x+100 FROM t5;
  358. SELECT rowid, x FROM t5;
  359. }
  360. } {1 1 2 2 3 3 12 101 13 102 16 103}
  361. do_test insert-9.2 {
  362. execsql {
  363. CREATE TABLE t6(x INTEGER PRIMARY KEY, y);
  364. INSERT INTO t6 VALUES(1,1);
  365. INSERT INTO t6 VALUES(2,2);
  366. INSERT INTO t6 VALUES(3,3);
  367. INSERT INTO t6 SELECT nullif(y*2+10,14), y+100 FROM t6;
  368. SELECT x, y FROM t6;
  369. }
  370. } {1 1 2 2 3 3 12 101 13 102 16 103}
  371. # Multiple VALUES clauses
  372. #
  373. ifcapable compound {
  374. do_test insert-10.1 {
  375. execsql {
  376. CREATE TABLE t10(a,b,c);
  377. INSERT INTO t10 VALUES(1,2,3), (4,5,6), (7,8,9);
  378. SELECT * FROM t10;
  379. }
  380. } {1 2 3 4 5 6 7 8 9}
  381. do_test insert-10.2 {
  382. catchsql {
  383. INSERT INTO t10 VALUES(11,12,13), (14,15);
  384. }
  385. } {1 {all VALUES must have the same number of terms}}
  386. }
  387. integrity_check insert-99.0
  388. finish_test