unique.test 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. # 2001 September 27
  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 UNIQUE INDEX statement,
  13. # and primary keys, and the UNIQUE constraint on table columns
  14. #
  15. # $Id: unique.test,v 1.9 2009/05/02 15:46:47 drh Exp $
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. # Try to create a table with two primary keys.
  19. # (This is allowed in SQLite even that it is not valid SQL)
  20. #
  21. do_test unique-1.1 {
  22. catchsql {
  23. CREATE TABLE t1(
  24. a int PRIMARY KEY,
  25. b int PRIMARY KEY,
  26. c text
  27. );
  28. }
  29. } {1 {table "t1" has more than one primary key}}
  30. do_test unique-1.1b {
  31. catchsql {
  32. CREATE TABLE t1(
  33. a int PRIMARY KEY,
  34. b int UNIQUE,
  35. c text
  36. );
  37. }
  38. } {0 {}}
  39. do_test unique-1.2 {
  40. catchsql {
  41. INSERT INTO t1(a,b,c) VALUES(1,2,3)
  42. }
  43. } {0 {}}
  44. do_test unique-1.3 {
  45. catchsql {
  46. INSERT INTO t1(a,b,c) VALUES(1,3,4)
  47. }
  48. } {1 {column a is not unique}}
  49. verify_ex_errcode unique-1.3b SQLITE_CONSTRAINT_UNIQUE
  50. do_test unique-1.4 {
  51. execsql {
  52. SELECT * FROM t1 ORDER BY a;
  53. }
  54. } {1 2 3}
  55. do_test unique-1.5 {
  56. catchsql {
  57. INSERT INTO t1(a,b,c) VALUES(3,2,4)
  58. }
  59. } {1 {column b is not unique}}
  60. verify_ex_errcode unique-1.5b SQLITE_CONSTRAINT_UNIQUE
  61. do_test unique-1.6 {
  62. execsql {
  63. SELECT * FROM t1 ORDER BY a;
  64. }
  65. } {1 2 3}
  66. do_test unique-1.7 {
  67. catchsql {
  68. INSERT INTO t1(a,b,c) VALUES(3,4,5)
  69. }
  70. } {0 {}}
  71. do_test unique-1.8 {
  72. execsql {
  73. SELECT * FROM t1 ORDER BY a;
  74. }
  75. } {1 2 3 3 4 5}
  76. integrity_check unique-1.9
  77. do_test unique-2.0 {
  78. execsql {
  79. DROP TABLE t1;
  80. CREATE TABLE t2(a int, b int);
  81. INSERT INTO t2(a,b) VALUES(1,2);
  82. INSERT INTO t2(a,b) VALUES(3,4);
  83. SELECT * FROM t2 ORDER BY a;
  84. }
  85. } {1 2 3 4}
  86. do_test unique-2.1 {
  87. catchsql {
  88. CREATE UNIQUE INDEX i2 ON t2(a)
  89. }
  90. } {0 {}}
  91. do_test unique-2.2 {
  92. catchsql {
  93. SELECT * FROM t2 ORDER BY a
  94. }
  95. } {0 {1 2 3 4}}
  96. do_test unique-2.3 {
  97. catchsql {
  98. INSERT INTO t2 VALUES(1,5);
  99. }
  100. } {1 {column a is not unique}}
  101. verify_ex_errcode unique-2.3b SQLITE_CONSTRAINT_UNIQUE
  102. do_test unique-2.4 {
  103. catchsql {
  104. SELECT * FROM t2 ORDER BY a
  105. }
  106. } {0 {1 2 3 4}}
  107. do_test unique-2.5 {
  108. catchsql {
  109. DROP INDEX i2;
  110. SELECT * FROM t2 ORDER BY a;
  111. }
  112. } {0 {1 2 3 4}}
  113. do_test unique-2.6 {
  114. catchsql {
  115. INSERT INTO t2 VALUES(1,5)
  116. }
  117. } {0 {}}
  118. do_test unique-2.7 {
  119. catchsql {
  120. SELECT * FROM t2 ORDER BY a, b;
  121. }
  122. } {0 {1 2 1 5 3 4}}
  123. do_test unique-2.8 {
  124. catchsql {
  125. CREATE UNIQUE INDEX i2 ON t2(a);
  126. }
  127. } {1 {indexed columns are not unique}}
  128. verify_ex_errcode unique-2.8b SQLITE_CONSTRAINT_UNIQUE
  129. do_test unique-2.9 {
  130. catchsql {
  131. CREATE INDEX i2 ON t2(a);
  132. }
  133. } {0 {}}
  134. integrity_check unique-2.10
  135. # Test the UNIQUE keyword as used on two or more fields.
  136. #
  137. do_test unique-3.1 {
  138. catchsql {
  139. CREATE TABLE t3(
  140. a int,
  141. b int,
  142. c int,
  143. d int,
  144. unique(a,c,d)
  145. );
  146. }
  147. } {0 {}}
  148. do_test unique-3.2 {
  149. catchsql {
  150. INSERT INTO t3(a,b,c,d) VALUES(1,2,3,4);
  151. SELECT * FROM t3 ORDER BY a,b,c,d;
  152. }
  153. } {0 {1 2 3 4}}
  154. do_test unique-3.3 {
  155. catchsql {
  156. INSERT INTO t3(a,b,c,d) VALUES(1,2,3,5);
  157. SELECT * FROM t3 ORDER BY a,b,c,d;
  158. }
  159. } {0 {1 2 3 4 1 2 3 5}}
  160. do_test unique-3.4 {
  161. catchsql {
  162. INSERT INTO t3(a,b,c,d) VALUES(1,4,3,5);
  163. SELECT * FROM t3 ORDER BY a,b,c,d;
  164. }
  165. } {1 {columns a, c, d are not unique}}
  166. verify_ex_errcode unique-3.4b SQLITE_CONSTRAINT_UNIQUE
  167. integrity_check unique-3.5
  168. # Make sure NULLs are distinct as far as the UNIQUE tests are
  169. # concerned.
  170. #
  171. do_test unique-4.1 {
  172. execsql {
  173. CREATE TABLE t4(a UNIQUE, b, c, UNIQUE(b,c));
  174. INSERT INTO t4 VALUES(1,2,3);
  175. INSERT INTO t4 VALUES(NULL, 2, NULL);
  176. SELECT * FROM t4;
  177. }
  178. } {1 2 3 {} 2 {}}
  179. do_test unique-4.2 {
  180. catchsql {
  181. INSERT INTO t4 VALUES(NULL, 3, 4);
  182. }
  183. } {0 {}}
  184. do_test unique-4.3 {
  185. execsql {
  186. SELECT * FROM t4
  187. }
  188. } {1 2 3 {} 2 {} {} 3 4}
  189. do_test unique-4.4 {
  190. catchsql {
  191. INSERT INTO t4 VALUES(2, 2, NULL);
  192. }
  193. } {0 {}}
  194. do_test unique-4.5 {
  195. execsql {
  196. SELECT * FROM t4
  197. }
  198. } {1 2 3 {} 2 {} {} 3 4 2 2 {}}
  199. # Ticket #1301. Any NULL value in a set of unique columns should
  200. # cause the rows to be distinct.
  201. #
  202. do_test unique-4.6 {
  203. catchsql {
  204. INSERT INTO t4 VALUES(NULL, 2, NULL);
  205. }
  206. } {0 {}}
  207. do_test unique-4.7 {
  208. execsql {SELECT * FROM t4}
  209. } {1 2 3 {} 2 {} {} 3 4 2 2 {} {} 2 {}}
  210. do_test unique-4.8 {
  211. catchsql {CREATE UNIQUE INDEX i4a ON t4(a,b)}
  212. } {0 {}}
  213. do_test unique-4.9 {
  214. catchsql {CREATE UNIQUE INDEX i4b ON t4(a,b,c)}
  215. } {0 {}}
  216. do_test unique-4.10 {
  217. catchsql {CREATE UNIQUE INDEX i4c ON t4(b)}
  218. } {1 {indexed columns are not unique}}
  219. verify_ex_errcode unique-4.10b SQLITE_CONSTRAINT_UNIQUE
  220. integrity_check unique-4.99
  221. # Test the error message generation logic. In particular, make sure we
  222. # do not overflow the static buffer used to generate the error message.
  223. #
  224. do_test unique-5.1 {
  225. execsql {
  226. CREATE TABLE t5(
  227. first_column_with_long_name,
  228. second_column_with_long_name,
  229. third_column_with_long_name,
  230. fourth_column_with_long_name,
  231. fifth_column_with_long_name,
  232. sixth_column_with_long_name,
  233. UNIQUE(
  234. first_column_with_long_name,
  235. second_column_with_long_name,
  236. third_column_with_long_name,
  237. fourth_column_with_long_name,
  238. fifth_column_with_long_name,
  239. sixth_column_with_long_name
  240. )
  241. );
  242. INSERT INTO t5 VALUES(1,2,3,4,5,6);
  243. SELECT * FROM t5;
  244. }
  245. } {1 2 3 4 5 6}
  246. do_test unique-5.2 {
  247. catchsql {
  248. INSERT INTO t5 VALUES(1,2,3,4,5,6);
  249. }
  250. } {1 {columns first_column_with_long_name, second_column_with_long_name, third_column_with_long_name, fourth_column_with_long_name, fifth_column_with_long_name, sixth_column_with_long_name are not unique}}
  251. verify_ex_errcode unique-5.2b SQLITE_CONSTRAINT_UNIQUE
  252. finish_test