簡體   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