1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
SET @@session.default_storage_engine = 'InnoDB';
# Case 1. All non-stored columns.
# This scenario is currently impossible due to the fact that virtual columns
# with a constant expression are not allowed.
# Case 2. CREATE
# - Column1: "real"
# - Column 2: virtual non-stored
create table t1 (a int, b int as (-a));
insert into t1 values (1,default);
select * from t1;
a b
1 -1
insert into t1 values (2,default);
select * from t1;
a b
1 -1
2 -2
drop table t1;
# Case 3. CREATE
# - Column1: "real"
# - Column 2: virtual stored
create table t1 (a int, b int as (-a) persistent);
insert into t1 values (1,default);
select * from t1;
a b
1 -1
insert into t1 values (2,default);
select * from t1;
a b
1 -1
2 -2
drop table t1;
# Case 4. CREATE
# - Column1: virtual non-stored
# - Column2: "real"
create table t1 (a int as (-b), b int);
insert into t1 values (default,1);
select * from t1;
a b
-1 1
insert into t1 values (default,2);
select * from t1;
a b
-1 1
-2 2
drop table t1;
# Case 5. CREATE
# - Column1: virtual stored
# - Column2: "real"
create table t1 (a int as (-b) persistent, b int);
insert into t1 values (default,1);
select * from t1;
a b
-1 1
insert into t1 values (default,2);
select * from t1;
a b
-1 1
-2 2
drop table t1;
# Case 6. CREATE
# - Column1: "real"
# - Column2: virtual non-stored
# - Column3: virtual stored
create table t1 (a int, b int as (-a), c int as (-a) persistent);
insert into t1 values (1,default,default);
select * from t1;
a b c
1 -1 -1
insert into t1 values (2,default,default);
select * from t1;
a b c
1 -1 -1
2 -2 -2
drop table t1;
# Case 7. ALTER. Modify virtual stored -> virtual non-stored
create table t1 (a int, b int as (a % 2) persistent);
alter table t1 modify b int as (a % 2);
ERROR HY000: This is not yet supported for generated columns
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) DEFAULT NULL,
`b` int(11) GENERATED ALWAYS AS (`a` MOD 2) STORED
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
drop table t1;
# Case 8. ALTER. Modify virtual non-stored -> virtual stored
create table t1 (a int, b int as (a % 2));
alter table t1 modify b int as (a % 2) persistent;
ERROR HY000: This is not yet supported for generated columns
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) DEFAULT NULL,
`b` int(11) GENERATED ALWAYS AS (`a` MOD 2) VIRTUAL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
drop table t1;
# Case 9. CREATE LIKE
# - Column1: "real"
# - Column2: virtual non-stored
# - Column3: virtual stored
create table t1 (a int, b int as (-a), c int as (-a) persistent);
create table t2 like t1;
insert into t2 values (1,default,default);
select * from t2;
a b c
1 -1 -1
insert into t2 values (2,default,default);
select * from t2;
a b c
1 -1 -1
2 -2 -2
drop table t2;
drop table t1;
# Case 10. ALTER. Dropping a virtual non-stored column.
# - Column1: virtual non-stored
# - Column2: "real"
create table t1 (a int as (-b), b int, c varchar(5));
insert into t1 values (default,1,'v1');
insert into t1 values (default,2,'v2');
select * from t1;
a b c
-1 1 v1
-2 2 v2
alter table t1 drop column a;
select * from t1;
b c
1 v1
2 v2
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`b` int(11) DEFAULT NULL,
`c` varchar(5) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
drop table t1;
# Case 11. ALTER. Dropping a virtual stored column.
# - Column1: virtual stored
# - Column2: "real"
create table t1 (a int as (-b) persistent, b int, c char(5));
insert into t1 values (default,1,'v1');
insert into t1 values (default,2,'v2');
select * from t1;
a b c
-1 1 v1
-2 2 v2
alter table t1 drop column a;
select * from t1;
b c
1 v1
2 v2
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`b` int(11) DEFAULT NULL,
`c` char(5) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
drop table t1;
# Case 12. ALTER. Adding a new virtual non-stored column.
create table t1 (a int, b datetime);
insert into t1 values (1,'2008-09-04');
insert into t1 values (2,'2008-09-05');
select * from t1;
a b
1 2008-09-04 00:00:00
2 2008-09-05 00:00:00
alter table t1 add column c int as (dayofyear(b)) after a;
select * from t1;
a c b
1 248 2008-09-04 00:00:00
2 249 2008-09-05 00:00:00
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) DEFAULT NULL,
`c` int(11) GENERATED ALWAYS AS (dayofyear(`b`)) VIRTUAL,
`b` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
drop table t1;
# Case 13. ALTER. Adding a new virtual stored column.
create table t1 (a int, b datetime);
insert into t1 values (1,'2008-09-04');
insert into t1 values (2,'2008-09-05');
select * from t1;
a b
1 2008-09-04 00:00:00
2 2008-09-05 00:00:00
alter table t1 add column c int as (dayofyear(b)) persistent after a;
select * from t1;
a c b
1 248 2008-09-04 00:00:00
2 249 2008-09-05 00:00:00
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) DEFAULT NULL,
`c` int(11) GENERATED ALWAYS AS (dayofyear(`b`)) STORED,
`b` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
drop table t1;
# Case 14. ALTER. Changing the expression of a virtual stored column.
create table t1 (a int, b datetime, c int as (week(b,0)) persistent);
insert into t1 values (1,'2008-09-04',default);
insert into t1 values (2,'2008-09-05',default);
select * from t1;
a b c
1 2008-09-04 00:00:00 35
2 2008-09-05 00:00:00 35
alter table t1 change column c c int as (week(b,1)) persistent;
select * from t1;
a b c
1 2008-09-04 00:00:00 36
2 2008-09-05 00:00:00 36
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) DEFAULT NULL,
`b` datetime DEFAULT NULL,
`c` int(11) GENERATED ALWAYS AS (week(`b`,1)) STORED
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
drop table t1;
# Case 15. ALTER. Changing the expression of a virtual non-stored column.
create table t1 (a int, b datetime, c int as (week(b,0)));
insert into t1 values (1,'2008-09-04',default);
insert into t1 values (2,'2008-09-05',default);
select * from t1;
a b c
1 2008-09-04 00:00:00 35
2 2008-09-05 00:00:00 35
alter table t1 change column c c int as (week(b,1));
select * from t1;
a b c
1 2008-09-04 00:00:00 36
2 2008-09-05 00:00:00 36
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) DEFAULT NULL,
`b` datetime DEFAULT NULL,
`c` int(11) GENERATED ALWAYS AS (week(`b`,1)) VIRTUAL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
drop table t1;
#
# MDEV-6103 - Adding/removing non-materialized virtual column triggers
# table recreation
#
CREATE TABLE t1(a INT);
INSERT INTO t1 VALUES(1),(1),(1),(1),(1),(1),(1),(1),(1),(1);
ALTER TABLE t1 ADD COLUMN b INT AS (a + 1) VIRTUAL;
affected rows: 0
info: Records: 0 Duplicates: 0 Warnings: 0
ALTER TABLE t1 DROP COLUMN b;
affected rows: 0
info: Records: 0 Duplicates: 0 Warnings: 0
CHECK TABLE t1;
Table Op Msg_type Msg_text
test.t1 check status OK
DROP TABLE t1;
|