一尘不染

SQLSTATE [23000]:违反完整性约束:1452无法添加或更新子行:外键约束失败

mysql

我试图在我的注释表中插入值,但出现错误。俗话说,我不能添加或更新子行,我也不知道这意味着什么。我的架构如下所示:

--
-- Baza danych: `koxu1996_test`
--

-- --------------------------------------------------------

--
-- Struktura tabeli dla tabeli `user`
--

CREATE TABLE IF NOT EXISTS `user` (
  `id` int(8) NOT NULL AUTO_INCREMENT,
  `username` varchar(32) COLLATE utf8_bin NOT NULL,
  `password` varchar(64) COLLATE utf8_bin NOT NULL,
  `password_real` char(32) COLLATE utf8_bin NOT NULL,
  `email` varchar(32) COLLATE utf8_bin NOT NULL,
  `code` char(8) COLLATE utf8_bin NOT NULL,
  `activated` enum('0','1') COLLATE utf8_bin NOT NULL DEFAULT '0',
  `activation_key` char(32) COLLATE utf8_bin NOT NULL,
  `reset_key` varchar(32) COLLATE utf8_bin NOT NULL,
  `name` varchar(32) COLLATE utf8_bin NOT NULL,
  `street` varchar(32) COLLATE utf8_bin NOT NULL,
  `house_number` varchar(32) COLLATE utf8_bin NOT NULL,
  `apartment_number` varchar(32) COLLATE utf8_bin NOT NULL,
  `city` varchar(32) COLLATE utf8_bin NOT NULL,
  `zip_code` varchar(32) COLLATE utf8_bin NOT NULL,
  `phone_number` varchar(16) COLLATE utf8_bin NOT NULL,
  `country` int(8) NOT NULL,
  `province` int(8) NOT NULL,
  `pesel` varchar(32) COLLATE utf8_bin NOT NULL,
  `register_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `authorised_time` datetime NOT NULL,
  `edit_time` datetime NOT NULL,
  `saldo` decimal(9,2) NOT NULL,
  `referer_id` int(8) NOT NULL,
  `level` int(8) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `country` (`country`),
  KEY `province` (`province`),
  KEY `referer_id` (`referer_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=83 ;

我尝试执行的mysql语句如下所示:

INSERT INTO `user` (`password`, `code`, `activation_key`, `reset_key`, `register_time`,                `edit_time`, `saldo`, `referer_id`, `level`) VALUES (:yp0, :yp1, :yp2, :yp3, NOW(), NOW(), :yp4, :yp5, :yp6). Bound with :yp0='fa1269ea0d8c8723b5734305e48f7d46', :yp1='F154', :yp2='adc53c85bb2982e4b719470d3c247973', :yp3='', :yp4='0', :yp5=0, :yp6=1

我得到的错误看起来像这样:

SQLSTATE
[23000]:违反完整性约束:1452无法添加或更新子行:外键约束失败(koxu1996_testuser,CONSTRAINT
user_ibfk_1FOREIGN KEY(country)引用country_typeid)删除无操作,更新无操作)


阅读 576

收藏
2020-05-17

共1个答案

一尘不染

这仅表示在表 country_type 上不存在要插入的表注释上的列country值,或者没有在表 user
上插入country的值。请记住,表注释上的列country的值取决于表 country_type 上的ID的值。

2020-05-17