<s>
A	O
surrogate	B-Application
key	I-Application
(	O
or	O
synthetic	O
key	O
,	O
pseudokey	B-Application
,	O
entity	O
identifier	O
,	O
factless	O
key	O
,	O
or	O
technical	B-Application
key	I-Application
)	O
in	O
a	O
database	O
is	O
a	O
unique	O
identifier	O
for	O
either	O
an	O
entity	O
in	O
the	O
modeled	O
world	O
or	O
an	O
object	O
in	O
the	O
database	O
.	O
</s>
<s>
The	O
surrogate	B-Application
key	I-Application
is	O
not	O
derived	O
from	O
application	O
data	O
,	O
unlike	O
a	O
natural	O
(	O
or	O
business	O
)	O
key	O
.	O
</s>
<s>
The	O
Surrogate	O
(	O
1	O
)	O
definition	O
relates	O
to	O
a	O
data	B-Application
model	I-Application
rather	O
than	O
a	O
storage	B-General_Concept
model	I-General_Concept
and	O
is	O
used	O
throughout	O
this	O
article	O
.	O
</s>
<s>
An	O
important	O
distinction	O
between	O
a	O
surrogate	O
and	O
a	O
primary	B-Application
key	I-Application
depends	O
on	O
whether	O
the	O
database	O
is	O
a	O
current	O
database	O
or	O
a	O
temporal	O
database	O
.	O
</s>
<s>
Since	O
a	O
current	O
database	O
stores	O
only	O
currently	O
valid	O
data	O
,	O
there	O
is	O
a	O
one-to-one	O
correspondence	O
between	O
a	O
surrogate	O
in	O
the	O
modeled	O
world	O
and	O
the	O
primary	B-Application
key	I-Application
of	O
the	O
database	O
.	O
</s>
<s>
In	O
this	O
case	O
the	O
surrogate	O
may	O
be	O
used	O
as	O
a	O
primary	B-Application
key	I-Application
,	O
resulting	O
in	O
the	O
term	O
surrogate	B-Application
key	I-Application
.	O
</s>
<s>
In	O
a	O
temporal	O
database	O
,	O
however	O
,	O
there	O
is	O
a	O
many-to-one	O
relationship	O
between	O
primary	B-Application
keys	I-Application
and	O
the	O
surrogate	O
.	O
</s>
<s>
Since	O
there	O
may	O
be	O
several	O
objects	O
in	O
the	O
database	O
corresponding	O
to	O
a	O
single	O
surrogate	O
,	O
we	O
cannot	O
use	O
the	O
surrogate	O
as	O
a	O
primary	B-Application
key	I-Application
;	O
another	O
attribute	O
is	O
required	O
,	O
in	O
addition	O
to	O
the	O
surrogate	O
,	O
to	O
uniquely	O
identify	O
each	O
object	O
.	O
</s>
<s>
In	O
a	O
current	O
database	O
,	O
the	O
surrogate	B-Application
key	I-Application
can	O
be	O
the	O
primary	B-Application
key	I-Application
,	O
generated	O
by	O
the	O
database	B-Application
management	I-Application
system	I-Application
and	O
not	O
derived	O
from	O
any	O
application	O
data	O
in	O
the	O
database	O
.	O
</s>
<s>
The	O
only	O
significance	O
of	O
the	O
surrogate	B-Application
key	I-Application
is	O
to	O
act	O
as	O
the	O
primary	B-Application
key	I-Application
.	O
</s>
<s>
It	O
is	O
also	O
possible	O
that	O
the	O
surrogate	B-Application
key	I-Application
exists	O
in	O
addition	O
to	O
the	O
database-generated	O
UUID	O
(	O
for	O
example	O
,	O
an	O
HR	O
number	O
for	O
each	O
employee	O
other	O
than	O
the	O
UUID	O
of	O
each	O
employee	O
)	O
.	O
</s>
<s>
A	O
surrogate	B-Application
key	I-Application
is	O
frequently	O
a	O
sequential	O
number	O
(	O
e.g.	O
</s>
<s>
a	O
Sybase	B-Application
or	O
SQL	B-Application
Server	I-Application
"	O
identity	O
column	O
"	O
,	O
a	O
PostgreSQL	B-Application
or	O
Informix	B-Application
serial	O
,	O
an	O
Oracle	B-Application
or	O
SQL	B-Application
Server	I-Application
SEQUENCE	O
or	O
a	O
column	O
defined	O
with	O
AUTO_INCREMENT	O
in	O
MySQL	B-Application
)	O
.	O
</s>
<s>
Some	O
databases	B-Application
provide	O
UUID/GUID	O
as	O
a	O
possible	O
data	O
type	O
for	O
surrogate	B-Application
keys	I-Application
(	O
e.g.	O
</s>
<s>
In	O
a	O
temporal	O
database	O
,	O
it	O
is	O
necessary	O
to	O
distinguish	O
between	O
the	O
surrogate	B-Application
key	I-Application
and	O
the	O
business	B-Application
key	I-Application
.	O
</s>
<s>
Every	O
row	O
would	O
have	O
both	O
a	O
business	B-Application
key	I-Application
and	O
a	O
surrogate	B-Application
key	I-Application
.	O
</s>
<s>
The	O
surrogate	B-Application
key	I-Application
identifies	O
one	O
unique	O
row	O
in	O
the	O
database	O
,	O
the	O
business	B-Application
key	I-Application
identifies	O
one	O
unique	O
entity	O
of	O
the	O
modeled	O
world	O
.	O
</s>
<s>
The	O
business	B-Application
key	I-Application
for	O
one	O
contract	O
will	O
be	O
identical	O
(	O
non-unique	O
)	O
in	O
both	O
rows	O
however	O
the	O
surrogate	B-Application
key	I-Application
for	O
each	O
row	O
is	O
unique	O
.	O
</s>
<s>
Some	O
database	O
designers	O
use	O
surrogate	B-Application
keys	I-Application
systematically	O
regardless	O
of	O
the	O
suitability	O
of	O
other	O
candidate	B-Application
keys	I-Application
,	O
while	O
others	O
will	O
use	O
a	O
key	O
already	O
present	O
in	O
the	O
data	O
,	O
if	O
there	O
is	O
one	O
.	O
</s>
<s>
AS	O
IDENTITY	O
GENERATED	O
BY	O
DEFAULT	O
in	O
IBM	B-Application
Db2	I-Application
&	O
PostgreSQL	B-Application
.	O
</s>
<s>
Surrogate	B-Application
keys	I-Application
typically	O
do	O
not	O
change	O
while	O
the	O
row	O
exists	O
.	O
</s>
<s>
The	O
primary	O
or	O
natural	B-Application
key	I-Application
data	O
can	O
always	O
be	O
modified	O
,	O
even	O
with	O
databases	B-Application
that	O
do	O
not	O
support	O
cascading	O
updates	O
across	O
related	O
foreign	B-Application
keys	I-Application
.	O
</s>
<s>
Attributes	O
that	O
uniquely	O
identify	O
an	O
entity	O
might	O
change	O
,	O
which	O
might	O
invalidate	O
the	O
suitability	O
of	O
natural	B-Application
keys	I-Application
.	O
</s>
<s>
An	O
employee	O
's	O
network	O
user	O
name	O
is	O
chosen	O
as	O
a	O
natural	B-Application
key	I-Application
.	O
</s>
<s>
In	O
these	O
cases	O
,	O
generally	O
a	O
new	O
attribute	O
must	O
be	O
added	O
to	O
the	O
natural	B-Application
key	I-Application
(	O
for	O
example	O
,	O
an	O
original_company	O
column	O
)	O
.	O
</s>
<s>
With	O
a	O
surrogate	B-Application
key	I-Application
,	O
only	O
the	O
table	O
that	O
defines	O
the	O
surrogate	B-Application
key	I-Application
must	O
be	O
changed	O
.	O
</s>
<s>
With	O
natural	B-Application
keys	I-Application
,	O
all	O
tables	O
(	O
and	O
possibly	O
other	O
,	O
related	O
software	O
)	O
that	O
use	O
the	O
natural	B-Application
key	I-Application
will	O
have	O
to	O
change	O
.	O
</s>
<s>
Some	O
problem	O
domains	O
do	O
not	O
clearly	O
identify	O
a	O
suitable	O
natural	B-Application
key	I-Application
.	O
</s>
<s>
Surrogate	B-Application
keys	I-Application
avoid	O
choosing	O
a	O
natural	B-Application
key	I-Application
that	O
might	O
be	O
incorrect	O
.	O
</s>
<s>
Surrogate	B-Application
keys	I-Application
tend	O
to	O
be	O
a	O
compact	O
data	O
type	O
,	O
such	O
as	O
a	O
four-byte	O
integer	O
.	O
</s>
<s>
Furthermore	O
,	O
a	O
non-redundant	O
distribution	O
of	O
keys	O
causes	O
the	O
resulting	O
b-tree	B-Architecture
index	B-Data_Structure
to	O
be	O
completely	O
balanced	O
.	O
</s>
<s>
Surrogate	B-Application
keys	I-Application
are	O
also	O
less	O
expensive	O
to	O
join	O
(	O
fewer	O
columns	O
to	O
compare	O
)	O
than	O
compound	B-Application
key	I-Application
.	O
</s>
<s>
While	O
using	O
several	O
database	O
application	O
development	O
systems	O
,	O
drivers	O
,	O
and	O
object-relational	B-General_Concept
mapping	I-General_Concept
systems	O
,	O
such	O
as	O
Ruby	B-Application
on	I-Application
Rails	I-Application
or	O
Hibernate	B-Library
,	O
it	O
is	O
much	O
easier	O
to	O
use	O
an	O
integer	O
or	O
GUID	O
surrogate	B-Application
keys	I-Application
for	O
every	O
table	O
instead	O
of	O
natural	B-Application
keys	I-Application
in	O
order	O
to	O
support	O
database-system-agnostic	O
operations	O
and	O
object-to-row	O
mapping	O
.	O
</s>
<s>
When	O
every	O
table	O
has	O
a	O
uniform	O
surrogate	B-Application
key	I-Application
,	O
some	O
tasks	O
can	O
be	O
easily	O
automated	O
by	O
writing	O
the	O
code	O
in	O
a	O
table-independent	O
way	O
.	O
</s>
<s>
However	O
,	O
this	O
characteristic	O
of	O
the	O
surrogate	B-Application
keys	I-Application
should	O
never	O
be	O
used	O
to	O
drive	O
any	O
of	O
the	O
logic	O
of	O
the	O
applications	O
themselves	O
,	O
as	O
this	O
would	O
violate	O
the	O
principles	O
of	O
Database	B-Application
normalization	I-Application
.	O
</s>
<s>
The	O
values	O
of	O
generated	O
surrogate	B-Application
keys	I-Application
have	O
no	O
relationship	O
to	O
the	O
real-world	O
meaning	O
of	O
the	O
data	O
held	O
in	O
a	O
row	O
.	O
</s>
<s>
When	O
inspecting	O
a	O
row	O
holding	O
a	O
foreign	B-Application
key	I-Application
reference	O
to	O
another	O
table	O
using	O
a	O
surrogate	B-Application
key	I-Application
,	O
the	O
meaning	O
of	O
the	O
surrogate	B-Application
key	I-Application
's	O
row	O
cannot	O
be	O
discerned	O
from	O
the	O
key	O
itself	O
.	O
</s>
<s>
Every	O
foreign	B-Application
key	I-Application
must	O
be	O
joined	O
to	O
see	O
the	O
related	O
data	O
item	O
.	O
</s>
<s>
If	O
appropriate	O
database	O
constraints	O
have	O
not	O
been	O
set	O
,	O
or	O
data	O
imported	O
from	O
a	O
legacy	O
system	O
where	O
referential	B-General_Concept
integrity	I-General_Concept
was	O
not	O
employed	O
,	O
it	O
is	O
possible	O
to	O
have	O
a	O
foreign-key	O
value	O
that	O
does	O
not	O
correspond	O
to	O
a	O
primary-key	O
value	O
and	O
is	O
therefore	O
invalid	O
.	O
</s>
<s>
Date	O
regards	O
the	O
meaninglessness	O
of	O
surrogate	B-Application
keys	I-Application
as	O
an	O
advantage	O
.	O
)	O
</s>
<s>
To	O
discover	O
such	O
errors	O
,	O
one	O
must	O
perform	O
a	O
query	O
that	O
uses	O
a	O
left	O
outer	O
join	O
between	O
the	O
table	O
with	O
the	O
foreign	B-Application
key	I-Application
and	O
the	O
table	O
with	O
the	O
primary	B-Application
key	I-Application
,	O
showing	O
both	O
key	O
fields	O
in	O
addition	O
to	O
any	O
fields	O
required	O
to	O
distinguish	O
the	O
record	O
;	O
all	O
invalid	O
foreign-key	O
values	O
will	O
have	O
the	O
primary-key	O
column	O
as	O
NULL	O
.	O
</s>
<s>
The	O
need	O
to	O
perform	O
such	O
a	O
check	O
is	O
so	O
common	O
that	O
Microsoft	B-Application
Access	I-Application
actually	O
provides	O
a	O
"	O
Find	O
Unmatched	O
Query	O
"	O
wizard	O
that	O
generates	O
the	O
appropriate	O
SQL	O
after	O
walking	O
the	O
user	O
through	O
a	O
dialog	O
.	O
</s>
<s>
Surrogate	B-Application
keys	I-Application
are	O
unnatural	O
for	O
data	O
that	O
is	O
exported	O
and	O
shared	O
.	O
</s>
<s>
This	O
can	O
be	O
mitigated	O
by	O
NOT	O
exporting	O
surrogate	B-Application
keys	I-Application
,	O
except	O
as	O
transient	O
data	O
(	O
most	O
obviously	O
,	O
in	O
executing	O
applications	O
that	O
have	O
a	O
"	O
live	O
"	O
connection	O
to	O
the	O
database	O
)	O
.	O
</s>
<s>
When	O
surrogate	B-Application
keys	I-Application
supplant	O
natural	B-Application
keys	I-Application
,	O
then	O
domain	O
specific	O
referential	B-General_Concept
integrity	I-General_Concept
will	O
be	O
compromised	O
.	O
</s>
<s>
For	O
example	O
,	O
in	O
a	O
customer	O
master	O
table	O
,	O
the	O
same	O
customer	O
may	O
have	O
multiple	O
records	O
under	O
separate	O
customer	O
IDs	O
,	O
even	O
though	O
the	O
natural	B-Application
key	I-Application
(	O
a	O
combination	O
of	O
customer	O
name	O
,	O
date	O
of	O
birth	O
,	O
and	O
e-mail	O
address	O
)	O
would	O
be	O
unique	O
.	O
</s>
<s>
To	O
prevent	O
compromise	O
,	O
the	O
natural	B-Application
key	I-Application
of	O
the	O
table	O
must	O
NOT	O
be	O
supplanted	O
:	O
it	O
must	O
be	O
preserved	O
as	O
a	O
unique	O
constraint	O
,	O
which	O
is	O
implemented	O
as	O
a	O
unique	O
index	B-Data_Structure
on	O
the	O
combination	O
of	O
natural-key	O
fields	O
.	O
</s>
<s>
Relational	O
databases	B-Application
assume	O
a	O
unique	O
index	B-Data_Structure
is	O
applied	O
to	O
a	O
table	O
's	O
primary	B-Application
key	I-Application
.	O
</s>
<s>
The	O
unique	O
index	B-Data_Structure
serves	O
two	O
purposes	O
:	O
(	O
i	O
)	O
to	O
enforce	O
entity	O
integrity	O
,	O
since	O
primary	B-Application
key	I-Application
data	O
must	O
be	O
unique	O
across	O
rows	O
and	O
(	O
ii	O
)	O
to	O
quickly	O
search	O
for	O
rows	O
when	O
queried	O
.	O
</s>
<s>
Since	O
surrogate	B-Application
keys	I-Application
replace	O
a	O
table	O
's	O
identifying	O
attributes	O
—	O
the	O
natural	B-Application
key	I-Application
—	O
and	O
since	O
the	O
identifying	O
attributes	O
are	O
likely	O
to	O
be	O
those	O
queried	O
,	O
then	O
the	O
query	O
optimizer	O
is	O
forced	O
to	O
perform	O
a	O
full	O
table	O
scan	O
when	O
fulfilling	O
likely	O
queries	O
.	O
</s>
<s>
Where	O
such	O
sets	O
are	O
themselves	O
a	O
candidate	B-Application
key	I-Application
,	O
the	O
index	B-Data_Structure
can	O
be	O
a	O
unique	O
index	B-Data_Structure
.	O
</s>
<s>
Surrogate	B-Application
keys	I-Application
can	O
result	O
in	O
duplicate	O
values	O
in	O
any	O
natural	B-Application
keys	I-Application
.	O
</s>
<s>
To	O
prevent	O
duplication	O
,	O
one	O
must	O
preserve	O
the	O
role	O
of	O
the	O
natural	B-Application
keys	I-Application
as	O
unique	O
constraints	O
when	O
defining	O
the	O
table	O
using	O
either	O
SQL	O
's	O
CREATE	O
TABLE	O
statement	O
or	O
ALTER	O
TABLE	O
...	O
ADD	O
CONSTRAINT	O
statement	O
,	O
if	O
the	O
constraints	O
are	O
added	O
as	O
an	O
afterthought	O
.	O
</s>
<s>
Because	O
surrogate	B-Application
keys	I-Application
are	O
unnatural	O
,	O
flaws	O
can	O
appear	O
when	O
modeling	O
the	O
business	O
requirements	O
.	O
</s>
<s>
Business	O
requirements	O
,	O
relying	O
on	O
the	O
natural	B-Application
key	I-Application
,	O
then	O
need	O
to	O
be	O
translated	O
to	O
the	O
surrogate	B-Application
key	I-Application
.	O
</s>
<s>
A	O
strategy	O
is	O
to	O
draw	O
a	O
clear	O
distinction	O
between	O
the	O
logical	O
model	O
(	O
in	O
which	O
surrogate	B-Application
keys	I-Application
do	O
not	O
appear	O
)	O
and	O
the	O
physical	O
implementation	O
of	O
that	O
model	O
,	O
to	O
ensure	O
that	O
the	O
logical	O
model	O
is	O
correct	O
and	O
reasonably	O
well	O
normalised	O
,	O
and	O
to	O
ensure	O
that	O
the	O
physical	O
model	O
is	O
a	O
correct	O
implementation	O
of	O
the	O
logical	O
model	O
.	O
</s>
<s>
Proprietary	O
information	O
can	O
be	O
leaked	O
if	O
surrogate	B-Application
keys	I-Application
are	O
generated	O
sequentially	O
.	O
</s>
<s>
Sequentially	O
generated	O
surrogate	B-Application
keys	I-Application
can	O
imply	O
that	O
events	O
with	O
a	O
higher	O
key	O
value	O
occurred	O
after	O
events	O
with	O
a	O
lower	O
value	O
.	O
</s>
