PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/src/sqlite/test/index.test

#
Unknown | 720 lines | 688 code | 32 blank | 0 comment | 0 complexity | 6ca1c2c4f8d204a509610bcf89589101 MD5 | raw file
Possible License(s): BSD-3-Clause
  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 INDEX statement.
  13. #
  14. # $Id: index.test,v 1.43 2008/01/16 18:20:42 danielk1977 Exp $
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. # Create a basic index and verify it is added to sqlite_master
  18. #
  19. do_test index-1.1 {
  20. execsql {CREATE TABLE test1(f1 int, f2 int, f3 int)}
  21. execsql {CREATE INDEX index1 ON test1(f1)}
  22. execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
  23. } {index1 test1}
  24. do_test index-1.1b {
  25. execsql {SELECT name, sql, tbl_name, type FROM sqlite_master
  26. WHERE name='index1'}
  27. } {index1 {CREATE INDEX index1 ON test1(f1)} test1 index}
  28. do_test index-1.1c {
  29. db close
  30. sqlite3 db test.db
  31. execsql {SELECT name, sql, tbl_name, type FROM sqlite_master
  32. WHERE name='index1'}
  33. } {index1 {CREATE INDEX index1 ON test1(f1)} test1 index}
  34. do_test index-1.1d {
  35. db close
  36. sqlite3 db test.db
  37. execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
  38. } {index1 test1}
  39. # Verify that the index dies with the table
  40. #
  41. do_test index-1.2 {
  42. execsql {DROP TABLE test1}
  43. execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
  44. } {}
  45. # Try adding an index to a table that does not exist
  46. #
  47. do_test index-2.1 {
  48. set v [catch {execsql {CREATE INDEX index1 ON test1(f1)}} msg]
  49. lappend v $msg
  50. } {1 {no such table: main.test1}}
  51. # Try adding an index on a column of a table where the table
  52. # exists but the column does not.
  53. #
  54. do_test index-2.1 {
  55. execsql {CREATE TABLE test1(f1 int, f2 int, f3 int)}
  56. set v [catch {execsql {CREATE INDEX index1 ON test1(f4)}} msg]
  57. lappend v $msg
  58. } {1 {table test1 has no column named f4}}
  59. # Try an index with some columns that match and others that do now.
  60. #
  61. do_test index-2.2 {
  62. set v [catch {execsql {CREATE INDEX index1 ON test1(f1, f2, f4, f3)}} msg]
  63. execsql {DROP TABLE test1}
  64. lappend v $msg
  65. } {1 {table test1 has no column named f4}}
  66. # Try creating a bunch of indices on the same table
  67. #
  68. set r {}
  69. for {set i 1} {$i<100} {incr i} {
  70. lappend r [format index%02d $i]
  71. }
  72. do_test index-3.1 {
  73. execsql {CREATE TABLE test1(f1 int, f2 int, f3 int, f4 int, f5 int)}
  74. for {set i 1} {$i<100} {incr i} {
  75. set sql "CREATE INDEX [format index%02d $i] ON test1(f[expr {($i%5)+1}])"
  76. execsql $sql
  77. }
  78. execsql {SELECT name FROM sqlite_master
  79. WHERE type='index' AND tbl_name='test1'
  80. ORDER BY name}
  81. } $r
  82. integrity_check index-3.2.1
  83. ifcapable {reindex} {
  84. do_test index-3.2.2 {
  85. execsql REINDEX
  86. } {}
  87. }
  88. integrity_check index-3.2.3
  89. # Verify that all the indices go away when we drop the table.
  90. #
  91. do_test index-3.3 {
  92. execsql {DROP TABLE test1}
  93. execsql {SELECT name FROM sqlite_master
  94. WHERE type='index' AND tbl_name='test1'
  95. ORDER BY name}
  96. } {}
  97. # Create a table and insert values into that table. Then create
  98. # an index on that table. Verify that we can select values
  99. # from the table correctly using the index.
  100. #
  101. # Note that the index names "index9" and "indext" are chosen because
  102. # they both have the same hash.
  103. #
  104. do_test index-4.1 {
  105. execsql {CREATE TABLE test1(cnt int, power int)}
  106. for {set i 1} {$i<20} {incr i} {
  107. execsql "INSERT INTO test1 VALUES($i,[expr {1<<$i}])"
  108. }
  109. execsql {CREATE INDEX index9 ON test1(cnt)}
  110. execsql {CREATE INDEX indext ON test1(power)}
  111. execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
  112. } {index9 indext test1}
  113. do_test index-4.2 {
  114. execsql {SELECT cnt FROM test1 WHERE power=4}
  115. } {2}
  116. do_test index-4.3 {
  117. execsql {SELECT cnt FROM test1 WHERE power=1024}
  118. } {10}
  119. do_test index-4.4 {
  120. execsql {SELECT power FROM test1 WHERE cnt=6}
  121. } {64}
  122. do_test index-4.5 {
  123. execsql {DROP INDEX indext}
  124. execsql {SELECT power FROM test1 WHERE cnt=6}
  125. } {64}
  126. do_test index-4.6 {
  127. execsql {SELECT cnt FROM test1 WHERE power=1024}
  128. } {10}
  129. do_test index-4.7 {
  130. execsql {CREATE INDEX indext ON test1(cnt)}
  131. execsql {SELECT power FROM test1 WHERE cnt=6}
  132. } {64}
  133. do_test index-4.8 {
  134. execsql {SELECT cnt FROM test1 WHERE power=1024}
  135. } {10}
  136. do_test index-4.9 {
  137. execsql {DROP INDEX index9}
  138. execsql {SELECT power FROM test1 WHERE cnt=6}
  139. } {64}
  140. do_test index-4.10 {
  141. execsql {SELECT cnt FROM test1 WHERE power=1024}
  142. } {10}
  143. do_test index-4.11 {
  144. execsql {DROP INDEX indext}
  145. execsql {SELECT power FROM test1 WHERE cnt=6}
  146. } {64}
  147. do_test index-4.12 {
  148. execsql {SELECT cnt FROM test1 WHERE power=1024}
  149. } {10}
  150. do_test index-4.13 {
  151. execsql {DROP TABLE test1}
  152. execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
  153. } {}
  154. integrity_check index-4.14
  155. # Do not allow indices to be added to sqlite_master
  156. #
  157. do_test index-5.1 {
  158. set v [catch {execsql {CREATE INDEX index1 ON sqlite_master(name)}} msg]
  159. lappend v $msg
  160. } {1 {table sqlite_master may not be indexed}}
  161. do_test index-5.2 {
  162. execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
  163. } {}
  164. # Do not allow indices with duplicate names to be added
  165. #
  166. do_test index-6.1 {
  167. execsql {CREATE TABLE test1(f1 int, f2 int)}
  168. execsql {CREATE TABLE test2(g1 real, g2 real)}
  169. execsql {CREATE INDEX index1 ON test1(f1)}
  170. set v [catch {execsql {CREATE INDEX index1 ON test2(g1)}} msg]
  171. lappend v $msg
  172. } {1 {index index1 already exists}}
  173. do_test index-6.1.1 {
  174. catchsql {CREATE INDEX [index1] ON test2(g1)}
  175. } {1 {index index1 already exists}}
  176. do_test index-6.1b {
  177. execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
  178. } {index1 test1 test2}
  179. do_test index-6.1c {
  180. catchsql {CREATE INDEX IF NOT EXISTS index1 ON test1(f1)}
  181. } {0 {}}
  182. do_test index-6.2 {
  183. set v [catch {execsql {CREATE INDEX test1 ON test2(g1)}} msg]
  184. lappend v $msg
  185. } {1 {there is already a table named test1}}
  186. do_test index-6.2b {
  187. execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
  188. } {index1 test1 test2}
  189. do_test index-6.3 {
  190. execsql {DROP TABLE test1}
  191. execsql {DROP TABLE test2}
  192. execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
  193. } {}
  194. do_test index-6.4 {
  195. execsql {
  196. CREATE TABLE test1(a,b);
  197. CREATE INDEX index1 ON test1(a);
  198. CREATE INDEX index2 ON test1(b);
  199. CREATE INDEX index3 ON test1(a,b);
  200. DROP TABLE test1;
  201. SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name;
  202. }
  203. } {}
  204. integrity_check index-6.5
  205. # Create a primary key
  206. #
  207. do_test index-7.1 {
  208. execsql {CREATE TABLE test1(f1 int, f2 int primary key)}
  209. for {set i 1} {$i<20} {incr i} {
  210. execsql "INSERT INTO test1 VALUES($i,[expr {1<<$i}])"
  211. }
  212. execsql {SELECT count(*) FROM test1}
  213. } {19}
  214. do_test index-7.2 {
  215. execsql {SELECT f1 FROM test1 WHERE f2=65536}
  216. } {16}
  217. do_test index-7.3 {
  218. execsql {
  219. SELECT name FROM sqlite_master
  220. WHERE type='index' AND tbl_name='test1'
  221. }
  222. } {sqlite_autoindex_test1_1}
  223. do_test index-7.4 {
  224. execsql {DROP table test1}
  225. execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
  226. } {}
  227. integrity_check index-7.5
  228. # Make sure we cannot drop a non-existant index.
  229. #
  230. do_test index-8.1 {
  231. set v [catch {execsql {DROP INDEX index1}} msg]
  232. lappend v $msg
  233. } {1 {no such index: index1}}
  234. # Make sure we don't actually create an index when the EXPLAIN keyword
  235. # is used.
  236. #
  237. do_test index-9.1 {
  238. execsql {CREATE TABLE tab1(a int)}
  239. ifcapable {explain} {
  240. execsql {EXPLAIN CREATE INDEX idx1 ON tab1(a)}
  241. }
  242. execsql {SELECT name FROM sqlite_master WHERE tbl_name='tab1'}
  243. } {tab1}
  244. do_test index-9.2 {
  245. execsql {CREATE INDEX idx1 ON tab1(a)}
  246. execsql {SELECT name FROM sqlite_master WHERE tbl_name='tab1' ORDER BY name}
  247. } {idx1 tab1}
  248. integrity_check index-9.3
  249. # Allow more than one entry with the same key.
  250. #
  251. do_test index-10.0 {
  252. execsql {
  253. CREATE TABLE t1(a int, b int);
  254. CREATE INDEX i1 ON t1(a);
  255. INSERT INTO t1 VALUES(1,2);
  256. INSERT INTO t1 VALUES(2,4);
  257. INSERT INTO t1 VALUES(3,8);
  258. INSERT INTO t1 VALUES(1,12);
  259. SELECT b FROM t1 WHERE a=1 ORDER BY b;
  260. }
  261. } {2 12}
  262. do_test index-10.1 {
  263. execsql {
  264. SELECT b FROM t1 WHERE a=2 ORDER BY b;
  265. }
  266. } {4}
  267. do_test index-10.2 {
  268. execsql {
  269. DELETE FROM t1 WHERE b=12;
  270. SELECT b FROM t1 WHERE a=1 ORDER BY b;
  271. }
  272. } {2}
  273. do_test index-10.3 {
  274. execsql {
  275. DELETE FROM t1 WHERE b=2;
  276. SELECT b FROM t1 WHERE a=1 ORDER BY b;
  277. }
  278. } {}
  279. do_test index-10.4 {
  280. execsql {
  281. DELETE FROM t1;
  282. INSERT INTO t1 VALUES (1,1);
  283. INSERT INTO t1 VALUES (1,2);
  284. INSERT INTO t1 VALUES (1,3);
  285. INSERT INTO t1 VALUES (1,4);
  286. INSERT INTO t1 VALUES (1,5);
  287. INSERT INTO t1 VALUES (1,6);
  288. INSERT INTO t1 VALUES (1,7);
  289. INSERT INTO t1 VALUES (1,8);
  290. INSERT INTO t1 VALUES (1,9);
  291. INSERT INTO t1 VALUES (2,0);
  292. SELECT b FROM t1 WHERE a=1 ORDER BY b;
  293. }
  294. } {1 2 3 4 5 6 7 8 9}
  295. do_test index-10.5 {
  296. ifcapable subquery {
  297. execsql { DELETE FROM t1 WHERE b IN (2, 4, 6, 8); }
  298. } else {
  299. execsql { DELETE FROM t1 WHERE b = 2 OR b = 4 OR b = 6 OR b = 8; }
  300. }
  301. execsql {
  302. SELECT b FROM t1 WHERE a=1 ORDER BY b;
  303. }
  304. } {1 3 5 7 9}
  305. do_test index-10.6 {
  306. execsql {
  307. DELETE FROM t1 WHERE b>2;
  308. SELECT b FROM t1 WHERE a=1 ORDER BY b;
  309. }
  310. } {1}
  311. do_test index-10.7 {
  312. execsql {
  313. DELETE FROM t1 WHERE b=1;
  314. SELECT b FROM t1 WHERE a=1 ORDER BY b;
  315. }
  316. } {}
  317. do_test index-10.8 {
  318. execsql {
  319. SELECT b FROM t1 ORDER BY b;
  320. }
  321. } {0}
  322. integrity_check index-10.9
  323. # Automatically create an index when we specify a primary key.
  324. #
  325. do_test index-11.1 {
  326. execsql {
  327. CREATE TABLE t3(
  328. a text,
  329. b int,
  330. c float,
  331. PRIMARY KEY(b)
  332. );
  333. }
  334. for {set i 1} {$i<=50} {incr i} {
  335. execsql "INSERT INTO t3 VALUES('x${i}x',$i,0.$i)"
  336. }
  337. set sqlite_search_count 0
  338. concat [execsql {SELECT c FROM t3 WHERE b==10}] $sqlite_search_count
  339. } {0.1 2}
  340. integrity_check index-11.2
  341. # Numeric strings should compare as if they were numbers. So even if the
  342. # strings are not character-by-character the same, if they represent the
  343. # same number they should compare equal to one another. Verify that this
  344. # is true in indices.
  345. #
  346. # Updated for sqlite3 v3: SQLite will now store these values as numbers
  347. # (because the affinity of column a is NUMERIC) so the quirky
  348. # representations are not retained. i.e. '+1.0' becomes '1'.
  349. do_test index-12.1 {
  350. execsql {
  351. CREATE TABLE t4(a NUM,b);
  352. INSERT INTO t4 VALUES('0.0',1);
  353. INSERT INTO t4 VALUES('0.00',2);
  354. INSERT INTO t4 VALUES('abc',3);
  355. INSERT INTO t4 VALUES('-1.0',4);
  356. INSERT INTO t4 VALUES('+1.0',5);
  357. INSERT INTO t4 VALUES('0',6);
  358. INSERT INTO t4 VALUES('00000',7);
  359. SELECT a FROM t4 ORDER BY b;
  360. }
  361. } {0 0 abc -1 1 0 0}
  362. do_test index-12.2 {
  363. execsql {
  364. SELECT a FROM t4 WHERE a==0 ORDER BY b
  365. }
  366. } {0 0 0 0}
  367. do_test index-12.3 {
  368. execsql {
  369. SELECT a FROM t4 WHERE a<0.5 ORDER BY b
  370. }
  371. } {0 0 -1 0 0}
  372. do_test index-12.4 {
  373. execsql {
  374. SELECT a FROM t4 WHERE a>-0.5 ORDER BY b
  375. }
  376. } {0 0 abc 1 0 0}
  377. do_test index-12.5 {
  378. execsql {
  379. CREATE INDEX t4i1 ON t4(a);
  380. SELECT a FROM t4 WHERE a==0 ORDER BY b
  381. }
  382. } {0 0 0 0}
  383. do_test index-12.6 {
  384. execsql {
  385. SELECT a FROM t4 WHERE a<0.5 ORDER BY b
  386. }
  387. } {0 0 -1 0 0}
  388. do_test index-12.7 {
  389. execsql {
  390. SELECT a FROM t4 WHERE a>-0.5 ORDER BY b
  391. }
  392. } {0 0 abc 1 0 0}
  393. integrity_check index-12.8
  394. # Make sure we cannot drop an automatically created index.
  395. #
  396. do_test index-13.1 {
  397. execsql {
  398. CREATE TABLE t5(
  399. a int UNIQUE,
  400. b float PRIMARY KEY,
  401. c varchar(10),
  402. UNIQUE(a,c)
  403. );
  404. INSERT INTO t5 VALUES(1,2,3);
  405. SELECT * FROM t5;
  406. }
  407. } {1 2.0 3}
  408. do_test index-13.2 {
  409. set ::idxlist [execsql {
  410. SELECT name FROM sqlite_master WHERE type="index" AND tbl_name="t5";
  411. }]
  412. llength $::idxlist
  413. } {3}
  414. for {set i 0} {$i<[llength $::idxlist]} {incr i} {
  415. do_test index-13.3.$i {
  416. catchsql "
  417. DROP INDEX '[lindex $::idxlist $i]';
  418. "
  419. } {1 {index associated with UNIQUE or PRIMARY KEY constraint cannot be dropped}}
  420. }
  421. do_test index-13.4 {
  422. execsql {
  423. INSERT INTO t5 VALUES('a','b','c');
  424. SELECT * FROM t5;
  425. }
  426. } {1 2.0 3 a b c}
  427. integrity_check index-13.5
  428. # Check the sort order of data in an index.
  429. #
  430. do_test index-14.1 {
  431. execsql {
  432. CREATE TABLE t6(a,b,c);
  433. CREATE INDEX t6i1 ON t6(a,b);
  434. INSERT INTO t6 VALUES('','',1);
  435. INSERT INTO t6 VALUES('',NULL,2);
  436. INSERT INTO t6 VALUES(NULL,'',3);
  437. INSERT INTO t6 VALUES('abc',123,4);
  438. INSERT INTO t6 VALUES(123,'abc',5);
  439. SELECT c FROM t6 ORDER BY a,b;
  440. }
  441. } {3 5 2 1 4}
  442. do_test index-14.2 {
  443. execsql {
  444. SELECT c FROM t6 WHERE a='';
  445. }
  446. } {2 1}
  447. do_test index-14.3 {
  448. execsql {
  449. SELECT c FROM t6 WHERE b='';
  450. }
  451. } {1 3}
  452. do_test index-14.4 {
  453. execsql {
  454. SELECT c FROM t6 WHERE a>'';
  455. }
  456. } {4}
  457. do_test index-14.5 {
  458. execsql {
  459. SELECT c FROM t6 WHERE a>='';
  460. }
  461. } {2 1 4}
  462. do_test index-14.6 {
  463. execsql {
  464. SELECT c FROM t6 WHERE a>123;
  465. }
  466. } {2 1 4}
  467. do_test index-14.7 {
  468. execsql {
  469. SELECT c FROM t6 WHERE a>=123;
  470. }
  471. } {5 2 1 4}
  472. do_test index-14.8 {
  473. execsql {
  474. SELECT c FROM t6 WHERE a<'abc';
  475. }
  476. } {5 2 1}
  477. do_test index-14.9 {
  478. execsql {
  479. SELECT c FROM t6 WHERE a<='abc';
  480. }
  481. } {5 2 1 4}
  482. do_test index-14.10 {
  483. execsql {
  484. SELECT c FROM t6 WHERE a<='';
  485. }
  486. } {5 2 1}
  487. do_test index-14.11 {
  488. execsql {
  489. SELECT c FROM t6 WHERE a<'';
  490. }
  491. } {5}
  492. integrity_check index-14.12
  493. do_test index-15.1 {
  494. execsql {
  495. DELETE FROM t1;
  496. SELECT * FROM t1;
  497. }
  498. } {}
  499. do_test index-15.2 {
  500. execsql {
  501. INSERT INTO t1 VALUES('1.234e5',1);
  502. INSERT INTO t1 VALUES('12.33e04',2);
  503. INSERT INTO t1 VALUES('12.35E4',3);
  504. INSERT INTO t1 VALUES('12.34e',4);
  505. INSERT INTO t1 VALUES('12.32e+4',5);
  506. INSERT INTO t1 VALUES('12.36E+04',6);
  507. INSERT INTO t1 VALUES('12.36E+',7);
  508. INSERT INTO t1 VALUES('+123.10000E+0003',8);
  509. INSERT INTO t1 VALUES('+',9);
  510. INSERT INTO t1 VALUES('+12347.E+02',10);
  511. INSERT INTO t1 VALUES('+12347E+02',11);
  512. INSERT INTO t1 VALUES('+.125E+04',12);
  513. INSERT INTO t1 VALUES('-.125E+04',13);
  514. INSERT INTO t1 VALUES('.125E+0',14);
  515. INSERT INTO t1 VALUES('.125',15);
  516. SELECT b FROM t1 ORDER BY a, b;
  517. }
  518. } {13 14 15 12 8 5 2 1 3 6 10 11 9 4 7}
  519. do_test index-15.3 {
  520. execsql {
  521. SELECT b FROM t1 WHERE typeof(a) IN ('integer','real') ORDER BY b;
  522. }
  523. } {1 2 3 5 6 8 10 11 12 13 14 15}
  524. integrity_check index-15.4
  525. # The following tests - index-16.* - test that when a table definition
  526. # includes qualifications that specify the same constraint twice only a
  527. # single index is generated to enforce the constraint.
  528. #
  529. # For example: "CREATE TABLE abc( x PRIMARY KEY, UNIQUE(x) );"
  530. #
  531. do_test index-16.1 {
  532. execsql {
  533. CREATE TABLE t7(c UNIQUE PRIMARY KEY);
  534. SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index';
  535. }
  536. } {1}
  537. do_test index-16.2 {
  538. execsql {
  539. DROP TABLE t7;
  540. CREATE TABLE t7(c UNIQUE PRIMARY KEY);
  541. SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index';
  542. }
  543. } {1}
  544. do_test index-16.3 {
  545. execsql {
  546. DROP TABLE t7;
  547. CREATE TABLE t7(c PRIMARY KEY, UNIQUE(c) );
  548. SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index';
  549. }
  550. } {1}
  551. do_test index-16.4 {
  552. execsql {
  553. DROP TABLE t7;
  554. CREATE TABLE t7(c, d , UNIQUE(c, d), PRIMARY KEY(c, d) );
  555. SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index';
  556. }
  557. } {1}
  558. do_test index-16.5 {
  559. execsql {
  560. DROP TABLE t7;
  561. CREATE TABLE t7(c, d , UNIQUE(c), PRIMARY KEY(c, d) );
  562. SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index';
  563. }
  564. } {2}
  565. # Test that automatically create indices are named correctly. The current
  566. # convention is: "sqlite_autoindex_<table name>_<integer>"
  567. #
  568. # Then check that it is an error to try to drop any automtically created
  569. # indices.
  570. do_test index-17.1 {
  571. execsql {
  572. DROP TABLE t7;
  573. CREATE TABLE t7(c, d UNIQUE, UNIQUE(c), PRIMARY KEY(c, d) );
  574. SELECT name FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index';
  575. }
  576. } {sqlite_autoindex_t7_1 sqlite_autoindex_t7_2 sqlite_autoindex_t7_3}
  577. do_test index-17.2 {
  578. catchsql {
  579. DROP INDEX sqlite_autoindex_t7_1;
  580. }
  581. } {1 {index associated with UNIQUE or PRIMARY KEY constraint cannot be dropped}}
  582. do_test index-17.3 {
  583. catchsql {
  584. DROP INDEX IF EXISTS sqlite_autoindex_t7_1;
  585. }
  586. } {1 {index associated with UNIQUE or PRIMARY KEY constraint cannot be dropped}}
  587. do_test index-17.4 {
  588. catchsql {
  589. DROP INDEX IF EXISTS no_such_index;
  590. }
  591. } {0 {}}
  592. # The following tests ensure that it is not possible to explicitly name
  593. # a schema object with a name beginning with "sqlite_". Granted that is a
  594. # little outside the focus of this test scripts, but this has got to be
  595. # tested somewhere.
  596. do_test index-18.1 {
  597. catchsql {
  598. CREATE TABLE sqlite_t1(a, b, c);
  599. }
  600. } {1 {object name reserved for internal use: sqlite_t1}}
  601. do_test index-18.2 {
  602. catchsql {
  603. CREATE INDEX sqlite_i1 ON t7(c);
  604. }
  605. } {1 {object name reserved for internal use: sqlite_i1}}
  606. ifcapable view {
  607. do_test index-18.3 {
  608. catchsql {
  609. CREATE VIEW sqlite_v1 AS SELECT * FROM t7;
  610. }
  611. } {1 {object name reserved for internal use: sqlite_v1}}
  612. } ;# ifcapable view
  613. ifcapable {trigger} {
  614. do_test index-18.4 {
  615. catchsql {
  616. CREATE TRIGGER sqlite_tr1 BEFORE INSERT ON t7 BEGIN SELECT 1; END;
  617. }
  618. } {1 {object name reserved for internal use: sqlite_tr1}}
  619. }
  620. do_test index-18.5 {
  621. execsql {
  622. DROP TABLE t7;
  623. }
  624. } {}
  625. # These tests ensure that if multiple table definition constraints are
  626. # implemented by a single indice, the correct ON CONFLICT policy applies.
  627. ifcapable conflict {
  628. do_test index-19.1 {
  629. execsql {
  630. CREATE TABLE t7(a UNIQUE PRIMARY KEY);
  631. CREATE TABLE t8(a UNIQUE PRIMARY KEY ON CONFLICT ROLLBACK);
  632. INSERT INTO t7 VALUES(1);
  633. INSERT INTO t8 VALUES(1);
  634. }
  635. } {}
  636. do_test index-19.2 {
  637. catchsql {
  638. BEGIN;
  639. INSERT INTO t7 VALUES(1);
  640. }
  641. } {1 {column a is not unique}}
  642. do_test index-19.3 {
  643. catchsql {
  644. BEGIN;
  645. }
  646. } {1 {cannot start a transaction within a transaction}}
  647. do_test index-19.4 {
  648. catchsql {
  649. INSERT INTO t8 VALUES(1);
  650. }
  651. } {1 {column a is not unique}}
  652. do_test index-19.5 {
  653. catchsql {
  654. BEGIN;
  655. COMMIT;
  656. }
  657. } {0 {}}
  658. do_test index-19.6 {
  659. catchsql {
  660. DROP TABLE t7;
  661. DROP TABLE t8;
  662. CREATE TABLE t7(
  663. a PRIMARY KEY ON CONFLICT FAIL,
  664. UNIQUE(a) ON CONFLICT IGNORE
  665. );
  666. }
  667. } {1 {conflicting ON CONFLICT clauses specified}}
  668. } ; # end of "ifcapable conflict" block
  669. ifcapable {reindex} {
  670. do_test index-19.7 {
  671. execsql REINDEX
  672. } {}
  673. }
  674. integrity_check index-19.8
  675. # Drop index with a quoted name. Ticket #695.
  676. #
  677. do_test index-20.1 {
  678. execsql {
  679. CREATE INDEX "t6i2" ON t6(c);
  680. DROP INDEX "t6i2";
  681. }
  682. } {}
  683. do_test index-20.2 {
  684. execsql {
  685. DROP INDEX "t6i1";
  686. }
  687. } {}
  688. finish_test