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 > General > Database Concepts & Design > DB design question

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-20-11, 15:20
unzip unzip is offline
Registered User
 
Join Date: May 2004
Posts: 14
DB design question

I have three tables
1. USERS (UID, USER_NAME)
2. AGENCY (AID, AGENCY_NAME)
3. GROUP (GID, GROUP_NAME)
and I have a case where each agency have groups and each group can have users. So, agency is super set which consist of group and group itself have users.
Now I am wondering what field to put in the new table(s) so that i can make following different queries.
1. List the users in the group in the agency.
2. List the groups in the agency.

Currently I have single table GROUPING(ID, AGENCY_ID, GROUP_ID, USER_ID). Easy with this is I can do query#1. I can do second query also but is this a good design?

Is there any optimum/efficient design for this situation?

Any help will be appreciated.
Reply With Quote
  #2 (permalink)  
Old 10-05-11, 13:56
shureg shureg is offline
Registered User
 
Join Date: Oct 2011
Location: Hamburg, Germany
Posts: 15
i would expand a tables with a foregn keys like this

1.USERS (UID, USER_NAME,GID)
2. AGENCY (AID, AGENCY_NAME)
3. GROUP (GID, GROUP_NAME,AID)

or it is not possible?
Reply With Quote
  #3 (permalink)  
Old 10-05-11, 14:32
unzip unzip is offline
Registered User
 
Join Date: May 2004
Posts: 14
can't modify the USER table

Sorry forgot to mention. Can't modify USER table.
Reply With Quote
  #4 (permalink)  
Old 10-05-11, 14:40
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
if you cannot modify the USER table to carry the group foreign key, then you will have to create a one-to-one table that can

agency (aid, agency_name)

group (gid, group_name, aid )

users ( uid, user_name)

user_group ( uid, gid )

KEY: tablename ( primarykey, foreignkey )
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 10-07-11, 11:43
unzip unzip is offline
Registered User
 
Join Date: May 2004
Posts: 14
Advantage

So it seems that same query
Code:
SELECT u.user_name, g.group_name
FROM group g, users u, user_group ug
WHERE g.aid='123' AND g.gid=ug.gid AND u.uid=ug.uid
works for both schema.

The only difference (or advantage??) I see is having user_group table one less column and adding 'aid' in group.

Let me know if any other. I am eager to learn "BASIC" of Database

Thanks
Reply With Quote
Reply

Tags
multiple design cross

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