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 

Primary Key Vs Unique Index

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


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Thu Nov 17, 2005 4:45 pm    Post subject: Primary Key Vs Unique Index Reply with quote

Is there any performance or less overhead to be gained by defining a table with PRIMARY KEY vs. just creating a UNIQUE INDEX?
Code:
CREATE TABLE HOURS.A2BKCNT               
    (SSNO         INTEGER    NOT NULL,   
     WRKPERIOD    DATE       NOT NULL,   
     A2COUNT      SMALLINT   NOT NULL,   
     MEMTYPE      CHAR(2)    NOT NULL,   
     OLDESTHRS    DATE       NOT NULL,   
     PARDTHRS     DATE       NOT NULL,   
     PARELG       DATE       NOT NULL,   
     DEPDTHRS     DATE       NOT NULL,   
     DEPELG       DATE       NOT NULL,   
     LASTUPDT     TIMESTAMP  NOT NULL,   
     USERID       CHAR(40)   NOT NULL,   
     PRIMARY KEY (SSNO))                 
       IN FDBHRS.FTSA2CNT               
       ;                                 
                                         
CREATE UNIQUE INDEX HOURS.XELG2CNT       
       ON HOURS.A2BKCNT                 
      (SSNO)                             
       USING STOGROUP FSGALLP           
       PRIQTY 5000                       
       SECQTY 1000                       
       CLUSTER                           
       BUFFERPOOL BP5                   
       COPY YES                         
       CLOSE NO;         

 VS
CREATE TABLE HOURS.A2BKCNT               
    (SSNO         INTEGER    NOT NULL,   
     WRKPERIOD    DATE       NOT NULL,   
     A2COUNT      SMALLINT   NOT NULL,   
     MEMTYPE      CHAR(2)    NOT NULL,   
     OLDESTHRS    DATE       NOT NULL,   
     PARDTHRS     DATE       NOT NULL,   
     PARELG       DATE       NOT NULL,   
     DEPDTHRS     DATE       NOT NULL,   
     DEPELG       DATE       NOT NULL,   
     LASTUPDT     TIMESTAMP  NOT NULL,   
     USERID       CHAR(40)   NOT NULL ) 
            IN FDBHRS.FTSA2CNT               
       ;                                 
                                         
CREATE UNIQUE INDEX HOURS.XELG2CNT       
       ON HOURS.A2BKCNT                 
      (SSNO)                             
       USING STOGROUP FSGALLP           
       PRIQTY 5000                       
       SECQTY 1000                       
       CLUSTER                           
       BUFFERPOOL BP5                   
       COPY YES                         
       CLOSE NO;       
                 
               

_________________
Thanks,
NASCAR9
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Thu Nov 17, 2005 5:35 pm    Post subject: Reply with quote

NASCAR9,

When you create a table with PRIMARY KEY , it's definition is incomplete until the primary index is created. Actually the Table is unavailable until the index is created.

Defining a column has advantages that it is a unique key and it cannot contain NULLS.

You can achieve the same concept by defining the column as not null and creating an Unique Index on it.

Hope this helps...

Cheers

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Thu Nov 17, 2005 5:48 pm    Post subject: Reply with quote

Thanks kolusu,
This is what I suspected. I'm in the process creating many new tables.
I want to make sure they are defined in the most efficient way possible. Some of the questions I ask may seem obvious to many on this board, but keep in mind we are a very young DB2 shop. (Less than 6 years)
_________________
Thanks,
NASCAR9
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Thu Nov 17, 2005 6:22 pm    Post subject: Reply with quote

Quote:

Some of the questions I ask may seem obvious to many on this board, but keep in mind we are a very young DB2 shop. (Less than 6 years)


Just add 2 more years to it and you have my experience ! Every question has its own merit. so Don't worry about it
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Dip
Beginner


Joined: 24 Mar 2006
Posts: 27
Topics: 14

PostPosted: Thu Jan 07, 2010 3:51 am    Post subject: Reply with quote

Can we define an Unique INDEX on two fields/keys where one of them can be NULL?


Thanks!
Dip.
Back to top
View user's profile Send private message Yahoo Messenger
Dip
Beginner


Joined: 24 Mar 2006
Posts: 27
Topics: 14

PostPosted: Thu Jan 07, 2010 4:10 am    Post subject: Reply with quote

Just now i read in a discussion forum that DB2 has no problem or restriction with NULLs in non primary key-index. Can someone confirm it?

Also want to know, if there will be any performace degradation if 5% of the total records having NULL in one of the key field which is part of the Unique index?

Thanks!
Dip.
Back to top
View user's profile Send private message Yahoo Messenger
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