Skip to main content
Inspiring
February 29, 2008
Question

Foreign Keys

  • February 29, 2008
  • 4 replies
  • 555 views
I'm a noob so please let me know if this comment should be posted elsewhere...

I creating a form to submit data to a MS database (using Ben Forta CFMX7 web app const. kit for reference). What I don't understand is the whole foreign key thing. If I'm populating form field data with reference field info but then storing the foreign key in my table once the form posts, how do I run queries on the data using the actual data rather than ID numbers?

I hope that makes sense.

Paul
This topic has been closed for replies.

4 replies

Inspiring
February 29, 2008
Mr. Angier wrote:
> What I don't understand is the whole foreign
> key thing. If I'm populating form field data with reference field info but
> then storing the foreign key in my table once the form posts, how do I run
> queries on the data using the actual data rather than ID numbers?
>
> I hope that makes sense.
>
> Paul
>

Not very much. Your question is rather broad and vague, but when one is
struggling with a new concept it can be hard to express ones problem
concisely.

Let's see if we can ferret out how much of the normalized database
design you understand. Normalizing data is the reason one would be
concerned about foreign keys.

When you normalize data you break it up so that you do not have literal
data repeated in a table, you pull out the repeated data into its own
table and reference that single record in the other table with a foreign
key.

I.E.
aTable
aField
bField
cField
FK

bTable
id
1Field
2Field
3Field

The foreign key (FK) field in aTable would hold an ID value for the
desired record from bTable. Thus when you want all the data for some
entity one would join these two tables together using these keys.

I.E.

SELECT
aField,bField,cField,1Field,2Field,3Field
FROM
aTable INNER JOIN bTable ON aTable.FK = bTable.id

Does that make sense? Does it help refine your question?

Inspiring
February 29, 2008
I have heard good things about the book, Teach Yourself SQL in 10 Minutes by Ben Forta.
Participating Frequently
February 29, 2008
You join the tables using the foreign key, then do your comparison.
Participating Frequently
February 29, 2008
Makes no sense to me at all since you have not provided any information about your data structure and what you are actually trying to do.

Phil