MySQL - 关于删除级联问题
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗨,它类似于以下内容
。我有两张桌子:
文章
commentary
Commentary有一个article.id的外键,当然,当删除一篇文章时,我希望也删除它的所有评论。我尝试按如下方式执行此操作:
如果我没有记错的话,这是根据文档(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:
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
您说表
A
和B
处于1:1
关系,但都有自动递增键。相反,您有一个A._id_B
字段,我认为该字段用于定位表B
中相应的 (1:1) 行。标准方法是删除
A._id_B
并在另一个表 (B
) 中,使主键不自动递增,并且使其成为A
主键的FOREIGN KEY
:You say that tables
A
andB
are in1:1
relationship but both have auto incrementing key. Instead you have anA._id_B
field that I suppose is used to locate the corresponding (1:1) row in tableB
.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 aFOREIGN KEY
to the primary key ofA
:您需要引入从 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.