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 > What is different between primary index and unique index

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-23-05, 10:49
M_RAS M_RAS is offline
Registered User
 
Join Date: Sep 2003
Location: canada
Posts: 230
What is different between primary index and unique index

Hi All,
I am working with db2 v8.2 aix 5.2.

Recently I be forced to change some unique index to primary index (I am using insert_update cluse in my import statement) , I want to know what is different between unique index and primary index. can we have null value in unique index? I know we can not have null value in primary index.

thank you in advance for your help.
Reply With Quote
  #2 (permalink)  
Old 02-23-05, 11:18
urquel urquel is offline
Registered User
 
Join Date: Aug 2004
Posts: 330
If you want to have nulls in a unique index, use the "WHERE NOT NULL" clause in your create index statement.
Reply With Quote
  #3 (permalink)  
Old 02-24-05, 02:24
jacampbell jacampbell is offline
Registered User
 
Join Date: Jan 2005
Posts: 191
Technically there's no such thing as a "primary index". However, in DB2, you have to have a unique index to guarantee the uniqueness of a primary key. When you define a primary key, DB2 LUW checks to see if an appropriate unique index exists. If one doesn't then DB2 creates a suitable index. If a suitable unique index exists then DB2 uses it.


James Campbell
Reply With Quote
  #4 (permalink)  
Old 02-24-05, 13:07
crisscross crisscross is offline
Registered User
 
Join Date: Feb 2004
Posts: 25
We only define a primary key if we are going to use the that table as the parent in an RI rule.
Reply With Quote
  #5 (permalink)  
Old 02-24-05, 13:15
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
Quote:
Originally Posted by urquel
If you want to have nulls in a unique index, use the "WHERE NOT NULL" clause in your create index statement.
WHERE NOT NULL is an option for create index on DB2 for OS/390 and z/OS (maybe DB2 for I-series ??), but is not available on DB2 for LUW.

You can have a null in a unique index on DB2 for LUW (assuming the column is nullable), but you can only have one of them.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #6 (permalink)  
Old 02-24-05, 14:02
urquel urquel is offline
Registered User
 
Join Date: Aug 2004
Posts: 330
Quote:
Originally Posted by Marcus_A
WHERE NOT NULL is an option for create index on DB2 for OS/390 and z/OS (maybe DB2 for I-series ??), but is not available on DB2 for LUW.

You can have a null in a unique index on DB2 for LUW (assuming the column is nullable), but you can only have one of them.

Interesting! So if you want to enforce an optional, non-identifying, one-to-one foreign key relationship on LUW, would it require triggers?

Last edited by urquel; 02-24-05 at 14:06.
Reply With Quote
  #7 (permalink)  
Old 02-24-05, 14:10
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
Triggers are one way to that (which I have seen done by one of my customers).

Another way is to create a new parent table that contain the unique values that are not null and establish a RI constraint. RI constraints will allow nulls in the child table (foreign key).
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #8 (permalink)  
Old 02-24-05, 16:04
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by crisscross
We only define a primary key if we are going to use the that table as the parent in an RI rule.
I feel sorry for the organization that pays your salary.
Reply With Quote
  #9 (permalink)  
Old 02-24-05, 16:42
urquel urquel is offline
Registered User
 
Join Date: Aug 2004
Posts: 330
Quote:
Originally Posted by Marcus_A
Triggers are one way to that (which I have seen done by one of my customers).

Another way is to create a new parent table that contain the unique values that are not null and establish a RI constraint. RI constraints will allow nulls in the child table (foreign key).
The creation of a parent table and RI constraint will enforce the optional one-to-many relationship but you would need a UNIQUE WHERE NOT NULL or a trigger to enforce the optional one-to-one.
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