简体   繁体   中英

How to drop Primary key thats needed in a foreign key constraint in MySQL?

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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM