mirror of
https://github.com/MariaDB/server.git
synced 2025-01-16 03:52:35 +01:00
cc810e64d4
Don't allow the referencing key column from NULL TO NOT NULL when 1) Foreign key constraint type is ON UPDATE SET NULL 2) Foreign key constraint type is ON DELETE SET NULL 3) Foreign key constraint type is UPDATE CASCADE and referenced column declared as NULL Don't allow the referenced key column from NOT NULL to NULL when foreign key constraint type is UPDATE CASCADE and referencing key columns doesn't allow NULL values get_foreign_key_info(): InnoDB sends the information about nullability of the foreign key fields and referenced key fields. fk_check_column_changes(): Enforce the above rules for COPY algorithm innobase_check_foreign_drop_col(): Checks whether the dropped column exists in existing foreign key relation innobase_check_foreign_low() : Enforce the above rules for INPLACE algorithm dict_foreign_t::check_fk_constraint_valid(): This is used by CREATE TABLE statement to check nullability for foreign key relation.
225 lines
10 KiB
Text
225 lines
10 KiB
Text
--source include/have_innodb.inc
|
|
call mtr.add_suppression("InnoDB: In ALTER TABLE .* has or is referenced in foreign key constraints which are not compatible with the new table definition.");
|
|
|
|
let $MYSQLD_DATADIR= `select @@datadir`;
|
|
let $algorithm=`select regexp_replace('$MTR_COMBINATIONS', 'innodb,\|,innodb', '')`;
|
|
|
|
--echo # modify child column NOT NULL on UPDATE CASCADE..parent column NULL
|
|
CREATE TABLE t1(f1 INT, f2 INT, PRIMARY KEY(f1), KEY(f2))ENGINE=InnoDB;
|
|
CREATE TABLE t2(f1 INT, FOREIGN KEY(f1) REFERENCES t1(f2) ON UPDATE CASCADE)ENGINE=InnoDB;
|
|
replace_result ,ALGORITHM=COPY '' ,ALGORITHM=INPLACE '';
|
|
--error ER_FK_COLUMN_NOT_NULL
|
|
eval ALTER TABLE t2 MODIFY COLUMN f1 INT NOT NULL,ALGORITHM=$algorithm;
|
|
DROP TABLE t2, t1;
|
|
|
|
--echo # modify child column NOT NULL ON DELETE CASCADE..parent column NULL
|
|
CREATE TABLE t1(f1 INT, f2 INT, PRIMARY KEY(f1), KEY(f2))ENGINE=InnoDB;
|
|
CREATE TABLE t2(f1 INT, FOREIGN KEY(f1) REFERENCES t1(f2) ON DELETE CASCADE)ENGINE=InnoDB;
|
|
replace_result ,ALGORITHM=COPY '' ,ALGORITHM=INPLACE '';
|
|
eval ALTER TABLE t2 MODIFY COLUMN f1 INT NOT NULL,ALGORITHM=$algorithm;
|
|
DROP TABLE t2, t1;
|
|
|
|
--echo # modify child column NOT NULL ON UPDATE SET NULL
|
|
CREATE TABLE t1(f1 INT, f2 INT, PRIMARY KEY(f1), KEY(f2))ENGINE=InnoDB;
|
|
CREATE TABLE t2(f1 INT, f2 INT, FOREIGN KEY(f1) REFERENCES t1(f1) ON UPDATE SET NULL)ENGINE=InnoDB;
|
|
replace_result ,ALGORITHM=COPY '' ,ALGORITHM=INPLACE '';
|
|
--error ER_FK_COLUMN_NOT_NULL
|
|
eval ALTER TABLE t2 MODIFY COLUMN f1 INT NOT NULL,ALGORITHM=$algorithm;
|
|
DROP TABLE t2, t1;
|
|
|
|
--echo # modify child column NOT NULL ON DELETE SET NULL
|
|
CREATE TABLE t1(f1 INT, f2 INT, PRIMARY KEY(f1), KEY(f2))ENGINE=InnoDB;
|
|
CREATE TABLE t2(f1 INT, f2 INT, FOREIGN KEY (f2) REFERENCES t1(f2) ON DELETE SET NULL)ENGINE=InnoDB;
|
|
replace_result ,ALGORITHM=COPY '' ,ALGORITHM=INPLACE '';
|
|
--error ER_FK_COLUMN_NOT_NULL
|
|
eval ALTER TABLE t2 MODIFY COLUMN f2 INT NOT NULL,ALGORITHM=$algorithm;
|
|
DROP TABLE t2, t1;
|
|
|
|
--echo # modify child column NOT NULL ON UPDATE RESTRICT..parent column NULL
|
|
CREATE TABLE t1(f1 INT, f2 INT, PRIMARY KEY(f1), KEY(f2))ENGINE=InnoDB;
|
|
CREATE TABLE t2(f1 INT, f2 INT, FOREIGN KEY (f2) REFERENCES t1(f2) ON UPDATE RESTRICT)ENGINE=InnoDB;
|
|
replace_result ,ALGORITHM=COPY '' ,ALGORITHM=INPLACE '';
|
|
eval ALTER TABLE t2 MODIFY COLUMN f2 INT NOT NULL,ALGORITHM=$algorithm;
|
|
DROP TABLE t2, t1;
|
|
|
|
--echo # modify child column NOT NULL ON DELETE RESTRICT..parent column NULL
|
|
CREATE TABLE t1(f1 INT, f2 INT, PRIMARY KEY(f1), KEY(f2))ENGINE=InnoDB;
|
|
CREATE TABLE t2(f1 INT, f2 INT, FOREIGN KEY (f2) REFERENCES t1(f2) ON DELETE RESTRICT)ENGINE=InnoDB;
|
|
replace_result ,ALGORITHM=COPY '' ,ALGORITHM=INPLACE '';
|
|
eval ALTER TABLE t2 MODIFY COLUMN f2 INT NOT NULL,ALGORITHM=$algorithm;
|
|
DROP TABLE t2, t1;
|
|
|
|
--echo # modify child column NOT NULL ON UPDATE NO ACTION..PARENT COLUMN NULL
|
|
CREATE TABLE t1(f1 INT, f2 INT, PRIMARY KEY(f1), KEY(f2))ENGINE=InnoDB;
|
|
CREATE TABLE t2(f1 INT, f2 INT, FOREIGN KEY (f2) REFERENCES t1(f2) ON UPDATE NO ACTION)ENGINE=InnoDB;
|
|
replace_result ,ALGORITHM=COPY '' ,ALGORITHM=INPLACE '';
|
|
eval ALTER TABLE t2 MODIFY COLUMN f2 INT NOT NULL,ALGORITHM=$algorithm;
|
|
DROP TABLE t2, t1;
|
|
|
|
--echo # modify child column NOT NULL ON DELETE NO ACTION..PARENT COLUMN NULL
|
|
CREATE TABLE t1(f1 INT, f2 INT, PRIMARY KEY(f1), KEY(f2))ENGINE=InnoDB;
|
|
CREATE TABLE t2(f1 INT, f2 INT, FOREIGN KEY (f2) REFERENCES t1(f2) ON DELETE NO ACTION)ENGINE=InnoDB;
|
|
replace_result ,ALGORITHM=COPY '' ,ALGORITHM=INPLACE '';
|
|
eval ALTER TABLE t2 MODIFY COLUMN f2 INT NOT NULL,ALGORITHM=$algorithm;
|
|
DROP TABLE t2, t1;
|
|
|
|
--echo # modify parent column NULL ON UPDATE CASCADE child column NOT NULL
|
|
CREATE TABLE `t#1`(f1 INT, f2 INT NOT NULL, PRIMARY KEY(f1), KEY(f2))ENGINE=InnoDB;
|
|
CREATE TABLE `t#2`(f1 INT NOT NULL,
|
|
FOREIGN KEY(f1) REFERENCES `t#1`(f2)
|
|
ON UPDATE CASCADE)ENGINE=InnoDB;
|
|
replace_result ,ALGORITHM=COPY '' ,ALGORITHM=INPLACE '';
|
|
--error ER_FK_COLUMN_CANNOT_CHANGE_CHILD
|
|
eval ALTER TABLE `t#1` MODIFY COLUMN f2 INT,ALGORITHM=$algorithm;
|
|
DROP TABLE `t#2`, `t#1`;
|
|
|
|
--echo # modify parent column NULL ON DELETE CASCADE child column NOT NULL
|
|
CREATE TABLE t1(f1 INT, f2 INT NOT NULL, PRIMARY KEY(f1), KEY(f2))ENGINE=InnoDB;
|
|
CREATE TABLE t2(f1 INT NOT NULL, FOREIGN KEY(f1) REFERENCES t1(f2) ON DELETE CASCADE)ENGINE=InnoDB;
|
|
replace_result ,ALGORITHM=COPY '' ,ALGORITHM=INPLACE '';
|
|
eval ALTER TABLE t1 MODIFY COLUMN f2 INT,ALGORITHM=$algorithm;
|
|
DROP TABLE t2, t1;
|
|
|
|
--echo # modify parent column NULL ON UPDATE SET NULL child column NOT NULL
|
|
CREATE TABLE t1(f1 INT, f2 INT NOT NULL, PRIMARY KEY(f1), KEY(f2))ENGINE=InnoDB;
|
|
--error ER_CANT_CREATE_TABLE
|
|
CREATE TABLE t2(f1 INT NOT NULL, FOREIGN KEY(f1) REFERENCES t1(f2) ON UPDATE SET NULL)ENGINE=InnoDB;
|
|
DROP TABLE t1;
|
|
|
|
--echo # modify parent column NULL ON DELETE SET NULL child NOT NULL
|
|
CREATE TABLE t1(f1 INT, f2 INT NOT NULL, PRIMARY KEY(f1), KEY(f2))ENGINE=InnoDB;
|
|
--error ER_CANT_CREATE_TABLE
|
|
CREATE TABLE t2(f1 INT NOT NULL, FOREIGN KEY(f1) REFERENCES t1(f2) ON DELETE SET NULL)ENGINE=InnoDB;
|
|
DROP TABLE t1;
|
|
|
|
--echo # modify parent column NULL ON UPDATE RESTRICT child column NOT NULL
|
|
CREATE TABLE t1(f1 INT, f2 INT NOT NULL, PRIMARY KEY(f1), KEY(f2))ENGINE=InnoDB;
|
|
CREATE TABLE t2(f1 INT NOT NULL, FOREIGN KEY(f1) REFERENCES t1(f2) ON UPDATE RESTRICT)ENGINE=InnoDB;
|
|
replace_result ,ALGORITHM=COPY '' ,ALGORITHM=INPLACE '';
|
|
eval ALTER TABLE t1 MODIFY COLUMN f2 INT,ALGORITHM=$algorithm;
|
|
DROP TABLE t2, t1;
|
|
|
|
--echo # modify parent column NULL ON DELETE RESTRICT child column NOT NULL
|
|
CREATE TABLE t1(f1 INT, f2 INT NOT NULL, PRIMARY KEY(f1), KEY(f2))ENGINE=InnoDB;
|
|
CREATE TABLE t2(f1 INT NOT NULL, FOREIGN KEY(f1) REFERENCES t1(f2) ON UPDATE RESTRICT)ENGINE=InnoDB;
|
|
replace_result ,ALGORITHM=COPY '' ,ALGORITHM=INPLACE '';
|
|
eval ALTER TABLE t1 MODIFY COLUMN f2 INT,ALGORITHM=$algorithm;
|
|
DROP TABLE t2, t1;
|
|
|
|
--echo # modify parent column NULL ON UPDATE NO ACTION child column NOT NULL
|
|
CREATE TABLE t1(f1 INT, f2 INT NOT NULL, PRIMARY KEY(f1), KEY(f2))ENGINE=InnoDB;
|
|
CREATE TABLE t2(f1 INT NOT NULL, FOREIGN KEY(f1) REFERENCES t1(f2) ON UPDATE NO ACTION)ENGINE=InnoDB;
|
|
replace_result ,ALGORITHM=COPY '' ,ALGORITHM=INPLACE '';
|
|
eval ALTER TABLE t1 MODIFY COLUMN f2 INT,ALGORITHM=$algorithm;
|
|
DROP TABLE t2, t1;
|
|
|
|
--echo # modify parent column NULL ON DELETE NO ACTION child column NOT NULL
|
|
CREATE TABLE t1(f1 INT, f2 INT NOT NULL, PRIMARY KEY(f1), KEY(f2))ENGINE=InnoDB;
|
|
CREATE TABLE t2(f1 INT NOT NULL, FOREIGN KEY(f1) REFERENCES t1(f2) ON DELETE NO ACTION)ENGINE=InnoDB;
|
|
replace_result ,ALGORITHM=COPY '' ,ALGORITHM=INPLACE '';
|
|
eval ALTER TABLE t1 MODIFY COLUMN f2 INT,ALGORITHM=$algorithm;
|
|
DROP TABLE t2, t1;
|
|
|
|
--echo # foreign key constraint for multiple columns
|
|
--echo # modify parent column NULL ON UPDATE CASCADE child column NOT NULL
|
|
CREATE TABLE t1(f1 INT NOT NULL, f2 INT NOT NULL,
|
|
INDEX(f1, f2))ENGINE=InnoDB;
|
|
CREATE TABLE t2(f1 INT NOT NULL, f2 INT NOT NULL,
|
|
INDEX(f1, f2),
|
|
FOREIGN KEY(f1, f2) REFERENCES t1(f1, f2) ON
|
|
UPDATE CASCADE)ENGINE=InnoDB;
|
|
replace_result ,ALGORITHM=COPY '' ,ALGORITHM=INPLACE '';
|
|
--error ER_FK_COLUMN_CANNOT_CHANGE_CHILD
|
|
eval ALTER TABLE t1 MODIFY COLUMN f1 INT,ALGORITHM=$algorithm;
|
|
DROP TABLE t2, t1;
|
|
|
|
--echo # modify child column NOT NULL ON UPDATE CASCADE parent column NULL
|
|
CREATE TABLE t1(f1 INT, f2 INT, INDEX(f1, f2))ENGINE=InnoDB;
|
|
CREATE TABLE t2(f1 INT, f2 INT, INDEX(f1, f2),
|
|
FOREIGN KEY(f1, f2) REFERENCES t1(f1, f2) ON
|
|
UPDATE CASCADE)ENGINE=InnoDB;
|
|
replace_result ,ALGORITHM=COPY '' ,ALGORITHM=INPLACE '';
|
|
--error ER_FK_COLUMN_NOT_NULL
|
|
eval ALTER TABLE t2 MODIFY COLUMN f2 INT NOT NULL,ALGORITHM=$algorithm;
|
|
DROP TABLE t2, t1;
|
|
|
|
--echo # allow foreign key constraints when parent table created later
|
|
SET FOREIGN_KEY_CHECKS=0;
|
|
CREATE TABLE t2(f1 INT, FOREIGN KEY(f1) REFERENCES t1(f2) ON UPDATE CASCADE)ENGINE=InnoDB;
|
|
SET FOREIGN_KEY_CHECKS=1;
|
|
ALTER TABLE t2 MODIFY COLUMN f1 INT NOT NULL;
|
|
CREATE TABLE t1(f1 INT, f2 INT, PRIMARY KEY(f1), KEY(f2))ENGINE=InnoDB;
|
|
INSERT INTO t1 VALUES(1, 1);
|
|
INSERT INTO t2 VALUES(1);
|
|
--error ER_ROW_IS_REFERENCED_2
|
|
UPDATE t1 SET f2= NULL;
|
|
SELECT * FROM t2;
|
|
SET FOREIGN_KEY_CHECKS=0;
|
|
UPDATE t1 SET f2= NULL;
|
|
SELECT * FROM t2;
|
|
DROP TABLE t2, t1;
|
|
|
|
--echo # Modify column + Drop column & Drop foreign key constraint
|
|
CREATE TABLE t1(f1 INT, f2 INT, KEY(f1), KEY(f2))ENGINE=InnoDB;
|
|
CREATE TABLE t2(f1 INT, f2 INT, f3 INT,
|
|
FOREIGN KEY fdx(f2) REFERENCES t1(f1),
|
|
FOREIGN KEY fdx2(f3) REFERENCES t1(f2))ENGINE=InnoDB;
|
|
|
|
replace_result ,ALGORITHM=COPY '' ,ALGORITHM=INPLACE '';
|
|
eval ALTER TABLE t2 MODIFY f2 INT NOT NULL, DROP FOREIGN KEY fdx,ALGORITHM=$algorithm;
|
|
|
|
replace_result ,ALGORITHM=COPY '' ,ALGORITHM=INPLACE '';
|
|
eval ALTER TABLE t2 ADD FOREIGN KEY fdx (f2) REFERENCES t1(f1),ALGORITHM=$algorithm;
|
|
|
|
replace_result ,ALGORITHM=COPY '' ,ALGORITHM=INPLACE '';
|
|
eval ALTER TABLE t2 DROP COLUMN f2, DROP FOREIGN KEY fdx,ALGORITHM=$algorithm;
|
|
DROP TABLE t2, t1;
|
|
|
|
--echo # Drop foreign index & modify column
|
|
CREATE TABLE `t#1`(f1 INT, f2 INT NOT NULL, PRIMARY KEY(f1), KEY(f2))ENGINE=InnoDB;
|
|
CREATE TABLE `t#2`(f1 INT NOT NULL, FOREIGN KEY(f1) REFERENCES `t#1`(f2) ON UPDATE CASCADE)ENGINE=InnoDB;
|
|
|
|
SET FOREIGN_KEY_CHECKS=0;
|
|
replace_result ,ALGORITHM=COPY '' ,ALGORITHM=INPLACE '';
|
|
eval ALTER TABLE `t#2` DROP INDEX f1,ALGORITHM=$algorithm;
|
|
SET FOREIGN_KEY_CHECKS=1;
|
|
|
|
let $error_code=0;
|
|
if ($algorithm == "COPY")
|
|
{
|
|
let $error_code= ER_ERROR_ON_RENAME;
|
|
}
|
|
|
|
--replace_regex /#sql-alter-[0-9a-f_\-]*/#sql-alter/
|
|
replace_result ,ALGORITHM=COPY '' ,ALGORITHM=INPLACE '' $MYSQLD_DATADIR ./;
|
|
--error $error_code
|
|
eval ALTER TABLE `t#1` MODIFY COLUMN f2 INT,ALGORITHM=$algorithm;
|
|
DROP TABLE `t#2`, `t#1`;
|
|
|
|
--echo # Drop referenced index and modify column
|
|
CREATE TABLE `t#1`(f1 INT, f2 INT, PRIMARY KEY(f1), KEY(f2))ENGINE=InnoDB;
|
|
CREATE TABLE `t#2`(f1 INT, FOREIGN KEY(f1) REFERENCES `t#1`(f2) ON UPDATE CASCADE)ENGINE=InnoDB;
|
|
SET FOREIGN_KEY_CHECKS=0;
|
|
|
|
replace_result ,ALGORITHM=COPY '' ,ALGORITHM=INPLACE '';
|
|
eval ALTER TABLE `t#1` DROP INDEX f2,ALGORITHM=$algorithm;
|
|
SET FOREIGN_KEY_CHECKS=1;
|
|
|
|
--replace_regex /#sql-alter-[0-9a-f_\-]*/#sql-alter/
|
|
replace_result ,ALGORITHM=COPY '' ,ALGORITHM=INPLACE '' $MYSQLD_DATADIR ./;
|
|
--error $error_code
|
|
eval ALTER TABLE `t#2` MODIFY COLUMN f1 INT NOT NULL,ALGORITHM=$algorithm;
|
|
DROP TABLE `t#2`, `t#1`;
|
|
|
|
--echo # Self referential modifying column
|
|
CREATE TABLE t1(f1 INT, f2 INT, index(f2), foreign key(f1) references t1(f2) ON UPDATE CASCADE)engine=innodb;
|
|
|
|
replace_result ,ALGORITHM=COPY '' ,ALGORITHM=INPLACE '';
|
|
eval ALTER TABLE t1 MODIFY COLUMN f2 INT NOT NULL,ALGORITHM=$algorithm;
|
|
|
|
replace_result ,ALGORITHM=COPY '' ,ALGORITHM=INPLACE '';
|
|
eval ALTER TABLE t1 MODIFY COLUMN f1 INT NOT NULL,ALGORITHM=$algorithm;
|
|
|
|
replace_result ,ALGORITHM=COPY '' ,ALGORITHM=INPLACE '';
|
|
eval ALTER TABLE t1 MODIFY COLUMN f1 INT,ALGORITHM=$algorithm;
|
|
DROP TABLE t1;
|