Page 1 of 1

using same id ref. to insert multiple rows on a diff. table

Posted: Tue Dec 06, 2005 2:25 pm
by will83
Ok,

I have 2 tables.

Per order, the first table will have one entry where as the second table will have 2 or three but i need to some how keep reference that all entries are related for that order.

I tried last_insert_id but fond that it is not suitable.

Any ideaS?

Posted: Wed Dec 07, 2005 8:21 am
by Hyarion
I take it you've got something similar to:

Table 1:
SalesGuy

Table 2:
Orders

Then when using the database each "SalesGuy" would have multiple entries in the Orders table and you need to know how to keep track of which orders belong to which sales person?

Try this:

Code: Select all

Table1 Structure
[SALESREP]
ID
Name
Surname
Etc


Table 2 Structure
[ORDERS]
S_ID
Date
Etc.
The s_id field in orders will always be the same as the ID field in salesrep table. So any entries in the second table can always be linked back to the first table. One *could* link to the Name field in the first table, but in my example that wouldn't be good as you might get two or more Johns or some other common name.

Posted: Wed Dec 07, 2005 5:14 pm
by timvw
keywords for websearch: "foreign key".