繁体   English   中英

如何删除 MySQL 中外键约束中所需的主键?

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

我想删除父亲中的主键。 所以我在child中设置外键来删除级联。 为什么这不起作用? 我需要先删除外键吗? 如果是这样,那么在级联语句上删除的目的是什么?

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;

在 MySQL 中使用 ON DELETE CASCADE 约束来自动删除子表中的行,当父表中的行被删除时

您想删除主键,在您的情况下,您首先需要先取消链接外键引用。 所以先删除外键。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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