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 > primary key and import with insert_update

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-10-09, 14:59
MarkhamDBA MarkhamDBA is offline
Registered User
 
Join Date: Dec 2008
Location: Toronto, Canada
Posts: 381
primary key and import with insert_update

DB2 v9.5/AIX 6.1
I always thought that PKs are needed for referencial integrity/constraint only. I am new in this company and I find we are using import with insert_update option which updates existing records in the table with matching PKs. I was told we are using PKs only for this purpose.

Is there any other way to do same task like import...insert_update without creating/using PKs on the table? Thanks
__________________
DB2 v9.5 ESE on AIX v6.1/ v9./10 on z/OS
Reply With Quote
  #2 (permalink)  
Old 09-10-09, 16:38
dbamota dbamota is offline
Registered User
 
Join Date: Sep 2003
Posts: 237
Commands manual says

INSERT_UPDATE
Adds rows of imported data to the target table, or updates existing rows (of the target table) with matching primary keys.

Looks like the import is looking for a primary key (or maybe an unique key) to match imported records to existing records. I don't think it will work without the keys.
__________________
mota
Reply With Quote
  #3 (permalink)  
Old 09-10-09, 21:37
MarkhamDBA MarkhamDBA is offline
Registered User
 
Join Date: Dec 2008
Location: Toronto, Canada
Posts: 381
Quote:
Originally Posted by dbamota
Commands manual says

INSERT_UPDATE
Adds rows of imported data to the target table, or updates existing rows (of the target table) with matching primary keys.

Looks like the import is looking for a primary key (or maybe an unique key) to match imported records to existing records. I don't think it will work without the keys.
So this is the question: do I have to have primary keys for this or it's enough to have unique index on these cols?

Thank you
__________________
DB2 v9.5 ESE on AIX v6.1/ v9./10 on z/OS
Reply With Quote
  #4 (permalink)  
Old 09-11-09, 06:45
dr_te_z dr_te_z is offline
Registered User
 
Join Date: Jan 2009
Location: Zoetermeer, Holland
Posts: 555
Quote:
Originally Posted by MarkhamDBA
do I have to have primary keys for this or it's enough to have unique index on these cols?
Thank you
Technically spoken DB2 can do without a Primary Key definition. Personally I do not accept tables without a PK definition.
Unique key - Wikipedia, the free encyclopedia
There is a catch with DB2: you can add the keywords "primary key" in the column defintion as part of the CREATE TABLE. DB2 will automaticly create an index for you. When you create you own index later (with unique/cluster/include parameters) you will have 2 almost-the-same indexes on that table causing only overhead and no gain what-so-ever.
So what they taught me:
- create table without the PK definition
- create the unique index (can be more columns)
- alter table to add the PK contraint
Now DB2 will check if there is already an index which can be used and will not create its own index. If you own index cannot be used, DB2 will create an index with the same name as you contraint.
Reply With Quote
  #5 (permalink)  
Old 09-11-09, 08:04
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
The basic point is that an index is not a constraint.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #6 (permalink)  
Old 09-11-09, 09:24
rdutton rdutton is offline
Registered User
 
Join Date: Dec 2008
Posts: 76
A unique index is not a declared constraint, although it acts as a defacto constraint.
__________________
RD

Last edited by rdutton; 09-11-09 at 09:36.
Reply With Quote
  #7 (permalink)  
Old 09-11-09, 11:09
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
That's a muddy area. An index (which is a concept for the physical layer of the database design) is just an access method. Personally, I find it really unfortunate to have something like an "unique index" where the index drags a constraint into the picture, which belongs to the conceptional database layer. My approach to avoid any misconceptions is therefore:
- if you need an access method, define an index
- if you need a unique constraint, define a primary key or a unique key
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #8 (permalink)  
Old 09-11-09, 11:10
Cougar8000 Cougar8000 is offline
Registered User
 
Join Date: Nov 2005
Location: IL
Posts: 554
what ever dr te z said.

To answer your original question is you DO NOT need UK nor PK to do the insert_update.
__________________
--
IBM Certified DBA on DB2 for Linux, UNIX, and Windows

DB2 v9.1.0.2 os 5.3.0.0
Reply With Quote
  #9 (permalink)  
Old 09-11-09, 11:31
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by Cougar8000

To answer your original question is you DO NOT need UK nor PK to do the insert_update.
I'm afraid you are wrong. Check SQL3203N, for example.
Reply With Quote
  #10 (permalink)  
Old 09-11-09, 11:52
Cougar8000 Cougar8000 is offline
Registered User
 
Join Date: Nov 2005
Location: IL
Posts: 554
I am not afraid to be wrong thanks for pointing this out. I guess I better refresh my memory on it.
__________________
--
IBM Certified DBA on DB2 for Linux, UNIX, and Windows

DB2 v9.1.0.2 os 5.3.0.0
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