intpkey.test 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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.
  12. #
  13. # This file implements tests for the special processing associated
  14. # with INTEGER PRIMARY KEY columns.
  15. #
  16. # $Id: intpkey.test,v 1.24 2007/11/29 17:43:28 danielk1977 Exp $
  17. set testdir [file dirname $argv0]
  18. source $testdir/tester.tcl
  19. # Create a table with a primary key and a datatype other than
  20. # integer
  21. #
  22. do_test intpkey-1.0 {
  23. execsql {
  24. CREATE TABLE t1(a TEXT PRIMARY KEY, b, c);
  25. }
  26. } {}
  27. # There should be an index associated with the primary key
  28. #
  29. do_test intpkey-1.1 {
  30. execsql {
  31. SELECT name FROM sqlite_master
  32. WHERE type='index' AND tbl_name='t1';
  33. }
  34. } {sqlite_autoindex_t1_1}
  35. # Now create a table with an integer primary key and verify that
  36. # there is no associated index.
  37. #
  38. do_test intpkey-1.2 {
  39. execsql {
  40. DROP TABLE t1;
  41. CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
  42. SELECT name FROM sqlite_master
  43. WHERE type='index' AND tbl_name='t1';
  44. }
  45. } {}
  46. # Insert some records into the new table. Specify the primary key
  47. # and verify that the key is used as the record number.
  48. #
  49. do_test intpkey-1.3 {
  50. execsql {
  51. INSERT INTO t1 VALUES(5,'hello','world');
  52. }
  53. db last_insert_rowid
  54. } {5}
  55. do_test intpkey-1.4 {
  56. execsql {
  57. SELECT * FROM t1;
  58. }
  59. } {5 hello world}
  60. do_test intpkey-1.5 {
  61. execsql {
  62. SELECT rowid, * FROM t1;
  63. }
  64. } {5 5 hello world}
  65. # Attempting to insert a duplicate primary key should give a constraint
  66. # failure.
  67. #
  68. do_test intpkey-1.6 {
  69. set r [catch {execsql {
  70. INSERT INTO t1 VALUES(5,'second','entry');
  71. }} msg]
  72. lappend r $msg
  73. } {1 {PRIMARY KEY must be unique}}
  74. do_test intpkey-1.7 {
  75. execsql {
  76. SELECT rowid, * FROM t1;
  77. }
  78. } {5 5 hello world}
  79. do_test intpkey-1.8 {
  80. set r [catch {execsql {
  81. INSERT INTO t1 VALUES(6,'second','entry');
  82. }} msg]
  83. lappend r $msg
  84. } {0 {}}
  85. do_test intpkey-1.8.1 {
  86. db last_insert_rowid
  87. } {6}
  88. do_test intpkey-1.9 {
  89. execsql {
  90. SELECT rowid, * FROM t1;
  91. }
  92. } {5 5 hello world 6 6 second entry}
  93. # A ROWID is automatically generated for new records that do not specify
  94. # the integer primary key.
  95. #
  96. do_test intpkey-1.10 {
  97. execsql {
  98. INSERT INTO t1(b,c) VALUES('one','two');
  99. SELECT b FROM t1 ORDER BY b;
  100. }
  101. } {hello one second}
  102. # Try to change the ROWID for the new entry.
  103. #
  104. do_test intpkey-1.11 {
  105. execsql {
  106. UPDATE t1 SET a=4 WHERE b='one';
  107. SELECT * FROM t1;
  108. }
  109. } {4 one two 5 hello world 6 second entry}
  110. # Make sure SELECT statements are able to use the primary key column
  111. # as an index.
  112. #
  113. do_test intpkey-1.12.1 {
  114. execsql {
  115. SELECT * FROM t1 WHERE a==4;
  116. }
  117. } {4 one two}
  118. do_test intpkey-1.12.2 {
  119. execsql {
  120. EXPLAIN QUERY PLAN
  121. SELECT * FROM t1 WHERE a==4;
  122. }
  123. } {/SEARCH TABLE t1 /}
  124. # Try to insert a non-integer value into the primary key field. This
  125. # should result in a data type mismatch.
  126. #
  127. do_test intpkey-1.13.1 {
  128. set r [catch {execsql {
  129. INSERT INTO t1 VALUES('x','y','z');
  130. }} msg]
  131. lappend r $msg
  132. } {1 {datatype mismatch}}
  133. do_test intpkey-1.13.2 {
  134. set r [catch {execsql {
  135. INSERT INTO t1 VALUES('','y','z');
  136. }} msg]
  137. lappend r $msg
  138. } {1 {datatype mismatch}}
  139. do_test intpkey-1.14 {
  140. set r [catch {execsql {
  141. INSERT INTO t1 VALUES(3.4,'y','z');
  142. }} msg]
  143. lappend r $msg
  144. } {1 {datatype mismatch}}
  145. do_test intpkey-1.15 {
  146. set r [catch {execsql {
  147. INSERT INTO t1 VALUES(-3,'y','z');
  148. }} msg]
  149. lappend r $msg
  150. } {0 {}}
  151. do_test intpkey-1.16 {
  152. execsql {SELECT * FROM t1}
  153. } {-3 y z 4 one two 5 hello world 6 second entry}
  154. #### INDICES
  155. # Check to make sure indices work correctly with integer primary keys
  156. #
  157. do_test intpkey-2.1 {
  158. execsql {
  159. CREATE INDEX i1 ON t1(b);
  160. SELECT * FROM t1 WHERE b=='y'
  161. }
  162. } {-3 y z}
  163. do_test intpkey-2.1.1 {
  164. execsql {
  165. SELECT * FROM t1 WHERE b=='y' AND rowid<0
  166. }
  167. } {-3 y z}
  168. do_test intpkey-2.1.2 {
  169. execsql {
  170. SELECT * FROM t1 WHERE b=='y' AND rowid<0 AND rowid>=-20
  171. }
  172. } {-3 y z}
  173. do_test intpkey-2.1.3 {
  174. execsql {
  175. SELECT * FROM t1 WHERE b>='y'
  176. }
  177. } {-3 y z}
  178. do_test intpkey-2.1.4 {
  179. execsql {
  180. SELECT * FROM t1 WHERE b>='y' AND rowid<10
  181. }
  182. } {-3 y z}
  183. do_test intpkey-2.2 {
  184. execsql {
  185. UPDATE t1 SET a=8 WHERE b=='y';
  186. SELECT * FROM t1 WHERE b=='y';
  187. }
  188. } {8 y z}
  189. do_test intpkey-2.3 {
  190. execsql {
  191. SELECT rowid, * FROM t1;
  192. }
  193. } {4 4 one two 5 5 hello world 6 6 second entry 8 8 y z}
  194. do_test intpkey-2.4 {
  195. execsql {
  196. SELECT rowid, * FROM t1 WHERE b<'second'
  197. }
  198. } {5 5 hello world 4 4 one two}
  199. do_test intpkey-2.4.1 {
  200. execsql {
  201. SELECT rowid, * FROM t1 WHERE 'second'>b
  202. }
  203. } {5 5 hello world 4 4 one two}
  204. do_test intpkey-2.4.2 {
  205. execsql {
  206. SELECT rowid, * FROM t1 WHERE 8>rowid AND 'second'>b
  207. }
  208. } {4 4 one two 5 5 hello world}
  209. do_test intpkey-2.4.3 {
  210. execsql {
  211. SELECT rowid, * FROM t1 WHERE 8>rowid AND 'second'>b AND 0<rowid
  212. }
  213. } {4 4 one two 5 5 hello world}
  214. do_test intpkey-2.5 {
  215. execsql {
  216. SELECT rowid, * FROM t1 WHERE b>'a'
  217. }
  218. } {5 5 hello world 4 4 one two 6 6 second entry 8 8 y z}
  219. do_test intpkey-2.6 {
  220. execsql {
  221. DELETE FROM t1 WHERE rowid=4;
  222. SELECT * FROM t1 WHERE b>'a';
  223. }
  224. } {5 hello world 6 second entry 8 y z}
  225. do_test intpkey-2.7 {
  226. execsql {
  227. UPDATE t1 SET a=-4 WHERE rowid=8;
  228. SELECT * FROM t1 WHERE b>'a';
  229. }
  230. } {5 hello world 6 second entry -4 y z}
  231. do_test intpkey-2.7 {
  232. execsql {
  233. SELECT * FROM t1
  234. }
  235. } {-4 y z 5 hello world 6 second entry}
  236. # Do an SQL statement. Append the search count to the end of the result.
  237. #
  238. proc count sql {
  239. set ::sqlite_search_count 0
  240. return [concat [execsql $sql] $::sqlite_search_count]
  241. }
  242. # Create indices that include the integer primary key as one of their
  243. # columns.
  244. #
  245. do_test intpkey-3.1 {
  246. execsql {
  247. CREATE INDEX i2 ON t1(a);
  248. }
  249. } {}
  250. do_test intpkey-3.2 {
  251. count {
  252. SELECT * FROM t1 WHERE a=5;
  253. }
  254. } {5 hello world 0}
  255. do_test intpkey-3.3 {
  256. count {
  257. SELECT * FROM t1 WHERE a>4 AND a<6;
  258. }
  259. } {5 hello world 2}
  260. do_test intpkey-3.4 {
  261. count {
  262. SELECT * FROM t1 WHERE b>='hello' AND b<'hello2';
  263. }
  264. } {5 hello world 3}
  265. do_test intpkey-3.5 {
  266. execsql {
  267. CREATE INDEX i3 ON t1(c,a);
  268. }
  269. } {}
  270. do_test intpkey-3.6 {
  271. count {
  272. SELECT * FROM t1 WHERE c=='world';
  273. }
  274. } {5 hello world 3}
  275. do_test intpkey-3.7 {
  276. execsql {INSERT INTO t1 VALUES(11,'hello','world')}
  277. count {
  278. SELECT * FROM t1 WHERE c=='world';
  279. }
  280. } {5 hello world 11 hello world 5}
  281. do_test intpkey-3.8 {
  282. count {
  283. SELECT * FROM t1 WHERE c=='world' AND a>7;
  284. }
  285. } {11 hello world 4}
  286. do_test intpkey-3.9 {
  287. count {
  288. SELECT * FROM t1 WHERE 7<a;
  289. }
  290. } {11 hello world 1}
  291. # Test inequality constraints on integer primary keys and rowids
  292. #
  293. do_test intpkey-4.1 {
  294. count {
  295. SELECT * FROM t1 WHERE 11=rowid
  296. }
  297. } {11 hello world 0}
  298. do_test intpkey-4.2 {
  299. count {
  300. SELECT * FROM t1 WHERE 11=rowid AND b=='hello'
  301. }
  302. } {11 hello world 0}
  303. do_test intpkey-4.3 {
  304. count {
  305. SELECT * FROM t1 WHERE 11=rowid AND b=='hello' AND c IS NOT NULL;
  306. }
  307. } {11 hello world 0}
  308. do_test intpkey-4.4 {
  309. count {
  310. SELECT * FROM t1 WHERE rowid==11
  311. }
  312. } {11 hello world 0}
  313. do_test intpkey-4.5 {
  314. count {
  315. SELECT * FROM t1 WHERE oid==11 AND b=='hello'
  316. }
  317. } {11 hello world 0}
  318. do_test intpkey-4.6 {
  319. count {
  320. SELECT * FROM t1 WHERE a==11 AND b=='hello' AND c IS NOT NULL;
  321. }
  322. } {11 hello world 0}
  323. do_test intpkey-4.7 {
  324. count {
  325. SELECT * FROM t1 WHERE 8<rowid;
  326. }
  327. } {11 hello world 1}
  328. do_test intpkey-4.8 {
  329. count {
  330. SELECT * FROM t1 WHERE 8<rowid AND 11>=oid;
  331. }
  332. } {11 hello world 1}
  333. do_test intpkey-4.9 {
  334. count {
  335. SELECT * FROM t1 WHERE 11<=_rowid_ AND 12>=a;
  336. }
  337. } {11 hello world 1}
  338. do_test intpkey-4.10 {
  339. count {
  340. SELECT * FROM t1 WHERE 0>=_rowid_;
  341. }
  342. } {-4 y z 1}
  343. do_test intpkey-4.11 {
  344. count {
  345. SELECT * FROM t1 WHERE a<0;
  346. }
  347. } {-4 y z 1}
  348. do_test intpkey-4.12 {
  349. count {
  350. SELECT * FROM t1 WHERE a<0 AND a>10;
  351. }
  352. } {1}
  353. # Make sure it is OK to insert a rowid of 0
  354. #
  355. do_test intpkey-5.1 {
  356. execsql {
  357. INSERT INTO t1 VALUES(0,'zero','entry');
  358. }
  359. count {
  360. SELECT * FROM t1 WHERE a=0;
  361. }
  362. } {0 zero entry 0}
  363. do_test intpkey-5.2 {
  364. execsql {
  365. SELECT rowid, a FROM t1 ORDER BY rowid
  366. }
  367. } {-4 -4 0 0 5 5 6 6 11 11}
  368. # Test the ability of the COPY command to put data into a
  369. # table that contains an integer primary key.
  370. #
  371. # COPY command has been removed. But we retain these tests so
  372. # that the tables will contain the right data for tests that follow.
  373. #
  374. do_test intpkey-6.1 {
  375. execsql {
  376. BEGIN;
  377. INSERT INTO t1 VALUES(20,'b-20','c-20');
  378. INSERT INTO t1 VALUES(21,'b-21','c-21');
  379. INSERT INTO t1 VALUES(22,'b-22','c-22');
  380. COMMIT;
  381. SELECT * FROM t1 WHERE a>=20;
  382. }
  383. } {20 b-20 c-20 21 b-21 c-21 22 b-22 c-22}
  384. do_test intpkey-6.2 {
  385. execsql {
  386. SELECT * FROM t1 WHERE b=='hello'
  387. }
  388. } {5 hello world 11 hello world}
  389. do_test intpkey-6.3 {
  390. execsql {
  391. DELETE FROM t1 WHERE b='b-21';
  392. SELECT * FROM t1 WHERE b=='b-21';
  393. }
  394. } {}
  395. do_test intpkey-6.4 {
  396. execsql {
  397. SELECT * FROM t1 WHERE a>=20
  398. }
  399. } {20 b-20 c-20 22 b-22 c-22}
  400. # Do an insert of values with the columns specified out of order.
  401. #
  402. do_test intpkey-7.1 {
  403. execsql {
  404. INSERT INTO t1(c,b,a) VALUES('row','new',30);
  405. SELECT * FROM t1 WHERE rowid>=30;
  406. }
  407. } {30 new row}
  408. do_test intpkey-7.2 {
  409. execsql {
  410. SELECT * FROM t1 WHERE rowid>20;
  411. }
  412. } {22 b-22 c-22 30 new row}
  413. # Do an insert from a select statement.
  414. #
  415. do_test intpkey-8.1 {
  416. execsql {
  417. CREATE TABLE t2(x INTEGER PRIMARY KEY, y, z);
  418. INSERT INTO t2 SELECT * FROM t1;
  419. SELECT rowid FROM t2;
  420. }
  421. } {-4 0 5 6 11 20 22 30}
  422. do_test intpkey-8.2 {
  423. execsql {
  424. SELECT x FROM t2;
  425. }
  426. } {-4 0 5 6 11 20 22 30}
  427. do_test intpkey-9.1 {
  428. execsql {
  429. UPDATE t1 SET c='www' WHERE c='world';
  430. SELECT rowid, a, c FROM t1 WHERE c=='www';
  431. }
  432. } {5 5 www 11 11 www}
  433. # Check insert of NULL for primary key
  434. #
  435. do_test intpkey-10.1 {
  436. execsql {
  437. DROP TABLE t2;
  438. CREATE TABLE t2(x INTEGER PRIMARY KEY, y, z);
  439. INSERT INTO t2 VALUES(NULL, 1, 2);
  440. SELECT * from t2;
  441. }
  442. } {1 1 2}
  443. do_test intpkey-10.2 {
  444. execsql {
  445. INSERT INTO t2 VALUES(NULL, 2, 3);
  446. SELECT * from t2 WHERE x=2;
  447. }
  448. } {2 2 3}
  449. do_test intpkey-10.3 {
  450. execsql {
  451. INSERT INTO t2 SELECT NULL, z, y FROM t2;
  452. SELECT * FROM t2;
  453. }
  454. } {1 1 2 2 2 3 3 2 1 4 3 2}
  455. # This tests checks to see if a floating point number can be used
  456. # to reference an integer primary key.
  457. #
  458. do_test intpkey-11.1 {
  459. execsql {
  460. SELECT b FROM t1 WHERE a=2.0+3.0;
  461. }
  462. } {hello}
  463. do_test intpkey-11.1 {
  464. execsql {
  465. SELECT b FROM t1 WHERE a=2.0+3.5;
  466. }
  467. } {}
  468. integrity_check intpkey-12.1
  469. # Try to use a string that looks like a floating point number as
  470. # an integer primary key. This should actually work when the floating
  471. # point value can be rounded to an integer without loss of data.
  472. #
  473. do_test intpkey-13.1 {
  474. execsql {
  475. SELECT * FROM t1 WHERE a=1;
  476. }
  477. } {}
  478. do_test intpkey-13.2 {
  479. execsql {
  480. INSERT INTO t1 VALUES('1.0',2,3);
  481. SELECT * FROM t1 WHERE a=1;
  482. }
  483. } {1 2 3}
  484. do_test intpkey-13.3 {
  485. catchsql {
  486. INSERT INTO t1 VALUES('1.5',3,4);
  487. }
  488. } {1 {datatype mismatch}}
  489. ifcapable {bloblit} {
  490. do_test intpkey-13.4 {
  491. catchsql {
  492. INSERT INTO t1 VALUES(x'123456',3,4);
  493. }
  494. } {1 {datatype mismatch}}
  495. }
  496. do_test intpkey-13.5 {
  497. catchsql {
  498. INSERT INTO t1 VALUES('+1234567890',3,4);
  499. }
  500. } {0 {}}
  501. # Compare an INTEGER PRIMARY KEY against a TEXT expression. The INTEGER
  502. # affinity should be applied to the text value before the comparison
  503. # takes place.
  504. #
  505. do_test intpkey-14.1 {
  506. execsql {
  507. CREATE TABLE t3(a INTEGER PRIMARY KEY, b INTEGER, c TEXT);
  508. INSERT INTO t3 VALUES(1, 1, 'one');
  509. INSERT INTO t3 VALUES(2, 2, '2');
  510. INSERT INTO t3 VALUES(3, 3, 3);
  511. }
  512. } {}
  513. do_test intpkey-14.2 {
  514. execsql {
  515. SELECT * FROM t3 WHERE a>2;
  516. }
  517. } {3 3 3}
  518. do_test intpkey-14.3 {
  519. execsql {
  520. SELECT * FROM t3 WHERE a>'2';
  521. }
  522. } {3 3 3}
  523. do_test intpkey-14.4 {
  524. execsql {
  525. SELECT * FROM t3 WHERE a<'2';
  526. }
  527. } {1 1 one}
  528. do_test intpkey-14.5 {
  529. execsql {
  530. SELECT * FROM t3 WHERE a<c;
  531. }
  532. } {1 1 one}
  533. do_test intpkey-14.6 {
  534. execsql {
  535. SELECT * FROM t3 WHERE a=c;
  536. }
  537. } {2 2 2 3 3 3}
  538. # Check for proper handling of primary keys greater than 2^31.
  539. # Ticket #1188
  540. #
  541. do_test intpkey-15.1 {
  542. execsql {
  543. INSERT INTO t1 VALUES(2147483647, 'big-1', 123);
  544. SELECT * FROM t1 WHERE a>2147483648;
  545. }
  546. } {}
  547. do_test intpkey-15.2 {
  548. execsql {
  549. INSERT INTO t1 VALUES(NULL, 'big-2', 234);
  550. SELECT b FROM t1 WHERE a>=2147483648;
  551. }
  552. } {big-2}
  553. do_test intpkey-15.3 {
  554. execsql {
  555. SELECT b FROM t1 WHERE a>2147483648;
  556. }
  557. } {}
  558. do_test intpkey-15.4 {
  559. execsql {
  560. SELECT b FROM t1 WHERE a>=2147483647;
  561. }
  562. } {big-1 big-2}
  563. do_test intpkey-15.5 {
  564. execsql {
  565. SELECT b FROM t1 WHERE a<2147483648;
  566. }
  567. } {y zero 2 hello second hello b-20 b-22 new 3 big-1}
  568. do_test intpkey-15.6 {
  569. execsql {
  570. SELECT b FROM t1 WHERE a<12345678901;
  571. }
  572. } {y zero 2 hello second hello b-20 b-22 new 3 big-1 big-2}
  573. do_test intpkey-15.7 {
  574. execsql {
  575. SELECT b FROM t1 WHERE a>12345678901;
  576. }
  577. } {}
  578. finish_test