|
|
Example: a relation
+-------+------------+---------------+----------+-----+ | 12345 | John Doe | 123 My Ln | 123-4567 | 3.5 | +-------+------------+---------------+----------+-----+ | 22222 | James Bond | 10 Downing St | 777-7777 | 3.7 | +-------+------------+---------------+----------+-----+ | 56789 | Jane Doe | 321 Your Way | 876-5678 | 3.8 | +-------+------------+---------------+----------+-----+ |
|
Schematically:
|
|
|
Relation Schema: Student(StuID, Name, Address, Level, GPA) Tuples: t1(1234, John, 123 South Ln, 3, 3.5) t2(2222, Jake, 456 North Ln, 2, 3.8) |
The key concept in the relational model is defined in an "unusually" difficult manner (well, in a Mathematical manner)...
|
Mathematically speaking:
A set of attribute SK is a
superkey
if and only if:
|
|
I.e., a key is a minimal superkey.
|
|
Once you have chosen the primary key for some relation, you must use that primary key throughout the database (or else, you database career life will be very miserable)
|
Example:
ID in GradeReport is a foreign key:
|
CID in GradeReport is also a foreign key:
|
|
|
NOTE:
|
|
Makes perfect sense:
Suppose relation schema R1 contains a foreign key (which is the primary key) of the relation schema R2 (in other words: R1 "points to" R2). Then the following constraint must hold:
|
Pictorially explanation of Referential integrity constraint:
(This also makes perfect sense because you must point to something that exists !!!)