Error question 'no parent key' when inserting oracle sqlplus table data.

Asked 2 years ago, Updated 2 years ago, 114 views

I am currently studying oracle sqlplus.

I'm asking you a question because an error occurred when inserting data after creating a table in the beginning while looking at the book.

    DEPTNO DNAME                        LOC
---------- ---------------------------- --------------------------
        10 ACCOUNTING                   NEW WORK
        20 RESEARCH                     DALLAS
        30 SALES                        CHICAGO
        40 OPERATIONS                   BOSTON

Above is the def table

create table emp(
empno    number(4)
constraint pk_emp primary key,
ename    varchar2(10),
job      varchar2(9),
mgr      number(4)
    constraint fk_mgr references emp(empno),
hiredate date,
sal      number(7,2),
comm     number(7,2),
deptno   number(2)
    constraint fk_deptno references emp(empno));

The emp table was created in the above format.

insert into emp values (7839,'KING','PRESIDENT',NULL,
  2     to_date ('17-11-1981','dd-mm-yyyy'),5000,NULL,10);
insert into emp values (7839,'KING','PRESIDENT',NULL,
*
Error in line 1:
ORA-02291: Integrity constraint (SYS.FK_DEPTNO) is violated - parent key not present

This is how the error occurs when entering data.

I don't know what the parent's height and the child's height is, and I'm embarrassed to see an error at the beginning of the book without any explanation.

I can't find a solution even if I search for it, so I'm asking you this question.

I'd appreciate it if you could tell me how to solve it.

oracle dbms database

2022-09-20 15:49

1 Answers

Use constraints to maintain data consistency in a relational database (RDB). When creating employee table (EMP), constrain was used for company number (DEPT_NO), and when trying to register with department code 10 for data INSERT in the EMP table, data with DEPT_NO of 10 is found internally in the DEPT table. The INSERT SQL statement looks fine. Please delete the emp table and create it as below. The def_no construct was created incorrectly.

create table emp( empno number(4) constraint pk_emp primary key, ename varchar2(10), job varchar2(9), mgr number(4) constraint fk_mgr references emp(empno), hiredate date, sal number(7,2), comm number(7,2), deptno number(2) constraint fk_deptno references dept(deptno));

Delete table is drop table emp.


2022-09-20 15:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.