473,395 Members | 1,986 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Database Design Question for a Newbie

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
Oct 13 '08 #1
4 1721
Atli
5,058 Expert 4TB
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.
Oct 13 '08 #2
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?
Oct 14 '08 #3
Atli
5,058 Expert 4TB
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.
Oct 14 '08 #4
Great...thanks so much.
Oct 14 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Lane Beneke | last post by:
All, New to the list and a relative newbie to PostgreSQL. Please forgive stupid questions. Designing an application server for a work order processing (et al) database. I have a good handle...
6
by: Chris Tyson | last post by:
I created an Access 97 database a while ago. It is protected by Workgroup Security, and it is not split. In order to compare different sets of results, a colleague has suggested that it would be...
1
by: sylviams | last post by:
I need a database to record students names etc and also to record their levels at six times during the year for a range of subjects. Obviously there can only be one entry per student subject per...
3
by: DM | last post by:
newbie trying to set up a Database and keep getting the same result. ERROR An error occured while retrieving the information from the database: Unable to cast COM object of type...
2
by: Mr Newbie | last post by:
I've just spent some considerable time putting together an application which works pretty much OK. The issue I have around design is mainly concerned with the Data Access layer. Yes, we can pull...
6
by: Jchick | last post by:
Im a newbie with a sql table in a MSDE database that contains fields of CustName, BrokerName, Type, Status. I am trying to write a trigger that does this: 1. When a new record in a table is...
3
by: ryan | last post by:
Lets say i have a database where users can upload their dvd collection. I want to be able to do a query where i can select all the users who have a certain DVD. Can someone point me in the right...
1
by: | last post by:
Hi. This is a a semi-newbie question about how to store arbitrary information about my apps such that I can code quickly, mimizing complexity and the number of things I have to hold in my brain. I...
47
by: Jo | last post by:
Hi there, I'm Jo and it's the first time I've posted here. I'm in process of creating a database at work and have come a little unstuck.....I'm a bit of a novice and wondered if anyone could...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.