繁体   English   中英

由于引用同一张表,因此通过外键进行插入约束

[英]insertion constraint by Foreign key due to referencing same table

我有一个表ssn作为主键的雇员表,另一个supr_ssn属性是一个引用雇员表本身的ssn的外键。 因此,在插入表时,我得到了外键约束,因为我没有在ssn中插入任何值。 这是我的桌子:

create table employee(
    Fname varchar(15) not null,
    Minit char,
    Lname Varchar(15) not null,
    ssn char(9) not null,
    Bdate Date,
    Address varchar(30),
    sex char,
    Salary decimal(10,2),
    super_ssn char(9),
    Dno int not null,
    primary key(ssn),
    foreign key(super_ssn)references employee(ssn),
    foreign key(dno)references department(Dnumber)
)

当我插入表时,通过代码我得到了外键约束:

insert into sample.employee(
    Fname, Minit, Lname, ssn, Bdate,
    Address, sex, salary, Dno
)
values(
    'John', 'B', 'Smith', '123456789', 01/09/1965,
    '731 fondren,housten', 'M', 30000, '333445555', 5
)

如果主键值不存在,则不能为外键输入值。 因此,输入主键值,然后尝试相应地更新外键值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM