Thursday, February 9, 2012

(Simple) SQL question regarding P/F keys and inserting data...

Hi, all:

I'm a newbie trying to understand the concept of referential integrity and
dealing with Primary and Foreign Keys. I'm sure mine is a simple problem...

I've created 3 tables as follows:

MemberTable
--
1 Member ID (Primary key)
2 Member Name
3 etc...

FoodsTable
--
1 Food ID (Primary key)
2 Food Name

MemberFoods
--
1 Member ID (Foreign key)
2 Food ID (Foreign key)

Now, I've just learned about referential integrity and all that, and this is
my first foray into creating primary/foreign keys, linking tables, etc. (so
I'm assuming the above tables are fine). What I don't understand is this:
if I have a (checkbox) form from which members can choose their favorite
foods, how do I insert that data? For example:

Food Form
--
Which of these are your favorite foods:
1) Steak
2) Chicken
3) Potatoes
4) etc...

If I want to insert, let's say, 'Steak' and 'Chicken', do I have to first do
a query on my Foods table to retrieve the Food ID's for Steak and Chicken,
and then do an "insert" using those ID's into my MemberFoods table? (I am
using the actual Food Names (not ID's) as values on my form.) If so, can
someone please share the SQL for retrieving and then inserting?

I don' t know...maybe I'm overcomplicating the issue, but it just seems like
a lot of extra work to maintain a "linking table" of foreign keys.

Thanks for the help.Replied in microsoft.public.sqlserver.programming

Please don't multi-post.

--
David Portas
----
Please reply only to the newsgroup
--

No comments:

Post a Comment