Home > Software design >  How to drop Primary key thats needed in a foreign key constraint in MySQL?
How to drop Primary key thats needed in a foreign key constraint in MySQL?

Time:07-16

I want to drop the primary key in father. So I set the foreign key in child to delete cascade. Why this doesnt work ? Do I need to delete the foreign key first ? If so, then what the propurse of the delete on cascade statement ?

create database table_test;

use table_test;

create table father(
cod int, 
primary key (cod)
);

create table child(
cod int,
cod_father int,
primary key(cod),
constraint fk_cod_father foreign key (cod_father) references father(cod)
on delete cascade
);

alter table father
drop primary key;

CodePudding user response:

ON DELETE CASCADE constraint is used in MySQL to delete the rows from the child table automatically, when the rows from the parent table are deleted

You want to remove primary key and in your case first you need to unlink the foreign key reference first. So drop foreign key first.

  • Related