简体   繁体   中英

insertion constraint by Foreign key due to referencing same table

I have a table employee which has ssn as primary key and another supr_ssn attribute which is a foreign key referencing ssn of employee table itself. So while inserting into table I get foreign key constraint because I have not inserted any value into ssn . Here is my table:

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

When I do insertion into table, with the code I get foreign key constraint:

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
)

You cannot enter a value to a foreign key if the primary key value doesn't exist. So, Enter the primary key values then try updating the foreign key value accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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