|
| 1 | +--echo # |
| 2 | +--echo # Delete with value from subquery on the same table |
| 3 | +--echo # |
| 4 | + |
| 5 | +analyze table t1 persistent for all; |
| 6 | + |
| 7 | +let $c = c1=(select a.c3 from t1 a where a.c3 = t1.c3); |
| 8 | +eval create table tmp as select * from t1 where $c; |
| 9 | +let $q = delete from t1 where $c; |
| 10 | +eval explain select * from t1 where $c; |
| 11 | +eval explain $q; |
| 12 | +--enable_info ONCE |
| 13 | +eval $q; |
| 14 | +--sorted_result |
| 15 | +select * from t1; |
| 16 | +insert into t1(c1,c2,c3) select c1,c2,c3 from tmp; |
| 17 | +drop table tmp; |
| 18 | + |
| 19 | +--echo # |
| 20 | +--echo # Delete with EXISTS subquery over the updated table |
| 21 | +--echo # in WHERE + possibly sargable condition |
| 22 | +--echo # |
| 23 | + |
| 24 | +analyze table t1 persistent for all; |
| 25 | + |
| 26 | +let $c = c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2); |
| 27 | +eval create table tmp as select * from t1 where $c; |
| 28 | +let $q = delete from t1 where $c; |
| 29 | +eval explain select * from t1 where $c; |
| 30 | +eval explain $q; |
| 31 | +eval analyze $q; |
| 32 | +--sorted_result |
| 33 | +select * from t1; |
| 34 | +insert into t1(c1,c2,c3) select c1,c2,c3 from tmp; |
| 35 | +drop table tmp; |
| 36 | + |
| 37 | +--echo # |
| 38 | +--echo # Delete with IN predicand over the updated table in WHERE |
| 39 | +--echo # |
| 40 | +let $c = c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2); |
| 41 | +eval create table tmp as select * from t1 where $c; |
| 42 | +let $q = delete from t1 where $c; |
| 43 | +eval explain select * from t1 where $c; |
| 44 | +eval explain $q; |
| 45 | +--enable_info ONCE |
| 46 | +eval $q; |
| 47 | +--sorted_result |
| 48 | +select * from t1; |
| 49 | +insert into t1(c1,c2,c3) select c1,c2,c3 from tmp; |
| 50 | +drop table tmp; |
| 51 | + |
| 52 | +--echo # |
| 53 | +--echo # Delete with a limit - can be deleted |
| 54 | +--echo # |
| 55 | +let $c = c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1; |
| 56 | +eval create table tmp as select * from t1 where $c; |
| 57 | +let $q = delete from t1 where $c; |
| 58 | +eval explain select * from t1 where $c; |
| 59 | +eval explain $q; |
| 60 | +eval analyze $q; |
| 61 | +--sorted_result |
| 62 | +select * from t1; |
| 63 | +insert into t1(c1,c2,c3) select c1,c2,c3 from tmp; |
| 64 | +drop table tmp; |
| 65 | + |
| 66 | +--echo # |
| 67 | +--echo # Delete with a limit and an order by |
| 68 | +--echo # |
| 69 | + |
| 70 | +let $c = c1 in (select a.c2 from t1 a where a.c3 = t1.c3) |
| 71 | + order by c3 desc limit 1; |
| 72 | +eval create table tmp as select * from t1 where $c; |
| 73 | +let $q = delete from t1 where $c; |
| 74 | +eval explain select * from t1 where $c; |
| 75 | +eval explain $q; |
| 76 | +--enable_info ONCE |
| 77 | +eval $q; |
| 78 | +--sorted_result |
| 79 | +select * from t1; |
| 80 | +insert into t1(c1,c2,c3) select c1,c2,c3 from tmp; |
| 81 | +drop table tmp; |
| 82 | + |
| 83 | +--echo # |
| 84 | +--echo # Delete: 2 execution of PS |
| 85 | +--echo # |
| 86 | + |
| 87 | +prepare create_tmp_stmt from |
| 88 | + "create table tmp as select * from t1 |
| 89 | + where c2=(select a.c3 from t1 a where a.c3 = ?)"; |
| 90 | +prepare delete_t1_stmt from |
| 91 | + "delete from t1 where c2=(select a.c3 from t1 a where a.c3 = ?)"; |
| 92 | +set @a:=5; |
| 93 | +execute create_tmp_stmt using @a; |
| 94 | +execute delete_t1_stmt using @a; |
| 95 | +execute delete_t1_stmt using @a; |
| 96 | +--sorted_result |
| 97 | +select * from t1; |
| 98 | + |
| 99 | +prepare insert_tmp_stmt from |
| 100 | + "insert into tmp(c1,c2,c3) select * from t1 |
| 101 | + where c2=(select a.c3 from t1 a where a.c3 = ?)"; |
| 102 | +set @a:=2; |
| 103 | +execute insert_tmp_stmt using @a; |
| 104 | +execute delete_t1_stmt using @a; |
| 105 | +--sorted_result |
| 106 | +select * from t1; |
| 107 | + |
| 108 | +insert into t1(c1,c2,c3) select c1,c2,c3 from tmp; |
| 109 | +--sorted_result |
| 110 | +select * from t1; |
| 111 | + |
| 112 | +drop table tmp; |
| 113 | + |
| 114 | +--echo # |
| 115 | +--echo # Delete in stored procedure |
| 116 | +--echo # |
| 117 | + |
| 118 | +delimiter //; |
| 119 | +create procedure sp() |
| 120 | +begin |
| 121 | + delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3) |
| 122 | + order by c3 desc limit 1; |
| 123 | +end |
| 124 | +// |
| 125 | +delimiter ;// |
| 126 | + |
| 127 | +create table tmp as select * from t1 |
| 128 | + where c1 in (select a.c2 from t1 a where a.c3 = t1.c3) |
| 129 | + order by c3 desc limit 1; |
| 130 | +CALL sp; |
| 131 | +insert into tmp(c1,c2,c3) select * from t1 |
| 132 | + where c1 in (select a.c2 from t1 a where a.c3 = t1.c3) |
| 133 | + order by c3 desc limit 1; |
| 134 | +CALL sp; |
| 135 | +insert into tmp(c1,c2,c3) select * from t1 |
| 136 | + where c1 in (select a.c2 from t1 a where a.c3 = t1.c3) |
| 137 | + order by c3 desc limit 1; |
| 138 | +CALL sp; |
| 139 | +--sorted_result |
| 140 | +select * from t1; |
| 141 | +insert into t1(c1,c2,c3) select c1,c2,c3 from tmp; |
| 142 | +drop procedure sp; |
| 143 | +drop table tmp; |
| 144 | + |
| 145 | +--echo # |
| 146 | +--echo # Delete in stored function |
| 147 | +--echo # |
| 148 | +delimiter //; |
| 149 | +create function f1(IN a INT) returns int |
| 150 | +begin |
| 151 | + delete from t1 where c3 < a order by c3 limit 1; |
| 152 | + return 1; |
| 153 | +end;// |
| 154 | +delimiter ;// |
| 155 | + |
| 156 | +set @a:=7; |
| 157 | +create table tmp as select * from t1 where c3 < @a |
| 158 | + order by c3 limit 1; |
| 159 | +select f1(@a); |
| 160 | +insert into tmp(c1,c2,c3) select * from t1 where c3 < @a |
| 161 | + order by c3 limit 1; |
| 162 | +select f1(@a); |
| 163 | +--sorted_result |
| 164 | +select * from t1; |
| 165 | +insert into t1(c1,c2,c3) select c1,c2,c3 from tmp; |
| 166 | +drop function f1; |
| 167 | +drop table tmp; |
| 168 | + |
| 169 | +--echo # |
| 170 | +--echo # Delete in trigger |
| 171 | +--echo # |
| 172 | + |
| 173 | +create table t2 (c1 integer); |
| 174 | +insert into t2(c1) values (1), (2), (3), (4), (5), (6), (7), (8); |
| 175 | + |
| 176 | +CREATE TABLE cnt(del integer); |
| 177 | +INSERT INTO cnt VALUES(0); |
| 178 | + |
| 179 | +CREATE TRIGGER tr1 AFTER DELETE ON t1 FOR EACH ROW |
| 180 | + UPDATE cnt SET del=del+1; |
| 181 | +CREATE TRIGGER tr2 AFTER DELETE ON t1 FOR EACH ROW |
| 182 | + DELETE FROM t2 WHERE c1> (SELECT count(*)-1 FROM t2); |
| 183 | + |
| 184 | +CREATE TABLE tmp as SELECT * FROM t1 WHERE c2>=3; |
| 185 | +--enable_info ONCE |
| 186 | +DELETE FROM t1 WHERE c2>=3; |
| 187 | + |
| 188 | +--sorted_result |
| 189 | +select * from t1; |
| 190 | +--sorted_result |
| 191 | +SELECT * FROM t2; |
| 192 | +SELECT * FROM cnt; |
| 193 | + |
| 194 | +insert into t1(c1,c2,c3) select c1,c2,c3 from tmp; |
| 195 | + |
| 196 | +DROP TRIGGER tr1; |
| 197 | +DROP TRIGGER tr2; |
| 198 | +drop table t2, cnt, tmp; |
| 199 | + |
| 200 | +--echo # |
| 201 | +--echo # Delete with a reference to view in subquery |
| 202 | +--echo # |
| 203 | +let $c = t1.c2 in ( select max(a.c2) from v1 a |
| 204 | + where a.c1 = t1.c1); |
| 205 | +eval create table tmp as select * from t1 where $c; |
| 206 | +let $q = delete from t1 where $c; |
| 207 | +eval explain select * from t1 where $c; |
| 208 | +eval explain $q; |
| 209 | +eval analyze $q; |
| 210 | +--sorted_result |
| 211 | +select * from t1; |
| 212 | +insert into t1(c1,c2,c3) select c1,c2,c3 from tmp; |
| 213 | +drop table tmp; |
| 214 | + |
0 commit comments