ORA-02291: integrity constraint violated - parent key not found - oracle
SO here is my Database:
CREATE TABLE courses (
ccode VARCHAR2(10) CONSTRAINT courses_PK PRIMARY KEY,
cname VARCHAR2(50),
coursefee NUMBER(6)
);
CREATE TABLE faculty (
fcode VARCHAR2(5) CONSTRAINT faculty_PK PRIMARY KEY,
name VARCHAR2(50)
);
CREATE TABLE batches (
bcode varchar2(5) CONSTRAINT batches_PK PRIMARY KEY,
ccode varchar2(5) CONSTRAINT batches_ccode_FK REFERENCES COURSES(ccode),
fcode varchar2(5) CONSTRAINT batches_fcode_FK REFERENCES FACULTY(fcode),
stdate date CONSTRAINT batches_stdate_nn not null,
enddate date,
timing number(1) CONSTRAINT batches_timing_chk check( timing in (1,2,3) ),
CONSTRAINT batches_date_chk check ( stdate <= enddate)
);
CREATE TABLE students (
rollno number(5) CONSTRAINT students_PK PRIMARY KEY,
bcode varchar2(5) CONSTRAINT students_bcode_FK REFERENCES batches(bcode),
name varchar2(30),
gender char(1) CONSTRAINT students_gender_chk check( upper(gender) in ('M','F')),
dj date,
phone varchar2(10),
email varchar2(30)
);
Insert Table:
INSERT INTO batches VALUES('A0001','A0016','A0031','08-05-13','08-06-17',1);
INSERT INTO batches VALUES('B0002','E0017','B0032','08-05-13','08-06-26',2);
INSERT INTO batches VALUES('C0003','C0018','C0033','08-06-27','08-06-28',2);
INSERT INTO batches VALUES('D0004','D0019','D0034','08-06-27','08-08-11',3);
INSERT INTO batches VALUES('E0005','E0020','E0035','08-07-01','08-08-09',2);
INSERT INTO batches VALUES('F0006','F0021','F0036','08-07-21','08-08-11',1);
INSERT INTO batches VALUES('G0007','V0022','G0037','08-08-31','08-09-21',3);
INSERT INTO batches VALUES('H0008','H0023','H0038','08-09-17','08-10-14',3);
INSERT INTO batches VALUES('I0009','I0024','I0039','08-09-23','08-10-20',2);
INSERT INTO batches VALUES('J0010','P0025','J0040','08-10-04','08-11-26',1);
INSERT INTO batches VALUES('K0011','F0026','K0041','08-10-10','08-11-17',2);
INSERT INTO batches VALUES('L0012','C0027','L0042','08-11-02','09-02-23',2);
INSERT INTO batches VALUES('M0013','M0028','M0043','08-12-09','09-01-02',3);
INSERT INTO batches VALUES('N0014','N0029','N0044','08-12-11','09-01-08',3);
INSERT INTO batches VALUES('O0015','O0030','O0045','08-12-15','09-01-12',2);
INSERT INTO students VALUES(001,'A0001','Andrew Johnson','M','08-01-10','677-0323','AndyJ#hotmail.com');
INSERT INTO students VALUES(002,'B0002','Dionne Peterson','F','08-01-10','324-9705','dionnepet#gmail.com');
INSERT INTO students VALUES(003,'C0003','Sydney Peters','F','08-01-10','624-9664','SydersP#yahoo.com');
INSERT INTO students VALUES(004,'D0004','Tsian Figaro','F','08-09-01','310-4957','tsianfigaro#hotmail.com');
INSERT INTO students VALUES(005,'E0005','Jewel Forbes','F','08-09-01','666-5948','Jewel#gmail.com');
INSERT INTO students VALUES(006,'F0006','Jason Fitzpatrick','M','08-01-02','657-4958','JasonFitz#hotmail.com');
INSERT INTO students VALUES(007,'G0007','Ali Maharaj','M','08-01-02','674-4437','AliMaharaj#gmail.com');
INSERT INTO students VALUES(008,'H0008','Mariah Sunderstone','F','08-01-02','707-4056','mariahS#yahoo.com');
INSERT INTO students VALUES(009,'I0009','Ariah Baron','F','08-01-02','673-9685','AriBar#yahoo.com');
INSERT INTO students VALUES(010,'J0010','Joe Zheng ','M','08-09-01','348-4567','Zhengdo#hotmail.com');
INSERT INTO students VALUES(011,'K0011','Donald Yen','M','08-09-01','328-4749','DonnieYen#gmail.com');
INSERT INTO students VALUES(012,'L0012','Jackson Chang','M','08-08-27','345-6847','JackieChang#yahoo.com');
INSERT INTO students VALUES(013,'M0013','Scott Smith','M','08-01-02','702-9903','Scottsmith#hotmail.com');
INSERT INTO students VALUES(014,'N0014','Michelle Shareef','F','08-02-27','321-0589','michelleShareef#gmail.com');
INSERT INTO students VALUES(015,'O0015','Jean Louis Pierre ','M','08-09-01','428-8378','JLPierre#gmail.com');
The error that keeps coming up for batches Table is :
ORA-02290: check constraint (EJAY.BATCHES_DATE_CHK) violated
ANd the error that comes up for students Table is:
ORA-02291: integrity constraint violated - parent key not found
What I'm confused about is that it only shows up for some rows in the tables. faculty and courses are fine, it's just these two.
If the answers is obvious, I apologize, I'm a beginner. Please help
Insert in Batches is failing because you have your dates mixed up; they're dd-mm-yy.
For example, INSERT INTO batches VALUES('D0004','D0019','D0034','08-06-27','08-08-11',3); fails the test stdate <= enddate.
The insert in Students is failing because you're referring to a value in Batches that does not exist (because the inserts failed?).
Related
Alter Table , adding a Foreign key constraint at a column ORA-02253
Hello there i am studying for the Oracle Certification of SQL Associate . And trying to do some Examples . I have an issue where i cannot find easily a reference on this . create table employees (employee_id number NOT NULL, first_name varchar(20), last_name varchar(30), constraint employee_pk primary key (employee_id)); create table employee_notes (employee_notes_id number, employee_notes varchar(500), constraint pk_employee_notes primary key (employee_notes_id)); create sequence employee_notes_seq start with 1 increment by 1 Now i want to add a new column at employee_notes table with a foreign key constraint . I can't find out in syntax where is the problem . ****alter table employee_notes add employee_id number constraint fk_employee_notes foreign key (employee_id) references employees (employee_id);**** i get this error ORA-02253: constraint specification not allowed her I also tried to alter the table and add column and then the constraint but cannot alter table employee_notes add employee_id number; -- alter table employee notes add constraint fk_employee_notes foreign key (employee_id) references employees (employee_id); ORA-02253: constraint specification not allowed here I would like to know how i can do this and why this syntax is wrong :)
You did something wrong because - it works OK: SQL> CREATE TABLE employees 2 ( 3 employee_id NUMBER NOT NULL, 4 first_name VARCHAR (20), 5 last_name VARCHAR (30), 6 CONSTRAINT employee_pk PRIMARY KEY (employee_id) 7 ); Table created. SQL> SQL> CREATE TABLE employee_notes 2 ( 3 employee_notes_id NUMBER, 4 employee_notes VARCHAR (500), 5 CONSTRAINT pk_employee_notes PRIMARY KEY (employee_notes_id) 6 ); Table created. SQL> ALTER TABLE employee_notes ADD employee_id NUMBER; Table altered. SQL> ALTER TABLE employee_notes ADD CONSTRAINT fk_employee_notes 2 FOREIGN KEY (employee_id) 3 REFERENCES employees (employee_id); Table altered. SQL>
When you use ALTER TABLE ... ADD in order to add a column and a constraint in one statement, do the following: -- notice the () and the comma! alter table employee_notes add ( employee_id number , constraint fk_employee_notes foreign key (employee_id) references employees (employee_id) ) ; That should do the trick. See dbfiddle. The syntax is similar to CREATE TABLE, where you'd also write all column names, data types etc in (), separated by commas.
Error "no data foun" on insert in my trigger ORACLE
I've three tables: create table person( id_per number(1) primary key not null, name_per varchar(15) not null); create table training( id_training number(1) primary key not null, start_training date not null, final_training date not null); create table training_person( id_tp number(3) primary key not null, id_per number(8) not null, id_training number(1) not null); I created a trigger whose function is verify that the user can't to insert in the table training_person if the user have a active training ... but when I want to insert a new register but the person don't have any training registered before, oracle show me the following error: "no data found". This is my trigger: CREATE OR REPLACE TRIGGER VERIFY_TRAINING BEFORE INSERT OR UPDATE ON training_person FOR EACH ROW DECLARE GET_FINAL_TRAINING DATE; GET_START_TRAINING DATE; BEGIN select MAX(final_training) into GET_FINAL_TRAINING from training t join training_person x on t.id_training=x.id_training join person e on e.id_per=x.id_per WHERE e.id_per=:new.id_per; select start_training into GET_START_TRAINING from training t join training_person x on t.id_training=x.id_training where t.id_training=:new.id_training; IF (GET_FINAL_TRAINING > GET_START_TRAINING) THEN RAISE_APPLICATION_ERROR(-20091,'U CANT INSERT TRAINING.'); END IF; EXCEPTION WHEN NO_DATA_FOUND THEN INSERT INTO training_person values(:new.id_tp,:new.id_per,:new.id_training); END; I'm learning Oracle, so I dunno which is the problem. Thanks.
Triggers in oracle PL/SQL
I am trying to do a trigger that updates a sum of all credits on update of the grade or a insert of a new one. That's my tables create table Department ( Department_ID varchar(4) not null, Name varchar(25) unique, Department_Head_ID varchar(9), College_ID varchar(4), Credits_To_Degree NUMBER(3), constraint pkDepartment primary key (Department_ID)); create table Enrollment ( Student_ID varchar(9) not null, Course_ID varchar(5) not null, Registered_Date date,Grade NUMBER, Status varchar(4),constraint pkEnrollment primary key (Student_ID, Course_ID)); create table Student ( Student_ID varchar(9) not null, First_Name varchar(25), Last_Name varchar(25), Phone char(11), Birth_Date date, Street varchar(100), Zip_Code char(5), Department varchar(4), Credits integer, Eligible char(4), constraint pkStudent primary key (Student_ID),constraint fkDeptId foreign key (Department) references Department(Department_ID)); ALTER TABLE Department ADD FOREIGN KEY (Department_Head_ID) REFERENCES Faculty(Faculty_Id) INITIALLY DEFERRED; ALTER TABLE Department ADD FOREIGN KEY(College_ID) REFERENCES College(College_ID) INITIALLY DEFERRED; the trigger: create or replace TRIGGER Credits after INSERT OR UPDATE ON enrollment REFERENCING NEW AS New OLD AS Old FOR EACH ROW BEGIN UPDATE STUDENT SET CREDITS=(SELECT SUM(c.CREDITS) FROM COURSE c,ENROLLMENT e WHERE c.COURSE_ID = e.COURSE_ID and e.STUDENT_ID = :new.student_id and e.GRADE>= 60 ) WHERE STUDENT.STUDENT_ID=:new.student_id; END; The trigger compiles but when i change value of the grade a get this error UPDATE "FELIX"."ENROLLMENT" SET GRADE = '60' WHERE ROWID = 'AAAGUSAABAAALKJAAF' AND ORA_ROWSCN = '3540016' ORA-20003: An error has occurred while performing credits trigger ORA-06512: at "FELIX.CREDITS", line 5 ORA-04088: error during execution of trigger 'FELIX.CREDITS' One error saving changes to table "FELIX"."ENROLLMENT": Row 6: ORA-20003: An error has occurred while performing credits trigger ORA-06512: at "FELIX.CREDITS", line 5 ORA-04088: error during execution of trigger 'FELIX.CREDITS' line 5 is a FOR EACH ROW command I figure out that the problem is in the :new.student_id. But how could i get the id of the row that the triggers fires upon. Thanks in advance for the help.
Employee/History - Part of composite key as foreign key
I've got 2 entities: 1) EMPLOYEES (Parent) CREATE TABLE EMPLOYEES ( employee_id NUMBER (3) NOT NULL, first_name VARCHAR (20) NOT NULL, last_name VARCHAR (20) NOT NULL, job_title VARCHAR (20) NOT NULL, employee_type VARCHAR (1) NOT NULL, salary NUMBER (5), hourly_pay NUMBER (5,2), bonus_pay NUMBER (5,2), CONSTRAINT employee_pk PRIMARY KEY(employee_id)); 2) EMPLOYEE_HISTORY (Child) CREATE TABLE EMPLOYEE_HISTORY ( start_date DATE NOT NULL, employee_id NUMBER (3) NOT NULL, end_date DATE, job_title VARCHAR (10) NOT NULL, hourly_rate NUMBER (5,2) NOT NULL, CONSTRAINT employee_history_pk PRIMARY KEY(start_date, employee_id)); I'm trying to create: ALTER TABLE employee_history ADD CONSTRAINT employee_history_fk FOREIGN KEY (employee_id) REFERENCES employee_history(employee_id); When I do this, I get an error ORA-02270: no matching unique or primary key for this column-list My guess is that I cannot create the constraint on just employee_id because I have a composite key in my child table. I understand when an employee gets put into the database, the parent table is filled out and the "start date" should be filled out along with everything else. However, I do not understand how this would work if I had start_date in my parent table as well. I would be able to create my constraint, yes, but how will I be able to keep a record of changes in start_date if my start_date was inputted at the time of when the employee was entered into the database.I thought about using job_title as a primary key instead of start_date because it's present in both tables, but what happens when an employee gets promoted and demoted again? Won't a duplicate value constraint come up when the same employee_id and job_title is getting inserted?
Your references clause needs to reference the parent table. Not the child table ALTER TABLE employee_history ADD CONSTRAINT employee_history_fk FOREIGN KEY (employee_id) REFERENCES employee(employee_id); -- employee not employee_history The SQL you posted is trying to create a self-referential foreign key where employee_history is both the parent and the child. That doesn't make sense in this case.
Why it tell "invalid identifier (ORA-00904)"? when I call function in CONSTRAINT CHECK
This is some kind of study database in sql file. I use oracle 11g and process it in sqlplus. I wrote two function for check the course number and the department number, so its depend on the major and the minor department of each student. For example, I am student of CS department (major) and BIO department (minor), so I can not enroll the course which is about math department. When I call it in CHECK but I don't know why it told that. This is output when I create all table, (from sqlplus) .... .... .... table created. ALTER TABLE sections ADD CONSTRAINT CK_course_depart CHECK (FindMYDeparture(stuid,find_dno_from_cno(cno)) = 'true'); ERROR at line 1: ORA-0094: "FIND_DNO_FROM_CNO":invalid identifier This is in .sql file DROP TABLE department CASCADE CONSTRAINTS; CREATE TABLE department ( dnumber number(4) not null, dname varchar(25) not null, primary key (dnumber) ); DROP TABLE courses CASCADE CONSTRAINTS; CREATE TABLE courses ( cno number(4) not null, cname varchar(15) not null, credit number(1) not null, dnumber number(4) not null, primary key (cno), foreign key (dnumber) references department(dnumber), CONSTRAINT credits CHECK (credit > 0 AND credit <= 5) ); DROP TABLE student CASCADE CONSTRAINTS; CREATE TABLE student ( stuid char(9) not null, fname varchar(15) not null, lname varchar(15) not null, dMjno number(4) not null, dMnno number(4), primary key (stuid), CONSTRAINT depart_M_n CHECK (dMjno <> dMnno), CONSTRAINT dMinor_check CHECK (dMnno = 1 OR dMnno = 2 OR dMnno = 3) ); DROP TABLE sections CASCADE CONSTRAINTS; CREATE TABLE sections ( sno number(4) not null, cno number(4) not null, stuid char(9) not null, semester varchar(6) not null, year varchar(4) not null, instructor varchar(15) not null, CONSTRAINT combine_pk primary key (sno,stuid), foreign key (cno) references courses(cno), foreign key (stuid) references student(stuid), CONSTRAINT cant_enroll CHECK (semester <> 'Spring' AND year <> 2007) ); DROP TABLE grading CASCADE CONSTRAINTS; CREATE TABLE grading ( sno number(4) not null, stuid char(9) not null, grade numeric(1,2), foreign key (sno,stuid) references sections(sno,stuid), foreign key (stuid) references student(stuid), CONSTRAINT grading_check CHECK (grade >= 0 AND grade <= 4) ); DROP FUNCTION FindMYDeparture; CREATE OR REPLACE FUNCTION FindMYDeparture(stuid_in IN char,depart_course IN NUMBER) RETURN NUMBER AS departMa_no NUMBER; departMi_no NUMBER; report varchar(10); CURSOR cdno is SELECT dMjno,dMnno FROM student WHERE stuid = stuid_in; BEGIN OPEN cdno; LOOP FETCH cdno INTO departMa_no,departMi_no; IF (departMa_no = depart_course OR departMi_no = depart_course) THEN report := 'true'; EXIT; ELSE report := 'flase'; END IF; EXIT WHEN cdno%NOTFOUND; END LOOP; CLOSE cdno; RETURN report; END; / DROP FUNCTION find_dno_from_cno; CREATE OR REPLACE FUNCTION find_dno_from_cno(cno_in IN NUMBER) RETURN NUMBER AS depart_no NUMBER; CURSOR cdno is SELECT dnumber FROM courses WHERE cno = cno_in; BEGIN OPEN cdno; FETCH cdno INTO depart_no; CLOSE cdno; RETURN depart_no; END; / ALTER TABLE sections ADD CONSTRAINT CK_course_depart CHECK (FindMYDeparture(stuid,find_dno_from_cno(cno)) = 'true');
Not going to happen. You are not allowed to use your pl/sql functions in check constraint by design: •Conditions of check constraints cannot contain the following constructs: •Subqueries and scalar subquery expressions •Calls to the functions that are not deterministic (CURRENT_DATE, CURRENT_TIMESTAMP, DBTIMEZONE, LOCALTIMESTAMP, SESSIONTIMEZONE, SYSDATE, SYSTIMESTAMP, UID, USER, and USERENV) •Calls to user-defined functions •Dereferencing of REF columns (for example, using the DEREF function) •Nested table columns or attributes •The pseudocolumns CURRVAL, NEXTVAL, LEVEL, or ROWNUM •Date constants that are not fully specified http://docs.oracle.com/cd/B19306_01/server.102/b14200/clauses002.htm
Try running every statement in that function, This is the case of non existent column for a table or using a not existing row.