Skip to content

Commit

Permalink
MDEV-11114 Cannot drop column referenced by CHECK constraint
Browse files Browse the repository at this point in the history
SQL Standard behavior for DROP COLUMN xxx RESTRICT:
* If a constraint (UNIQUE or CHECK) uses only the dropped column,
  it's automatically dropped too. If it uses many columns - an error.
  • Loading branch information
vuvova committed Aug 14, 2017
1 parent 28ddc9b commit 04b288a
Show file tree
Hide file tree
Showing 17 changed files with 121 additions and 200 deletions.
51 changes: 51 additions & 0 deletions mysql-test/r/alter_table.result
Original file line number Diff line number Diff line change
Expand Up @@ -2171,3 +2171,54 @@ DROP TABLE t1;
CREATE TABLE t1(a INT, b INT, CONSTRAINT min check (a>5),
CONSTRAINT min check (b>5));
ERROR HY000: Duplicate CHECK constraint name 'min'
create table t1 (a int, b int, check(a>b));
alter table t1 drop column a;
ERROR 42S22: Unknown column 'a' in 'CHECK'
alter table t1 drop column b, add column b bigint first;
ERROR 42S22: Unknown column 'b' in 'CHECK'
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) DEFAULT NULL,
`b` int(11) DEFAULT NULL,
CONSTRAINT `CONSTRAINT_1` CHECK (`a` > `b`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop table t1;
create table t1 (a int, b int, check(a>0));
alter table t1 drop column a;
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`b` int(11) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop table t1;
create table t1 (a int, b int, check(a>0));
alter table t1 drop column a, add column a bigint first;
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` bigint(20) DEFAULT NULL,
`b` int(11) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop table t1;
create table t1 (a int, b int, c int, unique(a));
alter table t1 drop column a;
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`b` int(11) DEFAULT NULL,
`c` int(11) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop table t1;
create table t1 (a int, b int, c int, unique(a,b));
alter table t1 drop column a;
ERROR 42000: Key column 'a' doesn't exist in table
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) DEFAULT NULL,
`b` int(11) DEFAULT NULL,
`c` int(11) DEFAULT NULL,
UNIQUE KEY `a` (`a`,`b`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop table t1;
4 changes: 0 additions & 4 deletions mysql-test/r/check_constraint.result
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,6 @@ insert into t1(c1) values(1);
ERROR 23000: CONSTRAINT `CONSTRAINT_1` failed for `test`.`t1`
insert into t1(c1) values(2);
drop table t1;
create table t1 (a int, b int, check(a>0));
alter table t1 drop column a;
ERROR 42S22: Unknown column 'a' in 'CHECK'
drop table t1;
create or replace table t1( c1 int auto_increment primary key, check( c1 > 0 or c1 is null ) );
ERROR HY000: Function or expression 'AUTO_INCREMENT' cannot be used in the CHECK clause of `c1`
create table t1 (a int check (@b in (select user from mysql.user)));
Expand Down
4 changes: 2 additions & 2 deletions mysql-test/r/sp-destruct.result
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ create function bug14233_f() returns int
return 42;
create table t1 (id int);
create trigger t1_ai after insert on t1 for each row call bug14233();
alter table mysql.proc drop type;
alter table mysql.proc drop security_type;
call bug14233();
ERROR HY000: Column count of mysql.proc is wrong. Expected 20, found 19. The table is probably corrupted
create view v1 as select bug14233_f();
Expand Down Expand Up @@ -142,7 +142,7 @@ drop database if exists mysqltest;
flush table mysql.proc;
create database mysqltest;
# Corrupt mysql.proc to make it unusable by current version of server.
alter table mysql.proc drop column type;
alter table mysql.proc drop column security_type;
# The below statement should not cause assertion failure.
drop database mysqltest;
Warnings:
Expand Down
2 changes: 1 addition & 1 deletion mysql-test/r/statistics.result
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,7 @@ test t2 idx4 1 6.2000
test t2 idx4 2 1.7222
test t2 idx4 3 1.1154
test t2 idx4 4 1.0000
ALTER TABLE t2 DROP COLUMN b;
ALTER TABLE t2 DROP COLUMN b, DROP PRIMARY KEY, ADD PRIMARY KEY(a);
SELECT * FROM mysql.index_stats ORDER BY index_name, prefix_arity, table_name;
db_name table_name index_name prefix_arity avg_frequency
test t2 idx2 1 7.0000
Expand Down
6 changes: 0 additions & 6 deletions mysql-test/suite/funcs_1/datadict/is_key_column_usage.inc
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,6 @@ eval $my_select;
ALTER TABLE db_datadict.t1_my_tablex ADD UNIQUE my_idx (f4,first_col);
eval $my_select;
#
# Check DROP COLUMN
eval $my_select;
ALTER TABLE db_datadict.t1_my_tablex
DROP COLUMN first_col;
eval $my_select;
#
# Check impact of DROP TABLE
SELECT table_name, column_name
FROM information_schema.key_column_usage
Expand Down
24 changes: 2 additions & 22 deletions mysql-test/suite/funcs_1/r/is_key_column_usage.result
Original file line number Diff line number Diff line change
Expand Up @@ -345,35 +345,15 @@ db_datadict my_idx db_datadict t1_my_tablex f4 1
db_datadict my_idx db_datadict t1_my_tablex first_col 2
db_datadict PRIMARY db_datadict t1_my_tablex first_col 1
db_datadict PRIMARY db_datadict t1_my_tablex f2 2
SELECT constraint_schema, constraint_name, table_schema,
table_name, column_name, ordinal_position
FROM information_schema.key_column_usage
WHERE table_name = 't1_my_tablex'
ORDER BY constraint_schema, constraint_name, table_schema,
table_name, ordinal_position;
constraint_schema constraint_name table_schema table_name column_name ordinal_position
db_datadict my_idx db_datadict t1_my_tablex f4 1
db_datadict my_idx db_datadict t1_my_tablex first_col 2
db_datadict PRIMARY db_datadict t1_my_tablex first_col 1
db_datadict PRIMARY db_datadict t1_my_tablex f2 2
ALTER TABLE db_datadict.t1_my_tablex
DROP COLUMN first_col;
SELECT constraint_schema, constraint_name, table_schema,
table_name, column_name, ordinal_position
FROM information_schema.key_column_usage
WHERE table_name = 't1_my_tablex'
ORDER BY constraint_schema, constraint_name, table_schema,
table_name, ordinal_position;
constraint_schema constraint_name table_schema table_name column_name ordinal_position
db_datadict my_idx db_datadict t1_my_tablex f4 1
db_datadict PRIMARY db_datadict t1_my_tablex f2 1
SELECT table_name, column_name
FROM information_schema.key_column_usage
WHERE table_name = 't1_my_tablex'
ORDER BY table_name, column_name;
table_name column_name
t1_my_tablex f2
t1_my_tablex f4
t1_my_tablex first_col
t1_my_tablex first_col
DROP TABLE db_datadict.t1_my_tablex;
SELECT table_name, column_name
FROM information_schema.key_column_usage
Expand Down
2 changes: 1 addition & 1 deletion mysql-test/suite/funcs_1/r/is_table_constraints.result
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ SELECT table_name FROM information_schema.table_constraints
WHERE table_name LIKE 't1_my_table%';
table_name
CREATE TABLE test.t1_my_table
(f1 CHAR(12), f2 TIMESTAMP, f4 BIGINT, PRIMARY KEY(f1,f2))
(f1 CHAR(12), f2 TIMESTAMP, f4 BIGINT, PRIMARY KEY(f1))
DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci
ENGINE = <engine_type>;
SELECT constraint_name, table_schema, table_name, constraint_type
Expand Down
2 changes: 1 addition & 1 deletion mysql-test/suite/funcs_1/t/is_table_constraints.test
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ WHERE table_name LIKE 't1_my_table%';
--replace_result $engine_type <engine_type>
eval
CREATE TABLE test.t1_my_table
(f1 CHAR(12), f2 TIMESTAMP, f4 BIGINT, PRIMARY KEY(f1,f2))
(f1 CHAR(12), f2 TIMESTAMP, f4 BIGINT, PRIMARY KEY(f1))
DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci
ENGINE = $engine_type;
# Settings used in CREATE TABLE must be visible
Expand Down
18 changes: 0 additions & 18 deletions mysql-test/suite/gcol/inc/gcol_keys.inc
Original file line number Diff line number Diff line change
Expand Up @@ -322,24 +322,6 @@ ALTER TABLE t1 add unique index idx(pk);
DESC t1;
DROP TABLE t1;

--echo #
--echo # Bug#21346132: WL8149:INNODB: FAILING ASSERTION:
--echo # PRIMARY_KEY_NO == -1 || PRIMARY_KEY_NO == 0
--echo #
CREATE TABLE t1 (
col_int_nokey int(11) NOT NULL,
col_int_key int(11) GENERATED ALWAYS AS (col_int_nokey),
col_varchar_nokey varchar(1) NOT NULL,
col_varchar_key varchar(2) GENERATED ALWAYS AS (col_varchar_nokey),
UNIQUE KEY col_int_key (col_int_key),
UNIQUE KEY col_varchar_key (col_varchar_key),
UNIQUE KEY col_int_key_2 (col_int_key,col_varchar_key),
UNIQUE KEY col_varchar_key_2 (col_varchar_key,col_varchar_nokey),
KEY col_int_key_3 (col_int_key,col_int_nokey)
);

ALTER TABLE t1 DROP COLUMN col_varchar_key;
DROP TABLE t1;
--echo #
--echo # Bug#21320151 WL8149: WRONG RESULT WITH INDEX SCAN
--echo #
Expand Down
17 changes: 0 additions & 17 deletions mysql-test/suite/gcol/r/gcol_keys_myisam.result
Original file line number Diff line number Diff line change
Expand Up @@ -303,23 +303,6 @@ col_int_nokey int(11) YES NULL
col_int_key int(11) YES UNI NULL VIRTUAL GENERATED
DROP TABLE t1;
#
# Bug#21346132: WL8149:INNODB: FAILING ASSERTION:
# PRIMARY_KEY_NO == -1 || PRIMARY_KEY_NO == 0
#
CREATE TABLE t1 (
col_int_nokey int(11) NOT NULL,
col_int_key int(11) GENERATED ALWAYS AS (col_int_nokey),
col_varchar_nokey varchar(1) NOT NULL,
col_varchar_key varchar(2) GENERATED ALWAYS AS (col_varchar_nokey),
UNIQUE KEY col_int_key (col_int_key),
UNIQUE KEY col_varchar_key (col_varchar_key),
UNIQUE KEY col_int_key_2 (col_int_key,col_varchar_key),
UNIQUE KEY col_varchar_key_2 (col_varchar_key,col_varchar_nokey),
KEY col_int_key_3 (col_int_key,col_int_nokey)
);
ALTER TABLE t1 DROP COLUMN col_varchar_key;
DROP TABLE t1;
#
# Bug#21320151 WL8149: WRONG RESULT WITH INDEX SCAN
#
CREATE TABLE t1 (
Expand Down
75 changes: 0 additions & 75 deletions mysql-test/suite/gcol/r/innodb_virtual_basic.result
Original file line number Diff line number Diff line change
Expand Up @@ -930,81 +930,6 @@ create index idx on t(c);
Warnings:
Warning 1365 Division by 0
DROP TABLE t;
CREATE TABLE t (
pk INTEGER AUTO_INCREMENT,
col_int_nokey INTEGER /*! NULL */,
col_int INT GENERATED ALWAYS AS (col_int_nokey + col_int_nokey) STORED,
col_int_key INTEGER GENERATED ALWAYS AS (col_int + col_int_nokey) VIRTUAL,
col_date_nokey DATE /*! NULL */,
col_date DATE GENERATED ALWAYS AS (DATE_ADD(col_date_nokey,interval 30 day)) STORED,
col_date_key DATE GENERATED ALWAYS AS (DATE_ADD(col_date,interval 30 day)) VIRTUAL,
col_datetime_nokey DATETIME /*! NULL */,
col_time_nokey TIME /*! NULL */,
col_datetime DATETIME GENERATED ALWAYS AS (ADDTIME(col_datetime_nokey, col_time_nokey)) STORED,
col_time TIME GENERATED ALWAYS AS (ADDTIME(col_datetime_nokey, col_time_nokey)) STORED,
col_datetime_key DATETIME GENERATED ALWAYS AS (ADDTIME(col_datetime, col_time_nokey)) VIRTUAL,
col_time_key TIME GENERATED ALWAYS AS (ADDTIME(col_datetime_nokey, col_time)) VIRTUAL,
col_varchar_nokey VARCHAR(1) /*! NULL */,
col_varchar VARCHAR(2) GENERATED ALWAYS AS (CONCAT(col_varchar_nokey,col_varchar_nokey)) STORED,
col_varchar_key VARCHAR(2) GENERATED ALWAYS AS (CONCAT(col_varchar, 'x')) VIRTUAL,
unique KEY (pk,col_int_key),
KEY(col_int),
KEY(col_date),
KEY(col_datetime),
KEY(col_time),
KEY(col_varchar),
UNIQUE KEY (col_int_key),
KEY (col_time_key),
KEY (col_datetime_key),
UNIQUE KEY (col_int_key, col_varchar_key),
KEY (col_int_key, col_int_nokey),
KEY(col_int_key,col_date_key),
KEY(col_int_key, col_time_key),
KEY(col_int_key, col_datetime_key),
KEY(col_date_key,col_time_key,col_datetime_key),
KEY (col_varchar_key, col_varchar_nokey),
UNIQUE KEY (col_int_key, col_varchar_key, col_date_key, col_time_key, col_datetime_key)
) AUTO_INCREMENT=10 ENGINE=INNODB PARTITION BY KEY(col_int_key) PARTITIONS 3;
ALTER TABLE t DROP COLUMN `pk`;
SHOW CREATE TABLE t;
Table Create Table
t CREATE TABLE `t` (
`col_int_nokey` int(11) DEFAULT NULL,
`col_int` int(11) GENERATED ALWAYS AS (`col_int_nokey` + `col_int_nokey`) STORED,
`col_int_key` int(11) GENERATED ALWAYS AS (`col_int` + `col_int_nokey`) VIRTUAL,
`col_date_nokey` date DEFAULT NULL,
`col_date` date GENERATED ALWAYS AS (`col_date_nokey` + interval 30 day) STORED,
`col_date_key` date GENERATED ALWAYS AS (`col_date` + interval 30 day) VIRTUAL,
`col_datetime_nokey` datetime DEFAULT NULL,
`col_time_nokey` time DEFAULT NULL,
`col_datetime` datetime GENERATED ALWAYS AS (addtime(`col_datetime_nokey`,`col_time_nokey`)) STORED,
`col_time` time GENERATED ALWAYS AS (addtime(`col_datetime_nokey`,`col_time_nokey`)) STORED,
`col_datetime_key` datetime GENERATED ALWAYS AS (addtime(`col_datetime`,`col_time_nokey`)) VIRTUAL,
`col_time_key` time GENERATED ALWAYS AS (addtime(`col_datetime_nokey`,`col_time`)) VIRTUAL,
`col_varchar_nokey` varchar(1) DEFAULT NULL,
`col_varchar` varchar(2) GENERATED ALWAYS AS (concat(`col_varchar_nokey`,`col_varchar_nokey`)) STORED,
`col_varchar_key` varchar(2) GENERATED ALWAYS AS (concat(`col_varchar`,'x')) VIRTUAL,
UNIQUE KEY `pk` (`col_int_key`),
UNIQUE KEY `col_int_key` (`col_int_key`),
UNIQUE KEY `col_int_key_2` (`col_int_key`,`col_varchar_key`),
UNIQUE KEY `col_int_key_7` (`col_int_key`,`col_varchar_key`,`col_date_key`,`col_time_key`,`col_datetime_key`),
KEY `col_int` (`col_int`),
KEY `col_date` (`col_date`),
KEY `col_datetime` (`col_datetime`),
KEY `col_time` (`col_time`),
KEY `col_varchar` (`col_varchar`),
KEY `col_time_key` (`col_time_key`),
KEY `col_datetime_key` (`col_datetime_key`),
KEY `col_int_key_3` (`col_int_key`,`col_int_nokey`),
KEY `col_int_key_4` (`col_int_key`,`col_date_key`),
KEY `col_int_key_5` (`col_int_key`,`col_time_key`),
KEY `col_int_key_6` (`col_int_key`,`col_datetime_key`),
KEY `col_date_key` (`col_date_key`,`col_time_key`,`col_datetime_key`),
KEY `col_varchar_key` (`col_varchar_key`,`col_varchar_nokey`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
PARTITION BY KEY (`col_int_key`)
PARTITIONS 3
DROP TABLE t;
CREATE TABLE t (a INT, b INT, c INT GENERATED ALWAYS AS(a+b), h VARCHAR(10));
INSERT INTO t VALUES (11, 3, DEFAULT, 'mm');
INSERT INTO t VALUES (18, 1, DEFAULT, 'mm');
Expand Down
42 changes: 0 additions & 42 deletions mysql-test/suite/gcol/t/innodb_virtual_basic.test
Original file line number Diff line number Diff line change
Expand Up @@ -780,48 +780,6 @@ alter table t add c INT GENERATED ALWAYS AS(a/b);
create index idx on t(c);
DROP TABLE t;

CREATE TABLE t (
pk INTEGER AUTO_INCREMENT,
col_int_nokey INTEGER /*! NULL */,
col_int INT GENERATED ALWAYS AS (col_int_nokey + col_int_nokey) STORED,
col_int_key INTEGER GENERATED ALWAYS AS (col_int + col_int_nokey) VIRTUAL,
col_date_nokey DATE /*! NULL */,
col_date DATE GENERATED ALWAYS AS (DATE_ADD(col_date_nokey,interval 30 day)) STORED,
col_date_key DATE GENERATED ALWAYS AS (DATE_ADD(col_date,interval 30 day)) VIRTUAL,
col_datetime_nokey DATETIME /*! NULL */,
col_time_nokey TIME /*! NULL */,
col_datetime DATETIME GENERATED ALWAYS AS (ADDTIME(col_datetime_nokey, col_time_nokey)) STORED,
col_time TIME GENERATED ALWAYS AS (ADDTIME(col_datetime_nokey, col_time_nokey)) STORED,
col_datetime_key DATETIME GENERATED ALWAYS AS (ADDTIME(col_datetime, col_time_nokey)) VIRTUAL,
col_time_key TIME GENERATED ALWAYS AS (ADDTIME(col_datetime_nokey, col_time)) VIRTUAL,
col_varchar_nokey VARCHAR(1) /*! NULL */,
col_varchar VARCHAR(2) GENERATED ALWAYS AS (CONCAT(col_varchar_nokey,col_varchar_nokey)) STORED,
col_varchar_key VARCHAR(2) GENERATED ALWAYS AS (CONCAT(col_varchar, 'x')) VIRTUAL,
unique KEY (pk,col_int_key),
KEY(col_int),
KEY(col_date),
KEY(col_datetime),
KEY(col_time),
KEY(col_varchar),
UNIQUE KEY (col_int_key),
KEY (col_time_key),
KEY (col_datetime_key),
UNIQUE KEY (col_int_key, col_varchar_key),
KEY (col_int_key, col_int_nokey),
KEY(col_int_key,col_date_key),
KEY(col_int_key, col_time_key),
KEY(col_int_key, col_datetime_key),
KEY(col_date_key,col_time_key,col_datetime_key),
KEY (col_varchar_key, col_varchar_nokey),
UNIQUE KEY (col_int_key, col_varchar_key, col_date_key, col_time_key, col_datetime_key)
) AUTO_INCREMENT=10 ENGINE=INNODB PARTITION BY KEY(col_int_key) PARTITIONS 3;

ALTER TABLE t DROP COLUMN `pk`;

SHOW CREATE TABLE t;

DROP TABLE t;

CREATE TABLE t (a INT, b INT, c INT GENERATED ALWAYS AS(a+b), h VARCHAR(10));

INSERT INTO t VALUES (11, 3, DEFAULT, 'mm');
Expand Down
32 changes: 32 additions & 0 deletions mysql-test/t/alter_table.test
Original file line number Diff line number Diff line change
Expand Up @@ -1801,3 +1801,35 @@ DROP TABLE t1;
--error ER_DUP_CONSTRAINT_NAME
CREATE TABLE t1(a INT, b INT, CONSTRAINT min check (a>5),
CONSTRAINT min check (b>5));

#
# MDEV-11114 Cannot drop column referenced by CHECK constraint
#
create table t1 (a int, b int, check(a>b));
--error ER_BAD_FIELD_ERROR
alter table t1 drop column a;
--error ER_BAD_FIELD_ERROR
alter table t1 drop column b, add column b bigint first;
show create table t1;
drop table t1;

create table t1 (a int, b int, check(a>0));
alter table t1 drop column a;
show create table t1;
drop table t1;

create table t1 (a int, b int, check(a>0));
alter table t1 drop column a, add column a bigint first;
show create table t1;
drop table t1;

create table t1 (a int, b int, c int, unique(a));
alter table t1 drop column a;
show create table t1;
drop table t1;

create table t1 (a int, b int, c int, unique(a,b));
--error ER_KEY_COLUMN_DOES_NOT_EXITS
alter table t1 drop column a;
show create table t1;
drop table t1;
8 changes: 0 additions & 8 deletions mysql-test/t/check_constraint.test
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,6 @@ insert into t1(c1) values(1);
insert into t1(c1) values(2);
drop table t1;

#
# MDEV-11114 Cannot drop column referenced by CHECK constraint: Unknown column 'a' in 'virtual column function'
#
create table t1 (a int, b int, check(a>0));
--error ER_BAD_FIELD_ERROR
alter table t1 drop column a;
drop table t1;

#
# MDEV-11117 CHECK constraint fails on intermediate step of ALTER
#
Expand Down
Loading

0 comments on commit 04b288a

Please sign in to comment.