If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > generated by default as identity

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-23-05, 05:49
Klp Klp is offline
Registered User
 
Join Date: Dec 2004
Posts: 10
generated by default as identity

I have installed a DB2 v. 7.2

After that I run a sql script ( text file ) to insert some test data.
In the script I declare the pk myself.

When I after that run my app, the pk constraint fails because the DB tryies to insert existing pk.

Can I tell the DB to figure out what the next pk should be or somethin like that
Reply With Quote
  #2 (permalink)  
Old 09-23-05, 09:42
Wim Wim is offline
Registered User
 
Join Date: Nov 2004
Posts: 1,279
If you use this, DB2 genereates the value of the PK for you. Don't supply a value for ID yourself.
Code:
CREATE TABLE xxxx (
	ID	INTEGER		NOT NULL	GENERATED ALWAYS AS IDENTITY,
	field2 CHAR(10),
	CONSTRAINT PK_xxx PRIMARY KEY (ID)
);

INSERT INTO xxx (field2) VALUES('A');
INSERT INTO xxx (field2) VALUES('Z');
INSERT INTO xxx (field2) VALUES('B');
INSERT INTO xxx (field2) VALUES('C');

SELECT * FROM xxx order by field2;

ID	fileld2
1	A
3	B
4	C
2	Z
The ID was not supplied, but generated by DB2.
__________________
With kind regards . . . . . SQL Server 2000/2005/2008/2008 R2 Earned beers: 16
Wim
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald Knuth
Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On