无法更新我的数据库中的子行(MySQL)

时间:2016-08-25 02:29:15

标签: mysql sql database-design foreign-keys

使用以下SQL语句..

INSERT INTO `debates` (`id`, `unit_id`, `starter_pack_id`, `academic_id`, `title`, `date_due`, `date_created`, `date_modified`) VALUES
(1, 1, 1, 5, 'On Campus', '2016-12-25 00:00:00', now(), NULL),
(2, 1, 1, 5, 'Off Campus', '2016-12-25 00:00:00', now(), NULL);

导致输出以下错误。

#1452 - Cannot add or update a child row: a foreign key constraint fails (`my_database`.`debates`, CONSTRAINT `fk_debates_starter_pack_id` FOREIGN KEY (`starter_pack_id`) REFERENCES `debate_starter_packs` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
  

为什么我无法更新此表?

请注意:以下是与此SQL语句关联的所有表。目前没有表格包含数据库中的任何数据。

辩论:

CREATE TABLE `debates`(
  `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `unit_id` INT(11) UNSIGNED NOT NULL,
  `starter_pack_id` TINYINT(3) UNSIGNED NOT NULL,
  `academic_id` INT(11) UNSIGNED NOT NULL,
  `title` VARCHAR(255) NOT NULL,
  `date_due` DATETIME NOT NULL,
  `date_created` DATETIME NOT NULL,
  `date_modified` DATETIME DEFAULT NULL,
  CONSTRAINT `pk_debates_id` PRIMARY KEY(`id`),
  CONSTRAINT `fk_debates_starter_pack_id` FOREIGN KEY(`starter_pack_id`) REFERENCES `debate_starter_packs`(`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `fk_debates_unit_id` FOREIGN KEY(`unit_id`) REFERENCES `units`(`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `fk_debates_user_role_id` FOREIGN KEY(`academic_id`) REFERENCES `user_roles`(`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8;

单位(Unit_ID参考):

CREATE TABLE `units` (
  `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `academic_id` int(11) UNSIGNED NOT NULL,
  `unit_code` varchar(6) NOT NULL,
  `title` varchar(100) DEFAULT NULL,
  `date_added` datetime NOT NULL,
  `date_modified` datetime DEFAULT NULL,
  CONSTRAINT `pk_units_id` PRIMARY KEY(`id`),
  CONSTRAINT `fk_units_academic_id` FOREIGN KEY (`academic_id`) REFERENCES `user_roles` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Starter Packs(Starter_Pack_ID参考):

CREATE TABLE `debate_starter_packs` (
  `id` tinyint(3) UNSIGNED NOT NULL AUTO_INCREMENT,
  `academic_id` int(11) UNSIGNED NOT NULL,
  `title` varchar(100) NOT NULL,
  `description` text NOT NULL,
  `date_created` datetime NOT NULL,
  `date_modified` datetime DEFAULT NULL,
  CONSTRAINT `pk_debate_starter_packs_id` PRIMARY KEY(`id`),
  CONSTRAINT `fk_debate_starter_packs_user_role_id` FOREIGN KEY (`academic_id`) REFERENCES `user_roles` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

用户(Academic_ID参考):

CREATE TABLE `users` (
  `id` int(11) UNSIGNED NOT NULL,
  `username` varchar(100) DEFAULT NULL,
  `first_name` varchar(50) DEFAULT NULL,
  `last_name` varchar(50) DEFAULT NULL,
  `email` varchar(100) NOT NULL,
  `password` varchar(255) NOT NULL,
  `salt` varchar(255) DEFAULT NULL,
  `organisation` varchar(100) DEFAULT NULL,
  `phone` varchar(20) DEFAULT NULL,
  `date_created` datetime NOT NULL,
  `date_last_login` datetime DEFAULT NULL,
  `remember_code` varchar(40) DEFAULT NULL,
  `active` tinyint(1) UNSIGNED NOT NULL,
  CONSTRAINT `pk_users_id` PRIMARY KEY(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

1 个答案:

答案 0 :(得分:3)

你有记录ID =' 1'在辩论表_starter_packs?

表辩论具有表辩论_starter_packs的外键引用ID。

CONSTRAINT `fk_debates_starter_pack_id` FOREIGN KEY(`starter_pack_id`) REFERENCES `debate_starter_packs`(`id`) ON DELETE CASCADE ON UPDATE CASCADE,

您应该插入记录ID =' 1'首先到辩论表_starter_packs。