This is my first time asking my question at stackoverflow. I’m working on a database project and I’m having some issues regarding inserting the data into my table. First here is the table that I created using this statement:
CREATE TABLE enrolls(
uno NUMBER(5),
eno NUMBER(5),
startTime DATE,
finishTime DATE,
CONSTRAINT enrolls_pk PRIMARY KEY(uno, eno),
FOREIGN KEY(uno) REFERENCES users(uno),
FOREIGN KEY (eno) REFERENCES exam(eno)
);`
Here is the insert statement:
INSERT INTO enrolls(uno,eno,startTime,finishTime)
VALUES
(1,1,to_date('2012/02/15 10:00:00', 'yyyy/mm/dd hh24:mi:ss'),
to_date('2012/02/22 10:00:00', 'yyyy/mm/dd hh24:mi:ss'));`
The error that I get is this:
"ORA-02291: integrity constraint (username.SYS_C0093024) violated - parent key
not found"
Based on a little research that I’ve done, the problem would be the way I put in the foreign key and I need two of them. How do I fix this error? What other way should I write two foreign keys for this? Please let me know. Thanks.
Edit1:
I used the CONSTRAINT on both of the foreign keys and when I inserted the same insert statement from above, it says that the error is coming from “uno”. Do you think there is something going on with the “users” table? Here is what I have for the users table:
CREATE TABLE users(
uno NUMBER(5),
email VARCHAR2(64),
password VARCHAR2(64),
fname VARCHAR2 (64) NOT NULL,
lname VARCHAR2 (64) NOT NULL,
address1 VARCHAR2 (64),
address2 VARCHAR2(64),
city VARCHAR2(64),
state VARCHAR2(64),
zip NUMBER(5),
CONSTRAINT users_pk PRIMARY KEY(uno)
);
One for exam:
CREATE TABLE exam(
eno NUMBER(5),
etitle VARCHAR2(50),
timeAllowed NUMBER (8),
numberOfQuestionsPerPage NUMBER(3),
CONSTRAINT exam_pk PRIMARY KEY(eno)
);
Is whatever that I did correct or am I doing something wrong here?
Edit2:
Alright, I think I figured out the problem. When I was checking my “users” table, apparently, the server from university that I’m using my SQL programming did not save the data that I’ve implemented in there. When I put the data again and implemented the the data for “enrolls”, it didn’t gave me an error. I think that was the problem to begin with. Also, how do I save my SQL stuff in UNIX so that this won’t happen again?
As far as I see, what is happening is that you are trying to insert foreign keys where the key does not exist in the other table(s). Is there a users row with a key of 1? Is there an exam row with a key of 1? If not for this error, referential integrity is broken.
Or, maybe you built the relationships wrong, in which case you need to remove one or both of the FOREIGN KEY constraint(s) from the enrolls table
UPDATE TO REFLECT YOUR UPDATE :):
If the error is coming from the Users table, then that means that the value you are trying to submit for uno in the enrolls table does not exist in the uno table. You will need to add that to the users table first:
Once this is done, you will now have a users record with an uno value of 1. Then your constraint will find the matching values and pass without error