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 > Database design

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-23-11, 18:48
rzwarts rzwarts is offline
Registered User
 
Join Date: Jan 2011
Posts: 2
Database design

I am faced with a design problem within my database where I end up in a Many to MANY relationship....

I have two tables (simplified)

Users: userid,username
Codes: code,description

Now I need to keep track of what codes have been handed out to each user as one user may only receive the code once.

How do I go about that without ending up with one big table ? Is there a smarter way to do this ?

Raymond
Reply With Quote
  #2 (permalink)  
Old 01-23-11, 20:18
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
you need a third table, into which you will place the ids of the user and the code
Code:
CREATE TABLE usercodes
( userid INTEGER NOT NULL REFERENCES  users (userid )
, code VARCHAR(9) NOT NULL REFERENCES codes (code )
, PRIMARY KEY ( userid , code )
);
the PK ensures that each user can get any given code only once
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 01-24-11, 06:35
rzwarts rzwarts is offline
Registered User
 
Join Date: Jan 2011
Posts: 2
My problem is that this table will get VERY huge... 10.000 codes times say 500 users...

Is there a more intelligent solution because I already thought of this...

Quote:
Originally Posted by r937 View Post
you need a third table, into which you will place the ids of the user and the code
Code:
CREATE TABLE usercodes
( userid INTEGER NOT NULL REFERENCES  users (userid )
, code VARCHAR(9) NOT NULL REFERENCES codes (code )
, PRIMARY KEY ( userid , code )
);
the PK ensures that each user can get any given code only once
Reply With Quote
  #4 (permalink)  
Old 01-24-11, 06:45
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
Quote:
Originally Posted by rzwarts View Post
Is there a more intelligent solution because I already thought of this...
you might have thought of it, but you are worrying needlessly, because that ~is~ the intelligent solution

10,000 codes times 500 users is only 5,000,000 rows, which is a routine medium-sized table

properly indexed, there is no performance problem

try it

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 02-10-11, 06:12
Hocker Hocker is offline
Registered User
 
Join Date: Dec 2010
Posts: 2
Suppose we have the following scheme for our library database (assuming some simplifications). Old loan data are preserved. For every book, we only have one occurrence.
Book(bno, title, author, publisher)
Reader(rno, name, address, city)
Loan(bno, rno, loan-date, return-date)
This scheme of our library database does not support the possibility to have several occurrences of the same book. Extend the scheme to do so, using SQL/DDL.
Reply With Quote
  #6 (permalink)  
Old 02-10-11, 06:46
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
please don't hijack threads, by all means add to the thread if you are propsing an answer, or if you have a seemingly identical problem, but please don't tack on any question onto someone else's thread.

why?
becuase the OP may not have got an answer, and your hijack can make thsat even less likely
some posters base their responses ont he thread title, and they may not see your posting or may not interpret as one they can answer

so new question, new thread
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
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