Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a primary key and a surrogate key?

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.

like image 547
Dom Avatar asked Apr 21 '16 14:04

Dom


People also ask

Is a surrogate key always a primary key?

In a current database, the surrogate key can be the primary key, generated by the database management system and not derived from any application data in the database. The only significance of the surrogate key is to act as the primary key.

What is meant by surrogate key?

A surrogate key is a unique key for an entity in the client's business or for an object in the database. Sometimes natural keys cannot be used to create a unique primary key of the table. This is when the data modeler or architect decides to use surrogate or helping keys for a table in the LDM.

Is surrogate key and alternate key same?

We tag by using the concept of an alternate key. For example, if Employee Identifier is the surrogate key of the Employee table, then an alternate key could be Employee Social Security Number. Order Line may have Order Line Identifier as its surrogate key, and both Product Code and Order Number as its alternate key.


2 Answers

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.

like image 177
Bacon Bits Avatar answered Oct 15 '22 12:10

Bacon Bits


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 ) 
like image 30
tobypls Avatar answered Oct 15 '22 12:10

tobypls