PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > Diference betwen Foreign Key and Primary Foreign Key
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Diference betwen Foreign Key and Primary Foreign Key

Réponse
 
LinkBack Outils de la discussion
Vieux 14/02/2008, 08h50   #1
froditus@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Diference betwen Foreign Key and Primary Foreign Key

Hallo everyone,

Basicly i am new in database design. I've been designing a database
for my personal project application. i'm using Database design / ER
design software Toad Data Modelere Freeware.
the software devine a Foreign key (FK) and Primary Foreign Key (PFK).

If using Identifying Relationship between two tables then the Primary
Key (PK) on table one will identified as Primary Foreign Key(PFK) on
table two.

If using Non Identifying Relationship then the Primary Key (PK) on
table one will identified as Foreign Key(FK) on table two.

My question is what is the diference between Foreign Key and Primary
Foreign Key.

Thank you for your attention.
God Bless.
  Réponse avec citation
Vieux 14/02/2008, 10h27   #2
Toby A Inkster
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Diference betwen Foreign Key and Primary Foreign Key

froditus@gmail.com wrote:

> My question is what is the diference between Foreign Key and Primary
> Foreign Key.


Not much distinction is normally drawn between the two concepts.

Essentially a primary foreign key (PFK) is a column or group of columns
that is both a foreign key (FK) *and* the primary key (PK) for its table.

Consider a table of employees for a company. It might be set up like this:

CREATE TABLE employees (
employee_number integer NOT NULL,
surname varchar NOT NULL,
forenames varchar NOT NULL,
position varchar,
dept varchar,
recruitment timestamp,
termination timestamp,
payroll_number integer NOT NULL
);

A candidate key column is any column (or combination of columns) which is
going to be non-null for all rows, and unique for each row. In this
example, two columns jump out as possibilities: employee_number and
payroll_number.

Now, while both of these columns are keys, database design theory asks us
to choose one as the *primary* key. Here let's choose employee_number.

ALTER TABLE employees
ADD PRIMARY KEY (employee_number);
ALTER TABLE employees
ADD UNIQUE (payroll_number);

Now let's add another table for storing notes on each employee, such that
multiple notes can be stored against each person. We'll need to include a
way of matching up entries in the new employee_notes table to entries in
the employees table, so we include an employee_number columns:

CREATE TABLE employee_notes (
employee_number integer NOT NULL,
note_id integer NOT NULL,
note varchar NOT NULL,
added_on timestamp,
added_by integer,
PRIMARY KEY (employee_number, note_id)
);

Now, imagine that with have three employees in the employees table with
employee_numbers of 100, 200 and 300. What is to stop us from inserting
this row into the employee_notes table?

INSERT INTO employee_notes
VALUES (400, 1, 'Hello world', CURRENT_TIMESTAMP(), 100);

Nothing stops us, so we introduce an FK constraint. We say that the
employee_notes.employee_number number field has to reference an extant
record from the employees table:

ALTER TABLE employee_notes
ADD FOREIGN KEY (employee_number)
REFERENCES employees (employee_number)
ON UPDATE CASCADE
ON DELETE RESTRICT;

This establishes a FK-to-PK relationship from employee_notes to employees.
(Further it tells the database that if Joe Blogg's employee number in the
employees table changes from 100 to 101, then the change should be
cascaded to the employee_notes table, and if someone tries to delete Joe
Bloggs from the employees table, creating orphan records in the
employee_notes table, this should result in an error. An alternative would
be to simply cascade the delete too.)

In this case, because the target of the relationship
(employees.employee_number) is a PK, we could have used a slightly
abbreviated syntax when creating the relationship:

ALTER TABLE employee_notes
ADD FOREIGN KEY (employee_number)
REFERENCES employees
ON UPDATE CASCADE
ON DELETE RESTRICT;

We'll probably also want to make sure that the added_by column references
a real employee:

ALTER TABLE employee_notes
ADD FOREIGN KEY (added_by)
REFERENCES employees
ON UPDATE CASCADE
ON DELETE SET NULL;

Also, let's record employee salaries. For tax purposes we need to keep
track of historic salary levels.

CREATE TABLE salaries (
payroll_number integer NOT NULL,
salary integer NOT NULL,
valid_from timestamp NOT NULL,
valid_to timestamp,
PRIMARY KEY (payroll_number, valid_from)
);

And now we can set up a foreign key relationship to the employees table:

ALTER TABLE salaries
ADD FOREIGN KEY (payroll_number)
REFERENCES employees (payroll_number)
ON UPDATE CASCADE
ON DELETE RESTRICT;

Note that on the previous table we established a FK-to-PK relationship.
Here we're establishing a FK-to-AK relationship. (AK = alternative key.)

So now we've set up two foreign keys, but so far no primary foreign keys.
For our last table we'll set up a primary foreign key:

CREATE TABLE current_management (
employee_number integer,
responsibility varchar,
PRIMARY KEY (employee_number),
FOREIGN KEY (employee_number) REFERENCES employees
ON UPDATE CASCADE
ON DELETE CASCADE
);

Notice that employee_number is both the PK of current_management and also
a FK referencing the employees table. Thus the column employee_number is
the PFK of the current_management table.

An implication of this is that the current_management table will never
have more rows than the employees table.

Also of interest, of the following three queries:

SELECT * FROM employees NATURAL JOIN employee_notes;
SELECT * FROM employees NATURAL JOIN salaries;
SELECT * FROM employees NATURAL JOIN current_management;

only the last of them can be guaranteed to *never* return a result set
that lists the same employee twice. This is because of the PFK-to-PK
relationship that exists between the two tables -- which is conceptually
an even stronger tie than FK-to-PK.

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 15 days, 15:23.]

Mince & Dumplings
http://tobyinkster.co.uk/blog/2008/0...nce-dumplings/
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 22h56.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11782 seconds with 10 queries