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 for table db2

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-26-09, 21:36
Somasundaram1 Somasundaram1 is offline
Registered User
 
Join Date: Oct 2009
Posts: 24
primary key for table db2

i take database class before. teacher say all tables must have primary key. now i use db2. db2 does not require primary key - is this wrong in db2? sorry my bad englis.
Reply With Quote
  #2 (permalink)  
Old 10-26-09, 22:35
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by Somasundaram1
db2 does not require primary key - is this wrong in db2?
yes, some people would say that it is wrong...

... but most other database systems (mysql, sql server, etc.) don't require a primary key either

nevertheless, your teacher is right, you should never create a table without declaring a primary key yourself

and your english is fine

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 10-26-09, 22:52
Somasundaram1 Somasundaram1 is offline
Registered User
 
Join Date: Oct 2009
Posts: 24
so if no key in table then i just make one up - surrogate key? yes? your ideas are good and helpful - i will buy your book
Reply With Quote
  #4 (permalink)  
Old 10-26-09, 23:24
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by Somasundaram1
so if no key in table then i just make one up - surrogate key? yes?
a surrogate key only under one of two conditions --

1. if the natural key is unweildy (e.g. multiple columns)

2. if there really, really is no natural key (but then you probably didn't look hard enough)

Quote:
Originally Posted by Somasundaram1
your ideas are good and helpful - i will buy your book
thanks
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 10-26-09, 23:35
Somasundaram1 Somasundaram1 is offline
Registered User
 
Join Date: Oct 2009
Posts: 24
i work project for cash register. cash register give us file every day: field 1 - date, field 2 - product upc. we want to load file and cound how many each product sold every day, every week, every month. so file look this:
.
2009-01-01, 1_ltr_of_milk
2009-01-01, 1_loaf_of_bread
2009-01-01, 1_ltr_of_milk
.
query look like "select product,count(*) from table where sale_date='2009-01-01' group by product"
.
what primary key here?
Reply With Quote
  #6 (permalink)  
Old 10-26-09, 23:51
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by Somasundaram1
what primary key here?
primary key = date + time + register number + product code
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 10-26-09, 23:57
Somasundaram1 Somasundaram1 is offline
Registered User
 
Join Date: Oct 2009
Posts: 24
my dear friend book writer! you no understand - we dont have time or register number , nor do we care what they are. all given to us is date and upc. we will be perfectly happy to load this in table and count how many sold in a day. why do we need primary key?
Reply With Quote
  #8 (permalink)  
Old 10-27-09, 00:36
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by Somasundaram1
why do we need primary key?
you don't

you did not ask me if you need a primary key

you asked me what it would be, and i gave you the minimum (4 columns) that i thought would be required
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 10-27-09, 00:41
Somasundaram1 Somasundaram1 is offline
Registered User
 
Join Date: Oct 2009
Posts: 24
Quote:
Originally Posted by r937
you should never create a table without declaring a primary key yourself
Quote:
Originally Posted by r937
you don't <need a primary key>
.... a contradiction it seems
Reply With Quote
  #10 (permalink)  
Old 10-27-09, 06:46
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by Somasundaram1
.... a contradiction it seems
yes, but ~you~ manoeuvered us into it

if it makes youi feel better, you can create a surrogate PK for your two-column cash register table
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #11 (permalink)  
Old 10-27-09, 07:02
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by r937
yes, but ~you~ manoeuvered us into it
That's what happens when you feed a troll, Rudy.
Reply With Quote
  #12 (permalink)  
Old 10-27-09, 08:56
Somasundaram1 Somasundaram1 is offline
Registered User
 
Join Date: Oct 2009
Posts: 24
Somasundaram not troll. Somasundaram - DBA!!
Reply With Quote
  #13 (permalink)  
Old 10-27-09, 13:53
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Quote:
Originally Posted by Somasundaram1
.... a contradiction it seems
I don't see any contradiction: the first sentence states what you should do while the second states what you must do (i.e. nothing for primary keys).

Just one other aspect: in the context of relational database systems, there is nothing special about a "primary key". It is just a unique constraint with a different name (and the default target for referential constraints). Therefore, I would rather say that each table should have a unique constraint.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #14 (permalink)  
Old 10-27-09, 15:08
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by stolze
Therefore, I would rather say that each table should have a unique constraint.
good point
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #15 (permalink)  
Old 11-02-09, 13:48
MarkhamDBA MarkhamDBA is offline
Registered User
 
Join Date: Dec 2008
Location: Toronto, Canada
Posts: 381
i can not read this nonsense!

tables in DB2 don't necessarily need PKs. Decision to create PKs is driven by business requirements. If you need to enforce uniqueness you can and should use unique indexes. PKs created in 2 cases only:

- referential constraint
- when you need to do import with insert_update
__________________
DB2 v9.5 ESE on AIX v6.1/ v9./10 on z/OS
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