notnull.test 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. # 2002 January 29
  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 NOT NULL constraint.
  14. #
  15. # $Id: notnull.test,v 1.2 2002/01/31 15:54:23 drh Exp $
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. do_test notnull-1.0 {
  19. execsql {
  20. CREATE TABLE t1 (
  21. a NOT NULL,
  22. b NOT NULL DEFAULT 5,
  23. c NOT NULL ON CONFLICT REPLACE DEFAULT 6,
  24. d NOT NULL ON CONFLICT IGNORE DEFAULT 7,
  25. e NOT NULL ON CONFLICT ABORT DEFAULT 8
  26. );
  27. SELECT * FROM t1;
  28. }
  29. } {}
  30. do_test notnull-1.1 {
  31. catchsql {
  32. DELETE FROM t1;
  33. INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5);
  34. SELECT * FROM t1 order by a;
  35. }
  36. } {0 {1 2 3 4 5}}
  37. do_test notnull-1.2 {
  38. catchsql {
  39. DELETE FROM t1;
  40. INSERT INTO t1(b,c,d,e) VALUES(2,3,4,5);
  41. SELECT * FROM t1 order by a;
  42. }
  43. } {1 {constraint failed}}
  44. do_test notnull-1.3 {
  45. catchsql {
  46. DELETE FROM t1;
  47. INSERT OR IGNORE INTO t1(b,c,d,e) VALUES(2,3,4,5);
  48. SELECT * FROM t1 order by a;
  49. }
  50. } {0 {}}
  51. do_test notnull-1.4 {
  52. catchsql {
  53. DELETE FROM t1;
  54. INSERT OR REPLACE INTO t1(b,c,d,e) VALUES(2,3,4,5);
  55. SELECT * FROM t1 order by a;
  56. }
  57. } {1 {constraint failed}}
  58. do_test notnull-1.5 {
  59. catchsql {
  60. DELETE FROM t1;
  61. INSERT OR ABORT INTO t1(b,c,d,e) VALUES(2,3,4,5);
  62. SELECT * FROM t1 order by a;
  63. }
  64. } {1 {constraint failed}}
  65. do_test notnull-1.6 {
  66. catchsql {
  67. DELETE FROM t1;
  68. INSERT INTO t1(a,c,d,e) VALUES(1,3,4,5);
  69. SELECT * FROM t1 order by a;
  70. }
  71. } {0 {1 5 3 4 5}}
  72. do_test notnull-1.7 {
  73. catchsql {
  74. DELETE FROM t1;
  75. INSERT OR IGNORE INTO t1(a,c,d,e) VALUES(1,3,4,5);
  76. SELECT * FROM t1 order by a;
  77. }
  78. } {0 {1 5 3 4 5}}
  79. do_test notnull-1.8 {
  80. catchsql {
  81. DELETE FROM t1;
  82. INSERT OR REPLACE INTO t1(a,c,d,e) VALUES(1,3,4,5);
  83. SELECT * FROM t1 order by a;
  84. }
  85. } {0 {1 5 3 4 5}}
  86. do_test notnull-1.9 {
  87. catchsql {
  88. DELETE FROM t1;
  89. INSERT OR ABORT INTO t1(a,c,d,e) VALUES(1,3,4,5);
  90. SELECT * FROM t1 order by a;
  91. }
  92. } {0 {1 5 3 4 5}}
  93. do_test notnull-1.10 {
  94. catchsql {
  95. DELETE FROM t1;
  96. INSERT INTO t1(a,b,c,d,e) VALUES(1,null,3,4,5);
  97. SELECT * FROM t1 order by a;
  98. }
  99. } {1 {constraint failed}}
  100. do_test notnull-1.11 {
  101. catchsql {
  102. DELETE FROM t1;
  103. INSERT OR IGNORE INTO t1(a,b,c,d,e) VALUES(1,null,3,4,5);
  104. SELECT * FROM t1 order by a;
  105. }
  106. } {0 {}}
  107. do_test notnull-1.12 {
  108. catchsql {
  109. DELETE FROM t1;
  110. INSERT OR REPLACE INTO t1(a,b,c,d,e) VALUES(1,null,3,4,5);
  111. SELECT * FROM t1 order by a;
  112. }
  113. } {0 {1 5 3 4 5}}
  114. do_test notnull-1.13 {
  115. catchsql {
  116. DELETE FROM t1;
  117. INSERT INTO t1(a,b,c,d,e) VALUES(1,2,null,4,5);
  118. SELECT * FROM t1 order by a;
  119. }
  120. } {0 {1 2 6 4 5}}
  121. do_test notnull-1.14 {
  122. catchsql {
  123. DELETE FROM t1;
  124. INSERT OR IGNORE INTO t1(a,b,c,d,e) VALUES(1,2,null,4,5);
  125. SELECT * FROM t1 order by a;
  126. }
  127. } {0 {}}
  128. do_test notnull-1.15 {
  129. catchsql {
  130. DELETE FROM t1;
  131. INSERT OR REPLACE INTO t1(a,b,c,d,e) VALUES(1,2,null,4,5);
  132. SELECT * FROM t1 order by a;
  133. }
  134. } {0 {1 2 6 4 5}}
  135. do_test notnull-1.16 {
  136. catchsql {
  137. DELETE FROM t1;
  138. INSERT OR ABORT INTO t1(a,b,c,d,e) VALUES(1,2,null,4,5);
  139. SELECT * FROM t1 order by a;
  140. }
  141. } {1 {constraint failed}}
  142. do_test notnull-1.17 {
  143. catchsql {
  144. DELETE FROM t1;
  145. INSERT OR ABORT INTO t1(a,b,c,d,e) VALUES(1,2,3,null,5);
  146. SELECT * FROM t1 order by a;
  147. }
  148. } {1 {constraint failed}}
  149. do_test notnull-1.18 {
  150. catchsql {
  151. DELETE FROM t1;
  152. INSERT OR ABORT INTO t1(a,b,c,e) VALUES(1,2,3,5);
  153. SELECT * FROM t1 order by a;
  154. }
  155. } {0 {1 2 3 7 5}}
  156. do_test notnull-1.19 {
  157. catchsql {
  158. DELETE FROM t1;
  159. INSERT INTO t1(a,b,c,d) VALUES(1,2,3,4);
  160. SELECT * FROM t1 order by a;
  161. }
  162. } {0 {1 2 3 4 8}}
  163. do_test notnull-1.20 {
  164. catchsql {
  165. DELETE FROM t1;
  166. INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,null);
  167. SELECT * FROM t1 order by a;
  168. }
  169. } {1 {constraint failed}}
  170. do_test notnull-1.21 {
  171. catchsql {
  172. DELETE FROM t1;
  173. INSERT OR REPLACE INTO t1(e,d,c,b,a) VALUES(1,2,3,null,5);
  174. SELECT * FROM t1 order by a;
  175. }
  176. } {0 {5 5 3 2 1}}
  177. do_test notnull-2.1 {
  178. catchsql {
  179. DELETE FROM t1;
  180. INSERT INTO t1 VALUES(1,2,3,4,5);
  181. UPDATE t1 SET a=null;
  182. SELECT * FROM t1 ORDER BY a;
  183. }
  184. } {1 {constraint failed}}
  185. do_test notnull-2.2 {
  186. catchsql {
  187. DELETE FROM t1;
  188. INSERT INTO t1 VALUES(1,2,3,4,5);
  189. UPDATE OR REPLACE t1 SET a=null;
  190. SELECT * FROM t1 ORDER BY a;
  191. }
  192. } {1 {constraint failed}}
  193. do_test notnull-2.3 {
  194. catchsql {
  195. DELETE FROM t1;
  196. INSERT INTO t1 VALUES(1,2,3,4,5);
  197. UPDATE OR IGNORE t1 SET a=null;
  198. SELECT * FROM t1 ORDER BY a;
  199. }
  200. } {0 {1 2 3 4 5}}
  201. do_test notnull-2.4 {
  202. catchsql {
  203. DELETE FROM t1;
  204. INSERT INTO t1 VALUES(1,2,3,4,5);
  205. UPDATE OR ABORT t1 SET a=null;
  206. SELECT * FROM t1 ORDER BY a;
  207. }
  208. } {1 {constraint failed}}
  209. do_test notnull-2.5 {
  210. catchsql {
  211. DELETE FROM t1;
  212. INSERT INTO t1 VALUES(1,2,3,4,5);
  213. UPDATE t1 SET b=null;
  214. SELECT * FROM t1 ORDER BY a;
  215. }
  216. } {1 {constraint failed}}
  217. do_test notnull-2.6 {
  218. catchsql {
  219. DELETE FROM t1;
  220. INSERT INTO t1 VALUES(1,2,3,4,5);
  221. UPDATE OR REPLACE t1 SET b=null, d=e, e=d;
  222. SELECT * FROM t1 ORDER BY a;
  223. }
  224. } {0 {1 5 3 5 4}}
  225. do_test notnull-2.7 {
  226. catchsql {
  227. DELETE FROM t1;
  228. INSERT INTO t1 VALUES(1,2,3,4,5);
  229. UPDATE OR IGNORE t1 SET b=null, d=e, e=d;
  230. SELECT * FROM t1 ORDER BY a;
  231. }
  232. } {0 {1 2 3 4 5}}
  233. do_test notnull-2.8 {
  234. catchsql {
  235. DELETE FROM t1;
  236. INSERT INTO t1 VALUES(1,2,3,4,5);
  237. UPDATE t1 SET c=null, d=e, e=d;
  238. SELECT * FROM t1 ORDER BY a;
  239. }
  240. } {0 {1 2 6 5 4}}
  241. do_test notnull-2.9 {
  242. catchsql {
  243. DELETE FROM t1;
  244. INSERT INTO t1 VALUES(1,2,3,4,5);
  245. UPDATE t1 SET d=null, a=b, b=a;
  246. SELECT * FROM t1 ORDER BY a;
  247. }
  248. } {0 {1 2 3 4 5}}
  249. do_test notnull-2.10 {
  250. catchsql {
  251. DELETE FROM t1;
  252. INSERT INTO t1 VALUES(1,2,3,4,5);
  253. UPDATE t1 SET e=null, a=b, b=a;
  254. SELECT * FROM t1 ORDER BY a;
  255. }
  256. } {1 {constraint failed}}
  257. do_test notnull-3.0 {
  258. execsql {
  259. CREATE INDEX t1a ON t1(a);
  260. CREATE INDEX t1b ON t1(b);
  261. CREATE INDEX t1c ON t1(c);
  262. CREATE INDEX t1d ON t1(d);
  263. CREATE INDEX t1e ON t1(e);
  264. CREATE INDEX t1abc ON t1(a,b,c);
  265. }
  266. } {}
  267. do_test notnull-3.1 {
  268. catchsql {
  269. DELETE FROM t1;
  270. INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5);
  271. SELECT * FROM t1 order by a;
  272. }
  273. } {0 {1 2 3 4 5}}
  274. do_test notnull-3.2 {
  275. catchsql {
  276. DELETE FROM t1;
  277. INSERT INTO t1(b,c,d,e) VALUES(2,3,4,5);
  278. SELECT * FROM t1 order by a;
  279. }
  280. } {1 {constraint failed}}
  281. do_test notnull-3.3 {
  282. catchsql {
  283. DELETE FROM t1;
  284. INSERT OR IGNORE INTO t1(b,c,d,e) VALUES(2,3,4,5);
  285. SELECT * FROM t1 order by a;
  286. }
  287. } {0 {}}
  288. do_test notnull-3.4 {
  289. catchsql {
  290. DELETE FROM t1;
  291. INSERT OR REPLACE INTO t1(b,c,d,e) VALUES(2,3,4,5);
  292. SELECT * FROM t1 order by a;
  293. }
  294. } {1 {constraint failed}}
  295. do_test notnull-3.5 {
  296. catchsql {
  297. DELETE FROM t1;
  298. INSERT OR ABORT INTO t1(b,c,d,e) VALUES(2,3,4,5);
  299. SELECT * FROM t1 order by a;
  300. }
  301. } {1 {constraint failed}}
  302. do_test notnull-3.6 {
  303. catchsql {
  304. DELETE FROM t1;
  305. INSERT INTO t1(a,c,d,e) VALUES(1,3,4,5);
  306. SELECT * FROM t1 order by a;
  307. }
  308. } {0 {1 5 3 4 5}}
  309. do_test notnull-3.7 {
  310. catchsql {
  311. DELETE FROM t1;
  312. INSERT OR IGNORE INTO t1(a,c,d,e) VALUES(1,3,4,5);
  313. SELECT * FROM t1 order by a;
  314. }
  315. } {0 {1 5 3 4 5}}
  316. do_test notnull-3.8 {
  317. catchsql {
  318. DELETE FROM t1;
  319. INSERT OR REPLACE INTO t1(a,c,d,e) VALUES(1,3,4,5);
  320. SELECT * FROM t1 order by a;
  321. }
  322. } {0 {1 5 3 4 5}}
  323. do_test notnull-3.9 {
  324. catchsql {
  325. DELETE FROM t1;
  326. INSERT OR ABORT INTO t1(a,c,d,e) VALUES(1,3,4,5);
  327. SELECT * FROM t1 order by a;
  328. }
  329. } {0 {1 5 3 4 5}}
  330. do_test notnull-3.10 {
  331. catchsql {
  332. DELETE FROM t1;
  333. INSERT INTO t1(a,b,c,d,e) VALUES(1,null,3,4,5);
  334. SELECT * FROM t1 order by a;
  335. }
  336. } {1 {constraint failed}}
  337. do_test notnull-3.11 {
  338. catchsql {
  339. DELETE FROM t1;
  340. INSERT OR IGNORE INTO t1(a,b,c,d,e) VALUES(1,null,3,4,5);
  341. SELECT * FROM t1 order by a;
  342. }
  343. } {0 {}}
  344. do_test notnull-3.12 {
  345. catchsql {
  346. DELETE FROM t1;
  347. INSERT OR REPLACE INTO t1(a,b,c,d,e) VALUES(1,null,3,4,5);
  348. SELECT * FROM t1 order by a;
  349. }
  350. } {0 {1 5 3 4 5}}
  351. do_test notnull-3.13 {
  352. catchsql {
  353. DELETE FROM t1;
  354. INSERT INTO t1(a,b,c,d,e) VALUES(1,2,null,4,5);
  355. SELECT * FROM t1 order by a;
  356. }
  357. } {0 {1 2 6 4 5}}
  358. do_test notnull-3.14 {
  359. catchsql {
  360. DELETE FROM t1;
  361. INSERT OR IGNORE INTO t1(a,b,c,d,e) VALUES(1,2,null,4,5);
  362. SELECT * FROM t1 order by a;
  363. }
  364. } {0 {}}
  365. do_test notnull-3.15 {
  366. catchsql {
  367. DELETE FROM t1;
  368. INSERT OR REPLACE INTO t1(a,b,c,d,e) VALUES(1,2,null,4,5);
  369. SELECT * FROM t1 order by a;
  370. }
  371. } {0 {1 2 6 4 5}}
  372. do_test notnull-3.16 {
  373. catchsql {
  374. DELETE FROM t1;
  375. INSERT OR ABORT INTO t1(a,b,c,d,e) VALUES(1,2,null,4,5);
  376. SELECT * FROM t1 order by a;
  377. }
  378. } {1 {constraint failed}}
  379. do_test notnull-3.17 {
  380. catchsql {
  381. DELETE FROM t1;
  382. INSERT OR ABORT INTO t1(a,b,c,d,e) VALUES(1,2,3,null,5);
  383. SELECT * FROM t1 order by a;
  384. }
  385. } {1 {constraint failed}}
  386. do_test notnull-3.18 {
  387. catchsql {
  388. DELETE FROM t1;
  389. INSERT OR ABORT INTO t1(a,b,c,e) VALUES(1,2,3,5);
  390. SELECT * FROM t1 order by a;
  391. }
  392. } {0 {1 2 3 7 5}}
  393. do_test notnull-3.19 {
  394. catchsql {
  395. DELETE FROM t1;
  396. INSERT INTO t1(a,b,c,d) VALUES(1,2,3,4);
  397. SELECT * FROM t1 order by a;
  398. }
  399. } {0 {1 2 3 4 8}}
  400. do_test notnull-3.20 {
  401. catchsql {
  402. DELETE FROM t1;
  403. INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,null);
  404. SELECT * FROM t1 order by a;
  405. }
  406. } {1 {constraint failed}}
  407. do_test notnull-3.21 {
  408. catchsql {
  409. DELETE FROM t1;
  410. INSERT OR REPLACE INTO t1(e,d,c,b,a) VALUES(1,2,3,null,5);
  411. SELECT * FROM t1 order by a;
  412. }
  413. } {0 {5 5 3 2 1}}
  414. do_test notnull-4.1 {
  415. catchsql {
  416. DELETE FROM t1;
  417. INSERT INTO t1 VALUES(1,2,3,4,5);
  418. UPDATE t1 SET a=null;
  419. SELECT * FROM t1 ORDER BY a;
  420. }
  421. } {1 {constraint failed}}
  422. do_test notnull-4.2 {
  423. catchsql {
  424. DELETE FROM t1;
  425. INSERT INTO t1 VALUES(1,2,3,4,5);
  426. UPDATE OR REPLACE t1 SET a=null;
  427. SELECT * FROM t1 ORDER BY a;
  428. }
  429. } {1 {constraint failed}}
  430. do_test notnull-4.3 {
  431. catchsql {
  432. DELETE FROM t1;
  433. INSERT INTO t1 VALUES(1,2,3,4,5);
  434. UPDATE OR IGNORE t1 SET a=null;
  435. SELECT * FROM t1 ORDER BY a;
  436. }
  437. } {0 {1 2 3 4 5}}
  438. do_test notnull-4.4 {
  439. catchsql {
  440. DELETE FROM t1;
  441. INSERT INTO t1 VALUES(1,2,3,4,5);
  442. UPDATE OR ABORT t1 SET a=null;
  443. SELECT * FROM t1 ORDER BY a;
  444. }
  445. } {1 {constraint failed}}
  446. do_test notnull-4.5 {
  447. catchsql {
  448. DELETE FROM t1;
  449. INSERT INTO t1 VALUES(1,2,3,4,5);
  450. UPDATE t1 SET b=null;
  451. SELECT * FROM t1 ORDER BY a;
  452. }
  453. } {1 {constraint failed}}
  454. do_test notnull-4.6 {
  455. catchsql {
  456. DELETE FROM t1;
  457. INSERT INTO t1 VALUES(1,2,3,4,5);
  458. UPDATE OR REPLACE t1 SET b=null, d=e, e=d;
  459. SELECT * FROM t1 ORDER BY a;
  460. }
  461. } {0 {1 5 3 5 4}}
  462. do_test notnull-4.7 {
  463. catchsql {
  464. DELETE FROM t1;
  465. INSERT INTO t1 VALUES(1,2,3,4,5);
  466. UPDATE OR IGNORE t1 SET b=null, d=e, e=d;
  467. SELECT * FROM t1 ORDER BY a;
  468. }
  469. } {0 {1 2 3 4 5}}
  470. do_test notnull-4.8 {
  471. catchsql {
  472. DELETE FROM t1;
  473. INSERT INTO t1 VALUES(1,2,3,4,5);
  474. UPDATE t1 SET c=null, d=e, e=d;
  475. SELECT * FROM t1 ORDER BY a;
  476. }
  477. } {0 {1 2 6 5 4}}
  478. do_test notnull-4.9 {
  479. catchsql {
  480. DELETE FROM t1;
  481. INSERT INTO t1 VALUES(1,2,3,4,5);
  482. UPDATE t1 SET d=null, a=b, b=a;
  483. SELECT * FROM t1 ORDER BY a;
  484. }
  485. } {0 {1 2 3 4 5}}
  486. do_test notnull-4.10 {
  487. catchsql {
  488. DELETE FROM t1;
  489. INSERT INTO t1 VALUES(1,2,3,4,5);
  490. UPDATE t1 SET e=null, a=b, b=a;
  491. SELECT * FROM t1 ORDER BY a;
  492. }
  493. } {1 {constraint failed}}
  494. finish_test