Sie sind auf Seite 1von 7

Diference between Primary Key & Foreign Key

Primary Key
Foreign Key
Primary key uniquely identify a record in the table.
Foreign key is a feld in the table that is primary key in another table.
Primary Key cant accept null !alues.
Foreign key can accept multiple null !alue.
"y default# Primary key is clustered inde$ and data in the database table is physically
organi%ed in the sequence of clustered inde$.
Foreign key do not automatically create an inde$# clustered or non&clustered. 'ou can
manually create an inde$ on foreign key.
(e can ha!e only one Primary key in a table.
(e can ha!e more than one foreign key in a table.
Defne Primary key and Foreign key
1. --Create Parent Table
2. CREATE TABLE Department
3. (
4. DeptID int PRIMARY KEY, --define primary key
5. Name ar!"ar (5#$ N%T N&LL,
'. A((re)) ar!"ar(1##$ N&LL
*. $
+. ,%
-. --Create Child Table
1#. CREATE TABLE Emp./0ee
11. (
12. EmpID int PRIMARY KEY, --define primary key
13. Name ar!"ar (5#$ N%T N&LL,
14. 1a.ar0 int N&LL,
15. --define foreign key
1'. DeptID int 2%REI,N KEY RE2ERENCE1 Department(DeptID$
1*. $
Note
)s *+arc ,ellinek suggested# - would like to add the below points about foreign key .
/. Foreign keys do not automatically create an inde$# clustered or non&clustered.
'ou must manually create an inde$ on foreign keys.
0. 1here are actual ad!antages to ha!ing a foreign key be supported with a
clustered inde$# but you get only one per table. (hats the ad!antage2 -f you are
selecting the parent plus all child records# you want the child records ne$t to each other.
1his is easy to accomplish using a clustered inde$.
3. 4a!ing a null foreign key is usually a bad idea. -n the e$ample below# the record
in 5dbo6.5child6 is what would be referred to as an 7orphan record7. 1hink long and hard
before doing this.
1. I2 E3I1T1 (1ELECT 4 2R%M 5)0)6.5)!"ema)6 5)!"6 INNER 7%IN 5)0)6.
5ta8.e)6 5t8.6 %N 5)!"6.5)!"ema9i(6 : 5t8.6.5)!"ema9i(6 ;<ERE 5)!"6.
5name6 : =(8/= AND 5t8.6.5name6 : =!"i.(=$
2. DR%P TABLE 5(8/6.5!"i.(6
3. I2 E3I1T1 (1ELECT 4 2R%M 5)0)6.5)!"ema)6 5)!"6 INNER 7%IN 5)0)6.
5ta8.e)6 5t8.6 %N 5)!"6.5)!"ema9i(6 : 5t8.6.5)!"ema9i(6 ;<ERE 5)!"6.
5name6 : =(8/= AND 5t8.6.5name6 : =parent=$ DR%P TABLE 5(8/6.
5parent6
4. CREATE TABLE 5(8/6.5parent6
5. (
'. 5i(6 5int6 IDENTITY N%T N&LL,
*. 5name6 5ar!"ar6(25#$ N%T N&LL,
+. C%N1TRAINT 5PK9(8/99parent6 PRIMARY KEY N%NCL&1TERED (5i(6$
-. $
1#. CREATE TABLE 5(8/6.5!"i.(6
11. (
12. 5i(6 5int6 IDENTITY N%T N&LL, 5parent9i(6 5int6 N&LL,
13. 5name6 5ar!"ar6(25#$ N%T N&LL,
14. C%N1TRAINT 5PK9(8/99!"i.(6 PRIMARY KEY N%NCL&1TERED (5i(6$,
15. C%N1TRAINT 52K9(8/99!"i.(99(8/99parent6 2%REI,N KEY (5parent9i(6$
RE2ERENCE1 5(8/6.5parent6(5i(6$
1'. $
1*. --Insert data
1+. IN1ERT INT% 5(8/6.5parent6 (5name6$ >AL&E1 (=parent1=$
1-. IN1ERT INT% 5(8/6.5!"i.(6 (5parent9i(6, 5name6$>AL&E1(1, =!"i.( 1=$
2#. IN1ERT INT% 5(8/6.5!"i.(6 (5parent9i(6, 5name6$>AL&E1(N&LL, =!"i.(
2=$
21. --Select data
22. 1ELECT 4 2R%M 5(8/6.5!"i.(6
Summary
- hope you will en8oy these tricks while programming with 9:; 9er!er. - would like to
ha!e feedback from my blog readers. 'our !aluable feedback# question# or comments
about this article are always welcome.
Database Keys
Introduction
For the purposes of clarity we will refer to keys in terms of RDBMS tables but the same
definition, principle and naming applies equally to Entity Modelling and ormalisation!
"eys are, as their name suggests, a key part of a relational database and a #ital part of the
structure of a table! $hey ensure each record within a table can be uniquely identified by one or a
combination of fields within the table! $hey help enforce integrity and help identify the
relationship between tables! $here are three main types of keys, candidate keys, primary keys
and foreign keys! $here is also an alternati#e key or secondary key that can be used, as the name
suggests, as a secondary or alternati#e key to the primary key
Super Key
% Super key is any combination of fields within a table that uniquely identifies each record
within that table!
Candidate Key
% candidate is a subset of a super key! % candidate key is a single field or the least combination
of fields that uniquely identifies each record in the table! $he least combination of fields
distinguishes a candidate key from a super key! E#ery table must ha#e at least one candidate key
but at the same time can ha#e se#eral!
%s an e&ample we might ha#e a student'id that uniquely identifies the students in a student table!
$his would be a candidate key! But in the same table we might ha#e the student(s first name and
last name that also, when combined, uniquely identify the student in a student table! $hese would
both be candidate keys!
)n order to be eligible for a candidate key it must pass certain criteria!
)t must contain unique #alues
)t must not contain null #alues
)t contains the minimum number of fields to ensure uniqueness
)t must uniquely identify each record in the table
*nce your candidate keys ha#e been identified you can now select one to be your primary key
Primary Key
% primary key is a candidate key that is most appropriate to be the main reference key for the
table! %s its name suggests, it is the primary key of reference for the table and is used throughout
the database to help establish relationships with other tables! %s with any candidate key the
primary key must contain unique #alues, must ne#er be null and uniquely identify each record in
the table!
%s an e&ample, a student id might be a primary key in a student table, a department code in a
table of all departments in an organisation! $his module has the code D+,D ,- that is no doubt
used in a database somewhere to identify RDBMS as a unit in a table of modules! )n the table
below we ha#e selected the candidate key student'id to be our most appropriate primary key
.rimary keys are mandatory for e#ery table each record must ha#e a #alue for its primary key!
/hen choosing a primary key from the pool of candidate keys always choose a single simple key
o#er a composite key!
Foreign Key
% foreign key is generally a primary key from one table that appears as a field in another where
the first table has a relationship to the second! )n other words, if we had a table % with a primary
key 0 that linked to a table B where 0 was a field in B, then 0 would be a foreign key in B!
%n e&ample might be a student table that contains the course'id the student is attending! %nother
table lists the courses on offer with course'id being the primary key! $he 1 tables are linked
through course'id and as such course'id would be a foreign key in the student table!
Secondary Key or Alternative Key
% table may ha#e one or more choices for the primary key! 2ollecti#ely these are known as
candidate keys as discuss earlier! *ne is selected as the primary key! $hose not selected are
known as secondary keys or alternati#e keys!
For e&ample in the table showing candidate keys abo#e we identified two candidate keys,
student)d and firstame 3 lastame! $he student)d would be the most appropriate for a primary
key lea#ing the other candidate key as secondary or alternati#e key! )t should be noted for the
other key to be candidate keys, we are assuming you will ne#er ha#e a person with the same first
and last name combination! %s this is unlikely we might consider fistame3lastame to be a
suspect candidate key as it would be restricti#e of the data you might enter! )t would seem a
shame to not allow 4ohn Smith onto a course 5ust because there was already another 4ohn Smith!
Simple Key
%ny of the keys described before 6ie primary, secondary or foreign7 may comprise one or more
fields, for e&ample if firstame and lastame was our key this would be a key of two fields
where as student)d is only one! % simple key consists of a single field to uniquely identify a
record! )n addition the field in itself cannot be broken down into other fields, for e&ample,
student)d, which uniquely identifies a particular student, is a single field and therefore is a simple
key! o two students would ha#e the same student number!
Compound Key
% compound key consists of more than one field to uniquely identify a record! % compound key
is distinguished from a composite key because each field, which makes up the primary key, is
also a simple key in its own right! %n e&ample might be a table that represents the modules a
student is attending! $his table has a student)d and a module2ode as its primary key! Each of the
fields that make up the primary key are simple keys because each represents a unique reference
when identifying a student in one instance and a module in the other!
Composite
% composite key consists of more than one field to uniquely identify a record! $his differs from a
compound key in that one or more of the attributes, which make up the key, are not simple keys
in their own right! $aking the e&ample from compound key, imagine we identified a student by
their firstame 3 lastame! )n our table representing students on modules our primary key
would now be firstame 3 lastame 3 module2ode! Because firstame 3 lastame represent a
unique reference to a student, they are not each simple keys, they ha#e to be combined in order
to uniquely identify the student! $herefore the key for this table is a composite key!

Das könnte Ihnen auch gefallen