<s>
A	O
foreign	B-Application
key	I-Application
is	O
a	O
set	O
of	O
attributes	B-General_Concept
in	O
a	O
table	O
that	O
refers	O
to	O
the	O
primary	B-Application
key	I-Application
of	O
another	O
table	O
.	O
</s>
<s>
The	O
foreign	B-Application
key	I-Application
links	O
these	O
two	O
tables	O
.	O
</s>
<s>
Another	O
way	O
to	O
put	O
it	O
:	O
In	O
the	O
context	O
of	O
relational	B-Application
databases	I-Application
,	O
a	O
foreign	B-Application
key	I-Application
is	O
a	O
set	O
of	O
attributes	B-General_Concept
subject	O
to	O
a	O
certain	O
kind	O
of	O
inclusion	O
dependency	O
constraints	O
,	O
specifically	O
a	O
constraint	O
that	O
the	O
tuples	B-Application
consisting	O
of	O
the	O
foreign	B-Application
key	I-Application
attributes	B-General_Concept
in	O
one	O
relation	B-Language
,	O
R	O
,	O
must	O
also	O
exist	O
in	O
some	O
other	O
(	O
not	O
necessarily	O
distinct	O
)	O
relation	B-Language
,	O
S	O
,	O
and	O
furthermore	O
that	O
those	O
attributes	B-General_Concept
must	O
also	O
be	O
a	O
candidate	B-Application
key	I-Application
in	O
S	O
.	O
In	O
simpler	O
words	O
,	O
a	O
foreign	B-Application
key	I-Application
is	O
a	O
set	O
of	O
attributes	B-General_Concept
that	O
references	O
a	O
candidate	B-Application
key	I-Application
.	O
</s>
<s>
For	O
example	O
,	O
a	O
table	O
called	O
TEAM	O
may	O
have	O
an	O
attribute	O
,	O
MEMBER_NAME	O
,	O
which	O
is	O
a	O
foreign	B-Application
key	I-Application
referencing	O
a	O
candidate	B-Application
key	I-Application
,	O
PERSON_NAME	O
,	O
in	O
the	O
PERSON	O
table	O
.	O
</s>
<s>
Since	O
MEMBER_NAME	O
is	O
a	O
foreign	B-Application
key	I-Application
,	O
any	O
value	O
existing	O
as	O
the	O
name	O
of	O
a	O
member	O
in	O
TEAM	O
must	O
also	O
exist	O
as	O
a	O
person	O
's	O
name	O
in	O
the	O
PERSON	O
table	O
;	O
in	O
other	O
words	O
,	O
every	O
member	O
of	O
a	O
TEAM	O
is	O
also	O
a	O
PERSON	O
.	O
</s>
<s>
The	O
table	O
containing	O
the	O
foreign	B-Application
key	I-Application
is	O
called	O
the	O
child	O
table	O
,	O
and	O
the	O
table	O
containing	O
the	O
candidate	B-Application
key	I-Application
is	O
called	O
the	O
referenced	O
or	O
parent	O
table	O
.	O
</s>
<s>
In	O
database	B-Application
relational	I-Application
modeling	O
and	O
implementation	O
,	O
a	O
candidate	B-Application
key	I-Application
is	O
a	O
set	O
of	O
zero	O
or	O
more	O
attributes	B-General_Concept
,	O
the	O
values	O
of	O
which	O
are	O
guaranteed	O
to	O
be	O
unique	O
for	O
each	O
tuple	B-Application
(	O
row	O
)	O
in	O
a	O
relation	B-Language
.	O
</s>
<s>
The	O
value	O
or	O
combination	O
of	O
values	O
of	O
candidate	B-Application
key	I-Application
attributes	B-General_Concept
for	O
any	O
tuple	B-Application
cannot	O
be	O
duplicated	O
for	O
any	O
other	O
tuple	B-Application
in	O
that	O
relation	B-Language
.	O
</s>
<s>
Since	O
the	O
purpose	O
of	O
the	O
foreign	B-Application
key	I-Application
is	O
to	O
identify	O
a	O
particular	O
row	O
of	O
referenced	O
table	O
,	O
it	O
is	O
generally	O
required	O
that	O
the	O
foreign	B-Application
key	I-Application
is	O
equal	O
to	O
the	O
candidate	B-Application
key	I-Application
in	O
some	O
row	O
of	O
the	O
primary	O
table	O
,	O
or	O
else	O
have	O
no	O
value	O
(	O
the	O
NULL	B-Language
value	O
.	O
)	O
.	O
</s>
<s>
This	O
rule	O
is	O
called	O
a	O
referential	B-General_Concept
integrity	I-General_Concept
constraint	I-General_Concept
between	O
the	O
two	O
tables	O
.	O
</s>
<s>
Because	O
violations	O
of	O
these	O
constraints	O
can	O
be	O
the	O
source	O
of	O
many	O
database	O
problems	O
,	O
most	O
database	B-Application
management	I-Application
systems	I-Application
provide	O
mechanisms	O
to	O
ensure	O
that	O
every	O
non-null	O
foreign	B-Application
key	I-Application
corresponds	O
to	O
a	O
row	O
of	O
the	O
referenced	O
table	O
.	O
</s>
<s>
To	O
reflect	O
this	O
in	O
the	O
database	O
,	O
a	O
foreign	B-Application
key	I-Application
column	O
is	O
added	O
to	O
the	O
ORDER	O
table	O
(	O
e.g.	O
,	O
CUSTOMERID	O
)	O
,	O
which	O
references	O
the	O
primary	B-Application
key	I-Application
of	O
CUSTOMER	O
(	O
e.g.	O
</s>
<s>
Because	O
the	O
primary	B-Application
key	I-Application
of	O
a	O
table	O
must	O
be	O
unique	O
,	O
and	O
because	O
CUSTOMERID	O
only	O
contains	O
values	O
from	O
that	O
primary	B-Application
key	I-Application
field	O
,	O
we	O
may	O
assume	O
that	O
,	O
when	O
it	O
has	O
a	O
value	O
,	O
CUSTOMERID	O
will	O
identify	O
the	O
particular	O
customer	O
which	O
placed	O
the	O
order	O
.	O
</s>
<s>
Many	O
real	O
world	O
databases	B-Application
work	O
around	O
this	O
problem	O
by	O
'	O
inactivating	O
 '	O
rather	O
than	O
physically	O
deleting	O
master	O
table	O
foreign	B-Application
keys	I-Application
,	O
or	O
by	O
complex	O
update	B-Language
programs	O
that	O
modify	O
all	O
references	O
to	O
a	O
foreign	B-Application
key	I-Application
when	O
a	O
change	O
is	O
needed	O
.	O
</s>
<s>
Foreign	B-Application
keys	I-Application
play	O
an	O
essential	O
role	O
in	O
database	B-General_Concept
design	I-General_Concept
.	O
</s>
<s>
One	O
important	O
part	O
of	O
database	B-General_Concept
design	I-General_Concept
is	O
making	O
sure	O
that	O
relationships	O
between	O
real-world	O
entities	O
are	O
reflected	O
in	O
the	O
database	O
by	O
references	O
,	O
using	O
foreign	B-Application
keys	I-Application
to	O
refer	O
from	O
one	O
table	O
to	O
another	O
.	O
</s>
<s>
Another	O
important	O
part	O
of	O
database	B-General_Concept
design	I-General_Concept
is	O
database	B-Application
normalization	I-Application
,	O
in	O
which	O
tables	O
are	O
broken	O
apart	O
and	O
foreign	B-Application
keys	I-Application
make	O
it	O
possible	O
for	O
them	O
to	O
be	O
reconstructed	O
.	O
</s>
<s>
In	O
this	O
case	O
,	O
the	O
relationship	O
between	O
the	O
two	O
tables	O
is	O
called	O
a	O
one	B-Application
to	I-Application
many	I-Application
relationship	I-Application
between	O
the	O
referencing	O
table	O
and	O
the	O
referenced	O
table	O
.	O
</s>
<s>
the	O
foreign	B-Application
key	I-Application
refers	O
back	O
to	O
the	O
same	O
table	O
.	O
</s>
<s>
Such	O
a	O
foreign	B-Application
key	I-Application
is	O
known	O
in	O
SQL:2003	B-Language
as	O
a	O
self-referencing	O
or	O
recursive	O
foreign	B-Application
key	I-Application
.	O
</s>
<s>
In	O
database	B-Application
management	I-Application
systems	I-Application
,	O
this	O
is	O
often	O
accomplished	O
by	O
linking	O
a	O
first	O
and	O
second	O
reference	O
to	O
the	O
same	O
table	O
.	O
</s>
<s>
A	O
table	O
may	O
have	O
multiple	O
foreign	B-Application
keys	I-Application
,	O
and	O
each	O
foreign	B-Application
key	I-Application
can	O
have	O
a	O
different	O
parent	O
table	O
.	O
</s>
<s>
Each	O
foreign	B-Application
key	I-Application
is	O
enforced	O
independently	O
by	O
the	O
database	B-Application
system	I-Application
.	O
</s>
<s>
Therefore	O
,	O
cascading	O
relationships	O
between	O
tables	O
can	O
be	O
established	O
using	O
foreign	B-Application
keys	I-Application
.	O
</s>
<s>
A	O
foreign	B-Application
key	I-Application
is	O
defined	O
as	O
an	O
attribute	O
or	O
set	O
of	O
attributes	B-General_Concept
in	O
a	O
relation	B-Language
whose	O
values	O
match	O
a	O
primary	B-Application
key	I-Application
in	O
another	O
relation	B-Language
.	O
</s>
<s>
The	O
syntax	O
to	O
add	O
such	O
a	O
constraint	O
to	O
an	O
existing	O
table	O
is	O
defined	O
in	O
SQL:2003	B-Language
as	O
shown	O
below	O
.	O
</s>
<s>
Omitting	O
the	O
column	O
list	O
in	O
the	O
REFERENCES	O
clause	O
implies	O
that	O
the	O
foreign	B-Application
key	I-Application
shall	O
reference	O
the	O
primary	B-Application
key	I-Application
of	O
the	O
referenced	O
table	O
.	O
</s>
<s>
Likewise	O
,	O
foreign	B-Application
keys	I-Application
can	O
be	O
defined	O
as	O
part	O
of	O
the	O
CREATE	O
TABLE	O
SQL	O
statement	O
.	O
</s>
<s>
If	O
the	O
foreign	B-Application
key	I-Application
is	O
a	O
single	O
column	O
only	O
,	O
the	O
column	O
can	O
be	O
marked	O
as	O
such	O
using	O
the	O
following	O
syntax	O
:	O
</s>
<s>
Foreign	B-Application
keys	I-Application
can	O
be	O
defined	O
with	O
a	O
stored	B-General_Concept
procedure	I-General_Concept
statement	O
.	O
</s>
<s>
child_table	O
:	O
the	O
name	O
of	O
the	O
table	O
or	O
view	O
that	O
contains	O
the	O
foreign	B-Application
key	I-Application
to	O
be	O
defined	O
.	O
</s>
<s>
parent_table	O
:	O
the	O
name	O
of	O
the	O
table	O
or	O
view	O
that	O
has	O
the	O
primary	B-Application
key	I-Application
to	O
which	O
the	O
foreign	B-Application
key	I-Application
applies	O
.	O
</s>
<s>
The	O
primary	B-Application
key	I-Application
must	O
already	O
be	O
defined	O
.	O
</s>
<s>
col3	O
and	O
col4	O
:	O
the	O
name	O
of	O
the	O
columns	O
that	O
make	O
up	O
the	O
foreign	B-Application
key	I-Application
.	O
</s>
<s>
The	O
foreign	B-Application
key	I-Application
must	O
have	O
at	O
least	O
one	O
column	O
and	O
at	O
most	O
eight	O
columns	O
.	O
</s>
<s>
Because	O
the	O
database	B-Application
management	I-Application
system	I-Application
enforces	O
referential	B-General_Concept
constraints	I-General_Concept
,	O
it	O
must	O
ensure	O
data	O
integrity	O
if	O
rows	O
in	O
a	O
referenced	O
table	O
are	O
to	O
be	O
deleted	O
(	O
or	O
updated	O
)	O
.	O
</s>
<s>
SQL:2003	B-Language
specifies	O
5	O
different	O
referential	O
actions	O
that	O
shall	O
take	O
place	O
in	O
such	O
occurrences	O
:	O
</s>
<s>
Whenever	O
rows	O
in	O
the	O
parent	O
(	O
referenced	O
)	O
table	O
are	O
deleted	O
(	O
or	O
updated	O
)	O
,	O
the	O
respective	O
rows	O
of	O
the	O
child	O
(	O
referencing	O
)	O
table	O
with	O
a	O
matching	O
foreign	B-Application
key	I-Application
column	O
will	O
be	O
deleted	O
(	O
or	O
updated	O
)	O
as	O
well	O
.	O
</s>
<s>
This	O
is	O
called	O
a	O
cascade	O
delete	B-Language
(	O
or	O
update	B-Language
)	O
.	O
</s>
<s>
For	O
example	O
,	O
ON	O
DELETE	B-Language
CASCADE	O
effectively	O
says	O
"	O
When	O
the	O
referenced	O
row	O
is	O
deleted	O
from	O
the	O
other	O
table	O
(	O
master	O
table	O
)	O
,	O
then	O
delete	B-Language
also	O
from	O
me	O
"	O
.	O
</s>
<s>
So	O
,	O
ON	O
DELETE	B-Language
RESTRICT	O
effectively	O
says	O
:	O
"	O
When	O
someone	O
tries	O
to	O
delete	B-Language
the	O
row	O
from	O
the	O
other	O
table	O
(	O
master	O
table	O
)	O
,	O
prevent	O
deletion	O
from	O
that	O
other	O
table	O
(	O
and	O
of	O
course	O
,	O
also	O
do	O
n't	O
delete	B-Language
from	O
me	O
,	O
but	O
that	O
's	O
not	O
the	O
main	O
point	O
here	O
)	O
.	O
"	O
</s>
<s>
The	O
main	O
difference	O
between	O
NO	O
ACTION	O
and	O
RESTRICT	O
is	O
that	O
with	O
NO	O
ACTION	O
the	O
referential	B-General_Concept
integrity	I-General_Concept
check	O
is	O
done	O
after	O
trying	O
to	O
alter	O
the	O
table	O
.	O
</s>
<s>
RESTRICT	O
does	O
the	O
check	O
before	O
trying	O
to	O
execute	O
the	O
UPDATE	B-Language
or	O
DELETE	B-Language
statement	O
.	O
</s>
<s>
Both	O
referential	O
actions	O
act	O
the	O
same	O
if	O
the	O
referential	B-General_Concept
integrity	I-General_Concept
check	O
fails	O
:	O
the	O
UPDATE	B-Language
or	O
DELETE	B-Language
statement	O
will	O
result	O
in	O
an	O
error	O
.	O
</s>
<s>
In	O
other	O
words	O
,	O
when	O
an	O
UPDATE	B-Language
or	O
DELETE	B-Language
statement	O
is	O
executed	O
on	O
the	O
referenced	O
table	O
using	O
the	O
referential	O
action	O
NO	O
ACTION	O
,	O
the	O
DBMS	B-General_Concept
verifies	O
at	O
the	O
end	O
of	O
the	O
statement	O
execution	O
that	O
none	O
of	O
the	O
referential	O
relationships	O
are	O
violated	O
.	O
</s>
<s>
Using	O
NO	O
ACTION	O
,	O
the	O
triggers	B-Language
or	O
the	O
semantics	O
of	O
the	O
statement	O
itself	O
may	O
yield	O
an	O
end	O
state	O
in	O
which	O
no	O
foreign	B-Application
key	I-Application
relationships	O
are	O
violated	O
by	O
the	O
time	O
the	O
constraint	O
is	O
finally	O
checked	O
,	O
thus	O
allowing	O
the	O
statement	O
to	O
complete	O
successfully	O
.	O
</s>
<s>
In	O
general	O
,	O
the	O
action	O
taken	O
by	O
the	O
DBMS	B-General_Concept
for	O
SET	O
NULL	B-Language
or	O
SET	O
DEFAULT	O
is	O
the	O
same	O
for	O
both	O
ON	O
DELETE	B-Language
or	O
ON	O
UPDATE	B-Language
:	O
the	O
value	O
of	O
the	O
affected	O
referencing	O
attributes	B-General_Concept
is	O
changed	O
to	O
NULL	B-Language
for	O
SET	O
NULL	B-Language
,	O
and	O
to	O
the	O
specified	O
default	O
value	O
for	O
SET	O
DEFAULT	O
.	O
</s>
<s>
Referential	O
actions	O
are	O
generally	O
implemented	O
as	O
implied	O
triggers	B-Language
(	O
i.e.	O
</s>
<s>
triggers	B-Language
with	O
system-generated	O
names	O
,	O
often	O
hidden	O
.	O
)	O
</s>
<s>
As	O
such	O
,	O
they	O
are	O
subject	O
to	O
the	O
same	O
limitations	O
as	O
user-defined	O
triggers	B-Language
,	O
and	O
their	O
order	O
of	O
execution	O
relative	O
to	O
other	O
triggers	B-Language
may	O
need	O
to	O
be	O
considered	O
;	O
in	O
some	O
cases	O
it	O
may	O
become	O
necessary	O
to	O
replace	O
the	O
referential	O
action	O
with	O
its	O
equivalent	O
user-defined	O
trigger	O
to	O
ensure	O
proper	O
execution	O
order	O
,	O
or	O
to	O
work	O
around	O
mutating-table	O
limitations	O
.	O
</s>
<s>
Another	O
important	O
limitation	O
appears	O
with	O
transaction	B-General_Concept
isolation	I-General_Concept
:	O
your	O
changes	O
to	O
a	O
row	O
may	O
not	O
be	O
able	O
to	O
fully	O
cascade	O
because	O
the	O
row	O
is	O
referenced	O
by	O
data	O
your	O
transaction	O
cannot	O
"	O
see	O
"	O
,	O
and	O
therefore	O
cannot	O
cascade	O
onto	O
.	O
</s>
<s>
As	O
a	O
first	O
example	O
to	O
illustrate	O
foreign	B-Application
keys	I-Application
,	O
suppose	O
an	O
accounts	O
database	O
has	O
a	O
table	O
with	O
invoices	O
and	O
each	O
invoice	O
is	O
associated	O
with	O
a	O
particular	O
supplier	O
.	O
</s>
<s>
Then	O
,	O
the	O
'	O
supplier	O
number	O
 '	O
is	O
the	O
primary	B-Application
key	I-Application
in	O
the	O
Supplier	O
table	O
.	O
</s>
<s>
The	O
foreign	B-Application
key	I-Application
in	O
the	O
Invoice	O
table	O
points	O
to	O
that	O
primary	B-Application
key	I-Application
.	O
</s>
<s>
Primary	B-Application
keys	I-Application
are	O
marked	O
in	O
bold	O
,	O
and	O
foreign	B-Application
keys	I-Application
are	O
marked	O
in	O
italics	O
.	O
</s>
<s>
The	O
corresponding	O
Data	B-Language
Definition	I-Language
Language	I-Language
statement	O
is	O
as	O
follows	O
.	O
</s>
