Page 1 of 1

Linked Tables

PostPosted: 23. July 2007 17:50
by SirChick
How do i link tables using a particular field?

For example i have my users info each unique by the user ID which is primary key also.

If i wanted to link the userID to more info on a different table how is this done? do i just make a field in the new table exactly the same name as the other table for userID and it automatically connects the two ?


So like table one would have userid as its first field
as would table two and both have them as primary key would this make them link ?

PostPosted: 24. July 2007 23:58
by outlaw
- they will only "link" if you link them using a join operation
- if you name both fields the same (you should) then use natural join:
Code: Select all
select * from users natural join subscriptions
  --equivalent to--
select * from users u join subscriptions s on (s.userID = u.userID)



btw:
- the primary key doesn't have to be the first field
- if you will be dealing with many updates/deletes then you should consider changing some tables to innoDB. You can then map relations like this, and specify actions on update,delete operations
ie: if you delete a user, also delete everything associated with him in other tables