Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.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
Mysql 无法添加或更新子行:尝试添加新行时外键约束失败_Mysql_Sql_Phpmyadmin_Insert - Fatal编程技术网

Mysql 无法添加或更新子行:尝试添加新行时外键约束失败

Mysql 无法添加或更新子行:尝试添加新行时外键约束失败,mysql,sql,phpmyadmin,insert,Mysql,Sql,Phpmyadmin,Insert,我当前正在尝试在表中添加一个新条目。在这个表中,我对users表(用户id)有一个约束。在某些情况下,我的用户id还不能设置。如果它是空的,我会得到一个错误: Cannot add or update a child row: a foreign key constraint fails 以下是我设置约束的方式: 那么,有没有办法将空值插入约束字段?如果否,除了移除约束,最好的解决方案是什么 那么,有没有办法将空值插入约束字段 是的,有。只要外键控制的列未定义为notnull,就可以在其中插

我当前正在尝试在表中添加一个新条目。在这个表中,我对users表(用户id)有一个约束。在某些情况下,我的用户id还不能设置。如果它是空的,我会得到一个错误:

Cannot add or update a child row: a foreign key constraint fails
以下是我设置约束的方式:

那么,有没有办法将空值插入约束字段?如果否,除了移除约束,最好的解决方案是什么

那么,有没有办法将空值插入约束字段

是的,有。只要外键控制的列未定义为
notnull
,就可以在其中插入
NULL
值,如中所述。不允许插入父表中不存在的非空值(包括空字符串!)

MySQL基本上实现了
MATCH SIMPLE
定义的语义,它允许外键全部或部分为
NULL
。在这种情况下,允许插入包含此类外键的(子表)行,并且该行与引用的(父表)表中的任何行都不匹配

考虑一下这一点:

-- parent table
create table parent (id int primary key);

-- child table
create table child (
    id int primary key, 
    parent_id int null,  -- allow `NULL` values
    constraint parent_id_fk foreign key(parent_id) references parent(id)
);

-- create a parent record
insert into parent(id) values(1);

-- insert a child record that references the parent: ok
insert into child(id, parent_id) values(1, 1);

-- insert a child record with a NULL parent_id : ok
insert into child(id, parent_id) values(2, NULL);

-- insert a child record with a (non-NULL) unknown parent_id
insert into child(id, parent_id) values(3, 2);
-- Error: Cannot add or update a child row: a foreign key constraint fails 
那么,有没有办法将空值插入约束字段

是的,有。只要外键控制的列未定义为
notnull
,就可以在其中插入
NULL
值,如中所述。不允许插入父表中不存在的非空值(包括空字符串!)

MySQL基本上实现了
MATCH SIMPLE
定义的语义,它允许外键全部或部分为
NULL
。在这种情况下,允许插入包含此类外键的(子表)行,并且该行与引用的(父表)表中的任何行都不匹配

考虑一下这一点:

-- parent table
create table parent (id int primary key);

-- child table
create table child (
    id int primary key, 
    parent_id int null,  -- allow `NULL` values
    constraint parent_id_fk foreign key(parent_id) references parent(id)
);

-- create a parent record
insert into parent(id) values(1);

-- insert a child record that references the parent: ok
insert into child(id, parent_id) values(1, 1);

-- insert a child record with a NULL parent_id : ok
insert into child(id, parent_id) values(2, NULL);

-- insert a child record with a (non-NULL) unknown parent_id
insert into child(id, parent_id) values(3, 2);
-- Error: Cannot add or update a child row: a foreign key constraint fails