【问题标题】:Deleting from table with foreign keys使用外键从表中删除
【发布时间】:2012-11-21 03:29:06
【问题描述】:

我不确定我是否误解了这里的基本内容,但这里是:

I have a persons table (persons have the typical attributes for persons: names, address, etc.) and a table of elected representatives.代表具有其他属性(例如,副代表/替代代表或他们自己可以替代的人)。因为我有很多人,只有少数代表(n

representatives 
(numbers are the pk for persons, and empty cells here are NULL in the db)
_____________________________
|id|has_substitute|is_sub_for|
| 1|           2  |          |
| 2|              |        1 |
| 3|              |          |
| 5|              |          |

所以有一天,代表被剥夺了投票权,我需要将他从代表中删除,但不在人员表中(他仍然是一个人)。他是替补的人,或者曾经是他替补的人也是如此。它们在我的模式中是 FK,但我不想将它们作为人删除,只是与出去的代表的关系。只是代表表中的行。

DELETE FROM representatives WHERE id=1;

一切都错了。 '无法删除或更新父行:外键约束失败' 但我不打算删除人员,只是用“代表”表创建的关系。

  1. mysql 是否有DELETE FROM 并忽略关系(只删除行)-函数

  2. 我的建模错了吗?如果是这样,有什么更好的方法?

PS:这里是创建表信息

CREATE TABLE `representatives` (
  `person_id` varchar(33) NOT NULL,
  `permanent_substitute_for_id` varchar(33) DEFAULT NULL,
  `temporarily_substitute_for_id` varchar(33) DEFAULT NULL,
  PRIMARY KEY (`person_id`),
  KEY `representatives_250f5a24` (`permanent_substitute_for_id`),
  KEY `representatives_79c95594` (`temporarily_substitute_for_id`),
  CONSTRAINT `permanent_substitute_for_id_5c64807b` FOREIGN KEY (`permanent_substitute_for_id`) REFERENCES `persons` (`id`),
  CONSTRAINT `person_id_refs_id_5c64807b` FOREIGN KEY (`person_id`) REFERENCES `persones` (`id`),
  CONSTRAINT `temporarily_substitute_for_id_5c64807b` FOREIGN KEY (`temporarily_substitute_for_id`) REFERENCES `persones` (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `persones` (
  `id` varchar(33) NOT NULL,
  `first_name` varchar(150) NOT NULL,
  ..[more stuff]..
  PRIMARY KEY (`id`),
  KEY `fylkesperspektiv_personer_70fdfe4` (`fylke_id`),
  KEY `fylkesperspektiv_personer_3ab19c51` (`parti_id`),
  CONSTRAINT `fylke_id_refs_id_36bce012` FOREIGN KEY (`fylke_id`) REFERENCES `fylkesperspektiv_fylker` (`id`),
  CONSTRAINT `parti_id_refs_id_c381e045` FOREIGN KEY (`parti_id`) REFERENCES `fylkesperspektiv_partier` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

【问题讨论】:

标签: mysql foreign-keys delete-row


【解决方案1】:

错误消息表明您将persons.representative_id 作为representative.id 的外键,并带有ON DELETE RESTRICT 选项(默认值)。您可能需要ON DELETE SET NULL,这样您就可以删除代表并保留人员数据。

或者,您可以将representative.person_id 作为person.id 的外键。

【讨论】:

    【解决方案2】:

    我会为每个人定义一个rep_id 列并将其与rep 表联系起来:

    ALTER TABLE person
        ADD CONSTRAINT FK_person_rep
        FOREIGN KEY (rep_id) REFERENCES rep(rep_id)
        ON UPDATE CASCADE ON DELETE SET NULL;
    

    我也不会使用 rep 表中的 redundant has_substitute 列,而只使用 is_sub_for,然后将其绑定到同一张表中的 rep_id

    ALTER TABLE rep
    ADD CONSTRAINT FK_rep_rep
    FOREIGN KEY (is_sub_for) REFERENCES rep(rep_id)
    ON DELETE CASCADE ON UPDATE CASCADE;
    

    这样,当一个代表被删除时,他们所有的成员的 rep_id 列都被更新为 NULL,并且他们在同一个表中的 subs 被删除。如果 rep_id 发生了变化(不应该发生,但以防万一),更新会级联到两个表中的子表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-26
      • 2019-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多