简体   繁体   English

mysql-外键级联更新

[英]mysql - foreign key cascade update

I have a question about how to update a field on cascade with a second field as constraint. 我有一个关于如何使用第二个字段作为约束的级联更新字段的问题。

The structure is this (I removed the unnecessary columns): 结构是这样的(我删除了不必要的列):

Table nodes with columns idNode and idDimension (together they form the primary key). 具有idNodeidDimension列的表nodes (它们一起构成主键)。

Table forces with columns idForce (PK), idNode (foreign key to nodes . idNode ) and idDimension . forces使用idForce (PK), idNodenodes外键idNode )和idDimension

Cascade update and delete on everything. 级联更新和删除所有内容。

The problem in this structure that it seems to appear is this: 这种结构中似乎出现的问题是:

If in nodes I have an entry like (1, 1) and one like (1, 2) and in forces (1, 1, 1) and (1, 1, 2) and I update or delete first entry from nodes both entries in forces will be affected. 如果在nodes我有一个条目(1,1)和一个类似(1,2)且在forces (1,1,1)和(1,1,2)中并且我从nodes更新或删除了第一个条目在forces将受到影响。

I need to affect only the one that also has the corresponding idDimension. 我只需要影响也具有相应idDimension的那个。 How can I modify current structure to do that? 我该如何修改当前结构?

Edit: Tables - Nodes: 编辑:表格-节点:

CREATE TABLE IF NOT EXISTS `nodes` (
  `idNode` varchar(11) NOT NULL,
  `idDimension` int(10) unsigned NOT NULL,
  `idNetwork` int(10) unsigned NOT NULL DEFAULT '0',
  `level` int(11) unsigned NOT NULL DEFAULT '0',
  `energy` bigint(20) DEFAULT NULL,
  `resources` bigint(20) unsigned NOT NULL DEFAULT '0',
  `x` int(11) NOT NULL,
  `y` int(11) NOT NULL,
  `name` varchar(20) DEFAULT NULL,
  `order` tinyint(3) DEFAULT '0' COMMENT 'energy 0\nassemble 1\nupgrade 2',
  `core` tinyint(1) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`idNode`,`idDimension`),
  KEY `network_dimension` (`idDimension`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='Node table';

Forces: 军队:

 CREATE TABLE IF NOT EXISTS `forces` (
  `idForce` bigint(10) unsigned NOT NULL AUTO_INCREMENT,
  `idNode` varchar(11) NOT NULL,
  `idDimension` int(10) unsigned NOT NULL,
  `drones` bigint(20) DEFAULT NULL,
  `stance` tinyint(3) DEFAULT NULL COMMENT '0 - defense\n1 - neutral\n2 - attack      \n\nIf planet is parano and you are not allied to owner you can only be in attack.\n\nIf owner is allied you can only be in defense or neutral.\n\nIf you are owner you can only be in defense.',
   `order` tinyint(3) DEFAULT '0' COMMENT 'extract energy 1\nbuild node 2\nreplicate 3\nmove 4',
  `value` text,
   PRIMARY KEY (`idForce`),
   KEY `idNode` (`idNode`,`idDimension`),
   KEY `idDimension` (`idDimension`)
  ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

-- -

-- Constraints for table forces -约束表forces

  ALTER TABLE `forces`
    ADD CONSTRAINT `forces_ibfk_2` FOREIGN KEY (`idDimension`) REFERENCES `nodes`  (`idDimension`) ON DELETE CASCADE ON UPDATE CASCADE,
    ADD CONSTRAINT `fk_forces_nodes1` FOREIGN KEY (`idNode`, `idDimension`) REFERENCES `nodes` (`idNode`, `idDimension`) ON DELETE NO ACTION ON UPDATE NO ACTION,
    ADD CONSTRAINT `forces_ibfk_1` FOREIGN KEY (`idNode`) REFERENCES `nodes` (`idNode`) ON DELETE CASCADE ON UPDATE CASCADE;

My constraints are not working as I would like so feel free to ignore them :). 我的约束无法正常运行,请随时忽略它们:)。

There are two strange foreign key ( forces_ibfk_1 and forces_ibfk_2 ) which refers to non unique fields. 有两个奇怪的外键( forces_ibfk_1forces_ibfk_2 ),它们引用非唯一字段。 Remove them - 删除它们-

ALTER TABLE forces DROP FOREIGN KEY forces_ibfk_1;
ALTER TABLE forces DROP FOREIGN KEY forces_ibfk_2;

Then recreate fk_forces_nodes1 that refers to unique pair of fields with CASCADE action option - 然后使用CASCADE操作选项重新创建引用唯一字段对的fk_forces_nodes1

ALTER TABLE forces
  DROP FOREIGN KEY fk_forces_nodes1;

ALTER TABLE orces
  ADD CONSTRAINT fk_forces_nodes1 FOREIGN KEY (idNode, idDimension)
    REFERENCES nodes(idNode, idDimension) ON DELETE CASCADE ON UPDATE CASCADE;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM