unique.test 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.4 2002/05/26 20:54:35 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 {constraint failed}}
  49. do_test unique-1.4 {
  50. execsql {
  51. SELECT * FROM t1 ORDER BY a;
  52. }
  53. } {1 2 3}
  54. do_test unique-1.5 {
  55. catchsql {
  56. INSERT INTO t1(a,b,c) VALUES(3,2,4)
  57. }
  58. } {1 {constraint failed}}
  59. do_test unique-1.6 {
  60. execsql {
  61. SELECT * FROM t1 ORDER BY a;
  62. }
  63. } {1 2 3}
  64. do_test unique-1.7 {
  65. catchsql {
  66. INSERT INTO t1(a,b,c) VALUES(3,4,5)
  67. }
  68. } {0 {}}
  69. do_test unique-1.8 {
  70. execsql {
  71. SELECT * FROM t1 ORDER BY a;
  72. }
  73. } {1 2 3 3 4 5}
  74. do_test unique-2.0 {
  75. execsql {
  76. DROP TABLE t1;
  77. CREATE TABLE t2(a int, b int);
  78. INSERT INTO t2(a,b) VALUES(1,2);
  79. INSERT INTO t2(a,b) VALUES(3,4);
  80. SELECT * FROM t2 ORDER BY a;
  81. }
  82. } {1 2 3 4}
  83. do_test unique-2.1 {
  84. catchsql {
  85. CREATE UNIQUE INDEX i2 ON t2(a)
  86. }
  87. } {0 {}}
  88. do_test unique-2.2 {
  89. catchsql {
  90. SELECT * FROM t2 ORDER BY a
  91. }
  92. } {0 {1 2 3 4}}
  93. do_test unique-2.3 {
  94. catchsql {
  95. INSERT INTO t2 VALUES(1,5);
  96. }
  97. } {1 {constraint failed}}
  98. do_test unique-2.4 {
  99. catchsql {
  100. SELECT * FROM t2 ORDER BY a
  101. }
  102. } {0 {1 2 3 4}}
  103. do_test unique-2.5 {
  104. catchsql {
  105. DROP INDEX i2;
  106. SELECT * FROM t2 ORDER BY a;
  107. }
  108. } {0 {1 2 3 4}}
  109. do_test unique-2.6 {
  110. catchsql {
  111. INSERT INTO t2 VALUES(1,5)
  112. }
  113. } {0 {}}
  114. do_test unique-2.7 {
  115. catchsql {
  116. SELECT * FROM t2 ORDER BY a, b;
  117. }
  118. } {0 {1 2 1 5 3 4}}
  119. do_test unique-2.8 {
  120. catchsql {
  121. CREATE UNIQUE INDEX i2 ON t2(a);
  122. }
  123. } {1 {constraint failed}}
  124. do_test unique-2.9 {
  125. catchsql {
  126. CREATE INDEX i2 ON t2(a);
  127. }
  128. } {0 {}}
  129. # Test the UNIQUE keyword as used on two or more fields.
  130. #
  131. do_test unique-3.1 {
  132. catchsql {
  133. CREATE TABLE t3(
  134. a int,
  135. b int,
  136. c int,
  137. d int,
  138. unique(a,c,d)
  139. );
  140. }
  141. } {0 {}}
  142. do_test unique-3.2 {
  143. catchsql {
  144. INSERT INTO t3(a,b,c,d) VALUES(1,2,3,4);
  145. SELECT * FROM t3 ORDER BY a,b,c,d;
  146. }
  147. } {0 {1 2 3 4}}
  148. do_test unique-3.3 {
  149. catchsql {
  150. INSERT INTO t3(a,b,c,d) VALUES(1,2,3,5);
  151. SELECT * FROM t3 ORDER BY a,b,c,d;
  152. }
  153. } {0 {1 2 3 4 1 2 3 5}}
  154. do_test unique-3.4 {
  155. catchsql {
  156. INSERT INTO t3(a,b,c,d) VALUES(1,4,3,5);
  157. SELECT * FROM t3 ORDER BY a,b,c,d;
  158. }
  159. } {1 {constraint failed}}
  160. # Make sure NULLs are distinct as far as the UNIQUE tests are
  161. # concerned.
  162. #
  163. do_test unique-4.1 {
  164. execsql {
  165. CREATE TABLE t4(a UNIQUE, b, c, UNIQUE(b,c));
  166. INSERT INTO t4 VALUES(1,2,3);
  167. INSERT INTO t4 VALUES(NULL, 2, NULL);
  168. SELECT * FROM t4;
  169. }
  170. } {1 2 3 {} 2 {}}
  171. do_test unique-4.2 {
  172. catchsql {
  173. INSERT INTO t4 VALUES(NULL, 3, 4);
  174. }
  175. } {0 {}}
  176. do_test unique-4.3 {
  177. execsql {
  178. SELECT * FROM t4
  179. }
  180. } {1 2 3 {} 2 {} {} 3 4}
  181. do_test unique-4.4 {
  182. catchsql {
  183. INSERT INTO t4 VALUES(2, 2, NULL);
  184. }
  185. } {0 {}}
  186. do_test unique-4.5 {
  187. execsql {
  188. SELECT * FROM t4
  189. }
  190. } {1 2 3 {} 2 {} {} 3 4 2 2 {}}
  191. finish_test