Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > IBM DB2 > error messages ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 17 Topic 9021 of 9520
Post > Topic >>

error messages for key constraint violations

by "Frank Swarbrick" <Frank.Swarbrick@[EMAIL PROTECTED] > Jun 25, 2008 at 06:22 PM

I have the following three tables

DROP TABLE CALLTRAK.SERVICE_CODES
@[EMAIL PROTECTED]
 TABLE CALLTRAK.SERVICE_CODES (
    CODE		CHAR(1)	NOT NULL
  , CONSTRAINT	SERVICE_CODES_PK
                  PRIMARY KEY (CODE)
  , DESCRIPTION	VARCHAR(50)	NOT NULL
)
@[EMAIL PROTECTED]
 TABLE CALLTRAK.CALLS
@[EMAIL PROTECTED]
 TABLE CALLTRAK.CALLS (
    CALL_ID		INTEGER	NOT NULL 
					GENERATED ALWAYS AS IDENTITY
  , CONSTRAINT 	CALLS_PK 	PRIMARY KEY (CALL_ID)
  , DATETIME 	TIMESTAMP 	NOT NULL
					WITH DEFAULT
  , CUST_NBR 	DECIMAL(9) 	NOT NULL
  , BILL_BRCH 	DECIMAL(3) 	NOT NULL
  , ACCT_BRCH 	DECIMAL(3) 	NOT NULL
  , BILL_ACCT	DECIMAL(10)	NOT NULL
  , ACCT_TYPE	CHAR(1)	NOT NULL
  , WAIVE_CHG_YN	CHAR(1)	NOT NULL
  , CONSTRAINT 	WAIVE_CHG_YN
                  CHECK(WAIVE_CHG_YN IN ('Y','N'))
  , MULTI_ACCT_CALL_IND CHAR(1)	NOT NULL
  , CONSTRAINT 	MULTI_ACCT_CALL_IND
            	CHECK(MULTI_ACCT_CALL_IND IN ('N','B','C','E'))
  , MULTI_ACCT_ORIG_DATETIME 	TIMESTAMP
  , COMMENTS	VARCHAR(54)	NOT NULL
  , BILL_ACCT_TYP	CHAR(1)	NOT NULL
  , OPERATOR	CHAR(3)	NOT NULL
)
@[EMAIL PROTECTED]
 TABLE CALLTRAK.SERVICES
@[EMAIL PROTECTED]
 TABLE CALLTRAK.SERVICES (
    SERVICES_ID	INTEGER	NOT NULL 
                              GENERATED ALWAYS AS IDENTITY
  , CONSTRAINT 	SERVICES_PK
                  PRIMARY KEY (SERVICES_ID)
  , CALL_ID		INTEGER		NOT NULL
  , CONSTRAINT 	CALL_ID_FK
                  FOREIGN KEY (CALL_ID) 
			REFERENCES CALLTRAK.CALLS (CALL_ID)
			ON DELETE CASCADE
  , SERVICE_CODE	CHAR(1)	NOT NULL 
  , CONSTRAINT	SERVICE_CODE_FK
                  FOREIGN KEY (SERVICE_CODE)
			REFERENCES CALLTRAK.SERVICE_CODES (CODE)
  , CONSTRAINT	SERVICES_UK1
                  UNIQUE (CALL_ID, SERVICE_CODE)
)
@[EMAIL PROTECTED]
 I try to insert a row into CALLTRAK.SERVICES where the SERVICE_CODE
value
is not present in CALLTRAK.SERVICE_CODES I get a nice, useful error
message:

SQL0530N  The insert or update value of the FOREIGN KEY
"CALLTRAK.SERVICES.SERVICE_CODE_FK" is not equal to any value of the
parent
key of the parent table.  SQLSTATE=23503

But if I try to insert a row that causes a unique (or primary) key
violation
I get something like this:

SQL0803N  One or more values in the INSERT statement, UPDATE statement, or
foreign key update caused by a DELETE statement are not valid because the 
primary key, unique constraint or unique index identified by "1"
constrains
table "CALLTRAK.SERVICE_CODES" from having duplicate values for the index
key. SQLSTATE=23505

I would think that it would return the name of the constraint that was
violated.  In this case "SERVICE_CODES_PK".  And in fact, when I follow
the
guidance of how to get the name of the constraint, it returns it to me. 
EG:

SELECT INDNAME, INDSCHEMA
  FROM SYSCAT.INDEXES
  WHERE IID = 1
  AND TABSCHEMA = 'CALLTRAK'
  AND TABNAME = 'SERVICE_CODES'
;


INDNAME          INDSCHEMA                                                
 
                                                                    
---------------- ---------
SERVICE_CODES_PK CALLTRAK

So my question is, why is this manual work (writing the above query)
necessary?  Why doesn't DB2 do it itself?

My other (loaded) question is does anyone have any comments on the names
of
my constraints?  Good?  Bad?  Who cares?

Thanks,
Frank
 




 17 Posts in Topic:
error messages for key constraint violations
"Frank Swarbrick&quo  2008-06-25 18:22:14 
Re: error messages for key constraint violations
"Dave Hughes" &  2008-06-25 19:49:36 
Re: error messages for key constraint violations
Ian <ianbjor@[EMAIL PR  2008-06-26 10:01:36 
Re: error messages for key constraint violations
Lennart <Erik.Lennart.  2008-06-26 12:50:48 
Re: error messages for key constraint violations
Lennart <Erik.Lennart.  2008-06-27 11:15:34 
Re: error messages for key constraint violations
"Dave Hughes" &  2008-06-27 13:48:55 
Re: error messages for key constraint violations
Ian <ianbjor@[EMAIL PR  2008-06-27 13:34:42 
Re: error messages for key constraint violations
Lennart <Erik.Lennart.  2008-06-27 22:14:04 
Re: error messages for key constraint violations
Lennart <Erik.Lennart.  2008-06-25 23:27:22 
Re: error messages for key constraint violations
Serge Rielau <srielau@  2008-06-26 07:28:25 
Re: error messages for key constraint violations
"Frank Swarbrick&quo  2008-06-26 09:09:33 
Re: error messages for key constraint violations
"Dave Hughes" &  2008-06-26 12:08:51 
Re: error messages for key constraint violations
"Frank Swarbrick&quo  2008-06-26 09:15:46 
Re: error messages for key constraint violations
"Frank Swarbrick&quo  2008-06-27 12:00:57 
Re: error messages for key constraint violations
"Dave Hughes" &  2008-06-27 15:54:08 
Re: error messages for key constraint violations
Ian <ianbjor@[EMAIL PR  2008-06-27 14:05:07 
Re: error messages for key constraint violations
"Frank Swarbrick&quo  2008-06-27 20:51:51 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Dec 2 20:29:49 CST 2008.