1# 2# Bug#22469130: FOREIGN KEY ON DELETE CASCADE NOT ALLOWED 3# WHEN A VIRTUAL INDEX EXISTS. 4# UPDATE CASCADE 5CREATE TABLE t1(fld1 INT NOT NULL PRIMARY KEY); 6CREATE TABLE t2(fld1 INT NOT NULL, fld2 INT AS (fld1) VIRTUAL, KEY(fld2), 7FOREIGN KEY(fld1) REFERENCES t1(fld1) ON UPDATE CASCADE); 8INSERT INTO t1 VALUES(1); 9INSERT INTO t2 VALUES(1, DEFAULT); 10UPDATE t1 SET fld1= 2; 11EXPLAIN SELECT fld2 FROM t2; 12id select_type table partitions type possible_keys key key_len ref rows filtered Extra 13x x t2 x index x fld2 x x x x x 14SELECT fld2 FROM t2; 15fld2 162 17SELECT * FROM t2; 18fld1 fld2 192 2 20DROP TABLE t2, t1; 21# UPDATE SET NULL 22CREATE TABLE t1(fld1 INT NOT NULL, fld2 INT NOT NULL PRIMARY KEY, 23KEY(fld1)); 24CREATE TABLE t2(fld1 INT, fld2 INT AS (fld1) VIRTUAL, KEY(fld2), 25FOREIGN KEY(fld1) REFERENCES t1(fld1) ON UPDATE SET NULL); 26INSERT INTO t1 VALUES(1, 2); 27INSERT INTO t2 VALUES(1, DEFAULT); 28UPDATE t1 SET fld1= 2; 29EXPLAIN SELECT fld2 FROM t2; 30id select_type table partitions type possible_keys key key_len ref rows filtered Extra 31x x t2 x index x fld2 x x x x x 32SELECT fld2 FROM t2; 33fld2 34NULL 35SELECT * FROM t2; 36fld1 fld2 37NULL NULL 38DROP TABLE t2, t1; 39# DELETE CASCADE 40CREATE TABLE t1(fld1 INT NOT NULL PRIMARY KEY); 41CREATE TABLE t2(fld1 INT, fld2 INT AS (fld1) VIRTUAL, KEY(fld2), 42FOREIGN KEY(fld1) REFERENCES t1(fld1) ON DELETE CASCADE); 43INSERT INTO t1 VALUES(1); 44INSERT INTO t1 VALUES(2); 45INSERT INTO t2 VALUES(1, DEFAULT); 46INSERT INTO t2 VALUES(2, DEFAULT); 47DELETE FROM t1 WHERE fld1= 1; 48EXPLAIN SELECT fld2 FROM t2; 49id select_type table partitions type possible_keys key key_len ref rows filtered Extra 50x x t2 x index x fld2 x x x x x 51SELECT fld2 FROM t2; 52fld2 532 54SELECT * FROM t2; 55fld1 fld2 562 2 57DROP TABLE t2, t1; 58# DELETE SET NULL 59CREATE TABLE t1(fld1 INT NOT NULL, fld2 INT NOT NULL PRIMARY KEY, KEY(fld1)); 60CREATE TABLE t2(fld1 INT, fld2 INT AS (fld1) VIRTUAL, KEY(fld2), 61FOREIGN KEY(fld1) REFERENCES t1(fld1) ON DELETE SET NULL); 62INSERT INTO t1 VALUES(1, 1); 63INSERT INTO t1 VALUES(2, 2); 64INSERT INTO t2 VALUES(1, DEFAULT); 65INSERT INTO t2 VALUES(2, DEFAULT); 66DELETE FROM t1 WHERE fld1= 1; 67EXPLAIN SELECT fld2 FROM t2; 68id select_type table partitions type possible_keys key key_len ref rows filtered Extra 69x x t2 x index x fld2 x x x x x 70SELECT fld2 FROM t2; 71fld2 72NULL 732 74SELECT * FROM t2; 75fld1 fld2 76NULL NULL 772 2 78DROP TABLE t2, t1; 79# VIRTUAL INDEX CONTAINS FK CONSTRAINT COLUMN 80CREATE TABLE t1(fld1 INT NOT NULL PRIMARY KEY); 81CREATE TABLE t2(fld1 INT NOT NULL, fld2 INT, fld3 INT AS (fld2) VIRTUAL, 82KEY(fld3, fld1), 83FOREIGN KEY(fld1) REFERENCES t1(fld1) ON UPDATE CASCADE); 84INSERT INTO t1(fld1) VALUES(1); 85INSERT INTO t2(fld1, fld2) VALUES(1, 3); 86UPDATE t1 SET fld1= 2; 87SELECT fld3, fld1 FROM t2; 88fld3 fld1 893 2 90EXPLAIN SELECT fld3, fld1 FROM t2; 91id select_type table partitions type possible_keys key key_len ref rows filtered Extra 92x x t2 x index x fld3 x x x x x 93SELECT * FROM t2; 94fld1 fld2 fld3 952 3 3 96DROP TABLE t2, t1; 97# Multiple level of VIRTUAL columns. 98CREATE TABLE t1(fld1 INT NOT NULL PRIMARY KEY); 99CREATE TABLE t2(fld1 INT NOT NULL, fld2 INT AS (fld1) VIRTUAL, 100fld3 INT AS (fld2) VIRTUAL, KEY(fld3), KEY(fld2), 101FOREIGN KEY(fld1) REFERENCES t1(fld1) ON UPDATE CASCADE); 102INSERT INTO t1(fld1) VALUES(1); 103INSERT INTO t2(fld1) VALUES(1); 104UPDATE t1 SET fld1= 2; 105SELECT fld2 FROM t2; 106fld2 1072 108EXPLAIN SELECT fld2 FROM t2; 109id select_type table partitions type possible_keys key key_len ref rows filtered Extra 110x x t2 x index x fld2 x x x x x 111SELECT fld3 FROM t2; 112fld3 1132 114EXPLAIN SELECT fld3 FROM t2; 115id select_type table partitions type possible_keys key key_len ref rows filtered Extra 116x x t2 x index x fld3 x x x x x 117SELECT * FROM t2; 118fld1 fld2 fld3 1192 2 2 120DROP TABLE t2, t1; 121# Drop the VIRTUAL INDEX using alter copy ALGORITHM 122CREATE TABLE t1(fld1 INT NOT NULL PRIMARY KEY); 123CREATE TABLE t2(fld1 INT NOT NULL, fld2 INT AS (fld1) VIRTUAL, KEY vk(fld2), 124KEY(fld1), FOREIGN KEY(fld1) REFERENCES t1(fld1) 125ON UPDATE CASCADE); 126INSERT INTO t1(fld1) VALUES(1); 127INSERT INTO t2(fld1) VALUES(1); 128UPDATE t1 SET fld1= 2; 129SELECT fld2, fld1 FROM t2; 130fld2 fld1 1312 2 132EXPLAIN SELECT fld2, fld1 FROM t2; 133id select_type table partitions type possible_keys key key_len ref rows filtered Extra 134x x t2 x ALL x NULL x x x x x 135ALTER TABLE t2 DROP INDEX vk, ALGORITHM= COPY; 136UPDATE t1 SET fld1= 3; 137SELECT fld2, fld1 FROM t2; 138fld2 fld1 1393 3 140EXPLAIN SELECT fld2, fld1 FROM t2; 141id select_type table partitions type possible_keys key key_len ref rows filtered Extra 142x x t2 x ALL x NULL x x x x x 143DROP TABLE t2, t1; 144# Drop the VIRTUAL INDEX using INPLACE alter ALGORITHM 145CREATE TABLE t1(fld1 INT NOT NULL PRIMARY KEY); 146CREATE TABLE t2(fld1 INT NOT NULL, fld2 INT AS (fld1) VIRTUAL, 147KEY vk(fld2), KEY(fld1), FOREIGN KEY(fld1) REFERENCES t1(fld1) 148ON UPDATE CASCADE); 149INSERT INTO t1(fld1) VALUES(1); 150INSERT INTO t2(fld1) VALUES(1); 151UPDATE t1 SET fld1= 2; 152SELECT fld2, fld1 FROM t2; 153fld2 fld1 1542 2 155EXPLAIN SELECT fld2, fld1 FROM t2; 156id select_type table partitions type possible_keys key key_len ref rows filtered Extra 157x x t2 x ALL x NULL x x x x x 158ALTER TABLE t2 DROP INDEX vk, ALGORITHM= COPY; 159UPDATE t1 SET fld1= 3; 160SELECT fld2, fld1 FROM t2; 161fld2 fld1 1623 3 163EXPLAIN SELECT fld2, fld1 FROM t2; 164id select_type table partitions type possible_keys key key_len ref rows filtered Extra 165x x t2 x ALL x NULL x x x x x 166DROP TABLE t2, t1; 167# Add the VIRTUAL INDEX using COPY alter ALGORITHM 168CREATE TABLE t1(fld1 INT NOT NULL PRIMARY KEY); 169CREATE TABLE t2(fld1 INT NOT NULL, fld2 INT AS (fld1) VIRTUAL, 170KEY(fld1), FOREIGN KEY(fld1) REFERENCES t1(fld1) 171ON UPDATE CASCADE); 172INSERT INTO t1(fld1) VALUES(1); 173INSERT INTO t2(fld1) VALUES(1); 174UPDATE t1 SET fld1= 2; 175SELECT fld2, fld1 FROM t2; 176fld2 fld1 1772 2 178ALTER TABLE t2 ADD INDEX vk(fld2), ALGORITHM= COPY; 179UPDATE t1 SET fld1= 3; 180SELECT fld2, fld1 FROM t2; 181fld2 fld1 1823 3 183EXPLAIN SELECT fld2, fld1 FROM t2; 184id select_type table partitions type possible_keys key key_len ref rows filtered Extra 185x x t2 x ALL x NULL x x x x x 186DROP TABLE t2, t1; 187# Add the VIRTUAL INDEX using INPLACE alter ALGORITHM 188CREATE TABLE t1(fld1 INT NOT NULL PRIMARY KEY); 189CREATE TABLE t2(fld1 INT NOT NULL,fld2 INT AS (fld1) VIRTUAL, 190KEY(fld1), FOREIGN KEY(fld1) REFERENCES t1(fld1) 191ON UPDATE CASCADE); 192INSERT INTO t1(fld1) VALUES(1); 193INSERT INTO t2(fld1) VALUES(1); 194UPDATE t1 SET fld1= 2; 195SELECT fld2, fld1 FROM t2; 196fld2 fld1 1972 2 198ALTER TABLE t2 ADD INDEX vk(fld2), ALGORITHM= INPLACE; 199UPDATE t1 SET fld1= 3; 200SELECT fld2, fld1 FROM t2; 201fld2 fld1 2023 3 203EXPLAIN SELECT fld2, fld1 FROM t2; 204id select_type table partitions type possible_keys key key_len ref rows filtered Extra 205x x t2 x ALL x NULL x x x x x 206DROP TABLE t2, t1; 207# Drop the VIRTUAL INDEX contains fk constraint column 208# using alter copy ALGORITHM 209CREATE TABLE t1(fld1 INT NOT NULL PRIMARY KEY); 210CREATE TABLE t2(fld1 INT NOT NULL, fld2 INT NOT NULL, 211fld3 INT AS (fld2) VIRTUAL, KEY vk(fld3, fld1), 212KEY(fld1), FOREIGN KEY(fld1) REFERENCES t1(fld1) 213ON UPDATE CASCADE); 214INSERT INTO t1(fld1) VALUES(1); 215INSERT INTO t2(fld1, fld2) VALUES(1, 2); 216UPDATE t1 SET fld1= 2; 217EXPLAIN SELECT fld3, fld1 FROM t2; 218id select_type table partitions type possible_keys key key_len ref rows filtered Extra 219x x t2 x index x vk x x x x x 220SELECT fld3, fld1 FROM t2; 221fld3 fld1 2222 2 223ALTER TABLE t2 DROP INDEX vk, ALGORITHM= COPY; 224UPDATE t1 SET fld1= 3; 225SELECT fld3, fld1 FROM t2; 226fld3 fld1 2272 3 228EXPLAIN SELECT fld3, fld1 FROM t2; 229id select_type table partitions type possible_keys key key_len ref rows filtered Extra 230x x t2 x ALL x NULL x x x x x 231DROP TABLE t2, t1; 232# Drop the VIRTUAL INDEX which contains fk constraint column 233# using INPLACE alter operation 234CREATE TABLE t1(fld1 INT NOT NULL PRIMARY KEY); 235CREATE TABLE t2(fld1 INT NOT NULL, fld2 INT NOT NULL, 236fld3 INT AS (fld2) VIRTUAL, KEY vk(fld3, fld1), 237KEY(fld1), FOREIGN KEY(fld1) REFERENCES t1(fld1) 238ON UPDATE CASCADE); 239INSERT INTO t1(fld1) VALUES(1); 240INSERT INTO t2(fld1, fld2) VALUES(1, 2); 241UPDATE t1 SET fld1= 2; 242EXPLAIN SELECT fld3, fld1 FROM t2; 243id select_type table partitions type possible_keys key key_len ref rows filtered Extra 244x x t2 x index x vk x x x x x 245SELECT fld3, fld1 FROM t2; 246fld3 fld1 2472 2 248alter TABLE t2 DROP INDEX vk, ALGORITHM= INPLACE; 249UPDATE t1 SET fld1= 3; 250SELECT fld3, fld1 FROM t2; 251fld3 fld1 2522 3 253EXPLAIN SELECT fld3, fld1 FROM t2; 254id select_type table partitions type possible_keys key key_len ref rows filtered Extra 255x x t2 x ALL x NULL x x x x x 256DROP TABLE t2, t1; 257# Add the VIRTUAL INDEX contains fk constraint column 258# using copy alter operatiON 259CREATE TABLE t1(fld1 INT NOT NULL PRIMARY KEY); 260CREATE TABLE t2(fld1 INT NOT NULL, fld2 INT NOT NULL, 261fld3 INT AS (fld2) VIRTUAL, KEY(fld1), 262FOREIGN KEY(fld1) REFERENCES t1(fld1) ON UPDATE CASCADE); 263INSERT INTO t1(fld1) VALUES(1); 264INSERT INTO t2(fld1, fld2) VALUES(1, 2); 265UPDATE t1 SET fld1= 2; 266SELECT fld3, fld1 FROM t2; 267fld3 fld1 2682 2 269alter TABLE t2 ADD INDEX vk(fld3, fld1), ALGORITHM= COPY; 270UPDATE t1 SET fld1= 3; 271SELECT fld3, fld1 FROM t2; 272fld3 fld1 2732 3 274EXPLAIN SELECT fld3, fld1 FROM t2; 275id select_type table partitions type possible_keys key key_len ref rows filtered Extra 276x x t2 x index x vk x x x x x 277DROP TABLE t2, t1; 278# Cascading UPDATEs and DELETEs for the multiple 279# fk dependent TABLEs 280CREATE TABLE t1(fld1 INT NOT NULL PRIMARY KEY); 281CREATE TABLE t2(fld1 INT NOT NULL, fld2 INT AS (fld1) VIRTUAL, 282KEY(fld1), KEY(fld2, fld1), 283FOREIGN KEY(fld1) REFERENCES t1(fld1) ON UPDATE CASCADE); 284CREATE TABLE t3(fld1 INT NOT NULL, fld2 INT AS (fld1) VIRTUAL, 285KEY(fld2, fld1), 286FOREIGN KEY(fld1) REFERENCES t2(fld1) ON UPDATE CASCADE); 287INSERT INTO t1 VALUES(1), (2); 288INSERT INTO t2(fld1) VALUES(1), (2); 289INSERT INTO t3(fld1) VALUES(1), (2); 290UPDATE t1 SET fld1= 4 WHERE fld1= 1; 291EXPLAIN SELECT fld2, fld1 FROM t2; 292id select_type table partitions type possible_keys key key_len ref rows filtered Extra 293x x t2 x index x fld2 x x x x x 294SELECT fld2, fld1 FROM t2; 295fld2 fld1 2962 2 2974 4 298EXPLAIN SELECT fld2, fld1 FROM t3; 299id select_type table partitions type possible_keys key key_len ref rows filtered Extra 300x x t3 x index x fld2 x x x x x 301SELECT fld2, fld1 FROM t3; 302fld2 fld1 3032 2 3044 4 305DROP TABLE t3, t2, t1; 306CREATE TABLE t1(fld1 INT NOT NULL PRIMARY KEY); 307CREATE TABLE t2(fld1 INT NOT NULL, fld2 INT NOT NULL, 308fld3 INT AS (fld2) VIRTUAL, KEY(fld3, fld1), KEY(fld1), 309FOREIGN KEY(fld1) REFERENCES t1(fld1) ON UPDATE CASCADE); 310CREATE TABLE t3(fld1 INT NOT NULL, fld2 INT NOT NULL, 311fld3 INT AS (fld2) VIRTUAL, KEY(fld3, fld1), 312FOREIGN KEY(fld1) REFERENCES t2(fld1) ON UPDATE CASCADE); 313INSERT INTO t1 VALUES(1), (2); 314INSERT INTO t2 VALUES(1, 1, DEFAULT), (2, 2, default); 315INSERT INTO t3 VALUES(1, 1, DEFAULT), (2, 2, default); 316UPDATE t1 SET fld1= 4 WHERE fld1= 1; 317EXPLAIN SELECT fld3, fld1 FROM t2; 318id select_type table partitions type possible_keys key key_len ref rows filtered Extra 319x x t2 x index x fld3 x x x x x 320SELECT fld3, fld1 FROM t2; 321fld3 fld1 3221 4 3232 2 324EXPLAIN SELECT fld3, fld1 FROM t3; 325id select_type table partitions type possible_keys key key_len ref rows filtered Extra 326x x t3 x index x fld3 x x x x x 327SELECT fld3, fld1 FROM t3; 328fld3 fld1 3291 4 3302 2 331DROP TABLE t3, t2, t1; 332CREATE TABLE t1(fld1 INT NOT NULL PRIMARY KEY); 333CREATE TABLE t2(fld1 INT NOT NULL, fld2 INT AS (fld1) VIRTUAL, 334KEY(fld1), KEY(fld2, fld1), 335FOREIGN KEY(fld1) REFERENCES t1(fld1) ON DELETE CASCADE); 336CREATE TABLE t3(fld1 INT NOT NULL, fld2 INT AS (fld1) VIRTUAL, 337KEY(fld2, fld1), FOREIGN KEY(fld1) REFERENCES t2(fld1) 338ON DELETE CASCADE); 339INSERT INTO t1 VALUES(1), (2); 340INSERT INTO t2(fld1) VALUES(1), (2); 341INSERT INTO t3(fld1) VALUES(1), (2); 342DELETE FROM t1 WHERE fld1= 1; 343EXPLAIN SELECT fld2, fld1 FROM t2; 344id select_type table partitions type possible_keys key key_len ref rows filtered Extra 345x x t2 x index x fld2 x x x x x 346SELECT fld2, fld1 FROM t2; 347fld2 fld1 3482 2 349EXPLAIN SELECT fld2, fld1 FROM t3; 350id select_type table partitions type possible_keys key key_len ref rows filtered Extra 351x x t3 x index x fld2 x x x x x 352SELECT fld2, fld1 FROM t3; 353fld2 fld1 3542 2 355DROP TABLE t3, t2, t1; 356CREATE TABLE t1(fld1 INT NOT NULL PRIMARY KEY); 357CREATE TABLE t2(fld1 INT NOT NULL, fld2 INT NOT NULL, 358fld3 INT AS (fld2) VIRTUAL, 359KEY(fld3, fld1), KEY(fld1), 360FOREIGN KEY(fld1) REFERENCES t1(fld1) 361ON DELETE CASCADE); 362CREATE TABLE t3(fld1 INT NOT NULL, fld2 INT NOT NULL, 363fld3 INT AS (fld2) VIRTUAL, KEY(fld3, fld1), 364FOREIGN KEY(fld1) REFERENCES t2(fld1) 365ON DELETE CASCADE); 366INSERT INTO t1 VALUES(1), (2); 367INSERT INTO t2 VALUES(1, 1, DEFAULT), (2, 2, default); 368INSERT INTO t3 VALUES(1, 1, DEFAULT), (2, 2, default); 369DELETE FROM t1 WHERE fld1= 1; 370EXPLAIN SELECT fld3, fld1 FROM t2; 371id select_type table partitions type possible_keys key key_len ref rows filtered Extra 372x x t2 x index x fld3 x x x x x 373SELECT fld3, fld1 FROM t2; 374fld3 fld1 3752 2 376EXPLAIN SELECT fld3, fld1 FROM t3; 377id select_type table partitions type possible_keys key key_len ref rows filtered Extra 378x x t3 x index x fld3 x x x x x 379SELECT fld3, fld1 FROM t3; 380fld3 fld1 3812 2 382DROP TABLE t3, t2, t1; 383# RENAME TABLE 384CREATE TABLE t1(fld1 INT NOT NULL PRIMARY KEY); 385CREATE TABLE t2(fld1 INT NOT NULL, 386fld2 INT AS (fld1) VIRTUAL, 387KEY(fld2, fld1), 388FOREIGN KEY(fld1) REFERENCES t1(fld1) 389ON DELETE CASCADE); 390INSERT INTO t1 VALUES(1), (2); 391INSERT INTO t2 VALUES(1, DEFAULT), (2, default); 392RENAME TABLE t2 to t3; 393DELETE FROM t1 WHERE fld1= 1; 394EXPLAIN SELECT fld2, fld1 FROM t3; 395id select_type table partitions type possible_keys key key_len ref rows filtered Extra 396x x t3 x index x fld2 x x x x x 397SELECT fld2, fld1 FROM t3; 398fld2 fld1 3992 2 400DROP TABLE t3, t1; 401# FOREIGN_KEY_CHECKS disabled DURING INPLACE ALTER 402CREATE TABLE t1(fld1 INT NOT NULL PRIMARY KEY); 403CREATE TABLE t2(fld1 INT NOT NULL, 404fld2 INT AS (fld1) VIRTUAL, 405FOREIGN KEY(fld1) REFERENCES t1(fld1) 406ON UPDATE CASCADE); 407INSERT INTO t1 VALUES(1), (2); 408INSERT INTO t2 VALUES(1, DEFAULT), (2, default); 409SET foreign_key_checks = 0; 410ALTER TABLE t2 ADD INDEX vk(fld2), ALGORITHM=INPLACE; 411SET foreign_key_checks = 1; 412UPDATE t1 SET fld1= 3 WHERE fld1= 2; 413SELECT fld2 FROM t2; 414fld2 4151 4163 417EXPLAIN SELECT fld2 FROM t2; 418id select_type table partitions type possible_keys key key_len ref rows filtered Extra 419x x t2 x index x vk x x x x x 420DROP TABLE t2, t1; 421# GENERATED COLUMN COMPUTATION FAILS when SQL_MODE 422# is set to ERROR_FOR_DIVISION_BY_ZERO 423CREATE TABLE t1(fld1 INT NOT NULL PRIMARY KEY); 424CREATE TABLE t2(fld1 INT NOT NULL, 425fld2 INT AS (100/fld1) VIRTUAL, 426KEY(fld2), 427FOREIGN KEY(fld1) REFERENCES t1(fld1) 428ON UPDATE CASCADE); 429INSERT INTO t1 VALUES(1), (2); 430INSERT INTO t2 VALUES(1, DEFAULT), (2, default); 431UPDATE t1 SET fld1= 0 WHERE fld1= 2; 432ERROR 22012: Division by 0 433EXPLAIN SELECT fld2 FROM t2; 434id select_type table partitions type possible_keys key key_len ref rows filtered Extra 435x x t2 x index x fld2 x x x x x 436SELECT fld2 FROM t2; 437fld2 43850 439100 440DROP TABLE t2, t1; 441# CHANGE SQL_MODE and try the ERROR_FOR_DIVISION_BY_ZERO 442SET sql_mode = STRICT_ALL_TABLES; 443Warnings: 444Warning 3135 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release. 445Warning 3090 Changing sql mode 'NO_AUTO_CREATE_USER' is deprecated. It will be removed in a future release. 446CREATE TABLE t1(fld1 INT NOT NULL PRIMARY KEY); 447CREATE TABLE t2(fld1 INT NOT NULL, 448fld2 INT AS (100/fld1) VIRTUAL, 449KEY(fld2), 450FOREIGN KEY(fld1) REFERENCES t1(fld1) 451ON UPDATE CASCADE); 452INSERT INTO t1 VALUES(1), (2); 453INSERT INTO t2 VALUES(1, DEFAULT), (2, default); 454UPDATE t1 SET fld1= 0 WHERE fld1= 2; 455EXPLAIN SELECT fld2 FROM t2; 456id select_type table partitions type possible_keys key key_len ref rows filtered Extra 457x x t2 x index x fld2 x x x x x 458SELECT fld2 FROM t2; 459fld2 460NULL 461100 462SELECT * FROM t2; 463fld1 fld2 4641 100 4650 NULL 466DROP TABLE t2, t1; 467SET sql_mode = default; 468# ADD FOREIGN CONSTRAINT USING COPY 469CREATE TABLE t1(fld1 INT NOT NULL PRIMARY KEY); 470CREATE TABLE t2(fld1 INT NOT NULL, 471fld2 INT AS (fld1) VIRTUAL, KEY(fld2)); 472ALTER TABLE t2 ADD FOREIGN KEY (fld1) 473REFERENCES t1(fld1) ON UPDATE CASCADE, 474ALGORITHM=copy; 475INSERT INTO t1 VALUES(1); 476INSERT INTO t2 VALUES(1, DEFAULT); 477UPDATE t1 SET fld1= 2; 478EXPLAIN SELECT fld2 FROM t2; 479id select_type table partitions type possible_keys key key_len ref rows filtered Extra 480x x t2 x index x fld2 x x x x x 481SELECT fld2 FROM t2; 482fld2 4832 484SELECT * FROM t2; 485fld1 fld2 4862 2 487DROP TABLE t2, t1; 488# ADD FOREIGN CONSTRAINT USING INPLACE 489CREATE TABLE t1(fld1 INT NOT NULL PRIMARY KEY); 490CREATE TABLE t2(fld1 INT NOT NULL, 491fld2 INT AS (fld1) VIRTUAL, KEY(fld2)); 492SET foreign_key_checks = 0; 493ALTER TABLE t2 ADD FOREIGN KEY (fld1) 494REFERENCES t1(fld1) ON UPDATE CASCADE, 495ALGORITHM=inplace; 496SET foreign_key_checks = 1; 497INSERT INTO t1 VALUES(1); 498INSERT INTO t2 VALUES(1, DEFAULT); 499UPDATE t1 SET fld1= 2; 500EXPLAIN SELECT fld2 FROM t2; 501id select_type table partitions type possible_keys key key_len ref rows filtered Extra 502x x t2 x index x fld2 x x x x x 503SELECT fld2 FROM t2; 504fld2 5052 506SELECT * FROM t2; 507fld1 fld2 5082 2 509DROP TABLE t2, t1; 510# DROP FOREIGN CONSTRAINT USING COPY 511CREATE TABLE t1(fld1 INT NOT NULL PRIMARY KEY); 512CREATE TABLE t2(fld1 INT NOT NULL, 513fld2 INT AS (fld1) VIRTUAL, KEY(fld2), 514CONSTRAINT fidx FOREIGN KEY (fld1) REFERENCES t1(fld1) 515ON UPDATE CASCADE); 516INSERT INTO t1 VALUES(1); 517INSERT INTO t2 VALUES(1, DEFAULT); 518ALTER TABLE t2 DROP FOREIGN KEY fidx, ALGORITHM=COPY; 519UPDATE t1 SET fld1= 2; 520EXPLAIN SELECT fld2 FROM t2; 521id select_type table partitions type possible_keys key key_len ref rows filtered Extra 522x x t2 x index x fld2 x x x x x 523SELECT fld2 FROM t2; 524fld2 5251 526SELECT * FROM t2; 527fld1 fld2 5281 1 529DROP TABLE t2, t1; 530# DROP FOREIGN CONSTRAINT USING INPLACE 531CREATE TABLE t1(fld1 INT NOT NULL PRIMARY KEY); 532CREATE TABLE t2(fld1 INT NOT NULL, 533fld2 INT AS (fld1) VIRTUAL, KEY(fld2), 534CONSTRAINT fidx FOREIGN KEY (fld1) REFERENCES t1(fld1) 535ON UPDATE CASCADE); 536SET foreign_key_checks = 0; 537ALTER TABLE t2 DROP FOREIGN KEY fidx, ALGORITHM=INPLACE; 538SET foreign_key_checks = 1; 539INSERT INTO t1 VALUES(1); 540INSERT INTO t2 VALUES(1, DEFAULT); 541UPDATE t1 SET fld1= 2; 542EXPLAIN SELECT fld2 FROM t2; 543id select_type table partitions type possible_keys key key_len ref rows filtered Extra 544x x t2 x index x fld2 x x x x x 545SELECT fld2 FROM t2; 546fld2 5471 548SELECT * FROM t2; 549fld1 fld2 5501 1 551DROP TABLE t2, t1; 552# ADD VC INDEX and ADD FK IN SAME COPY ALTER 553CREATE TABLE t1(fld1 INT NOT NULL PRIMARY KEY); 554CREATE TABLE t2(fld1 INT NOT NULL, 555fld2 INT AS (fld1) VIRTUAL); 556INSERT INTO t1 VALUES(1); 557INSERT INTO t2 VALUES(1, DEFAULT); 558ALTER TABLE t2 ADD INDEX(fld2), ADD FOREIGN KEY (fld1) REFERENCES t1(fld1) 559ON UPDATE CASCADE, ALGORITHM=copy; 560UPDATE t1 SET fld1= 2; 561EXPLAIN SELECT fld2 FROM t2; 562id select_type table partitions type possible_keys key key_len ref rows filtered Extra 563x x t2 x index x fld2 x x x x x 564SELECT fld2 FROM t2; 565fld2 5662 567SELECT * FROM t2; 568fld1 fld2 5692 2 570DROP TABLE t2, t1; 571# ADD VC INDEX and ADD FK IN SAME INPLACE ALTER 572CREATE TABLE t1(fld1 INT NOT NULL PRIMARY KEY); 573CREATE TABLE t2(fld1 INT NOT NULL, 574fld2 INT AS (fld1) VIRTUAL); 575INSERT INTO t1 VALUES(1); 576INSERT INTO t2 VALUES(1, DEFAULT); 577SET foreign_key_checks = 0; 578ALTER TABLE t2 ADD INDEX(fld2), ADD FOREIGN KEY (fld1) REFERENCES t1(fld1) 579ON UPDATE CASCADE, ALGORITHM=inplace; 580SET foreign_key_checks = 1; 581UPDATE t1 SET fld1= 2; 582EXPLAIN SELECT fld2 FROM t2; 583id select_type table partitions type possible_keys key key_len ref rows filtered Extra 584x x t2 x index x fld2 x x x x x 585SELECT fld2 FROM t2; 586fld2 5872 588SELECT * FROM t2; 589fld1 fld2 5902 2 591DROP TABLE t2, t1; 592# ADD VC INDEX and DROP FK IN SAME COPY ALTER 593CREATE TABLE t1(fld1 INT NOT NULL PRIMARY KEY); 594CREATE TABLE t2(fld1 INT NOT NULL, 595fld2 INT AS (fld1) VIRTUAL, 596CONSTRAINT fidx FOREIGN KEY(fld1) REFERENCES t1(fld1) 597ON UPDATE CASCADE); 598INSERT INTO t1 VALUES(1); 599INSERT INTO t2 VALUES(1, DEFAULT); 600ALTER TABLE t2 ADD INDEX(fld2), DROP FOREIGN KEY fidx, ALGORITHM=copy; 601UPDATE t1 SET fld1= 2; 602EXPLAIN SELECT fld2 FROM t2; 603id select_type table partitions type possible_keys key key_len ref rows filtered Extra 604x x t2 x index x fld2 x x x x x 605SELECT fld2 FROM t2; 606fld2 6071 608SELECT * FROM t2; 609fld1 fld2 6101 1 611DROP TABLE t2, t1; 612# ADD VC INDEX and DROP FK IN SAME INPLACE ALTER 613CREATE TABLE t1(fld1 INT NOT NULL PRIMARY KEY); 614CREATE TABLE t2(fld1 INT NOT NULL, 615fld2 INT AS (fld1) VIRTUAL, 616CONSTRAINT fidx FOREIGN KEY(fld1) REFERENCES t1(fld1) 617ON UPDATE CASCADE); 618INSERT INTO t1 VALUES(1); 619INSERT INTO t2 VALUES(1, DEFAULT); 620SET foreign_key_checks = 0; 621ALTER TABLE t2 ADD INDEX(fld2), DROP FOREIGN KEY fidx, ALGORITHM=inplace; 622SET foreign_key_checks = 1; 623UPDATE t1 SET fld1= 2; 624EXPLAIN SELECT fld2 FROM t2; 625id select_type table partitions type possible_keys key key_len ref rows filtered Extra 626x x t2 x index x fld2 x x x x x 627SELECT fld2 FROM t2; 628fld2 6291 630SELECT * FROM t2; 631fld1 fld2 6321 1 633DROP TABLE t2, t1; 634# DROP VC INDEX and ADD FK IN SAME COPY ALTER 635CREATE TABLE t1(fld1 INT NOT NULL PRIMARY KEY); 636CREATE TABLE t2(fld1 INT NOT NULL, 637fld2 INT AS (fld1) VIRTUAL, 638KEY idx(fld2)); 639INSERT INTO t1 VALUES(1); 640INSERT INTO t2 VALUES(1, DEFAULT); 641ALTER TABLE t2 DROP INDEX idx, ADD FOREIGN KEY (fld1) REFERENCES t1(fld1) 642ON UPDATE CASCADE, ALGORITHM=COPY; 643UPDATE t1 SET fld1= 2; 644EXPLAIN SELECT fld2 FROM t2; 645id select_type table partitions type possible_keys key key_len ref rows filtered Extra 646x x t2 x ALL x NULL x x x x x 647SELECT fld2 FROM t2; 648fld2 6492 650SELECT * FROM t2; 651fld1 fld2 6522 2 653DROP TABLE t2, t1; 654# DROP VC INDEX and ADD FK IN SAME INPLACE ALTER 655CREATE TABLE t1(fld1 INT NOT NULL PRIMARY KEY); 656CREATE TABLE t2(fld1 INT NOT NULL, 657fld2 INT AS (fld1) VIRTUAL, 658KEY idx(fld2)); 659INSERT INTO t1 VALUES(1); 660INSERT INTO t2 VALUES(1, DEFAULT); 661SET foreign_key_checks = 0; 662ALTER TABLE t2 DROP INDEX idx, ADD FOREIGN KEY (fld1) REFERENCES t1(fld1) 663ON UPDATE CASCADE, ALGORITHM=INPLACE; 664SET foreign_key_checks = 1; 665UPDATE t1 SET fld1= 2; 666EXPLAIN SELECT fld2 FROM t2; 667id select_type table partitions type possible_keys key key_len ref rows filtered Extra 668x x t2 x ALL x NULL x x x x x 669SELECT fld2 FROM t2; 670fld2 6712 672SELECT * FROM t2; 673fld1 fld2 6742 2 675DROP TABLE t2, t1; 676# DROP VC INDEX and DROP FK IN SAME COPY ALTER 677CREATE TABLE t1(fld1 INT NOT NULL PRIMARY KEY); 678CREATE TABLE t2(fld1 INT NOT NULL, 679fld2 INT AS (fld1) VIRTUAL, 680KEY idx(fld2), 681CONSTRAINT fidx FOREIGN KEY(fld1) REFERENCES t1(fld1) 682ON UPDATE CASCADE); 683INSERT INTO t1 VALUES(1); 684INSERT INTO t2 VALUES(1, DEFAULT); 685ALTER TABLE t2 DROP KEY idx, DROP FOREIGN KEY fidx, ALGORITHM=COPY; 686UPDATE t1 SET fld1= 2; 687EXPLAIN SELECT fld2 FROM t2; 688id select_type table partitions type possible_keys key key_len ref rows filtered Extra 689x x t2 x ALL x NULL x x x x x 690SELECT fld2 FROM t2; 691fld2 6921 693SELECT * FROM t2; 694fld1 fld2 6951 1 696DROP TABLE t2, t1; 697# DROP VC INDEX and DROP FK IN SAME INPLACE ALTER 698CREATE TABLE t1(fld1 INT NOT NULL PRIMARY KEY); 699CREATE TABLE t2(fld1 INT NOT NULL, 700fld2 INT AS (fld1) VIRTUAL, 701KEY idx(fld2), 702CONSTRAINT fidx FOREIGN KEY(fld1) REFERENCES t1(fld1) 703ON UPDATE CASCADE); 704INSERT INTO t1 VALUES(1); 705INSERT INTO t2 VALUES(1, DEFAULT); 706SET foreign_key_checks = 0; 707ALTER TABLE t2 DROP KEY idx, DROP FOREIGN KEY fidx, ALGORITHM=INPLACE; 708SET foreign_key_checks = 1; 709UPDATE t1 SET fld1= 2; 710EXPLAIN SELECT fld2 FROM t2; 711id select_type table partitions type possible_keys key key_len ref rows filtered Extra 712x x t2 x ALL x NULL x x x x x 713SELECT fld2 FROM t2; 714fld2 7151 716SELECT * FROM t2; 717fld1 fld2 7181 1 719DROP TABLE t2, t1; 720CREATE TABLE t1 (f1 INT NOT NULL PRIMARY KEY)ENGINE=INNODB; 721CREATE TABLE t2 (f1 INT NOT NULL, f2 INT AS (f1) VIRTUAL, 722KEY (f1, f2), FOREIGN KEY(f1) REFERENCES t1(f1))ENGINE=INNODB; 723INSERT INTO t1 VALUES(1); 724INSERT INTO t2(f1) VALUES(1); 725EXPLAIN SELECT f1, f2 FROM t2; 726id select_type table partitions type possible_keys key key_len ref rows filtered Extra 7271 SIMPLE t2 NULL index NULL f1 9 NULL 1 100.00 Using index 728Warnings: 729Note 1003 /* select#1 */ select `test`.`t2`.`f1` AS `f1`,`test`.`t2`.`f2` AS `f2` from `test`.`t2` 730SELECT f1, f2 FROM t2; 731f1 f2 7321 1 733INSERT INTO t2(f1) VALUES(2); 734ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`test`.`t2`, CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`f1`) REFERENCES `t1` (`f1`)) 735DROP TABLE t2, t1; 736CREATE TABLE t1 (f1 INT NOT NULL PRIMARY KEY)ENGINE=INNODB; 737CREATE TABLE t2 (f1 INT NOT NULL, f2 INT AS (f1) VIRTUAL, 738KEY (f1, f2), FOREIGN KEY(f1) REFERENCES t1(f1) 739ON UPDATE CASCADE)ENGINE=INNODB; 740INSERT INTO t1 VALUES(1); 741INSERT INTO t2(f1) VALUES(1); 742EXPLAIN SELECT f1, f2 FROM t2; 743id select_type table partitions type possible_keys key key_len ref rows filtered Extra 7441 SIMPLE t2 NULL index NULL f1 9 NULL 1 100.00 Using index 745Warnings: 746Note 1003 /* select#1 */ select `test`.`t2`.`f1` AS `f1`,`test`.`t2`.`f2` AS `f2` from `test`.`t2` 747SELECT f1, f2 FROM t2; 748f1 f2 7491 1 750UPDATE t1 SET f1 = 2 WHERE f1 = 1; 751EXPLAIN SELECT f1, f2 FROM t2; 752id select_type table partitions type possible_keys key key_len ref rows filtered Extra 7531 SIMPLE t2 NULL index NULL f1 9 NULL 1 100.00 Using index 754Warnings: 755Note 1003 /* select#1 */ select `test`.`t2`.`f1` AS `f1`,`test`.`t2`.`f2` AS `f2` from `test`.`t2` 756SELECT f1, f2 FROM t2; 757f1 f2 7582 2 759DROP TABLE t2, t1; 760CREATE TABLE t1 (f1 INT NOT NULL PRIMARY KEY)ENGINE=INNODB; 761CREATE TABLE t2 (f1 INT NOT NULL, f2 INT AS (f1) VIRTUAL, 762KEY (f1, f2))ENGINE=INNODB; 763INSERT INTO t1 VALUES(1); 764INSERT INTO t2(f1) VALUES(1); 765SET FOREIGN_KEY_CHECKS = 0; 766ALTER TABLE t2 ADD FOREIGN KEY (f1) REFERENCES t1(f1) 767ON UPDATE CASCADE, ALGORITHM=INPLACE; 768SET FOREIGN_KEY_CHECKS = 1; 769UPDATE t1 SET f1 = 3; 770EXPLAIN SELECT f1, f2 FROM t2; 771id select_type table partitions type possible_keys key key_len ref rows filtered Extra 7721 SIMPLE t2 NULL index NULL f1 9 NULL 1 100.00 Using index 773Warnings: 774Note 1003 /* select#1 */ select `test`.`t2`.`f1` AS `f1`,`test`.`t2`.`f2` AS `f2` from `test`.`t2` 775SELECT f1, f2 FROM t2; 776f1 f2 7773 3 778DROP TABLE t2, t1; 779CREATE TABLE t1 (f1 INT NOT NULL PRIMARY KEY)ENGINE=INNODB; 780CREATE TABLE t2 (f1 INT NOT NULL, f2 INT AS (f1) VIRTUAL, 781KEY (f1, f2))ENGINE=INNODB; 782INSERT INTO t1 VALUES(1); 783INSERT INTO t2(f1) VALUES(1); 784ALTER TABLE t2 ADD FOREIGN KEY (f1) REFERENCES t1(f1) 785ON UPDATE CASCADE, ALGORITHM=COPY; 786UPDATE t1 SET f1 = 3; 787EXPLAIN SELECT f1, f2 FROM t2; 788id select_type table partitions type possible_keys key key_len ref rows filtered Extra 7891 SIMPLE t2 NULL index NULL f1 9 NULL 1 100.00 Using index 790Warnings: 791Note 1003 /* select#1 */ select `test`.`t2`.`f1` AS `f1`,`test`.`t2`.`f2` AS `f2` from `test`.`t2` 792SELECT f1, f2 FROM t2; 793f1 f2 7943 3 795DROP TABLE t2, t1; 796CREATE TABLE t1(f1 INT NOT NULL, PRIMARY KEY(f1))ENGINE=INNODB; 797CREATE TABLE t2(f1 INT NOT NULL, f2 INT AS (1) VIRTUAL, 798f3 INT AS (2) VIRTUAL, 799FOREIGN KEY idx (f1) REFERENCES t1(f1) ON UPDATE CASCADE, 800KEY idx1 (f2, f1, f3))ENGINE=INNODB; 801INSERT INTO t1 VALUES(1); 802INSERT INTO t2(f1) VALUES(1); 803ALTER TABLE t2 DROP COLUMN f2, ALGORITHM=INPLACE; 804UPDATE t1 SET f1 = 3; 805EXPLAIN SELECT f1, f3 FROM t2; 806id select_type table partitions type possible_keys key key_len ref rows filtered Extra 8071 SIMPLE t2 NULL index NULL idx1 9 NULL 1 100.00 Using index 808Warnings: 809Note 1003 /* select#1 */ select `test`.`t2`.`f1` AS `f1`,`test`.`t2`.`f3` AS `f3` from `test`.`t2` 810SELECT f1, f3 FROM t2; 811f1 f3 8123 2 813DROP TABLE t2, t1; 814CREATE TABLE t1(f1 INT NOT NULL, PRIMARY KEY(f1))ENGINE=INNODB; 815CREATE TABLE t2(f1 INT NOT NULL, f2 INT AS (1) VIRTUAL, 816f3 INT AS (2) VIRTUAL, 817FOREIGN KEY idx (f1) REFERENCES t1(f1) ON UPDATE CASCADE, 818KEY idx1 (f2, f1, f3))ENGINE=INNODB; 819INSERT INTO t1 VALUES(1); 820INSERT INTO t2(f1) VALUES(1); 821ALTER TABLE t2 DROP COLUMN f2, ALGORITHM=COPY; 822UPDATE t1 SET f1 = 3; 823EXPLAIN SELECT f1, f3 FROM t2; 824id select_type table partitions type possible_keys key key_len ref rows filtered Extra 8251 SIMPLE t2 NULL index NULL idx1 9 NULL 1 100.00 Using index 826Warnings: 827Note 1003 /* select#1 */ select `test`.`t2`.`f1` AS `f1`,`test`.`t2`.`f3` AS `f3` from `test`.`t2` 828SELECT f1, f3 FROM t2; 829f1 f3 8303 2 831DROP TABLE t2, t1; 832# 833# Bug#32124113 MYSQL CRASH FOR GENERATED COLUMN DELETE WITH FOREIGN KEY 834# 835CREATE TABLE `emails` ( 836`id` int(10) unsigned NOT NULL AUTO_INCREMENT, 837PRIMARY KEY (`id`) 838) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci 839ROW_FORMAT=DYNAMIC; 840CREATE TABLE `email_stats` ( 841`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, 842`email_id` int(10) unsigned DEFAULT NULL, 843`date_sent` datetime NOT NULL, 844`generated_sent_date` date GENERATED ALWAYS AS 845(concat(year(`date_sent`),'-',lpad(month(`date_sent`),2,'0'), 846'-',lpad(dayofmonth(`date_sent`),2,'0'))) VIRTUAL, 847PRIMARY KEY (`id`), 848KEY `IDX_ES1` (`email_id`), 849KEY `mautic_generated_sent_date_email_id` 850(`generated_sent_date`,`email_id`), 851CONSTRAINT `FK_ES1` FOREIGN KEY (`email_id`) REFERENCES 852`emails` (`id`) ON DELETE SET NULL 853) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci 854ROW_FORMAT=DYNAMIC; 855CREATE TABLE `emails_metadata` ( 856`email_id` int(10) unsigned NOT NULL, 857PRIMARY KEY (`email_id`), 858CONSTRAINT `FK_EM1` FOREIGN KEY (`email_id`) REFERENCES 859`emails` (`id`) ON DELETE CASCADE 860) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci 861ROW_FORMAT=DYNAMIC; 862INSERT INTO `emails` VALUES (1); 863INSERT INTO `email_stats` (`id`, `email_id`, `date_sent`) VALUES 864(1,1,'2020-10-22 13:32:41'); 865INSERT INTO `emails_metadata` VALUES (1); 866DELETE FROM `emails`; 867DROP TABLE `email_stats`; 868DROP TABLE `emails_metadata`; 869DROP TABLE `emails`; 870