Hudzilla.org - the homepage of Paul Hudson
Contents > Databases > Introduction Wish List | Report Bug | About Me ]

9.1.8     Keys

This is NOT the latest copy of this book; click here for the latest version.

Some fields in tables are particularly important because they are used to define an individual record. Consider a table housing social security numbers - there should not be any duplicate numbers in there; each key is in there once and once only. In database terms we call this a primary key - you can select out one individual record from thousands by knowing its primary key.

A primary key when used as a field in another table other than its original table, is known as a foreign key. For example, in a WWW messageboard, each message must have the ID number of the person who posted a message (almost certainly the primary key in a members table), as well as the ID number of the messageboard it was posted on (almost certainly the primary key in a messageboards table). In this situation, both member ID and messageboard ID are primary keys in their own tables, but foreign keys in the messages table, because the messages primary key is the unique number of each message.

If that confused you, here's an example layout creating using Microsoft Access:



Above you can see the three tables, members, messageboards, and messages. Each has a primary key, ID, highlighted in bold. Note that MemberID and MessageBoardID exist in the messages table - these are the ID from the members table and the ID from the messageboards table respectively. Here is a picture of how that looks when linked up:



Now you can see clearly that messages has two foreign keys - one from members, and one from messageboards. Strictly speaking a foreign key is only a foreign key when defined as such, as there are special considerations to be taken into account when the primary key row is deleted from the master table - should the foreign key rows be deleted also? Having said that, if you just want to reference a primary key from another table, it works just as well in most situations.

MySQL supports primary keys completely, but not foreign keys fully as of yet. When foreign keys are supported across the board (version 5.1), you should be able to take advantage of referential integrity, which is where you can delete a record from a table, and any table which references it with a foreign key will also be updated. Primary keys are usually, but not always, unique - that is, you cannot have two values the same in a primary key column.





<< 9.1.7 Views   9.1.9 Referential integrity >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
mannic@gmail.com - 29 Aug 2008

Its not so much the data type in use but the function of that column that makes the difference. Different storage structures are able to process foreign keys other are not, those that can't generally are limited in the way they handle virtual Row IDS and how they create a virtual ( internal index based on the column )index.

JMH - 29 Aug 2008

singpolyma AT gmail.com

Sure it's just an INT, thats it's data type. ID gets treated differently if it is also defined as a primary/foreign key.

uggedal_at_gmail.com - 29 Aug 2008

He means that foreign key constraints are not supported in MyISAM, ISAM and HEAP tables, only in InnoDB tables.

singpolyma AT gmail.com - 29 Aug 2008

What do you mean foreign keys are not fully supported? A foreign key is just an INT with the ID from the other table stored in it...



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is one plus four?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow