70

I googled a lot, but I did not find the exact straight forward answer with an example.

Any example for this would be more helpful.

2
  • 1
    from what I understood, If In a table there is no such attribute which we're sure that it'll always be unique(not null) and can be made the Primary key of the table, then we use an artificial (not derived from the table) attribute(mostly Auto-Increment ID) to be the Primary key in the table that artificial column is the surrogate as it's purpose is to be a primary key and it's not derived from the table..
    – aMighty
    Commented Apr 7, 2019 at 8:07
  • it.toolbox.com/question/….
    – Shiwangini
    Commented Jun 11, 2020 at 17:53

6 Answers 6

108

The primary key is a unique key in your table that you choose that best uniquely identifies a record in the table. All tables should have a primary key, because if you ever need to update or delete a record you need to know how to uniquely identify it.

A surrogate key is an artificially generated key. They're useful when your records essentially have no natural key (such as a Person table, since it's possible for two people born on the same date to have the same name, or records in a log, since it's possible for two events to happen such they they carry the same timestamp). Most often you'll see these implemented as integers in an automatically incrementing field, or as GUIDs that are generated automatically for each record. ID numbers are almost always surrogate keys.

Unlike primary keys, not all tables need surrogate keys, however. If you have a table that lists the states in America, you don't really need an ID number for them. You could use the state abbreviation as a primary key code.

The main advantage of the surrogate key is that they're easy to guarantee as unique. The main disadvantage is that they don't have any meaning. There's no meaning that "28" is Wisconsin, for example, but when you see 'WI' in the State column of your Address table, you know what state you're talking about without needing to look up which state is which in your State table.

11
  • I think the main disadvantage is that sometimes when people use an autogenerated key (and integers are often used instead of the natural key not just when no natural key exists), they often forget to put unique indexes on the natural key that they didn't choose as the PK. This often allows duplicates to get into the system which can create problems. The two main advantages of autogenerated keys are that they generally increase performance in the joins (if integers not GUIDS) and they prevent mass updating of lots of child records when the Natural Key changes.
    – HLGEM
    Commented Apr 21, 2016 at 15:22
  • @HLGEM Sure, I'll buy those. I think I focus on lack of meaning because I've just worked in hypernormalized systems where essentially every field was it's own table. It made it impossible to tell where data entry errors had occurred, and very difficult to apply business rules to locate problems.
    – Bacon Bits
    Commented Apr 21, 2016 at 18:02
  • I love normalization but you can indeed take it too far.
    – HLGEM
    Commented Apr 21, 2016 at 18:13
  • @HLGEM: Interesting, tell me more. You knew your love for normalization had gone to far when...?
    – onedaywhen
    Commented Oct 13, 2016 at 15:27
  • 2
    @onedaywhen Imagine a database which never allowed null values for any field. As such, nearly every field is in it's own table, and nearly every join is an outer join because still don't have complete data. So, you haven't actually eliminated nulls, you've just eliminated storing them. Trying to validate such a system with business rules after the fact is virtually impossible because it's so difficult to compare records. Since every potential relation might be many-to-one, you have partial cross joins appearing. And the performance impact of every query having dozens of joins?
    – Bacon Bits
    Commented Mar 16, 2017 at 17:55
9

A surrogate key is a made up value with the sole purpose of uniquely identifying a row. Usually, this is represented by an auto incrementing ID.

Example code:

CREATE TABLE Example
(
    SurrogateKey INT IDENTITY(1,1) -- A surrogate key that increments automatically
)

A primary key is the identifying column or set of columns of a table. Can be surrogate key or any other unique combination of columns (for example a compound key). MUST be unique for any row and cannot be NULL.

Example code:

CREATE TABLE Example
(
    PrimaryKey INT PRIMARY KEY -- A primary key is just an unique identifier
)
5

All keys are identifiers used as surrogates for the things they identify. E.F.Codd explained the concept of system-assigned surrogates as follows [1]:

Database users may cause the system to generate or delete a surrogate, but they have no control over its value, nor is its value ever displayed to them.

This is what is commonly referred to as a surrogate key. The definition is immediately problematic however because Codd was assuming that such a feature would be provided by the DBMS. DBMSs in general have no such feature. The keys are normally visible to at least some DBMS users as, for obvious reasons, they have to be. The concept of a surrogate has therefore morphed slightly in usage. The term is generally used in the data management profession to mean a key that is not exposed and used as an identifier in the business domain. Note that this is essentially unrelated to how the key is generated or how "artificial" it is perceived to be. All keys consist of symbols invented by humans or machines. The only possible significance of the term surrogate therefore relates how the key is used, not how it is created or what its values are.

[1] Extending the database relational model to capture more meaning, E.F.Codd, 1979

1

This is a great treatment describing the various kinds of keys:

http://www.agiledata.org/essays/keys.html

1

A surrogate key is typically a numeric value. Within SQL Server, Microsoft allows you to define a column with an identity property to help generate surrogate key values.

The PRIMARY KEY constraint uniquely identifies each record in a database table. Primary keys must contain UNIQUE values. A primary key column cannot contain NULL values. Most tables should have a primary key, and each table can have only ONE primary key.

http://www.databasejournal.com/features/mssql/article.php/3922066/SQL-Server-Natural-Key-Verses-Surrogate-Key.htm

0

I think Michelle Poolet describes it in a very clear way:

A surrogate key is an artificially produced value, most often a system-managed, incrementing counter whose values can range from 1 to n, where n represents a table's maximum number of rows. In SQL Server, you create a surrogate key by assigning an identity property to a column that has a number data type.

http://sqlmag.com/business-intelligence/surrogate-key-vs-natural-key

It usually helps you use a surrogate key when you change a composite key with an identity column.

1
  • 3
    I give -1 because it does not explain the difference.
    – TomTom
    Commented Apr 21, 2016 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.