MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problem with Foreign Key Contraint

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
mvenkatesha
Beginner


Joined: 10 Oct 2006
Posts: 16
Topics: 6
Location: Bangalore

PostPosted: Thu Dec 07, 2006 12:56 am    Post subject: Problem with Foreign Key Contraint Reply with quote

Surprised I was preparing for DB2 700 exam and surprised why I am getting foreign key constraint SQLCODE = -530. Can anyone please let me know what I am doing wrong because I feel record must be inserted but ending up in error? I am attaching the spufi output for your reference.
Code:

CREATE TABLE RUSH_EMPLNO                                   
(EMPNO  CHAR(5) NOT NULL,                                 
LNAME   CHAR(10),                                         
FNAME   CHAR(10),                                         
DEPT    CHAR(15),                                         
GOVTID  CHAR(5),                                           
SALRY   INTEGER,                                           
PRIMARY KEY(EMPNO));                                       
---------+---------+---------+---------+---------+---------
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0 
---------+---------+---------+---------+---------+---------
                                                           
CREATE UNIQUE INDEX XEMP ON RUSH_EMPLNO (EMPNO);           
---------+---------+---------+---------+---------+---------
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0 
---------+---------+---------+---------+---------+---------
CREATE INDEX XGOVTID ON RUSH_EMPLNO (GOVTID);             
---------+---------+---------+---------+---------+---------
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0 
---------+---------+---------+---------+---------+---------
                                                           
                                                           
INSERT INTO RUSH_EMPLNO                                   
VALUES ('00001','LAST01','FIRST01','DEPT01','G0001',0100);
---------+---------+---------+---------+---------+---------
DSNE615I NUMBER OF ROWS AFFECTED IS 1                     
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0 
---------+---------+---------+---------+---------+---------
                                                           
INSERT INTO RUSH_EMPLNO                                   
VALUES  ('00002','LAST02','FIRST02','DEPT02','G0002',0200);
---------+---------+---------+---------+---------+---------
DSNE615I NUMBER OF ROWS AFFECTED IS 1                      DSNE615I NUMBER OF ROWS AFFECTED IS 1                             
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0         
---------+---------+---------+---------+---------+---------+-------
                                                                   
INSERT INTO RUSH_EMPLNO                                           
VALUES('00003','LAST03','FIRST03','DEPT03','G0003',0300);         
---------+---------+---------+---------+---------+---------+-------
DSNE615I NUMBER OF ROWS AFFECTED IS 1                             
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0         
---------+---------+---------+---------+---------+---------+-------
                                                                   
SELECT *                                                           
FROM RUSH_EMPLNO;                                                 
---------+---------+---------+---------+---------+---------+-------
EMPNO  LNAME       FNAME       DEPT             GOVTID        SALRY
---------+---------+---------+---------+---------+---------+-------
00001  LAST01      FIRST01     DEPT01           G0001           10000002  LAST02      FIRST02     DEPT02           G0002           200
00003  LAST03      FIRST03     DEPT03           G0003           300
DSNE610I NUMBER OF ROWS DISPLAYED IS 3                             
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 100       
---------+---------+---------+---------+---------+---------+-------
                                                                   
                                                                   
CREATE TABLE RUSH_BEN_CLAIM                                       
(CLAIMNO   CHAR(5) NOT NULL,                                       
GOVTID     CHAR(5),                                               
SEVICE_DEP CHAR(15),                                               
CLAIM_CODE CHAR(5),                                               
PRIMARY KEY(CLAIMNO),                                             
FOREIGN KEY FK1(GOVTID) REFERENCES RUSH_EMPLNO                     
ON DELETE RESTRICT);                                               
---------+---------+---------+---------+---------+---------+-------
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0          ---------+---------+---------+---------+---------+---------+---------+
                                                                     
CREATE UNIQUE INDEX XBEN ON RUSH_BEN_CLAIM (CLAIMNO);                 
---------+---------+---------+---------+---------+---------+---------+
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0             
---------+---------+---------+---------+---------+---------+---------+
                                                                     
CREATE INDEX XGOVTID1 ON RUSH_BEN_CLAIM (GOVTID);                     
---------+---------+---------+---------+---------+---------+---------+
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0             
---------+---------+---------+---------+---------+---------+---------+
                                                                     
INSERT INTO RUSH_BEN_CLAIM                                           
VALUES ('CL001','G0001','DEPT01','11');                               
---------+---------+---------+---------+---------+---------+---------+
DSNT408I [b]SQLCODE = -530, ERROR[/b]:  THE INSERT OR UPDATE VALUE OF FOREIGN KEY FK1         IS INVALID                                                       
DSNT418I SQLSTATE   = 23503 SQLSTATE RETURN CODE                           
DSNT415I SQLERRP    = DSNXRUID SQL PROCEDURE DETECTING ERROR               
DSNT416I SQLERRD    = -110  13172774  0  0  0  0 SQL DIAGNOSTIC INFORMATION
DSNT416I SQLERRD    = X'FFFFFF92'  X'00C90026'  X'00000000'  X'00000000'   
         X'00000000'  X'00000000' SQL DIAGNOSTIC INFORMATION               
---------+---------+---------+---------+---------+---------+---------+-----
DSNE614I AUTOCOMMIT IS NO; NO CHANGES COMMITTED

_________________
murthy
Back to top
View user's profile Send private message Send e-mail
bauer
Intermediate


Joined: 10 Oct 2003
Posts: 315
Topics: 49
Location: Germany

PostPosted: Thu Dec 07, 2006 2:54 am    Post subject: Reply with quote

mvenkatesha,

table RUSH_EMPLNO is parent, table RUSH_BEN_CLAIM is child.

Your foreign key is set from RUSH_EMPLNO.EMPNO (primary key of this table) to table RUSH_BEN_CLAIM.GOVTID with rule DELETE.

Your constraint is NOT set on column RUSH_EMPLNO.GOVTID, because this column is not primary key in your parent table.

Foreign key constraint connect primary key in parent table to any other column in child table and NOT any not primary key column in parent table with any columns in chold table.


bauer
Back to top
View user's profile Send private message
mvenkatesha
Beginner


Joined: 10 Oct 2006
Posts: 16
Topics: 6
Location: Bangalore

PostPosted: Thu Dec 07, 2006 7:40 am    Post subject: Reply with quote

Bauer

Thanks a lot for your information. I was able to correct my Query and able to run.
_________________
murthy
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group