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 > Problem designing a database (Normalization)

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-09-11, 03:49
Atomic_Sam Atomic_Sam is offline
Registered User
 
Join Date: Nov 2011
Posts: 3
Problem designing a database (Normalization)

Hi all,

I plan to design a website to learn languages where users can regsiter. I have thought of two options to create my database, I don't know if there are more.

First option:

I use only one table.

"users" :

id | name | email | city | english | french | German

Second option:

I use three tables.

"users" :

id | name | email | city

"userlanguages":

id | iduser | idlanguage

"languages":

id | language

The point is that number of languages will increase in the future and I want to know which advantages/disadvantages each option will offer.

Thank you very much,

Atomic_Sam
Reply With Quote
  #2 (permalink)  
Old 11-09-11, 06:46
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
the properly normalized structure is ~way~ better

please note that the "userlanguages" table should ~not~ have its own auto_increment
Code:
CREATE TABLE user_languages 
( user_id INTEGER NOT NULL
, lang_id INTEGER NOT NULL 
, PRIMARY KEY ( user_id, lang_id )
, INDEX language_users ( lang_id, user_id )
, FOREIGN KEY ( user_id ) REFERENCES users ( id )
, FOREIGN KEY ( lang_id ) REFERENCES languages ( id )
);
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 11-10-11, 09:54
Atomic_Sam Atomic_Sam is offline
Registered User
 
Join Date: Nov 2011
Posts: 3
Thank you very much r937 for your quick response.

I'll take the second solution.

So the user_languages primary key is composed by two fields and then it's not necessary to use its own auto_increment primary key, right? Becuase it's redundant. It's a more professional way.

What does " , INDEX language_users ( lang_id, user_id )
, FOREIGN KEY ( user_id ) REFERENCES users ( id )
, FOREIGN KEY ( lang_id ) REFERENCES languages ( id )
" do ?

Thank you very much again,
Atomic_Sam
Reply With Quote
  #4 (permalink)  
Old 11-10-11, 10:21
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by Atomic_Sam View Post
So the user_languages primary key is composed by two fields and then it's not necessary to use its own auto_increment primary key, right?
correct

Quote:
Originally Posted by Atomic_Sam View Post
What does " , INDEX language_users ( lang_id, user_id )
, FOREIGN KEY ( user_id ) REFERENCES users ( id )
, FOREIGN KEY ( lang_id ) REFERENCES languages ( id )
" do ?
the first line creates an index on the two columns but in reverse sequence from the primary key

the second and third lines create foreign key relationships to parent tables
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 11-10-11, 17:42
Atomic_Sam Atomic_Sam is offline
Registered User
 
Join Date: Nov 2011
Posts: 3
Thank you very much!!!!!
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