3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #*************************************************************************
13 set testdir [file dirname $argv0]
14 source $testdir/tester.tcl
15 set testprefix alterdropcol2
17 # If SQLITE_OMIT_ALTERTABLE is defined, omit this file.
18 ifcapable !altertable {
23 # EVIDENCE-OF: R-58318-35349 The DROP COLUMN syntax is used to remove an
24 # existing column from a table.
26 CREATE TABLE t1(c, b, a, PRIMARY KEY(b, a)) WITHOUT ROWID;
27 INSERT INTO t1 VALUES(1, 2, 3), (4, 5, 6);
30 ALTER TABLE t1 DROP c;
33 # EVIDENCE-OF: The DROP COLUMN command removes the named column from the table,
34 # and also rewrites the entire table to purge the data associated with that
36 do_execsql_test 1.2.1 {
40 do_execsql_test 1.2.2 {
41 SELECT sql FROM sqlite_schema;
43 {CREATE TABLE t1(b, a, PRIMARY KEY(b, a)) WITHOUT ROWID}
46 proc do_atdc_error_test {tn schema atdc error} {
49 uplevel [list do_catchsql_test $tn $atdc [list 1 [string trim $error]]]
52 #-------------------------------------------------------------------------
53 # Test cases 2.* attempt to verify the following:
55 # EVIDENCE-OF: R-24098-10282 The DROP COLUMN command only works if the column
56 # is not referenced by any other parts of the schema and is not a PRIMARY KEY
57 # and does not have a UNIQUE constraint.
60 # EVIDENCE-OF: R-52436-31752 The column is a PRIMARY KEY or part of one.
62 do_atdc_error_test 2.1.1 {
63 CREATE TABLE x1(a PRIMARY KEY, b, c);
65 ALTER TABLE x1 DROP COLUMN a
67 cannot drop PRIMARY KEY column: "a"
69 do_atdc_error_test 2.1.2 {
70 CREATE TABLE x1(a,b,c,d,e, PRIMARY KEY(b,c,d));
72 ALTER TABLE x1 DROP COLUMN c
74 cannot drop PRIMARY KEY column: "c"
77 # EVIDENCE-OF: R-43412-16016 The column has a UNIQUE constraint.
79 do_atdc_error_test 2.2.1 {
80 CREATE TABLE x1(a PRIMARY KEY, b, c UNIQUE);
82 ALTER TABLE x1 DROP COLUMN c
84 cannot drop UNIQUE column: "c"
86 do_atdc_error_test 2.2.2 {
87 CREATE TABLE x1(a PRIMARY KEY, b, c, UNIQUE(b, c));
89 ALTER TABLE x1 DROP COLUMN c
91 error in table x1 after drop column: no such column: c
94 # EVIDENCE-OF: R-46731-08965 The column is indexed.
96 do_atdc_error_test 2.3.1 {
97 CREATE TABLE 'one two'('x y', 'z 1', 'a b');
98 CREATE INDEX idx ON 'one two'('z 1');
100 ALTER TABLE 'one two' DROP COLUMN 'z 1'
102 error in index idx after drop column: no such column: z 1
104 do_atdc_error_test 2.3.2 {
105 CREATE TABLE x1(a, b, c);
106 CREATE INDEX idx ON x1(a);
108 ALTER TABLE x1 DROP COLUMN a;
110 error in index idx after drop column: no such column: a
113 # EVIDENCE-OF: R-46731-08965 The column is indexed.
115 do_atdc_error_test 2.4.1 {
116 CREATE TABLE x1234(a, b, c PRIMARY KEY) WITHOUT ROWID;
117 CREATE INDEX i1 ON x1234(b) WHERE ((a+5) % 10)==0;
119 ALTER TABLE x1234 DROP a
121 error in index i1 after drop column: no such column: a
124 # EVIDENCE-OF: R-47838-03249 The column is named in a table or column
125 # CHECK constraint not associated with the column being dropped.
127 do_atdc_error_test 2.5.1 {
128 CREATE TABLE x1234(a, b, c PRIMARY KEY, CHECK(((a+5)%10)!=0)) WITHOUT ROWID;
130 ALTER TABLE x1234 DROP a
132 error in table x1234 after drop column: no such column: a
135 # EVIDENCE-OF: R-55640-01652 The column is used in a foreign key constraint.
137 do_atdc_error_test 2.6.1 {
138 CREATE TABLE p1(x, y UNIQUE);
139 CREATE TABLE c1(u, v, FOREIGN KEY (v) REFERENCES p1(y))
141 ALTER TABLE c1 DROP v
143 error in table c1 after drop column: unknown column "v" in foreign key definition
146 # EVIDENCE-OF: R-20795-39479 The column is used in the expression of a
148 do_atdc_error_test 2.7.1 {
149 CREATE TABLE c1(u, v, w AS (u+v));
151 ALTER TABLE c1 DROP v
153 error in table c1 after drop column: no such column: v
155 do_atdc_error_test 2.7.2 {
156 CREATE TABLE c1(u, v, w AS (u+v) STORED);
158 ALTER TABLE c1 DROP u
160 error in table c1 after drop column: no such column: u
163 # EVIDENCE-OF: R-01515-49025 The column appears in a trigger or view.
165 do_atdc_error_test 2.8.1 {
167 CREATE TABLE c1(u, v, w);
168 CREATE TRIGGER tr1 AFTER INSERT ON c1 BEGIN
169 INSERT INTO log VALUES(new.w);
172 ALTER TABLE c1 DROP w
174 error in trigger tr1 after drop column: no such column: new.w
176 do_atdc_error_test 2.8.2 {
177 CREATE TABLE c1(u, v, w);
178 CREATE VIEW v1 AS SELECT u, v, w FROM c1;
180 ALTER TABLE c1 DROP w
182 error in view v1 after drop column: no such column: w
184 do_atdc_error_test 2.8.3 {
185 CREATE TABLE c1(u, v, w);
186 CREATE VIEW v1 AS SELECT * FROM c1 WHERE w IS NOT NULL;
188 ALTER TABLE c1 DROP w
190 error in view v1 after drop column: no such column: w
193 #-------------------------------------------------------------------------
194 # Verify that a column that is part of a CHECK constraint may be dropped
195 # if the CHECK constraint was specified as part of the column definition.
198 # STALE-EVIDENCE: R-60924-11170 However, the column being deleted can be used in a
199 # column CHECK constraint because the column CHECK constraint is dropped
200 # together with the column itself.
201 do_execsql_test 3.0 {
202 CREATE TABLE yyy(q, w, e CHECK (e > 0), r);
203 INSERT INTO yyy VALUES(1,1,1,1), (2,2,2,2);
205 CREATE TABLE zzz(q, w, e, r, CHECK (e > 0));
206 INSERT INTO zzz VALUES(1,1,1,1), (2,2,2,2);
208 do_catchsql_test 3.1.1 {
209 INSERT INTO yyy VALUES(0,0,0,0);
210 } {1 {CHECK constraint failed: e > 0}}
211 do_catchsql_test 3.1.2 {
212 INSERT INTO yyy VALUES(0,0,0,0);
213 } {1 {CHECK constraint failed: e > 0}}
215 do_execsql_test 3.2.1 {
216 ALTER TABLE yyy DROP e;
218 do_catchsql_test 3.2.2 {
219 ALTER TABLE zzz DROP e;
220 } {1 {error in table zzz after drop column: no such column: e}}