Jump to content

Table Joins or Association Tables?


gigantorTRON

Recommended Posts

Obviously the answer to this question is heavily dependent upon the situation, but, generally, is it better to create separate tables and use JOIN syntax to join them and pull data, or is it better to use an 'association' table that contains ID associations? Or, is there an even better alternative that I'm not aware of?

 

Thanks!

Link to comment
Share on other sites

Yeah.

What I'm referring to would be a table that associates PKs between tables. This would allow you to lookup data across tables by first checking the associations table, pulling the needed primary key and then using that to search another table rather than doing multiple joins.

 

I guess it would be handy for complex DB and speed up performance on SELECT queries?

Link to comment
Share on other sites

Let's take a common example. You are a company and a customer places several orders. Each order has just the one customer but each customer can have many orders.

 

You have, therefore, a simple 1-to-many relationship between customer and order and you just need ro put the custID in order to associate it with its custoner.

[pre]

customer              order

==========            ===========

custID    ---------< custID

cust_name            ordernum

etc                  orderdate

[/pre]

With the orders it now gets a little more complicated. Each order can be for sevaral products and each product can be ordered on several diffferent order.

 

You now have a many-to-many relationship between order and product. To handle this you need an "association" table (order_item)

[pre]

 

order                order_item              product

===========          ===========              ===========

custID        +----<  ordernum        +------  prodID

ordernum  ---+      prodID  >------+        description

orderdate            quantity                price

                                              etc

[/pre]

 

Whether you have the simple cust-order relationship or the more complex order-item-product relationship, you still need JOINs to pull the data using SQL queries

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.