MySQL - 关于删除级联问题

发布于 2024-12-08 14:07:05 字数 959 浏览 0 评论 0原文

我有 3 个表:A、B 和 C。 关系是:

  • A->B 1:1
  • A->C 1:n

表 A 是主表:

CREATE TABLE IF NOT EXISTS `A` (
  `_id` INT NOT NULL AUTO_INCREMENT ,
  ..........
  `_id_B` INT NOT NULL ,
  PRIMARY KEY (`_id`) ,
  INDEX `fk_Atable_Btable` (`_id_B` ASC) ,
  CONSTRAINT `fk_A_B`
    FOREIGN KEY (`_id_B` )
    REFERENCES `B` (`_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
ENGINE = InnoDB

CREATE TABLE IF NOT EXISTS `B` (
  `_id` INT NOT NULL AUTO_INCREMENT ,
  ........
  PRIMARY KEY (`_id`) )
ENGINE = InnoDB

CREATE TABLE IF NOT EXISTS `C` (
  `_id` INT NOT NULL ,
  `_id_A` INT NOT NULL ,
  PRIMARY KEY (`_id`) ,
  INDEX `fk_Ctable_Atable` (`_id_A` ASC) ,
  CONSTRAINT `fk_C_A`
    FOREIGN KEY (`_id_A` )
    REFERENCES `A` (`_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB

我要删除表 A 上的条目,并且我会删除 ON CASCADE 条目来自 B 和 C。

我怎样才能设置我的 sql 脚本来做到这一点?

I have 3 table: A,B and C.
Relations are:

  • A->B 1:1
  • A->C 1:n

Table A is the main table:

CREATE TABLE IF NOT EXISTS `A` (
  `_id` INT NOT NULL AUTO_INCREMENT ,
  ..........
  `_id_B` INT NOT NULL ,
  PRIMARY KEY (`_id`) ,
  INDEX `fk_Atable_Btable` (`_id_B` ASC) ,
  CONSTRAINT `fk_A_B`
    FOREIGN KEY (`_id_B` )
    REFERENCES `B` (`_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
ENGINE = InnoDB

CREATE TABLE IF NOT EXISTS `B` (
  `_id` INT NOT NULL AUTO_INCREMENT ,
  ........
  PRIMARY KEY (`_id`) )
ENGINE = InnoDB

CREATE TABLE IF NOT EXISTS `C` (
  `_id` INT NOT NULL ,
  `_id_A` INT NOT NULL ,
  PRIMARY KEY (`_id`) ,
  INDEX `fk_Ctable_Atable` (`_id_A` ASC) ,
  CONSTRAINT `fk_C_A`
    FOREIGN KEY (`_id_A` )
    REFERENCES `A` (`_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB

I would to delete an entry on table A and I would delete ON CASCADE the entries from B and C.

How can I set my sql scripts to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

流心雨 2024-12-15 14:07:05

嗨,它类似于以下内容

。我有两张桌子:
文章
commentary

Commentary有一个article.id的外键,当然,当删除一篇文章时,我希望也删除它的所有评论。我尝试按如下方式执行此操作:

create table article (
id int not null auto_increment,
title varchar(80) not null,
subtitle varchar(80) not null,
content text not null,
author varchar(40) not null,
time datetime not null,

PRIMARY KEY (id)
);

create table commentary (
id int not null auto_increment,
article_id int not null,
title varchar(30),
content tinytext not null,
author varchar(40) not null,
email varchar(50),
ip varchar(16) not null,
time datetime not null,

PRIMARY KEY (id),
FOREIGN KEY (article_id)
    REFERENCES article(id)
    ON DELETE CASCADE
);

如果我没有记错的话,这是根据文档(MySQL 网站上的海报)
带有简短说明

此链接可能对您也有用

Hi its similar like following

. I have two tables:
article
commentary

Commentary has a foreign key to article.id, and of course, when deleting one article, I want all it's comments deleted as well. I've tried to do it as follows:

create table article (
id int not null auto_increment,
title varchar(80) not null,
subtitle varchar(80) not null,
content text not null,
author varchar(40) not null,
time datetime not null,

PRIMARY KEY (id)
);

create table commentary (
id int not null auto_increment,
article_id int not null,
title varchar(30),
content tinytext not null,
author varchar(40) not null,
email varchar(50),
ip varchar(16) not null,
time datetime not null,

PRIMARY KEY (id),
FOREIGN KEY (article_id)
    REFERENCES article(id)
    ON DELETE CASCADE
);

This is according to documentation if I am not mistaken (a poster on the MySQL website
with short description

This link may also useful to you

一页 2024-12-15 14:07:05

您说表 AB 处于 1:1 关系,但都有自动递增键。相反,您有一个 A._id_B 字段,我认为该字段用于定位表 B 中相应的 (1:1) 行。

标准方法是删除 A._id_B 并在另一个表 (B) 中,使主键自动递增,并且使其成为 A 主键的 FOREIGN KEY

CREATE TABLE IF NOT EXISTS A 
( _id INT NOT NULL AUTO_INCREMENT ,
  ..........
  PRIMARY KEY (_id) 
)
ENGINE = InnoDB ;


CREATE TABLE IF NOT EXISTS B 
( _id INT NOT NULL ,
  ........
  PRIMARY KEY (_id) ,                 --- B._id  is a primary key
  CONSTRAINT fk_B_A
    FOREIGN KEY (_id)                 --- and a foreign key
      REFERENCES A (_id)
      ON DELETE CASCADE
      ON UPDATE CASCADE                
 )
ENGINE = InnoDB ;

You say that tables A and B are in 1:1 relationship but both have auto incrementing key. Instead you have an A._id_B field that I suppose is used to locate the corresponding (1:1) row in table B.

The standard approach would be to drop that A._id_B and in the other table (B), make the primary key not auto incrementing and also make it a FOREIGN KEY to the primary key of A:

CREATE TABLE IF NOT EXISTS A 
( _id INT NOT NULL AUTO_INCREMENT ,
  ..........
  PRIMARY KEY (_id) 
)
ENGINE = InnoDB ;


CREATE TABLE IF NOT EXISTS B 
( _id INT NOT NULL ,
  ........
  PRIMARY KEY (_id) ,                 --- B._id  is a primary key
  CONSTRAINT fk_B_A
    FOREIGN KEY (_id)                 --- and a foreign key
      REFERENCES A (_id)
      ON DELETE CASCADE
      ON UPDATE CASCADE                
 )
ENGINE = InnoDB ;
Smile简单爱 2024-12-15 14:07:05

您需要引入从 B 指向 A 的外键关系(即,B 需要包含具有 A id 的列)才能正常工作。然后将该关系的“ON DELETE”子句设置为“ON DELETE CASCADE”,对于 C 上的 fk_C_A 约束也类似。

You would need to introduce a foreign key relationship from B pointing back to A (that is, B needs to include a column with A's id) for this to work. Then set your 'ON DELETE' clause for that relationship to 'ON DELETE CASCADE', and similarly for the fk_C_A constraint on C.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文