I have two tables, parent
and child
, the child
table has a FK on the parent
table using the parent_id
column. When adding this FK I can see that the child
table has both the FK and an index to represent that FK.
But when I add a unique constraint on the child
table that contains parent_id
then I can see that the index representing the FK is replaced with the new unique constraint.
Ex:
create table parent
(
parent_id int unsigned not null auto_increment primary key
);
create table child
(
child_id int unsigned not null auto_increment primary key,
parent_id int unsigned not null,
age smallint unsigned null,
constraint FK__CHILD__PARENT
foreign key (parent_id) references parent (parent_id)
);
At this point I can see that both an FK and an index called FK_CHILD_PARENT
exist on the child
table.
Now I add the unique constraint:
alter table child add constraint unique_parentId_age unique (parent_id, age);
I can see that the unique_parentId_age
index replaced the FK__CHILD__PARENT
index.
I don't understand why this is happening? Is it impossible to have an index representing a FK if another index already exists which starts with the same column as the FK.
I'm using MySQL 5.7