Connecting Tech Pros Worldwide Forums | Help | Site Map

Database Design Question for a Newbie

Newbie
 
Join Date: Oct 2008
Location: Metro-Detroit Area
Posts: 11
#1: Oct 13 '08
I am attempting to build a RIA using Flex3, PHP, and MySQL. I am really a newbie when it comes to MySQL. I need some help in desiging my database for this application.

My application will allow people create a user account, and to log information they would like to keep track of. For this example, lets say they are logging bicycle rides. Each user will have one account, but will log many bike rides.

I would obviously have one master user table containing all user information, with a userid primary key. Would I need to create a seperate table for each user to log their bike rides? If this is what I should do, how would I create these tables automatically when a user registers? Here are basic examples of the tables I think I need...

USERS
userid INT PK
username VARCHAR
user_first_name VARCHAR
etc...

USER_BIKE_RIDES
userid INT
rideid INT PK
ride_date DATE
ride_distance INT
bike_used VARCHAR
etc...


Thanks, this will really help me!

Kevin

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,755
#2: Oct 14 '08

re: Database Design Question for a Newbie


Quote:

Originally Posted by djminus1

Would I need to create a seperate table for each user to log their bike rides?

No, that is usually not a good idea.

The typical way to create such a database would be to create one user table and one log table, like your example showed.

Then the two tables would be "connected" via a Foreign Key column in the log table, which would reference the userid in the user table.
This is typically called a one-to-many relation (1:N), because a single row in the user table could be linked to multiple rows in the log table.

The syntax to create such a relation could look something like:
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE `User` (
  2.   `UserID` INT Unsigned Not Null Auto_Increment,
  3.   `UserName` VarChar(255) Not Null,
  4.   /* Etc... */
  5.   Primary Key (`UserID`)
  6. )Engine=InnoDB;
  7.  
  8. CREATE TABLE `Log` (
  9.   `LogID` Int Unsigned Not Null Auto_Increment,
  10.   `UserID_FK` Int Unsigned Not Null,
  11.   `LogValue` VarChar(255) Not Null,
  12.   /* Etc... */
  13.   Primary Key (`LogID`),
  14.   Foreign Key (`UserID_FK`) REFERENCES `User`(`UserID`)
  15. )Engine=InnoDB;
  16.  
Note that I specify the InnoDB engine when I create the table in order to make sure that the Foreign Key constraint is enforced.
The MyISAM engine simply ignores them.
Newbie
 
Join Date: Oct 2008
Location: Metro-Detroit Area
Posts: 11
#3: Oct 14 '08

re: Database Design Question for a Newbie


OK...one more dumb question. Since we are assigning a foreign key to the "Logs" table to enforce the relationships, if I delete a user from the "Users" table, will that automatically remove the logs for that user from the "Logs" table?
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,755
#4: Oct 14 '08

re: Database Design Question for a Newbie


That depends.

By default, if a DELETE or UPDATE statement tries to alter data that is referenced elsewhere, it will fail with an error.

But, you can specify what action should be taken in such an event by using the ON DELETE [action] ON UPDATE [action] syntax.

Like, for example. This would instruct the tables I showed earlier to remove all logs for a user when the user is deleted from the User table, and to update all logs if a user is updated.
Expand|Select|Wrap|Line Numbers
  1. Foreign Key (`UserID_FK`) REFERENCES `User`(`UserID`)
  2. ON DELETE CASCADE ON UPDATE CASCADE
  3.  
You can also use SET NULL rather than CASCADE in order to keep the records, setting the foreign key column to null.
Note that you would have to set the column to allow nulls for this to work.
Newbie
 
Join Date: Oct 2008
Location: Metro-Detroit Area
Posts: 11
#5: Oct 14 '08

re: Database Design Question for a Newbie


Great...thanks so much.
Reply