• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

mutiple primary keys in a table

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all.

Im not sure if im doing this right. But i wanted to clarify. I have a table USER and i have column called ID which is configured as primary key. My intention is that every user would have a username (which must be unique). And therefore I was wonder if I should have another column called username and make that as primary key rather than ID column. Since when im querying I would be looking for username rather than the ID?

Would do you suggest leaving ID as a primary key? I mean isnt the concept of PRIMARY key to be for searching purpose?

I just wanted to make sure if I understand the concept of primary key right?

Thanks very much all.

Harish
 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not necessarily. Primary key is an value that doesn't change. By making the username the primary key, you are making it hard for users to change their username.

Note you can have a unique index on a column that isn't the primary key. That makes lookups fast too.
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In many programming shops it is customary that tables have a syntetic primary key column, autoincrement or with values taken from a sequence, in order to have a really constant ID value.
Fields coming from the problem space (like code, name etc.) can be used for indices, but the real primary key is a technical column.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Primary key is used to uniquely identify a row in a table and it is also used to extract the records from a table and the  primary key cannot have duplicate values. If you will try to add duplicate values in the primary key then you will get the error "primary key cannot have duplicate values". If you want to set the name attribute as the primary key then you need to add different names in each column.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic