Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: third_party/sqlite/src/test/e_createtable.test

Issue 901033002: Import SQLite 3.8.7.4. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Chromium changes to support SQLite 3.8.7.4. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # 2010 September 25 1 # 2010 September 25
2 # 2 #
3 # The author disclaims copyright to this source code. In place of 3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing: 4 # a legal notice, here is a blessing:
5 # 5 #
6 # May you do good and not evil. 6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others. 7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give. 8 # May you share freely, never taking more than you give.
9 # 9 #
10 #*********************************************************************** 10 #***********************************************************************
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 set master $a(name).sqlite_master 51 set master $a(name).sqlite_master
52 if {$dbname == "temp"} { set master sqlite_temp_master } 52 if {$dbname == "temp"} { set master sqlite_temp_master }
53 lappend res $dbname [ 53 lappend res $dbname [
54 db eval "SELECT DISTINCT tbl_name FROM $master ORDER BY tbl_name" 54 db eval "SELECT DISTINCT tbl_name FROM $master ORDER BY tbl_name"
55 ] 55 ]
56 } 56 }
57 set res 57 set res
58 } 58 }
59 59
60 60
61 # EVIDENCE-OF: R-25262-01881 -- syntax diagram type-name
62 #
63 do_createtable_tests 0.1.1 -repair { 61 do_createtable_tests 0.1.1 -repair {
64 drop_all_tables 62 drop_all_tables
65 } { 63 } {
66 1 "CREATE TABLE t1(c1 one)" {} 64 1 "CREATE TABLE t1(c1 one)" {}
67 2 "CREATE TABLE t1(c1 one two)" {} 65 2 "CREATE TABLE t1(c1 one two)" {}
68 3 "CREATE TABLE t1(c1 one two three)" {} 66 3 "CREATE TABLE t1(c1 one two three)" {}
69 4 "CREATE TABLE t1(c1 one two three four)" {} 67 4 "CREATE TABLE t1(c1 one two three four)" {}
70 5 "CREATE TABLE t1(c1 one two three four(14))" {} 68 5 "CREATE TABLE t1(c1 one two three four(14))" {}
71 6 "CREATE TABLE t1(c1 one two three four(14, 22))" {} 69 6 "CREATE TABLE t1(c1 one two three four(14, 22))" {}
72 7 "CREATE TABLE t1(c1 var(+14, -22.3))" {} 70 7 "CREATE TABLE t1(c1 var(+14, -22.3))" {}
73 8 "CREATE TABLE t1(c1 var(1.0e10))" {} 71 8 "CREATE TABLE t1(c1 var(1.0e10))" {}
74 } 72 }
75 do_createtable_tests 0.1.2 -error { 73 do_createtable_tests 0.1.2 -error {
76 near "%s": syntax error 74 near "%s": syntax error
77 } { 75 } {
78 1 "CREATE TABLE t1(c1 one(number))" {number} 76 1 "CREATE TABLE t1(c1 one(number))" {number}
79 } 77 }
80 78
81 79
82 # EVIDENCE-OF: R-18762-12428 -- syntax diagram column-constraint 80 # syntax diagram column-constraint
83 #
84 # Note: Not shown in the syntax diagram is the "NULL" constraint. This
85 # is the opposite of "NOT NULL" - it implies that the column may
86 # take a NULL value. This is the default anyway, so this type of
87 # constraint is rarely used.
88 # 81 #
89 do_createtable_tests 0.2.1 -repair { 82 do_createtable_tests 0.2.1 -repair {
90 drop_all_tables 83 drop_all_tables
91 execsql { CREATE TABLE t2(x PRIMARY KEY) } 84 execsql { CREATE TABLE t2(x PRIMARY KEY) }
92 } { 85 } {
93 1.1 "CREATE TABLE t1(c1 text PRIMARY KEY)" {} 86 1.1 "CREATE TABLE t1(c1 text PRIMARY KEY)" {}
94 1.2 "CREATE TABLE t1(c1 text PRIMARY KEY ASC)" {} 87 1.2 "CREATE TABLE t1(c1 text PRIMARY KEY ASC)" {}
95 1.3 "CREATE TABLE t1(c1 text PRIMARY KEY DESC)" {} 88 1.3 "CREATE TABLE t1(c1 text PRIMARY KEY DESC)" {}
96 1.4 "CREATE TABLE t1(c1 text CONSTRAINT cons PRIMARY KEY DESC)" {} 89 1.4 "CREATE TABLE t1(c1 text CONSTRAINT cons PRIMARY KEY DESC)" {}
97 90
(...skipping 26 matching lines...) Expand all
124 PRIMARY KEY NOT NULL UNIQUE CHECK(c1 IS 'ten') DEFAULT 123 REFERENCES t1 117 PRIMARY KEY NOT NULL UNIQUE CHECK(c1 IS 'ten') DEFAULT 123 REFERENCES t1
125 ); 118 );
126 } {} 119 } {}
127 8.2 { 120 8.2 {
128 CREATE TABLE t1(c1 121 CREATE TABLE t1(c1
129 REFERENCES t1 DEFAULT 123 CHECK(c1 IS 'ten') UNIQUE NOT NULL PRIMARY KEY 122 REFERENCES t1 DEFAULT 123 CHECK(c1 IS 'ten') UNIQUE NOT NULL PRIMARY KEY
130 ); 123 );
131 } {} 124 } {}
132 } 125 }
133 126
134 # EVIDENCE-OF: R-17905-31923 -- syntax diagram table-constraint 127 # -- syntax diagram table-constraint
135 # 128 #
136 do_createtable_tests 0.3.1 -repair { 129 do_createtable_tests 0.3.1 -repair {
137 drop_all_tables 130 drop_all_tables
138 execsql { CREATE TABLE t2(x PRIMARY KEY) } 131 execsql { CREATE TABLE t2(x PRIMARY KEY) }
139 } { 132 } {
140 1.1 "CREATE TABLE t1(c1, c2, PRIMARY KEY(c1))" {} 133 1.1 "CREATE TABLE t1(c1, c2, PRIMARY KEY(c1))" {}
141 1.2 "CREATE TABLE t1(c1, c2, PRIMARY KEY(c1, c2))" {} 134 1.2 "CREATE TABLE t1(c1, c2, PRIMARY KEY(c1, c2))" {}
142 1.3 "CREATE TABLE t1(c1, c2, PRIMARY KEY(c1, c2) ON CONFLICT IGNORE)" {} 135 1.3 "CREATE TABLE t1(c1, c2, PRIMARY KEY(c1, c2) ON CONFLICT IGNORE)" {}
143 136
144 2.1 "CREATE TABLE t1(c1, c2, UNIQUE(c1))" {} 137 2.1 "CREATE TABLE t1(c1, c2, UNIQUE(c1))" {}
145 2.2 "CREATE TABLE t1(c1, c2, UNIQUE(c1, c2))" {} 138 2.2 "CREATE TABLE t1(c1, c2, UNIQUE(c1, c2))" {}
146 2.3 "CREATE TABLE t1(c1, c2, UNIQUE(c1, c2) ON CONFLICT IGNORE)" {} 139 2.3 "CREATE TABLE t1(c1, c2, UNIQUE(c1, c2) ON CONFLICT IGNORE)" {}
147 140
148 3.1 "CREATE TABLE t1(c1, c2, CHECK(c1 IS NOT c2))" {} 141 3.1 "CREATE TABLE t1(c1, c2, CHECK(c1 IS NOT c2))" {}
149 142
150 4.1 "CREATE TABLE t1(c1, c2, FOREIGN KEY(c1) REFERENCES t2)" {} 143 4.1 "CREATE TABLE t1(c1, c2, FOREIGN KEY(c1) REFERENCES t2)" {}
151 } 144 }
152 145
153 # EVIDENCE-OF: R-18765-31171 -- syntax diagram column-def 146 # -- syntax diagram column-def
154 # 147 #
155 do_createtable_tests 0.4.1 -repair { 148 do_createtable_tests 0.4.1 -repair {
156 drop_all_tables 149 drop_all_tables
157 } { 150 } {
158 1 {CREATE TABLE t1( 151 1 {CREATE TABLE t1(
159 col1, 152 col1,
160 col2 TEXT, 153 col2 TEXT,
161 col3 INTEGER UNIQUE, 154 col3 INTEGER UNIQUE,
162 col4 VARCHAR(10, 10) PRIMARY KEY, 155 col4 VARCHAR(10, 10) PRIMARY KEY,
163 "name with spaces" REFERENCES t1 156 "name with spaces" REFERENCES t1
164 ); 157 );
165 } {} 158 } {}
166 } 159 }
167 160
168 # EVIDENCE-OF: R-59573-11075 -- syntax diagram create-table-stmt 161 # -- syntax diagram create-table-stmt
169 # 162 #
170 do_createtable_tests 0.5.1 -repair { 163 do_createtable_tests 0.5.1 -repair {
171 drop_all_tables 164 drop_all_tables
172 execsql { CREATE TABLE t2(a, b, c) } 165 execsql { CREATE TABLE t2(a, b, c) }
173 } { 166 } {
174 1 "CREATE TABLE t1(a, b, c)" {} 167 1 "CREATE TABLE t1(a, b, c)" {}
175 2 "CREATE TEMP TABLE t1(a, b, c)" {} 168 2 "CREATE TEMP TABLE t1(a, b, c)" {}
176 3 "CREATE TEMPORARY TABLE t1(a, b, c)" {} 169 3 "CREATE TEMPORARY TABLE t1(a, b, c)" {}
177 4 "CREATE TABLE IF NOT EXISTS t1(a, b, c)" {} 170 4 "CREATE TABLE IF NOT EXISTS t1(a, b, c)" {}
178 5 "CREATE TEMP TABLE IF NOT EXISTS t1(a, b, c)" {} 171 5 "CREATE TEMP TABLE IF NOT EXISTS t1(a, b, c)" {}
179 6 "CREATE TEMPORARY TABLE IF NOT EXISTS t1(a, b, c)" {} 172 6 "CREATE TEMPORARY TABLE IF NOT EXISTS t1(a, b, c)" {}
180 173
181 7 "CREATE TABLE main.t1(a, b, c)" {} 174 7 "CREATE TABLE main.t1(a, b, c)" {}
182 8 "CREATE TEMP TABLE temp.t1(a, b, c)" {} 175 8 "CREATE TEMP TABLE temp.t1(a, b, c)" {}
183 9 "CREATE TEMPORARY TABLE temp.t1(a, b, c)" {} 176 9 "CREATE TEMPORARY TABLE temp.t1(a, b, c)" {}
184 10 "CREATE TABLE IF NOT EXISTS main.t1(a, b, c)" {} 177 10 "CREATE TABLE IF NOT EXISTS main.t1(a, b, c)" {}
185 11 "CREATE TEMP TABLE IF NOT EXISTS temp.t1(a, b, c)" {} 178 11 "CREATE TEMP TABLE IF NOT EXISTS temp.t1(a, b, c)" {}
186 12 "CREATE TEMPORARY TABLE IF NOT EXISTS temp.t1(a, b, c)" {} 179 12 "CREATE TEMPORARY TABLE IF NOT EXISTS temp.t1(a, b, c)" {}
187 180
188 13 "CREATE TABLE t1 AS SELECT * FROM t2" {} 181 13 "CREATE TABLE t1 AS SELECT * FROM t2" {}
189 14 "CREATE TEMP TABLE t1 AS SELECT c, b, a FROM t2" {} 182 14 "CREATE TEMP TABLE t1 AS SELECT c, b, a FROM t2" {}
190 15 "CREATE TABLE t1 AS SELECT count(*), max(b), min(a) FROM t2" {} 183 15 "CREATE TABLE t1 AS SELECT count(*), max(b), min(a) FROM t2" {}
191 } 184 }
192 185
193 # EVIDENCE-OF: R-32138-02228 -- syntax diagram foreign-key-clause
194 # 186 #
195 # 1: Explicit parent-key columns. 187 # 1: Explicit parent-key columns.
196 # 2: Implicit child-key columns. 188 # 2: Implicit child-key columns.
197 # 189 #
198 # 1: MATCH FULL 190 # 1: MATCH FULL
199 # 2: MATCH PARTIAL 191 # 2: MATCH PARTIAL
200 # 3: MATCH SIMPLE 192 # 3: MATCH SIMPLE
201 # 4: MATCH STICK 193 # 4: MATCH STICK
202 # 5: 194 # 5:
203 # 195 #
(...skipping 659 matching lines...) Expand 10 before | Expand all | Expand 10 after
863 do_createtable_tests 3.2.3 -query { 855 do_createtable_tests 3.2.3 -query {
864 SELECT quote(x), quote(y), quote(z) FROM t1 856 SELECT quote(x), quote(y), quote(z) FROM t1
865 } -repair { 857 } -repair {
866 execsql { DELETE FROM t1 } 858 execsql { DELETE FROM t1 }
867 } { 859 } {
868 1 "INSERT INTO t1(x, y) VALUES('abc', 'xyz')" {'abc' 'xyz' NULL} 860 1 "INSERT INTO t1(x, y) VALUES('abc', 'xyz')" {'abc' 'xyz' NULL}
869 2 "INSERT INTO t1(x, z) VALUES('abc', 'xyz')" {'abc' NULL 'xyz'} 861 2 "INSERT INTO t1(x, z) VALUES('abc', 'xyz')" {'abc' NULL 'xyz'}
870 3 "INSERT INTO t1 DEFAULT VALUES" {NULL NULL NULL} 862 3 "INSERT INTO t1 DEFAULT VALUES" {NULL NULL NULL}
871 } 863 }
872 864
873 # EVIDENCE-OF: R-62940-43005 An explicit DEFAULT clause may specify that 865 # EVIDENCE-OF: R-07343-35026 An explicit DEFAULT clause may specify that
874 # the default value is NULL, a string constant, a blob constant, a 866 # the default value is NULL, a string constant, a blob constant, a
875 # signed-number, or any constant expression enclosed in parentheses. An 867 # signed-number, or any constant expression enclosed in parentheses. A
876 # explicit default value may also be one of the special case-independent 868 # default value may also be one of the special case-independent keywords
877 # keywords CURRENT_TIME, CURRENT_DATE or CURRENT_TIMESTAMP. 869 # CURRENT_TIME, CURRENT_DATE or CURRENT_TIMESTAMP.
878 # 870 #
879 do_execsql_test e_createtable-3.3.1 { 871 do_execsql_test e_createtable-3.3.1 {
880 CREATE TABLE t4( 872 CREATE TABLE t4(
881 a DEFAULT NULL, 873 a DEFAULT NULL,
882 b DEFAULT 'string constant', 874 b DEFAULT 'string constant',
883 c DEFAULT X'424C4F42', 875 c DEFAULT X'424C4F42',
884 d DEFAULT 1, 876 d DEFAULT 1,
885 e DEFAULT -1, 877 e DEFAULT -1,
886 f DEFAULT 3.14, 878 f DEFAULT 3.14,
887 g DEFAULT -3.14, 879 g DEFAULT -3.14,
888 h DEFAULT ( substr('abcd', 0, 2) || 'cd' ), 880 h DEFAULT ( substr('abcd', 0, 2) || 'cd' ),
889 i DEFAULT CURRENT_TIME, 881 i DEFAULT CURRENT_TIME,
890 j DEFAULT CURRENT_DATE, 882 j DEFAULT CURRENT_DATE,
891 k DEFAULT CURRENT_TIMESTAMP 883 k DEFAULT CURRENT_TIMESTAMP
892 ); 884 );
893 } {} 885 } {}
894 886
895 # EVIDENCE-OF: R-10288-43169 For the purposes of the DEFAULT clause, an 887 # EVIDENCE-OF: R-18415-27776 For the purposes of the DEFAULT clause, an
896 # expression is considered constant provided that it does not contain 888 # expression is considered constant if it does contains no sub-queries,
897 # any sub-queries or string constants enclosed in double quotes. 889 # column or table references, bound parameters, or string literals
890 # enclosed in double-quotes instead of single-quotes.
898 # 891 #
899 do_createtable_tests 3.4.1 -error { 892 do_createtable_tests 3.4.1 -error {
900 default value of column [x] is not constant 893 default value of column [x] is not constant
901 } { 894 } {
902 1 {CREATE TABLE t5(x DEFAULT ( (SELECT 1) ))} {} 895 1 {CREATE TABLE t5(x DEFAULT ( (SELECT 1) ))} {}
903 2 {CREATE TABLE t5(x DEFAULT ( "abc" ))} {} 896 2 {CREATE TABLE t5(x DEFAULT ( "abc" ))} {}
904 3 {CREATE TABLE t5(x DEFAULT ( 1 IN (SELECT 1) ))} {} 897 3 {CREATE TABLE t5(x DEFAULT ( 1 IN (SELECT 1) ))} {}
905 4 {CREATE TABLE t5(x DEFAULT ( EXISTS (SELECT 1) ))} {} 898 4 {CREATE TABLE t5(x DEFAULT ( EXISTS (SELECT 1) ))} {}
899 5 {CREATE TABLE t5(x DEFAULT ( x!=?1 ))} {}
906 } 900 }
907 do_createtable_tests 3.4.2 -repair { 901 do_createtable_tests 3.4.2 -repair {
908 catchsql { DROP TABLE t5 } 902 catchsql { DROP TABLE t5 }
909 } { 903 } {
910 1 {CREATE TABLE t5(x DEFAULT ( 'abc' ))} {} 904 1 {CREATE TABLE t5(x DEFAULT ( 'abc' ))} {}
911 2 {CREATE TABLE t5(x DEFAULT ( 1 IN (1, 2, 3) ))} {} 905 2 {CREATE TABLE t5(x DEFAULT ( 1 IN (1, 2, 3) ))} {}
912 } 906 }
913 907
914 # EVIDENCE-OF: R-18814-23501 Each time a row is inserted into the table 908 # EVIDENCE-OF: R-18814-23501 Each time a row is inserted into the table
915 # by an INSERT statement that does not provide explicit values for all 909 # by an INSERT statement that does not provide explicit values for all
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
982 } {1 2} 976 } {1 2}
983 do_execsql_test e_createtable-3.7.3 { 977 do_execsql_test e_createtable-3.7.3 {
984 INSERT INTO t6(a) VALUES('X'); 978 INSERT INTO t6(a) VALUES('X');
985 SELECT quote(a), quote(b) FROM t6; 979 SELECT quote(a), quote(b) FROM t6;
986 } {1 2 'X' 3} 980 } {1 2 'X' 3}
987 do_execsql_test e_createtable-3.7.4 { 981 do_execsql_test e_createtable-3.7.4 {
988 INSERT INTO t6(a) SELECT a FROM t6; 982 INSERT INTO t6(a) SELECT a FROM t6;
989 SELECT quote(a), quote(b) FROM t6; 983 SELECT quote(a), quote(b) FROM t6;
990 } {1 2 'X' 3 1 4 'X' 5} 984 } {1 2 'X' 3 1 4 'X' 5}
991 985
992 # EVIDENCE-OF: R-18683-56219 If the default value of a column is 986 # EVIDENCE-OF: R-15363-55230 If the default value of a column is
993 # CURRENT_TIME, CURRENT_DATE or CURRENT_DATETIME, then the value used in 987 # CURRENT_TIME, CURRENT_DATE or CURRENT_TIMESTAMP, then the value used
994 # the new row is a text representation of the current UTC date and/or 988 # in the new row is a text representation of the current UTC date and/or
995 # time. 989 # time.
996 # 990 #
997 # This is difficult to test literally without knowing what time the 991 # This is difficult to test literally without knowing what time the
998 # user will run the tests. Instead, we test that the three cases 992 # user will run the tests. Instead, we test that the three cases
999 # above set the value to the current date and/or time according to 993 # above set the value to the current date and/or time according to
1000 # the xCurrentTime() method of the VFS. Which is usually the same 994 # the xCurrentTime() method of the VFS. Which is usually the same
1001 # as UTC. In this case, however, we instrument it to always return 995 # as UTC. In this case, however, we instrument it to always return
1002 # a time equivalent to "2001-09-09 01:46:40 UTC". 996 # a time equivalent to "2001-09-09 01:46:40 UTC".
1003 # 997 #
1004 set sqlite_current_time 1000000000 998 set sqlite_current_time 1000000000
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
1104 } {1 {string or blob too big}} 1098 } {1 {string or blob too big}}
1105 1099
1106 #------------------------------------------------------------------------- 1100 #-------------------------------------------------------------------------
1107 # Tests for statements regarding constraints (PRIMARY KEY, UNIQUE, NOT 1101 # Tests for statements regarding constraints (PRIMARY KEY, UNIQUE, NOT
1108 # NULL and CHECK constraints). 1102 # NULL and CHECK constraints).
1109 # 1103 #
1110 1104
1111 # EVIDENCE-OF: R-52382-54248 Each table in SQLite may have at most one 1105 # EVIDENCE-OF: R-52382-54248 Each table in SQLite may have at most one
1112 # PRIMARY KEY. 1106 # PRIMARY KEY.
1113 # 1107 #
1114 # EVIDENCE-OF: R-18080-47271 If there is more than one PRIMARY KEY 1108 # EVIDENCE-OF: R-31826-01813 An error is raised if more than one PRIMARY
1115 # clause in a single CREATE TABLE statement, it is an error. 1109 # KEY clause appears in a CREATE TABLE statement.
1116 # 1110 #
1117 # To test the two above, show that zero primary keys is Ok, one primary 1111 # To test the two above, show that zero primary keys is Ok, one primary
1118 # key is Ok, and two or more primary keys is an error. 1112 # key is Ok, and two or more primary keys is an error.
1119 # 1113 #
1120 drop_all_tables 1114 drop_all_tables
1121 do_createtable_tests 4.1.1 { 1115 do_createtable_tests 4.1.1 {
1122 1 "CREATE TABLE t1(a, b, c)" {} 1116 1 "CREATE TABLE t1(a, b, c)" {}
1123 2 "CREATE TABLE t2(a PRIMARY KEY, b, c)" {} 1117 2 "CREATE TABLE t2(a PRIMARY KEY, b, c)" {}
1124 3 "CREATE TABLE t3(a, b, c, PRIMARY KEY(a))" {} 1118 3 "CREATE TABLE t3(a, b, c, PRIMARY KEY(a))" {}
1125 4 "CREATE TABLE t4(a, b, c, PRIMARY KEY(c,b,a))" {} 1119 4 "CREATE TABLE t4(a, b, c, PRIMARY KEY(c,b,a))" {}
1126 } 1120 }
1127 do_createtable_tests 4.1.2 -error { 1121 do_createtable_tests 4.1.2 -error {
1128 table "t5" has more than one primary key 1122 table "t5" has more than one primary key
1129 } { 1123 } {
1130 1 "CREATE TABLE t5(a PRIMARY KEY, b PRIMARY KEY, c)" {} 1124 1 "CREATE TABLE t5(a PRIMARY KEY, b PRIMARY KEY, c)" {}
1131 2 "CREATE TABLE t5(a, b PRIMARY KEY, c, PRIMARY KEY(a))" {} 1125 2 "CREATE TABLE t5(a, b PRIMARY KEY, c, PRIMARY KEY(a))" {}
1132 3 "CREATE TABLE t5(a INTEGER PRIMARY KEY, b PRIMARY KEY, c)" {} 1126 3 "CREATE TABLE t5(a INTEGER PRIMARY KEY, b PRIMARY KEY, c)" {}
1133 4 "CREATE TABLE t5(a INTEGER PRIMARY KEY, b, c, PRIMARY KEY(b, c))" {} 1127 4 "CREATE TABLE t5(a INTEGER PRIMARY KEY, b, c, PRIMARY KEY(b, c))" {}
1134 5 "CREATE TABLE t5(a PRIMARY KEY, b, c, PRIMARY KEY(a))" {} 1128 5 "CREATE TABLE t5(a PRIMARY KEY, b, c, PRIMARY KEY(a))" {}
1135 6 "CREATE TABLE t5(a INTEGER PRIMARY KEY, b, c, PRIMARY KEY(a))" {} 1129 6 "CREATE TABLE t5(a INTEGER PRIMARY KEY, b, c, PRIMARY KEY(a))" {}
1136 } 1130 }
1137 1131
1132 # EVIDENCE-OF: R-54755-39291 The PRIMARY KEY is optional for ordinary
1133 # tables but is required for WITHOUT ROWID tables.
1134 #
1135 do_catchsql_test 4.1.3 {
1136 CREATE TABLE t6(a, b); --ok
1137 } {0 {}}
1138 do_catchsql_test 4.1.4 {
1139 CREATE TABLE t7(a, b) WITHOUT ROWID; --Error, no PRIMARY KEY
1140 } {1 {PRIMARY KEY missing on table t7}}
1141
1142
1138 proc table_pk {tbl} { 1143 proc table_pk {tbl} {
1139 set pk [list] 1144 set pk [list]
1140 db eval "pragma table_info($tbl)" a { 1145 db eval "pragma table_info($tbl)" a {
1141 if {$a(pk)} { lappend pk $a(name) } 1146 if {$a(pk)} { lappend pk $a(name) }
1142 } 1147 }
1143 set pk 1148 set pk
1144 } 1149 }
1145 1150
1146 # EVIDENCE-OF: R-41411-18837 If the keywords PRIMARY KEY are added to a 1151 # EVIDENCE-OF: R-41411-18837 If the keywords PRIMARY KEY are added to a
1147 # column definition, then the primary key for the table consists of that 1152 # column definition, then the primary key for the table consists of that
(...skipping 13 matching lines...) Expand all
1161 table_pk t5 1166 table_pk t5
1162 } { 1167 } {
1163 1.1 "CREATE TABLE t5(a, b INTEGER PRIMARY KEY, c)" {b} 1168 1.1 "CREATE TABLE t5(a, b INTEGER PRIMARY KEY, c)" {b}
1164 1.2 "CREATE TABLE t5(a PRIMARY KEY, b, c)" {a} 1169 1.2 "CREATE TABLE t5(a PRIMARY KEY, b, c)" {a}
1165 1170
1166 2.1 "CREATE TABLE t5(a, b, c, PRIMARY KEY(a))" {a} 1171 2.1 "CREATE TABLE t5(a, b, c, PRIMARY KEY(a))" {a}
1167 2.2 "CREATE TABLE t5(a, b, c, PRIMARY KEY(c,b,a))" {a b c} 1172 2.2 "CREATE TABLE t5(a, b, c, PRIMARY KEY(c,b,a))" {a b c}
1168 2.3 "CREATE TABLE t5(a, b INTEGER PRIMARY KEY, c)" {b} 1173 2.3 "CREATE TABLE t5(a, b INTEGER PRIMARY KEY, c)" {b}
1169 } 1174 }
1170 1175
1171 # EVIDENCE-OF: R-33986-09410 Each row in a table with a primary key must 1176 # EVIDENCE-OF: R-59124-61339 Each row in a table with a primary key must
1172 # feature a unique combination of values in its primary key columns. 1177 # have a unique combination of values in its primary key columns.
1173 # 1178 #
1174 # EVIDENCE-OF: R-39102-06737 If an INSERT or UPDATE statement attempts 1179 # EVIDENCE-OF: R-06471-16287 If an INSERT or UPDATE statement attempts
1175 # to modify the table content so that two or more rows feature identical 1180 # to modify the table content so that two or more rows have identical
1176 # primary key values, it is a constraint violation. 1181 # primary key values, that is a constraint violation.
1177 # 1182 #
1178 drop_all_tables 1183 drop_all_tables
1179 do_execsql_test 4.3.0 { 1184 do_execsql_test 4.3.0 {
1180 CREATE TABLE t1(x PRIMARY KEY, y); 1185 CREATE TABLE t1(x PRIMARY KEY, y);
1181 INSERT INTO t1 VALUES(0, 'zero'); 1186 INSERT INTO t1 VALUES(0, 'zero');
1182 INSERT INTO t1 VALUES(45.5, 'one'); 1187 INSERT INTO t1 VALUES(45.5, 'one');
1183 INSERT INTO t1 VALUES('brambles', 'two'); 1188 INSERT INTO t1 VALUES('brambles', 'two');
1184 INSERT INTO t1 VALUES(X'ABCDEF', 'three'); 1189 INSERT INTO t1 VALUES(X'ABCDEF', 'three');
1185 1190
1186 CREATE TABLE t2(x, y, PRIMARY KEY(x, y)); 1191 CREATE TABLE t2(x, y, PRIMARY KEY(x, y));
1187 INSERT INTO t2 VALUES(0, 'zero'); 1192 INSERT INTO t2 VALUES(0, 'zero');
1188 INSERT INTO t2 VALUES(45.5, 'one'); 1193 INSERT INTO t2 VALUES(45.5, 'one');
1189 INSERT INTO t2 VALUES('brambles', 'two'); 1194 INSERT INTO t2 VALUES('brambles', 'two');
1190 INSERT INTO t2 VALUES(X'ABCDEF', 'three'); 1195 INSERT INTO t2 VALUES(X'ABCDEF', 'three');
1191 } {} 1196 } {}
1192 1197
1193 do_createtable_tests 4.3.1 -error { %s not unique } { 1198 do_createtable_tests 4.3.1 -error {UNIQUE constraint failed: t1.x} {
1194 1 "INSERT INTO t1 VALUES(0, 0)" {"column x is"} 1199 1 "INSERT INTO t1 VALUES(0, 0)" {"column x is"}
1195 2 "INSERT INTO t1 VALUES(45.5, 'abc')" {"column x is"} 1200 2 "INSERT INTO t1 VALUES(45.5, 'abc')" {"column x is"}
1196 3 "INSERT INTO t1 VALUES(0.0, 'abc')" {"column x is"} 1201 3 "INSERT INTO t1 VALUES(0.0, 'abc')" {"column x is"}
1197 4 "INSERT INTO t1 VALUES('brambles', 'abc')" {"column x is"} 1202 4 "INSERT INTO t1 VALUES('brambles', 'abc')" {"column x is"}
1198 5 "INSERT INTO t1 VALUES(X'ABCDEF', 'abc')" {"column x is"} 1203 5 "INSERT INTO t1 VALUES(X'ABCDEF', 'abc')" {"column x is"}
1199 1204 }
1205 do_createtable_tests 4.3.1 -error {UNIQUE constraint failed: t2.x, t2.y} {
1200 6 "INSERT INTO t2 VALUES(0, 'zero')" {"columns x, y are"} 1206 6 "INSERT INTO t2 VALUES(0, 'zero')" {"columns x, y are"}
1201 7 "INSERT INTO t2 VALUES(45.5, 'one')" {"columns x, y are"} 1207 7 "INSERT INTO t2 VALUES(45.5, 'one')" {"columns x, y are"}
1202 8 "INSERT INTO t2 VALUES(0.0, 'zero')" {"columns x, y are"} 1208 8 "INSERT INTO t2 VALUES(0.0, 'zero')" {"columns x, y are"}
1203 9 "INSERT INTO t2 VALUES('brambles', 'two')" {"columns x, y are"} 1209 9 "INSERT INTO t2 VALUES('brambles', 'two')" {"columns x, y are"}
1204 10 "INSERT INTO t2 VALUES(X'ABCDEF', 'three')" {"columns x, y are"} 1210 10 "INSERT INTO t2 VALUES(X'ABCDEF', 'three')" {"columns x, y are"}
1205 } 1211 }
1206 do_createtable_tests 4.3.2 { 1212 do_createtable_tests 4.3.2 {
1207 1 "INSERT INTO t1 VALUES(-1, 0)" {} 1213 1 "INSERT INTO t1 VALUES(-1, 0)" {}
1208 2 "INSERT INTO t1 VALUES(45.2, 'abc')" {} 1214 2 "INSERT INTO t1 VALUES(45.2, 'abc')" {}
1209 3 "INSERT INTO t1 VALUES(0.01, 'abc')" {} 1215 3 "INSERT INTO t1 VALUES(0.01, 'abc')" {}
1210 4 "INSERT INTO t1 VALUES('bramble', 'abc')" {} 1216 4 "INSERT INTO t1 VALUES('bramble', 'abc')" {}
1211 5 "INSERT INTO t1 VALUES(X'ABCDEE', 'abc')" {} 1217 5 "INSERT INTO t1 VALUES(X'ABCDEE', 'abc')" {}
1212 1218
1213 6 "INSERT INTO t2 VALUES(0, 0)" {} 1219 6 "INSERT INTO t2 VALUES(0, 0)" {}
1214 7 "INSERT INTO t2 VALUES(45.5, 'abc')" {} 1220 7 "INSERT INTO t2 VALUES(45.5, 'abc')" {}
1215 8 "INSERT INTO t2 VALUES(0.0, 'abc')" {} 1221 8 "INSERT INTO t2 VALUES(0.0, 'abc')" {}
1216 9 "INSERT INTO t2 VALUES('brambles', 'abc')" {} 1222 9 "INSERT INTO t2 VALUES('brambles', 'abc')" {}
1217 10 "INSERT INTO t2 VALUES(X'ABCDEF', 'abc')" {} 1223 10 "INSERT INTO t2 VALUES(X'ABCDEF', 'abc')" {}
1218 } 1224 }
1219 do_createtable_tests 4.3.3 -error { %s not unique } { 1225 do_createtable_tests 4.3.3 -error {UNIQUE constraint failed: t1.x} {
1220 1 "UPDATE t1 SET x=0 WHERE y='two'" {"column x is"} 1226 1 "UPDATE t1 SET x=0 WHERE y='two'" {"column x is"}
1221 2 "UPDATE t1 SET x='brambles' WHERE y='three'" {"column x is"} 1227 2 "UPDATE t1 SET x='brambles' WHERE y='three'" {"column x is"}
1222 3 "UPDATE t1 SET x=45.5 WHERE y='zero'" {"column x is"} 1228 3 "UPDATE t1 SET x=45.5 WHERE y='zero'" {"column x is"}
1223 4 "UPDATE t1 SET x=X'ABCDEF' WHERE y='one'" {"column x is"} 1229 4 "UPDATE t1 SET x=X'ABCDEF' WHERE y='one'" {"column x is"}
1224 5 "UPDATE t1 SET x=0.0 WHERE y='three'" {"column x is"} 1230 5 "UPDATE t1 SET x=0.0 WHERE y='three'" {"column x is"}
1225 1231 }
1232 do_createtable_tests 4.3.3 -error {UNIQUE constraint failed: t2.x, t2.y} {
1226 6 "UPDATE t2 SET x=0, y='zero' WHERE y='two'" {"columns x, y are"} 1233 6 "UPDATE t2 SET x=0, y='zero' WHERE y='two'" {"columns x, y are"}
1227 7 "UPDATE t2 SET x='brambles', y='two' WHERE y='three'" 1234 7 "UPDATE t2 SET x='brambles', y='two' WHERE y='three'"
1228 {"columns x, y are"} 1235 {"columns x, y are"}
1229 8 "UPDATE t2 SET x=45.5, y='one' WHERE y='zero'" {"columns x, y are"} 1236 8 "UPDATE t2 SET x=45.5, y='one' WHERE y='zero'" {"columns x, y are"}
1230 9 "UPDATE t2 SET x=X'ABCDEF', y='three' WHERE y='one'" 1237 9 "UPDATE t2 SET x=X'ABCDEF', y='three' WHERE y='one'"
1231 {"columns x, y are"} 1238 {"columns x, y are"}
1232 10 "UPDATE t2 SET x=0.0, y='zero' WHERE y='three'" 1239 10 "UPDATE t2 SET x=0.0, y='zero' WHERE y='three'"
1233 {"columns x, y are"} 1240 {"columns x, y are"}
1234 } 1241 }
1235 1242
(...skipping 15 matching lines...) Expand all
1251 8 "INSERT INTO t2 VALUES(0, NULL)" {} 1258 8 "INSERT INTO t2 VALUES(0, NULL)" {}
1252 9 "INSERT INTO t2 VALUES(45.5, NULL)" {} 1259 9 "INSERT INTO t2 VALUES(45.5, NULL)" {}
1253 10 "INSERT INTO t2 VALUES(0.0, NULL)" {} 1260 10 "INSERT INTO t2 VALUES(0.0, NULL)" {}
1254 11 "INSERT INTO t2 VALUES('brambles', NULL)" {} 1261 11 "INSERT INTO t2 VALUES('brambles', NULL)" {}
1255 12 "INSERT INTO t2 VALUES(X'ABCDEF', NULL)" {} 1262 12 "INSERT INTO t2 VALUES(X'ABCDEF', NULL)" {}
1256 1263
1257 13 "INSERT INTO t2 VALUES(NULL, NULL)" {} 1264 13 "INSERT INTO t2 VALUES(NULL, NULL)" {}
1258 14 "INSERT INTO t2 VALUES(NULL, NULL)" {} 1265 14 "INSERT INTO t2 VALUES(NULL, NULL)" {}
1259 } 1266 }
1260 1267
1261 # EVIDENCE-OF: R-61866-38053 Unless the column is an INTEGER PRIMARY KEY 1268 # EVIDENCE-OF: R-35113-43214 Unless the column is an INTEGER PRIMARY KEY
1262 # SQLite allows NULL values in a PRIMARY KEY column. 1269 # or the table is a WITHOUT ROWID table or the column is declared NOT
1270 # NULL, SQLite allows NULL values in a PRIMARY KEY column.
1263 # 1271 #
1264 # If the column is an integer primary key, attempting to insert a NULL 1272 # If the column is an integer primary key, attempting to insert a NULL
1265 # into the column triggers the auto-increment behaviour. Attempting 1273 # into the column triggers the auto-increment behavior. Attempting
1266 # to use UPDATE to set an ipk column to a NULL value is an error. 1274 # to use UPDATE to set an ipk column to a NULL value is an error.
1267 # 1275 #
1268 do_createtable_tests 4.5.1 { 1276 do_createtable_tests 4.5.1 {
1269 1 "SELECT count(*) FROM t1 WHERE x IS NULL" 3 1277 1 "SELECT count(*) FROM t1 WHERE x IS NULL" 3
1270 2 "SELECT count(*) FROM t2 WHERE x IS NULL" 6 1278 2 "SELECT count(*) FROM t2 WHERE x IS NULL" 6
1271 3 "SELECT count(*) FROM t2 WHERE y IS NULL" 7 1279 3 "SELECT count(*) FROM t2 WHERE y IS NULL" 7
1272 4 "SELECT count(*) FROM t2 WHERE x IS NULL AND y IS NULL" 2 1280 4 "SELECT count(*) FROM t2 WHERE x IS NULL AND y IS NULL" 2
1273 } 1281 }
1274 do_execsql_test 4.5.2 { 1282 do_execsql_test 4.5.2 {
1275 CREATE TABLE t3(s, u INTEGER PRIMARY KEY, v); 1283 CREATE TABLE t3(s, u INTEGER PRIMARY KEY, v);
1276 INSERT INTO t3 VALUES(1, NULL, 2); 1284 INSERT INTO t3 VALUES(1, NULL, 2);
1277 INSERT INTO t3 VALUES('x', NULL, 'y'); 1285 INSERT INTO t3 VALUES('x', NULL, 'y');
1278 SELECT u FROM t3; 1286 SELECT u FROM t3;
1279 } {1 2} 1287 } {1 2}
1280 do_catchsql_test 4.5.3 { 1288 do_catchsql_test 4.5.3 {
1281 INSERT INTO t3 VALUES(2, 5, 3); 1289 INSERT INTO t3 VALUES(2, 5, 3);
1282 UPDATE t3 SET u = NULL WHERE s = 2; 1290 UPDATE t3 SET u = NULL WHERE s = 2;
1283 } {1 {datatype mismatch}} 1291 } {1 {datatype mismatch}}
1292 do_catchsql_test 4.5.4 {
1293 CREATE TABLE t4(s, u INT PRIMARY KEY, v) WITHOUT ROWID;
1294 INSERT INTO t4 VALUES(1, NULL, 2);
1295 } {1 {NOT NULL constraint failed: t4.u}}
1296 do_catchsql_test 4.5.5 {
1297 CREATE TABLE t5(s, u INT PRIMARY KEY NOT NULL, v);
1298 INSERT INTO t5 VALUES(1, NULL, 2);
1299 } {1 {NOT NULL constraint failed: t5.u}}
1284 1300
1285 # EVIDENCE-OF: R-00227-21080 A UNIQUE constraint is similar to a PRIMARY 1301 # EVIDENCE-OF: R-00227-21080 A UNIQUE constraint is similar to a PRIMARY
1286 # KEY constraint, except that a single table may have any number of 1302 # KEY constraint, except that a single table may have any number of
1287 # UNIQUE constraints. 1303 # UNIQUE constraints.
1288 # 1304 #
1289 drop_all_tables 1305 drop_all_tables
1290 do_createtable_tests 4.6 { 1306 do_createtable_tests 4.6 {
1291 1 "CREATE TABLE t1(a UNIQUE, b UNIQUE)" {} 1307 1 "CREATE TABLE t1(a UNIQUE, b UNIQUE)" {}
1292 2 "CREATE TABLE t2(a UNIQUE, b, c, UNIQUE(c, b))" {} 1308 2 "CREATE TABLE t2(a UNIQUE, b, c, UNIQUE(c, b))" {}
1293 3 "CREATE TABLE t3(a, b, c, UNIQUE(a), UNIQUE(b), UNIQUE(c))" {} 1309 3 "CREATE TABLE t3(a, b, c, UNIQUE(a), UNIQUE(b), UNIQUE(c))" {}
1294 4 "CREATE TABLE t4(a, b, c, UNIQUE(a, b, c))" {} 1310 4 "CREATE TABLE t4(a, b, c, UNIQUE(a, b, c))" {}
1295 } 1311 }
1296 1312
1297 # EVIDENCE-OF: R-55240-58877 For each UNIQUE constraint on the table, 1313 # EVIDENCE-OF: R-30981-64168 For each UNIQUE constraint on the table,
1298 # each row must feature a unique combination of values in the columns 1314 # each row must contain a unique combination of values in the columns
1299 # identified by the UNIQUE constraint. 1315 # identified by the UNIQUE constraint.
1300 # 1316 #
1301 # EVIDENCE-OF: R-47733-51480 If an INSERT or UPDATE statement attempts 1317 # EVIDENCE-OF: R-59124-61339 Each row in a table with a primary key must
1302 # to modify the table content so that two or more rows feature identical 1318 # have a unique combination of values in its primary key columns.
1303 # values in a set of columns that are subject to a UNIQUE constraint, it
1304 # is a constraint violation.
1305 # 1319 #
1306 do_execsql_test 4.7.0 { 1320 do_execsql_test 4.7.0 {
1307 INSERT INTO t1 VALUES(1, 2); 1321 INSERT INTO t1 VALUES(1, 2);
1308 INSERT INTO t1 VALUES(4.3, 5.5); 1322 INSERT INTO t1 VALUES(4.3, 5.5);
1309 INSERT INTO t1 VALUES('reveal', 'variableness'); 1323 INSERT INTO t1 VALUES('reveal', 'variableness');
1310 INSERT INTO t1 VALUES(X'123456', X'654321'); 1324 INSERT INTO t1 VALUES(X'123456', X'654321');
1311 1325
1312 INSERT INTO t4 VALUES('xyx', 1, 1); 1326 INSERT INTO t4 VALUES('xyx', 1, 1);
1313 INSERT INTO t4 VALUES('xyx', 2, 1); 1327 INSERT INTO t4 VALUES('xyx', 2, 1);
1314 INSERT INTO t4 VALUES('uvw', 1, 1); 1328 INSERT INTO t4 VALUES('uvw', 1, 1);
1315 } 1329 }
1316 do_createtable_tests 4.7.1 -error { %s not unique } { 1330 do_createtable_tests 4.7.1 -error {UNIQUE constraint failed: %s} {
1317 1 "INSERT INTO t1 VALUES(1, 'one')" {{column a is}} 1331 1 "INSERT INTO t1 VALUES(1, 'one')" {{t1.a}}
1318 2 "INSERT INTO t1 VALUES(4.3, 'two')" {{column a is}} 1332 2 "INSERT INTO t1 VALUES(4.3, 'two')" {{t1.a}}
1319 3 "INSERT INTO t1 VALUES('reveal', 'three')" {{column a is}} 1333 3 "INSERT INTO t1 VALUES('reveal', 'three')" {{t1.a}}
1320 4 "INSERT INTO t1 VALUES(X'123456', 'four')" {{column a is}} 1334 4 "INSERT INTO t1 VALUES(X'123456', 'four')" {{t1.a}}
1321 1335
1322 5 "UPDATE t1 SET a = 1 WHERE rowid=2" {{column a is}} 1336 5 "UPDATE t1 SET a = 1 WHERE rowid=2" {{t1.a}}
1323 6 "UPDATE t1 SET a = 4.3 WHERE rowid=3" {{column a is}} 1337 6 "UPDATE t1 SET a = 4.3 WHERE rowid=3" {{t1.a}}
1324 7 "UPDATE t1 SET a = 'reveal' WHERE rowid=4" {{column a is}} 1338 7 "UPDATE t1 SET a = 'reveal' WHERE rowid=4" {{t1.a}}
1325 8 "UPDATE t1 SET a = X'123456' WHERE rowid=1" {{column a is}} 1339 8 "UPDATE t1 SET a = X'123456' WHERE rowid=1" {{t1.a}}
1326 1340
1327 9 "INSERT INTO t4 VALUES('xyx', 1, 1)" {{columns a, b, c are}} 1341 9 "INSERT INTO t4 VALUES('xyx', 1, 1)" {{t4.a, t4.b, t4.c}}
1328 10 "INSERT INTO t4 VALUES('xyx', 2, 1)" {{columns a, b, c are}} 1342 10 "INSERT INTO t4 VALUES('xyx', 2, 1)" {{t4.a, t4.b, t4.c}}
1329 11 "INSERT INTO t4 VALUES('uvw', 1, 1)" {{columns a, b, c are}} 1343 11 "INSERT INTO t4 VALUES('uvw', 1, 1)" {{t4.a, t4.b, t4.c}}
1330 1344
1331 12 "UPDATE t4 SET a='xyx' WHERE rowid=3" {{columns a, b, c are}} 1345 12 "UPDATE t4 SET a='xyx' WHERE rowid=3" {{t4.a, t4.b, t4.c}}
1332 13 "UPDATE t4 SET b=1 WHERE rowid=2" {{columns a, b, c are}} 1346 13 "UPDATE t4 SET b=1 WHERE rowid=2" {{t4.a, t4.b, t4.c}}
1333 14 "UPDATE t4 SET a=0, b=0, c=0" {{columns a, b, c are}} 1347 14 "UPDATE t4 SET a=0, b=0, c=0" {{t4.a, t4.b, t4.c}}
1334 } 1348 }
1335 1349
1336 # EVIDENCE-OF: R-21289-11559 As with PRIMARY KEY constraints, for the 1350 # EVIDENCE-OF: R-00404-17670 For the purposes of UNIQUE constraints,
1337 # purposes of UNIQUE constraints NULL values are considered distinct 1351 # NULL values are considered distinct from all other values, including
1338 # from all other values (including other NULLs). 1352 # other NULLs.
1339 # 1353 #
1340 do_createtable_tests 4.8 { 1354 do_createtable_tests 4.8 {
1341 1 "INSERT INTO t1 VALUES(NULL, NULL)" {} 1355 1 "INSERT INTO t1 VALUES(NULL, NULL)" {}
1342 2 "INSERT INTO t1 VALUES(NULL, NULL)" {} 1356 2 "INSERT INTO t1 VALUES(NULL, NULL)" {}
1343 3 "UPDATE t1 SET a = NULL" {} 1357 3 "UPDATE t1 SET a = NULL" {}
1344 4 "UPDATE t1 SET b = NULL" {} 1358 4 "UPDATE t1 SET b = NULL" {}
1345 1359
1346 5 "INSERT INTO t4 VALUES(NULL, NULL, NULL)" {} 1360 5 "INSERT INTO t4 VALUES(NULL, NULL, NULL)" {}
1347 6 "INSERT INTO t4 VALUES(NULL, NULL, NULL)" {} 1361 6 "INSERT INTO t4 VALUES(NULL, NULL, NULL)" {}
1348 7 "UPDATE t4 SET a = NULL" {} 1362 7 "UPDATE t4 SET a = NULL" {}
1349 8 "UPDATE t4 SET b = NULL" {} 1363 8 "UPDATE t4 SET b = NULL" {}
1350 9 "UPDATE t4 SET c = NULL" {} 1364 9 "UPDATE t4 SET c = NULL" {}
1351 } 1365 }
1352 1366
1353 # EVIDENCE-OF: R-26983-26377 INTEGER PRIMARY KEY columns aside, both 1367 # EVIDENCE-OF: R-55820-29984 In most cases, UNIQUE and PRIMARY KEY
1354 # UNIQUE and PRIMARY KEY constraints are implemented by creating an 1368 # constraints are implemented by creating a unique index in the
1355 # index in the database (in the same way as a "CREATE UNIQUE INDEX" 1369 # database.
1356 # statement would).
1357 do_createtable_tests 4.9 -repair drop_all_tables -query { 1370 do_createtable_tests 4.9 -repair drop_all_tables -query {
1358 SELECT count(*) FROM sqlite_master WHERE type='index' 1371 SELECT count(*) FROM sqlite_master WHERE type='index'
1359 } { 1372 } {
1360 1 "CREATE TABLE t1(a TEXT PRIMARY KEY, b)" 1 1373 1 "CREATE TABLE t1(a TEXT PRIMARY KEY, b)" 1
1361 2 "CREATE TABLE t1(a INTEGER PRIMARY KEY, b)" 0 1374 2 "CREATE TABLE t1(a INTEGER PRIMARY KEY, b)" 0
1362 3 "CREATE TABLE t1(a TEXT UNIQUE, b)" 1 1375 3 "CREATE TABLE t1(a TEXT UNIQUE, b)" 1
1363 4 "CREATE TABLE t1(a PRIMARY KEY, b TEXT UNIQUE)" 2 1376 4 "CREATE TABLE t1(a PRIMARY KEY, b TEXT UNIQUE)" 2
1364 5 "CREATE TABLE t1(a PRIMARY KEY, b, c, UNIQUE(c, b))" 2 1377 5 "CREATE TABLE t1(a PRIMARY KEY, b, c, UNIQUE(c, b))" 2
1365 } 1378 }
1366 1379
1367 # EVIDENCE-OF: R-02252-33116 Such an index is used like any other index 1380 # Obsolete: R-02252-33116 Such an index is used like any other index
1368 # in the database to optimize queries. 1381 # in the database to optimize queries.
1369 # 1382 #
1370 do_execsql_test 4.10.0 { 1383 do_execsql_test 4.10.0 {
1371 CREATE TABLE t1(a, b PRIMARY KEY); 1384 CREATE TABLE t1(a, b PRIMARY KEY);
1372 CREATE TABLE t2(a, b, c, UNIQUE(b, c)); 1385 CREATE TABLE t2(a, b, c, UNIQUE(b, c));
1373 } 1386 }
1374 do_createtable_tests 4.10 { 1387 do_createtable_tests 4.10 {
1375 1 "EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b = 5" 1388 1 "EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b = 5"
1376 {0 0 0 {SEARCH TABLE t1 USING INDEX sqlite_autoindex_t1_1 (b=?) (~1 rows) }} 1389 {0 0 0 {SEARCH TABLE t1 USING INDEX sqlite_autoindex_t1_1 (b=?)}}
1377 1390
1378 2 "EXPLAIN QUERY PLAN SELECT * FROM t2 ORDER BY b, c" 1391 2 "EXPLAIN QUERY PLAN SELECT * FROM t2 ORDER BY b, c"
1379 {0 0 0 {SCAN TABLE t2 USING INDEX sqlite_autoindex_t2_1 (~1000000 rows)}} 1392 {0 0 0 {SCAN TABLE t2 USING INDEX sqlite_autoindex_t2_1}}
1380 1393
1381 3 "EXPLAIN QUERY PLAN SELECT * FROM t2 WHERE b=10 AND c>10" 1394 3 "EXPLAIN QUERY PLAN SELECT * FROM t2 WHERE b=10 AND c>10"
1382 {0 0 0 {SEARCH TABLE t2 USING INDEX sqlite_autoindex_t2_1 (b=? AND c>?) ( ~2 rows)}} 1395 {0 0 0 {SEARCH TABLE t2 USING INDEX sqlite_autoindex_t2_1 (b=? AND c>?)}}
1383 } 1396 }
1384 1397
1385 # EVIDENCE-OF: R-45493-35653 A CHECK constraint may be attached to a 1398 # EVIDENCE-OF: R-45493-35653 A CHECK constraint may be attached to a
1386 # column definition or specified as a table constraint. In practice it 1399 # column definition or specified as a table constraint. In practice it
1387 # makes no difference. 1400 # makes no difference.
1388 # 1401 #
1389 # All the tests that deal with CHECK constraints below (4.11.* and 1402 # All the tests that deal with CHECK constraints below (4.11.* and
1390 # 4.12.*) are run once for a table with the check constraint attached 1403 # 4.12.*) are run once for a table with the check constraint attached
1391 # to a column definition, and once with a table where the check 1404 # to a column definition, and once with a table where the check
1392 # condition is specified as a table constraint. 1405 # condition is specified as a table constraint.
(...skipping 12 matching lines...) Expand all
1405 INSERT INTO x1 VALUES('y', 'yy'); 1418 INSERT INTO x1 VALUES('y', 'yy');
1406 INSERT INTO t1 SELECT * FROM x1; 1419 INSERT INTO t1 SELECT * FROM x1;
1407 1420
1408 CREATE TABLE x2(a CHECK( a||b ), b); 1421 CREATE TABLE x2(a CHECK( a||b ), b);
1409 CREATE TABLE t2(a, b, CHECK( a||b )); 1422 CREATE TABLE t2(a, b, CHECK( a||b ));
1410 INSERT INTO x2 VALUES(1, 'xx'); 1423 INSERT INTO x2 VALUES(1, 'xx');
1411 INSERT INTO x2 VALUES(1, 'yy'); 1424 INSERT INTO x2 VALUES(1, 'yy');
1412 INSERT INTO t2 SELECT * FROM x2; 1425 INSERT INTO t2 SELECT * FROM x2;
1413 } 1426 }
1414 1427
1415 do_createtable_tests 4.11 -error {constraint failed} { 1428 do_createtable_tests 4.11 -error {CHECK constraint failed: %s} {
1416 1a "INSERT INTO x1 VALUES('one', 0)" {} 1429 1a "INSERT INTO x1 VALUES('one', 0)" {x1}
1417 1b "INSERT INTO t1 VALUES('one', -4.0)" {} 1430 1b "INSERT INTO t1 VALUES('one', -4.0)" {t1}
1418 1431
1419 2a "INSERT INTO x2 VALUES('abc', 1)" {} 1432 2a "INSERT INTO x2 VALUES('abc', 1)" {x2}
1420 2b "INSERT INTO t2 VALUES('abc', 1)" {} 1433 2b "INSERT INTO t2 VALUES('abc', 1)" {t2}
1421 1434
1422 3a "INSERT INTO x2 VALUES(0, 'abc')" {} 1435 3a "INSERT INTO x2 VALUES(0, 'abc')" {x2}
1423 3b "INSERT INTO t2 VALUES(0, 'abc')" {} 1436 3b "INSERT INTO t2 VALUES(0, 'abc')" {t2}
1424 1437
1425 4a "UPDATE t1 SET b=-1 WHERE rowid=1" {} 1438 4a "UPDATE t1 SET b=-1 WHERE rowid=1" {t1}
1426 4b "UPDATE x1 SET b=-1 WHERE rowid=1" {} 1439 4b "UPDATE x1 SET b=-1 WHERE rowid=1" {x1}
1427 1440
1428 4a "UPDATE x2 SET a='' WHERE rowid=1" {} 1441 4a "UPDATE x2 SET a='' WHERE rowid=1" {x2}
1429 4b "UPDATE t2 SET a='' WHERE rowid=1" {} 1442 4b "UPDATE t2 SET a='' WHERE rowid=1" {t2}
1430 } 1443 }
1431 1444
1432 # EVIDENCE-OF: R-34109-39108 If the CHECK expression evaluates to NULL, 1445 # EVIDENCE-OF: R-34109-39108 If the CHECK expression evaluates to NULL,
1433 # or any other non-zero value, it is not a constraint violation. 1446 # or any other non-zero value, it is not a constraint violation.
1434 # 1447 #
1435 do_createtable_tests 4.12 { 1448 do_createtable_tests 4.12 {
1436 1a "INSERT INTO x1 VALUES('one', NULL)" {} 1449 1a "INSERT INTO x1 VALUES('one', NULL)" {}
1437 1b "INSERT INTO t1 VALUES('one', NULL)" {} 1450 1b "INSERT INTO t1 VALUES('one', NULL)" {}
1438 1451
1439 2a "INSERT INTO x1 VALUES('one', 2)" {} 1452 2a "INSERT INTO x1 VALUES('one', 2)" {}
(...skipping 30 matching lines...) Expand all
1470 do_execsql_test 4.14.0 { 1483 do_execsql_test 4.14.0 {
1471 INSERT INTO t1 VALUES('x', 'y'); 1484 INSERT INTO t1 VALUES('x', 'y');
1472 INSERT INTO t1 VALUES('z', NULL); 1485 INSERT INTO t1 VALUES('z', NULL);
1473 1486
1474 INSERT INTO t2 VALUES('x', 'y'); 1487 INSERT INTO t2 VALUES('x', 'y');
1475 INSERT INTO t2 VALUES('z', NULL); 1488 INSERT INTO t2 VALUES('z', NULL);
1476 1489
1477 INSERT INTO t3 VALUES('x', 'y', 'z'); 1490 INSERT INTO t3 VALUES('x', 'y', 'z');
1478 INSERT INTO t3 VALUES(1, 2, 3); 1491 INSERT INTO t3 VALUES(1, 2, 3);
1479 } 1492 }
1480 do_createtable_tests 4.14 -error { 1493 do_createtable_tests 4.14 -error {NOT NULL constraint failed: %s} {
1481 %s may not be NULL
1482 } {
1483 1 "INSERT INTO t1 VALUES(NULL, 'a')" {t1.a} 1494 1 "INSERT INTO t1 VALUES(NULL, 'a')" {t1.a}
1484 2 "INSERT INTO t2 VALUES(NULL, 'b')" {t2.a} 1495 2 "INSERT INTO t2 VALUES(NULL, 'b')" {t2.a}
1485 3 "INSERT INTO t3 VALUES('c', 'd', NULL)" {t3.c} 1496 3 "INSERT INTO t3 VALUES('c', 'd', NULL)" {t3.c}
1486 4 "INSERT INTO t3 VALUES('e', NULL, 'f')" {t3.b} 1497 4 "INSERT INTO t3 VALUES('e', NULL, 'f')" {t3.b}
1487 5 "INSERT INTO t3 VALUES(NULL, 'g', 'h')" {t3.a} 1498 5 "INSERT INTO t3 VALUES(NULL, 'g', 'h')" {t3.a}
1488 } 1499 }
1489 1500
1490 # EVIDENCE-OF: R-42511-39459 PRIMARY KEY, UNIQUE and NOT NULL 1501 # EVIDENCE-OF: R-42511-39459 PRIMARY KEY, UNIQUE and NOT NULL
1491 # constraints may be explicitly assigned a default conflict resolution 1502 # constraints may be explicitly assigned a default conflict resolution
1492 # algorithm by including a conflict-clause in their definitions. 1503 # algorithm by including a conflict-clause in their definitions.
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
1538 INSERT INTO t3_ab VALUES(1, 'one'); 1549 INSERT INTO t3_ab VALUES(1, 'one');
1539 INSERT INTO t3_ab VALUES(2, 'two'); 1550 INSERT INTO t3_ab VALUES(2, 'two');
1540 INSERT INTO t3_ro SELECT * FROM t3_ab; 1551 INSERT INTO t3_ro SELECT * FROM t3_ab;
1541 INSERT INTO t3_ig SELECT * FROM t3_ab; 1552 INSERT INTO t3_ig SELECT * FROM t3_ab;
1542 INSERT INTO t3_fa SELECT * FROM t3_ab; 1553 INSERT INTO t3_fa SELECT * FROM t3_ab;
1543 INSERT INTO t3_re SELECT * FROM t3_ab; 1554 INSERT INTO t3_re SELECT * FROM t3_ab;
1544 INSERT INTO t3_xx SELECT * FROM t3_ab; 1555 INSERT INTO t3_xx SELECT * FROM t3_ab;
1545 } 1556 }
1546 1557
1547 foreach {tn tbl res ac data} { 1558 foreach {tn tbl res ac data} {
1548 1 t1_ab {1 {column a is not unique}} 0 {1 one 2 two 3 three} 1559 1 t1_ab {1 {UNIQUE constraint failed: t1_ab.a}} 0 {1 one 2 two 3 three}
1549 2 t1_ro {1 {column a is not unique}} 1 {1 one 2 two} 1560 2 t1_ro {1 {UNIQUE constraint failed: t1_ro.a}} 1 {1 one 2 two}
1550 3 t1_fa {1 {column a is not unique}} 0 {1 one 2 two 3 three 4 string} 1561 3 t1_fa {1 {UNIQUE constraint failed: t1_fa.a}} 0 {1 one 2 two 3 three 4 string}
1551 4 t1_ig {0 {}} 0 {1 one 2 two 3 three 4 string 6 string} 1562 4 t1_ig {0 {}} 0 {1 one 2 two 3 three 4 string 6 string}
1552 5 t1_re {0 {}} 0 {1 one 2 two 4 string 3 string 6 string} 1563 5 t1_re {0 {}} 0 {1 one 2 two 4 string 3 string 6 string}
1553 6 t1_xx {1 {column a is not unique}} 0 {1 one 2 two 3 three} 1564 6 t1_xx {1 {UNIQUE constraint failed: t1_xx.a}} 0 {1 one 2 two 3 three}
1554 } { 1565 } {
1555 catchsql COMMIT 1566 catchsql COMMIT
1556 do_execsql_test 4.15.$tn.1 "BEGIN; INSERT INTO $tbl VALUES(3, 'three')" 1567 do_execsql_test 4.15.$tn.1 "BEGIN; INSERT INTO $tbl VALUES(3, 'three')"
1557 1568
1558 do_catchsql_test 4.15.$tn.2 " 1569 do_catchsql_test 4.15.$tn.2 "
1559 INSERT INTO $tbl SELECT ((a%2)*a+3), 'string' FROM $tbl; 1570 INSERT INTO $tbl SELECT ((a%2)*a+3), 'string' FROM $tbl;
1560 " $res 1571 " $res
1561 1572
1562 do_test e_createtable-4.15.$tn.3 { sqlite3_get_autocommit db } $ac 1573 do_test e_createtable-4.15.$tn.3 { sqlite3_get_autocommit db } $ac
1563 do_execsql_test 4.15.$tn.4 "SELECT * FROM $tbl" $data 1574 do_execsql_test 4.15.$tn.4 "SELECT * FROM $tbl" $data
1564 } 1575 }
1565 foreach {tn tbl res ac data} { 1576 foreach {tn tbl res ac data} {
1566 1 t2_ab {1 {t2_ab.b may not be NULL}} 0 {1 one 2 two 3 three} 1577 1 t2_ab {1 {NOT NULL constraint failed: t2_ab.b}} 0 {1 one 2 two 3 three}
1567 2 t2_ro {1 {t2_ro.b may not be NULL}} 1 {1 one 2 two} 1578 2 t2_ro {1 {NOT NULL constraint failed: t2_ro.b}} 1 {1 one 2 two}
1568 3 t2_fa {1 {t2_fa.b may not be NULL}} 0 {1 one 2 two 3 three 4 xx} 1579 3 t2_fa {1 {NOT NULL constraint failed: t2_fa.b}} 0 {1 one 2 two 3 three 4 xx}
1569 4 t2_ig {0 {}} 0 {1 one 2 two 3 three 4 xx 6 xx} 1580 4 t2_ig {0 {}} 0 {1 one 2 two 3 three 4 xx 6 xx}
1570 5 t2_re {1 {t2_re.b may not be NULL}} 0 {1 one 2 two 3 three} 1581 5 t2_re {1 {NOT NULL constraint failed: t2_re.b}} 0 {1 one 2 two 3 three}
1571 6 t2_xx {1 {t2_xx.b may not be NULL}} 0 {1 one 2 two 3 three} 1582 6 t2_xx {1 {NOT NULL constraint failed: t2_xx.b}} 0 {1 one 2 two 3 three}
1572 } { 1583 } {
1573 catchsql COMMIT 1584 catchsql COMMIT
1574 do_execsql_test 4.16.$tn.1 "BEGIN; INSERT INTO $tbl VALUES(3, 'three')" 1585 do_execsql_test 4.16.$tn.1 "BEGIN; INSERT INTO $tbl VALUES(3, 'three')"
1575 1586
1576 do_catchsql_test 4.16.$tn.2 " 1587 do_catchsql_test 4.16.$tn.2 "
1577 INSERT INTO $tbl SELECT a+3, CASE a WHEN 2 THEN NULL ELSE 'xx' END FROM $tbl 1588 INSERT INTO $tbl SELECT a+3, CASE a WHEN 2 THEN NULL ELSE 'xx' END FROM $tbl
1578 " $res 1589 " $res
1579 1590
1580 do_test e_createtable-4.16.$tn.3 { sqlite3_get_autocommit db } $ac 1591 do_test e_createtable-4.16.$tn.3 { sqlite3_get_autocommit db } $ac
1581 do_execsql_test 4.16.$tn.4 "SELECT * FROM $tbl" $data 1592 do_execsql_test 4.16.$tn.4 "SELECT * FROM $tbl" $data
1582 } 1593 }
1583 foreach {tn tbl res ac data} { 1594 foreach {tn tbl res ac data} {
1584 1 t3_ab {1 {columns a, b are not unique}} 0 {1 one 2 two 3 three} 1595 1 t3_ab {1 {UNIQUE constraint failed: t3_ab.a, t3_ab.b}}
1585 2 t3_ro {1 {columns a, b are not unique}} 1 {1 one 2 two} 1596 0 {1 one 2 two 3 three}
1586 3 t3_fa {1 {columns a, b are not unique}} 0 {1 one 2 two 3 three 4 three} 1597 2 t3_ro {1 {UNIQUE constraint failed: t3_ro.a, t3_ro.b}}
1598 1 {1 one 2 two}
1599 3 t3_fa {1 {UNIQUE constraint failed: t3_fa.a, t3_fa.b}}
1600 0 {1 one 2 two 3 three 4 three}
1587 4 t3_ig {0 {}} 0 {1 one 2 two 3 three 4 three 6 three} 1601 4 t3_ig {0 {}} 0 {1 one 2 two 3 three 4 three 6 three}
1588 5 t3_re {0 {}} 0 {1 one 2 two 4 three 3 three 6 three} 1602 5 t3_re {0 {}} 0 {1 one 2 two 4 three 3 three 6 three}
1589 6 t3_xx {1 {columns a, b are not unique}} 0 {1 one 2 two 3 three} 1603 6 t3_xx {1 {UNIQUE constraint failed: t3_xx.a, t3_xx.b}}
1604 0 {1 one 2 two 3 three}
1590 } { 1605 } {
1591 catchsql COMMIT 1606 catchsql COMMIT
1592 do_execsql_test 4.17.$tn.1 "BEGIN; INSERT INTO $tbl VALUES(3, 'three')" 1607 do_execsql_test 4.17.$tn.1 "BEGIN; INSERT INTO $tbl VALUES(3, 'three')"
1593 1608
1594 do_catchsql_test 4.17.$tn.2 " 1609 do_catchsql_test 4.17.$tn.2 "
1595 INSERT INTO $tbl SELECT ((a%2)*a+3), 'three' FROM $tbl 1610 INSERT INTO $tbl SELECT ((a%2)*a+3), 'three' FROM $tbl
1596 " $res 1611 " $res
1597 1612
1598 do_test e_createtable-4.17.$tn.3 { sqlite3_get_autocommit db } $ac 1613 do_test e_createtable-4.17.$tn.3 { sqlite3_get_autocommit db } $ac
1599 do_execsql_test 4.17.$tn.4 "SELECT * FROM $tbl" $data 1614 do_execsql_test 4.17.$tn.4 "SELECT * FROM $tbl ORDER BY rowid" $data
1600 } 1615 }
1601 catchsql COMMIT 1616 catchsql COMMIT
1602 1617
1603 # EVIDENCE-OF: R-12645-39772 Or, if a constraint definition does not 1618 # EVIDENCE-OF: R-12645-39772 Or, if a constraint definition does not
1604 # include a conflict-clause or it is a CHECK constraint, the default 1619 # include a conflict-clause or it is a CHECK constraint, the default
1605 # conflict resolution algorithm is ABORT. 1620 # conflict resolution algorithm is ABORT.
1606 # 1621 #
1607 # The first half of the above is tested along with explicit ON 1622 # The first half of the above is tested along with explicit ON
1608 # CONFLICT clauses above (specifically, the tests involving t1_xx, t2_xx 1623 # CONFLICT clauses above (specifically, the tests involving t1_xx, t2_xx
1609 # and t3_xx). The following just tests that the default conflict 1624 # and t3_xx). The following just tests that the default conflict
1610 # handling for CHECK constraints is ABORT. 1625 # handling for CHECK constraints is ABORT.
1611 # 1626 #
1612 do_execsql_test 4.18.1 { 1627 do_execsql_test 4.18.1 {
1613 CREATE TABLE t4(a, b CHECK (b!=10)); 1628 CREATE TABLE t4(a, b CHECK (b!=10));
1614 INSERT INTO t4 VALUES(1, 2); 1629 INSERT INTO t4 VALUES(1, 2);
1615 INSERT INTO t4 VALUES(3, 4); 1630 INSERT INTO t4 VALUES(3, 4);
1616 } 1631 }
1617 do_execsql_test 4.18.2 { BEGIN; INSERT INTO t4 VALUES(5, 6) } 1632 do_execsql_test 4.18.2 { BEGIN; INSERT INTO t4 VALUES(5, 6) }
1618 do_catchsql_test 4.18.3 { 1633 do_catchsql_test 4.18.3 {
1619 INSERT INTO t4 SELECT a+4, b+4 FROM t4 1634 INSERT INTO t4 SELECT a+4, b+4 FROM t4
1620 } {1 {constraint failed}} 1635 } {1 {CHECK constraint failed: t4}}
1621 do_test e_createtable-4.18.4 { sqlite3_get_autocommit db } 0 1636 do_test e_createtable-4.18.4 { sqlite3_get_autocommit db } 0
1622 do_execsql_test 4.18.5 { SELECT * FROM t4 } {1 2 3 4 5 6} 1637 do_execsql_test 4.18.5 { SELECT * FROM t4 } {1 2 3 4 5 6}
1623 1638
1624 # EVIDENCE-OF: R-19114-56113 Different constraints within the same table 1639 # EVIDENCE-OF: R-19114-56113 Different constraints within the same table
1625 # may have different default conflict resolution algorithms. 1640 # may have different default conflict resolution algorithms.
1626 # 1641 #
1627 do_execsql_test 4.19.0 { 1642 do_execsql_test 4.19.0 {
1628 CREATE TABLE t5(a NOT NULL ON CONFLICT IGNORE, b NOT NULL ON CONFLICT ABORT); 1643 CREATE TABLE t5(a NOT NULL ON CONFLICT IGNORE, b NOT NULL ON CONFLICT ABORT);
1629 } 1644 }
1630 do_catchsql_test 4.19.1 { INSERT INTO t5 VALUES(NULL, 'not null') } {0 {}} 1645 do_catchsql_test 4.19.1 { INSERT INTO t5 VALUES(NULL, 'not null') } {0 {}}
1631 do_execsql_test 4.19.2 { SELECT * FROM t5 } {} 1646 do_execsql_test 4.19.2 { SELECT * FROM t5 } {}
1632 do_catchsql_test 4.19.3 { INSERT INTO t5 VALUES('not null', NULL) } \ 1647 do_catchsql_test 4.19.3 { INSERT INTO t5 VALUES('not null', NULL) } \
1633 {1 {t5.b may not be NULL}} 1648 {1 {NOT NULL constraint failed: t5.b}}
1634 do_execsql_test 4.19.4 { SELECT * FROM t5 } {} 1649 do_execsql_test 4.19.4 { SELECT * FROM t5 } {}
1635 1650
1636 #------------------------------------------------------------------------ 1651 #------------------------------------------------------------------------
1637 # Tests for INTEGER PRIMARY KEY and rowid related statements. 1652 # Tests for INTEGER PRIMARY KEY and rowid related statements.
1638 # 1653 #
1639 1654
1640 # EVIDENCE-OF: R-52584-04009 The rowid value can be accessed using one 1655 # EVIDENCE-OF: R-52584-04009 The rowid value can be accessed using one
1641 # of the special case-independent names "rowid", "oid", or "_rowid_" in 1656 # of the special case-independent names "rowid", "oid", or "_rowid_" in
1642 # place of a column name. 1657 # place of a column name.
1643 # 1658 #
1659 # EVIDENCE-OF: R-06726-07466 A column name can be any of the names
1660 # defined in the CREATE TABLE statement or one of the following special
1661 # identifiers: "ROWID", "OID", or "_ROWID_".
1662 #
1644 drop_all_tables 1663 drop_all_tables
1645 do_execsql_test 5.1.0 { 1664 do_execsql_test 5.1.0 {
1646 CREATE TABLE t1(x, y); 1665 CREATE TABLE t1(x, y);
1647 INSERT INTO t1 VALUES('one', 'first'); 1666 INSERT INTO t1 VALUES('one', 'first');
1648 INSERT INTO t1 VALUES('two', 'second'); 1667 INSERT INTO t1 VALUES('two', 'second');
1649 INSERT INTO t1 VALUES('three', 'third'); 1668 INSERT INTO t1 VALUES('three', 'third');
1650 } 1669 }
1651 do_createtable_tests 5.1 { 1670 do_createtable_tests 5.1 {
1652 1 "SELECT rowid FROM t1" {1 2 3} 1671 1 "SELECT rowid FROM t1" {1 2 3}
1653 2 "SELECT oid FROM t1" {1 2 3} 1672 2 "SELECT oid FROM t1" {1 2 3}
1654 3 "SELECT _rowid_ FROM t1" {1 2 3} 1673 3 "SELECT _rowid_ FROM t1" {1 2 3}
1655 4 "SELECT ROWID FROM t1" {1 2 3} 1674 4 "SELECT ROWID FROM t1" {1 2 3}
1656 5 "SELECT OID FROM t1" {1 2 3} 1675 5 "SELECT OID FROM t1" {1 2 3}
1657 6 "SELECT _ROWID_ FROM t1" {1 2 3} 1676 6 "SELECT _ROWID_ FROM t1" {1 2 3}
1658 7 "SELECT RoWiD FROM t1" {1 2 3} 1677 7 "SELECT RoWiD FROM t1" {1 2 3}
1659 8 "SELECT OiD FROM t1" {1 2 3} 1678 8 "SELECT OiD FROM t1" {1 2 3}
1660 9 "SELECT _RoWiD_ FROM t1" {1 2 3} 1679 9 "SELECT _RoWiD_ FROM t1" {1 2 3}
1661 } 1680 }
1662 1681
1663 # EVIDENCE-OF: R-26501-17306 If a table contains a user defined column 1682 # EVIDENCE-OF: R-26501-17306 If a table contains a user defined column
1664 # named "rowid", "oid" or "_rowid_", then that name always refers the 1683 # named "rowid", "oid" or "_rowid_", then that name always refers the
1665 # explicitly declared column and cannot be used to retrieve the integer 1684 # explicitly declared column and cannot be used to retrieve the integer
1666 # rowid value. 1685 # rowid value.
1667 # 1686 #
1687 # EVIDENCE-OF: R-44615-33286 The special identifiers only refer to the
1688 # row key if the CREATE TABLE statement does not define a real column
1689 # with the same name.
1690 #
1668 do_execsql_test 5.2.0 { 1691 do_execsql_test 5.2.0 {
1669 CREATE TABLE t2(oid, b); 1692 CREATE TABLE t2(oid, b);
1670 CREATE TABLE t3(a, _rowid_); 1693 CREATE TABLE t3(a, _rowid_);
1671 CREATE TABLE t4(a, b, rowid); 1694 CREATE TABLE t4(a, b, rowid);
1672 1695
1673 INSERT INTO t2 VALUES('one', 'two'); 1696 INSERT INTO t2 VALUES('one', 'two');
1674 INSERT INTO t2 VALUES('three', 'four'); 1697 INSERT INTO t2 VALUES('three', 'four');
1675 1698
1676 INSERT INTO t3 VALUES('five', 'six'); 1699 INSERT INTO t3 VALUES('five', 'six');
1677 INSERT INTO t3 VALUES('seven', 'eight'); 1700 INSERT INTO t3 VALUES('seven', 'eight');
(...skipping 14 matching lines...) Expand all
1692 # 1715 #
1693 proc is_integer_primary_key {tbl col} { 1716 proc is_integer_primary_key {tbl col} {
1694 lindex [db eval [subst { 1717 lindex [db eval [subst {
1695 DELETE FROM $tbl; 1718 DELETE FROM $tbl;
1696 INSERT INTO $tbl ($col) VALUES(0); 1719 INSERT INTO $tbl ($col) VALUES(0);
1697 SELECT (rowid==$col) FROM $tbl; 1720 SELECT (rowid==$col) FROM $tbl;
1698 DELETE FROM $tbl; 1721 DELETE FROM $tbl;
1699 }]] 0 1722 }]] 0
1700 } 1723 }
1701 1724
1702 # EVIDENCE-OF: R-53738-31673 With one exception, if a table has a 1725 # EVIDENCE-OF: R-47901-33947 With one exception noted below, if a rowid
1703 # primary key that consists of a single column, and the declared type of 1726 # table has a primary key that consists of a single column and the
1704 # that column is "INTEGER" in any mixture of upper and lower case, then 1727 # declared type of that column is "INTEGER" in any mixture of upper and
1705 # the column becomes an alias for the rowid. 1728 # lower case, then the column becomes an alias for the rowid.
1706 # 1729 #
1707 # EVIDENCE-OF: R-45951-08347 if the declaration of a column with 1730 # EVIDENCE-OF: R-45951-08347 if the declaration of a column with
1708 # declared type "INTEGER" includes an "PRIMARY KEY DESC" clause, it does 1731 # declared type "INTEGER" includes an "PRIMARY KEY DESC" clause, it does
1709 # not become an alias for the rowid and is not classified as an integer 1732 # not become an alias for the rowid and is not classified as an integer
1710 # primary key. 1733 # primary key.
1711 # 1734 #
1712 do_createtable_tests 5.3 -tclquery { 1735 do_createtable_tests 5.3 -tclquery {
1713 is_integer_primary_key t5 pk 1736 is_integer_primary_key t5 pk
1714 } -repair { 1737 } -repair {
1715 catchsql { DROP TABLE t5 } 1738 catchsql { DROP TABLE t5 }
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
1748 INSERT INTO t8 VALUES('2.0'); 1771 INSERT INTO t8 VALUES('2.0');
1749 INSERT INTO t9 VALUES('2.0'); 1772 INSERT INTO t9 VALUES('2.0');
1750 SELECT typeof(pk), pk FROM t6; 1773 SELECT typeof(pk), pk FROM t6;
1751 SELECT typeof(pk), pk FROM t7; 1774 SELECT typeof(pk), pk FROM t7;
1752 SELECT typeof(pk), pk FROM t8; 1775 SELECT typeof(pk), pk FROM t8;
1753 SELECT typeof(pk), pk FROM t9; 1776 SELECT typeof(pk), pk FROM t9;
1754 } {integer 2 integer 2 integer 2 integer 2} 1777 } {integer 2 integer 2 integer 2 integer 2}
1755 1778
1756 do_catchsql_test 5.4.4.1 { 1779 do_catchsql_test 5.4.4.1 {
1757 INSERT INTO t6 VALUES(2) 1780 INSERT INTO t6 VALUES(2)
1758 } {1 {column pk is not unique}} 1781 } {1 {UNIQUE constraint failed: t6.pk}}
1759 do_catchsql_test 5.4.4.2 { 1782 do_catchsql_test 5.4.4.2 {
1760 INSERT INTO t7 VALUES(2) 1783 INSERT INTO t7 VALUES(2)
1761 } {1 {column pk is not unique}} 1784 } {1 {UNIQUE constraint failed: t7.pk}}
1762 do_catchsql_test 5.4.4.3 { 1785 do_catchsql_test 5.4.4.3 {
1763 INSERT INTO t8 VALUES(2) 1786 INSERT INTO t8 VALUES(2)
1764 } {1 {column pk is not unique}} 1787 } {1 {UNIQUE constraint failed: t8.pk}}
1765 do_catchsql_test 5.4.4.4 { 1788 do_catchsql_test 5.4.4.4 {
1766 INSERT INTO t9 VALUES(2) 1789 INSERT INTO t9 VALUES(2)
1767 } {1 {column pk is not unique}} 1790 } {1 {UNIQUE constraint failed: t9.pk}}
1768 1791
1769 # EVIDENCE-OF: R-56094-57830 the following three table declarations all 1792 # EVIDENCE-OF: R-56094-57830 the following three table declarations all
1770 # cause the column "x" to be an alias for the rowid (an integer primary 1793 # cause the column "x" to be an alias for the rowid (an integer primary
1771 # key): CREATE TABLE t(x INTEGER PRIMARY KEY ASC, y, z); CREATE TABLE 1794 # key): CREATE TABLE t(x INTEGER PRIMARY KEY ASC, y, z); CREATE TABLE
1772 # t(x INTEGER, y, z, PRIMARY KEY(x ASC)); CREATE TABLE t(x INTEGER, y, 1795 # t(x INTEGER, y, z, PRIMARY KEY(x ASC)); CREATE TABLE t(x INTEGER, y,
1773 # z, PRIMARY KEY(x DESC)); 1796 # z, PRIMARY KEY(x DESC));
1774 # 1797 #
1775 # EVIDENCE-OF: R-20149-25884 the following declaration does not result 1798 # EVIDENCE-OF: R-20149-25884 the following declaration does not result
1776 # in "x" being an alias for the rowid: CREATE TABLE t(x INTEGER PRIMARY 1799 # in "x" being an alias for the rowid: CREATE TABLE t(x INTEGER PRIMARY
1777 # KEY DESC, y, z); 1800 # KEY DESC, y, z);
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
1921 1 "INSERT INTO t12 DEFAULT VALUES" {integer 1} 1944 1 "INSERT INTO t12 DEFAULT VALUES" {integer 1}
1922 2 "INSERT INTO t12(y) VALUES(5)" {integer 2} 1945 2 "INSERT INTO t12(y) VALUES(5)" {integer 2}
1923 3 "INSERT INTO t12(x,y) VALUES(NULL, 10)" {integer 3} 1946 3 "INSERT INTO t12(x,y) VALUES(NULL, 10)" {integer 3}
1924 4 "INSERT INTO t12(x,y) SELECT NULL, 15 FROM t12" 1947 4 "INSERT INTO t12(x,y) SELECT NULL, 15 FROM t12"
1925 {integer 4 integer 5 integer 6} 1948 {integer 4 integer 5 integer 6}
1926 5 "INSERT INTO t12(y) SELECT 20 FROM t12 LIMIT 3" 1949 5 "INSERT INTO t12(y) SELECT 20 FROM t12 LIMIT 3"
1927 {integer 7 integer 8 integer 9} 1950 {integer 7 integer 8 integer 9}
1928 } 1951 }
1929 1952
1930 finish_test 1953 finish_test
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698