2016-09-05 53 views
1

我有2個表MySQL的:所有的子記錄更改狀態,如果父母的狀態更新

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 

現在我想在兩個表之間設置一個關係,以便如果父表中的記錄狀態改變,那麼它的所有子r的狀態就會改變ow也應該得到改變。

我知道我可以用觸發器做到這一點,但我認爲應該用一些方法來處理多列的關係和FK約束。

+0

我猜'ON UPDATE CASCADE'會做。 – 1000111

+0

但我應該是什麼關鍵我不能在狀態上設置FK關係,因爲我必須將它與parent_id關聯起來,如何設置這兩列之間的關係。 – Arpita

+0

可能您需要創建共軛外鍵。讓我檢查一下 – 1000111

回答

0

您需要在參考parent_idstatuschild表中創建複合外鍵。

這裏有一個演示:

-- ---------------------------- 
-- 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; 
相關問題