File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes regarding surrogate key and primary key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "regarding surrogate key and primary key" Watch "regarding surrogate key and primary key" New topic

regarding surrogate key and primary key

Raj chiru
Ranch Hand

Joined: Aug 12, 2008
Posts: 142
Can Anybody explain to me what is difference between surrogate key and primary key?
John Bengler
Ranch Hand

Joined: Feb 12, 2009
Posts: 133
Hi raj,

as far as I know a surrogate key is a special kind of a primary key. It will be used if there is no primary key available.

e.g. if I have the database table "Employees" and every employee in my company has a unique employee signon (or number,etc.) I could use this as the primary key for table employees. This would be a so called natural key. If no unique identificaton is available (or if the unique identification consists of many attributes) an "artificial" attribute can be added to the table as a primary key, holding a value which e.g.comes from a sequence.

Such an artificial pk attribute is called surrogate key...

Did this help you?

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33098

Or even if a unique key exists and you don't want to use it. For example, e-mail and phone number may be unique, but can change over time.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3753

Surrogate keys are often enforced with a uniqueness constraint. Any surrogate key is capable of being a primary key, although a table can only have one primary key.

Surrogates may also refer to 'natural' keys like social security numbers. In practice though, there's no such thing as natural keys since anything can change (for example, not everyone in this country has a social security number). In large enough systems, a numeric value is often the primary key and other unique fields aren't treated as keys unless you need to build an index on them.

[OCA 8 Book] [Blog]
I agree. Here's the link:
subject: regarding surrogate key and primary key
It's not a secret anymore!