【问题标题】:error code :1452 while inserting elements in table错误代码:1452 在表格中插入元素时
【发布时间】:2015-11-25 07:47:15
【问题描述】:

下面是我的桌子-

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'
)

这是我得到的错误-

错误代码:1452。无法添加或更新子行:外键 约束失败 (employee.employee, CONSTRAINT employee_ibfk_1 外键 (superssn) 引用 employee (ssn)

【问题讨论】:

  • 删除了 SQL Server 标记,因为此查询和错误来自 MySQL

标签: mysql


【解决方案1】:

错误很清楚,这意味着您正试图将superssn 中不存在的superssn 值插入employee

来自docs

外键关系涉及一个父表,该表包含 中心数据值,以及具有相同值指向的子表 回到它的父级。 FOREIGN KEY 子句在子句中指定 表。

它将拒绝任何尝试创建的 INSERT 或 UPDATE 操作 如果没有匹配项,则子表中的外键值 父表中的候选键值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-17
    • 2016-02-17
    • 1970-01-01
    • 2018-09-18
    • 2017-01-03
    • 2017-06-20
    • 1970-01-01
    • 2014-11-21
    相关资源
    最近更新 更多