If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Association table linking several tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-12-10, 16:01
pietb pietb is offline
Registered User
 
Join Date: Jun 2010
Posts: 6
Association table linking several tables

Hi all,

I need to create an association table that links several tables. Here is what I’ve got followed by what I think might be a good answer. But, I’d like you to chime in and provide feedback, particularly if there is a better way.

I have several “modules” on my site (i.e. users, accounts, teams, projects, etc.) that all need the ability to upload documents. I am looking to create one association table that links my modules to my documents. Here is the solution I came up with.

I have my existing modules:

users (user_id, name, etc.)
accounts (account_id, name, etc.)
teams (team_id, name, etc.)
projects (project_id, name, etc.)

I also have my documents table:

documents (doc_id, name, etc.)

I created a modules table so that I can have an id for each module (alternatively, I could create these as constants in my code):

modules (mod_id, name)
1 Users
2 Accounts
3 Teams
4 Projects

Then I created an association table to link the documents to my modules:

documents_xref (doc_id, mod_id, mod_entry_id)

100 1 23 | document w/id=100 in the user(1) modules w/user_id=23
101 2 5 | document w/id=101 in the accounts(2) module w/ account_id=5
102 4 568 | document w/id=102 in the project(4) modules w/project_id = 568

Is this an acceptable way of doing this?

Thanks in advance.
Piet
Reply With Quote
  #2 (permalink)  
Old 06-12-10, 16:50
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by pietb View Post
Is this an acceptable way of doing this?
no

use a separate association table for each "module"
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On