View Single Post
Old 02-26-2013, 11:53 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
This sounds like a many to one relationship which you intend to keep track of correct?
The purpose of the foreign key constraint is a preemptive effort to maintain the integrity of the data within the database (ie: orphan control). So for examples, if I had two tables (ignoring the third as the user table) called gamecodes and usergamecodes, I would have gamecodes with even a single field, which I'll call gamecode. My records look like so:
Code:
AAA
BBB
CCC
So three game codes in total.
Now I have the second table, usergamecodes (also with a key constraint to user), with the columns: userid, gamecodes, and lets say, dateclaimed. userid and gamecode are both foreign keys, and are a composite primary key. My data looks like so:
Code:
1 | AAA | 2013-01-17
1 | CCC | 2013-01-22
2 | AAA | 2013-02-14
3 | AAA | 2012-12-12
If I attempt to insert 1, DDD, 2013-02-14 for example, I would receive the error back from SQL server with the errno value of (lets see if I can get these right from the manual :P), 1452 which is a key constraint violation. This occurred since table gamecode does not include a gamecode of DDD.
If I were to attempt to insert 1, CCC, 2013-02-14, I would receive an error 1062 since userid 1 and gamecode CCC primary keys are already in use (ie: duplicate).

The thing I'm not sure of here is if I'm hung up on the INSERT part of your title or not. I'm not sure if these actually need inserting (hence my first reply indicating to just select if you don't need to insert anything).
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
bemore (02-28-2013)