gpt4 book ai didi

mysql - 错误代码 :1452 while inserting elements in table

转载 作者:行者123 更新时间:2023-11-29 02:54:23 24 4
gpt4 key购买 nike

下面是我的表格-

create table employee1(
fname varchar(15) not null,
minit char(15) ,
lname varchar(15) not null,
ssn char(9) not null,
bdate date,
address varchar(30),
sex char,
salary decimal(10,2),
superssn char(9),
dno int not null default 1,
constraint emppk
primary key (ssn),
constraint empsupkerfk
foreign key (superssn) references employee1(ssn) on delete set null on update cascade,
constraint empdeptfk
foreign key (dno) references department(dnumber) on delete set default on update cascade
);

这是我的插入语句-

insert into employee1 
(
fname,minit,lname,ssn,bdate,address,sex,salary,superssn,dno
)
values (
'jhon','b','smith','123456789','1955-01-09','731 fondren,houston, tx','m','30000','33344555','5'
)

这是我得到的错误-

Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (employee.employee, CONSTRAINT employee_ibfk_1 FOREIGN KEY (superssn) REFERENCES employee (ssn)

最佳答案

错误很明显,这意味着您正试图向 employee1 中插入一个 superssn 值,而该值在 employee 中不存在。

来自docs :

Foreign key relationships involve a parent table that holds the central data values, and a child table with identical values pointing back to its parent. The FOREIGN KEY clause is specified in the child table.

It will reject any INSERT or UPDATE operation that attempts to create a foreign key value in a child table if there is no a matching candidate key value in the parent table.

关于mysql - 错误代码 :1452 while inserting elements in table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32308476/

24 4 0