Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t在MySQL数据库上添加约束_Mysql - Fatal编程技术网

Can';t在MySQL数据库上添加约束

Can';t在MySQL数据库上添加约束,mysql,Mysql,我想用以下代码在MySQL数据库中添加一个约束: ALTER TABLE `artikelen` ADD CONSTRAINT `fk_art_grp` FOREIGN KEY (`groepsid`) REFERENCES `groepen`( `groepsid`) ON DELETE RESTRICT ON UPDATE CASCADE; 两列的属性相同。 类型:int(10) 排序规则:空 属性:无符号 空:没有 默认值:无 但我得到了以下错误: #1452 - Cann

我想用以下代码在MySQL数据库中添加一个约束:

ALTER TABLE `artikelen` 
  ADD CONSTRAINT `fk_art_grp` FOREIGN KEY (`groepsid`) REFERENCES `groepen`( 
  `groepsid`) ON DELETE RESTRICT ON UPDATE CASCADE; 
两列的属性相同。 类型:int(10) 排序规则:空 属性:无符号 空:没有 默认值:无

但我得到了以下错误:

#1452 - Cannot add or update a child row: a foreign key constraint fails (`bjornm_Keuringsmiddelen`.`#sql-14c6_138c51`, CONSTRAINT `fk_art_grp` FOREIGN KEY (`groepsId`) REFERENCES `groepen` (`GroepsId`) ON UPDATE CASCADE)

我做错了什么?

artikelen
中的
groepsId必须有一个值,该值不在
groepen
列中


在添加任何约束之前,您必须确保所有行都符合该约束。

请向我们展示table
groepen
create table
语句。以下是我用来创建table groepen的代码
CREATE TABLE
groepen`(
GroepsId
int(10)UNSIGNED NOT NULL,
Groepsnaam
varchar(255)NOT NULL)ENGINE=InnoDB DEFAULT CHARSET=latin1`您的表
artikelen
是否可能不是空的,并且
groepsId
的值不在
groepen
表中?在添加任何约束之前,您必须确保所有行都符合该约束!在表
artikelen
groepsId
列的记录中,有两个值不在表
groepen
groepsId列中。感谢今天的学习课!不客气!我为以后访问这个问题添加了答案。