【问题标题】:Mysql: Change status of all child records if status of parent is updatedMysql:如果父状态更新,则更改所有子记录的状态
【发布时间】:2016-09-05 07:37:49
【问题描述】:

我有两张桌子

1.家长

Parent_ID(PK)| name    | status
--------------------------------
1            |parent 1 |enable
2            |parent 2 |enable
3            |parent 3 |disable

2。孩子

Child_Id(PK)| Parent_ID(Fk of parent table) | name     | status
----------------------------------------------------------
1           |1                              | child 1  | enable
2           |1                              | child 2  | enable
3           |1                              | child 3  | enable
4           |1                              | child 4  | enable
5           |2                              | child 5  | enable
6           |2                              | child 6  | enable
7           |2                              | child 7  | enable
8           |2                              | child 8  | enable
9           |3                              | child 9  | disable
10          |3                              | child 10 | disable
11          |3                              | child 11 | disable
12          |3                              | child 12 | disable

现在我想设置两个表之间的关系,这样如果父表中记录的状态发生变化,那么其所有子行的状态也应该发生变化。

我知道我可以使用触发器来做到这一点,但我认为应该有一些方法可以通过多列上的关系和 FK 约束来做到这一点。

【问题讨论】:

  • 我猜ON UPDATE CASCADE 会做。
  • 但是应该是什么关键我的意思是我不能在状态上设置 FK 关系,因为我还必须将它与 parent_id 关联起来,如何在这两列之间设置这种关系。
  • 可能您需要创建共轭外键。让我检查一下
  • Arpita 只是作为旁注,根据 MySQL 文档,您无法使用 MySQL '触发器'完成您在代码中尝试执行的操作。更新触发器有 2 种模式 - 更新前和更新后,并且这两个选项都不允许更改表中的现有字段。我知道这篇文章已经过时了,无论如何这可能是有用的信息。

标签: mysql foreign-keys relational-database


【解决方案1】:

您需要在child 表中创建一个复合外键,引用parent_idstatus

这是一个演示:

-- ----------------------------
-- Table structure for `parenttable`
-- ----------------------------
DROP TABLE IF EXISTS `parenttable`;
CREATE TABLE `parenttable` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `status` int(11) NOT NULL,
  PRIMARY KEY (`ID`),
  KEY `ID` (`ID`,`status`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of parenttable
-- ----------------------------
INSERT INTO `parenttable` VALUES ('1', '1');
INSERT INTO `parenttable` VALUES ('2', '0');
INSERT INTO `parenttable` VALUES ('3', '1');

-- ----------------------------
-- Table structure for `childtable`
-- ----------------------------
DROP TABLE IF EXISTS `childtable`;
CREATE TABLE `childtable` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `parent_id` int(11) NOT NULL,
  `status` int(11) NOT NULL,
  PRIMARY KEY (`ID`),
  KEY `fk_childTable_parent_id` (`parent_id`,`status`),
  CONSTRAINT `fk_childTable_parent_id` FOREIGN KEY (`parent_id`, `status`) REFERENCES `parenttable` (`ID`, `status`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of childtable
-- ----------------------------
INSERT INTO `childtable` VALUES ('1', '1', '1');
INSERT INTO `childtable` VALUES ('3', '1', '1');
INSERT INTO `childtable` VALUES ('6', '1', '1');
INSERT INTO `childtable` VALUES ('4', '2', '0');
INSERT INTO `childtable` VALUES ('5', '2', '0');

测试:

现在尝试更新父表中ID = 1status 字段。

此更改也会触发child 表中所有子条目的状态值的更改。

UPDATE parentTable SET status = 0 WHERE ID = 1;

SELECT * FROM childTable WHERE parent_id = 1;

输出:

ID     parent_id     status

1         1            0
2         1            0
3         1            0

同样适用于DELETE 操作

See Demo


如果以后需要添加外键约束:

ALTER TABLE `childTable` ADD CONSTRAINT `fk_childTable_parent_id_status` FOREIGN KEY (
    `parent_id`,
    `status`
) REFERENCES `parentTable` (`ID`, `status`) ON DELETE CASCADE ON UPDATE CASCADE;

【讨论】:

    猜你喜欢
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    • 2019-07-12
    • 2021-03-31
    • 2019-12-23
    相关资源
    最近更新 更多