The title pretty much says it all. I have users posting content, and I would like to attach the user ID that is in the users table to the posts. Should I add a userid column in the posts table, should I join via the DB language, or should I use the scripting language to poll each table? If I should join, what kind of join should I do?
Sample content would look like this: “The teddy bears at my store are softer than the teddy goats, but the teddy goats are more lovable.” – Posted by James Teddyman at 3:36 PM.
A very often-used concept in situation like this is by having a users table and a post table, linking them together with a unique identifier. This identifier can be anything – a serialized id, a user name, mail address, etc – as long as it’s unique. Linking is done using a foreign key constraint. Exactly how this is achieved in MySQL I do not know, but in Postgres it’s done like this:
The tables are then merged using a join. This can be done in several ways, but here is a cross join after insertion of some values to play with:
YMMV in MySQL, but I think the constructions above will work straight off.
(edit: Added INSERTs for clarification)