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

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
will83
Forum Commoner
Posts: 53
Joined: Thu Nov 10, 2005 3:13 pm

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

Post 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?
Hyarion
Forum Newbie
Posts: 12
Joined: Tue Nov 01, 2005 4:30 am
Location: South Africa

Post 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.
Last edited by Hyarion on Thu Dec 08, 2005 4:04 am, edited 1 time in total.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

keywords for websearch: "foreign key".
Post Reply