Commit 2d74f908 authored by marko's avatar marko

branches/zip: innodb-index.test: Add tests for creating or dropping

FOREIGN KEY constraints.  At the moment, MySQL will rebuild the entire
table and indexes when such constraints are added or removed, even though
the constraints do not affect the storage format of the data.
parent 9d5af494
......@@ -483,12 +483,35 @@ ERROR HY000: Cannot drop index 'b': needed in a foreign key constraint
alter table t2 drop index b, drop index c, drop index d;
ERROR HY000: Cannot drop index 'b': needed in a foreign key constraint
create unique index dc on t2 (d,c);
create index dc on t1 (b,c);
alter table t2 add primary key (a);
insert into t1 values (1,1,1);
insert into t3 values (1,1,1);
insert into t4 values (1,1,1);
insert into t2 values (1,1,1,1,1);
commit;
alter table t4 add constraint dc foreign key (a) references t1(a);
show create table t4;
Table Create Table
t4 CREATE TABLE `t4` (
`a` int(11) NOT NULL,
`d` int(11) NOT NULL,
`e` int(11) DEFAULT NULL,
PRIMARY KEY (`a`),
KEY `d` (`d`),
CONSTRAINT `dc` FOREIGN KEY (`a`) REFERENCES `t1` (`a`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
alter table t3 add constraint dc foreign key (a) references t1(a);
ERROR HY000: Can't create table '#sql-temporary' (errno: 121)
show create table t3;
Table Create Table
t3 CREATE TABLE `t3` (
`a` int(11) NOT NULL,
`c` int(11) NOT NULL,
`d` int(11) DEFAULT NULL,
PRIMARY KEY (`a`),
KEY `c` (`c`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
alter table t2 drop index b, add index (b);
show create table t2;
Table Create Table
......@@ -507,9 +530,19 @@ t2 CREATE TABLE `t2` (
CONSTRAINT `t2_ibfk_3` FOREIGN KEY (`d`) REFERENCES `t4` (`d`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
delete from t1;
ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`t4`, CONSTRAINT `dc` FOREIGN KEY (`a`) REFERENCES `t1` (`a`))
drop index dc on t4;
ERROR 42000: Can't DROP 'dc'; check that column/key exists
alter table t3 drop foreign key dc;
ERROR HY000: Error on rename of './test/t3' to '#sql2-temporary' (errno: 152)
alter table t4 drop foreign key dc;
select * from t2;
a b c d e
1 1 1 1 1
delete from t1;
select * from t2;
a b c d e
drop table if exists t2,t1,t3,t4;
drop table t2,t4,t3,t1;
create table t1(a int not null, b int, c char(10), d varchar(20), primary key (a))
engine = innodb default charset=utf8;
insert into t1 values (1,1,'ab','ab'),(2,2,'ac','ac'),(3,2,'ad','ad'),(4,4,'afe','afe');
......
......@@ -126,6 +126,7 @@ alter table t2 drop index b;
alter table t2 drop index b, drop index c, drop index d;
-- Apparently, the following makes mysql_alter_table() drop index d.
create unique index dc on t2 (d,c);
create index dc on t1 (b,c);
-- This should preserve the foreign key constraints.
alter table t2 add primary key (a);
insert into t1 values (1,1,1);
......@@ -133,12 +134,29 @@ insert into t3 values (1,1,1);
insert into t4 values (1,1,1);
insert into t2 values (1,1,1,1,1);
commit;
alter table t4 add constraint dc foreign key (a) references t1(a);
show create table t4;
--replace_regex /'test\.#sql-[0-9a-f_]*'/'#sql-temporary'/
-- a foreign key 'test/dc' already exists
--error ER_CANT_CREATE_TABLE
alter table t3 add constraint dc foreign key (a) references t1(a);
show create table t3;
alter table t2 drop index b, add index (b);
show create table t2;
--error ER_ROW_IS_REFERENCED_2
delete from t1;
--error ER_CANT_DROP_FIELD_OR_KEY
drop index dc on t4;
-- there is no foreign key dc on t3
--replace_regex /'\.\/test\/#sql2-[0-9a-f-]*'/'#sql2-temporary'/
--error ER_ERROR_ON_RENAME
alter table t3 drop foreign key dc;
alter table t4 drop foreign key dc;
select * from t2;
delete from t1;
select * from t2;
drop table if exists t2,t1,t3,t4;
drop table t2,t4,t3,t1;
create table t1(a int not null, b int, c char(10), d varchar(20), primary key (a))
engine = innodb default charset=utf8;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment