This week's book giveaway is in the Spring forum.
We're giving away four copies of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 and have Marten Deinum & Iuliana Cosmina on-line!
See this thread for details.
Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

regarding surrogate key and primary key

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi....
Can Anybody explain to me what is difference between surrogate key and primary key?
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?



John
 
author & internet detective
Posts: 40552
825
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 4278
34
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic