Monday, January 30, 2012

How to design a simple mysql database that stores an order, user in 3rd normal formt?

I'm trying to design my first mysql database for a project, all I need to store right now is:


1. orders: user, image, simple text atributes like address, cost, etc


2. Users: username, password


3. images: filename and location





I only ever need to access a list of orders given a particular user, a list of all images, password given username








it all has to be in 3rd normal form which means "Third Normal Form (3NF) requires that all columns depend directly on the primary key. Tables violate the Third Normal Form when one column depends on another column, which in turn depends on the primary key (a transitive dependency)"





my confusion with 3rd normal form is that I'm not sure if I can depend on another column in another table, and if not does that mean that I have to store everything in the same table?








I've been a little bit confused by what I've been reading online so I'm wondering if anybody has any suggestions for how to design the table. The next step is learning how to implement in but right now I'm just in the design stage.





Thanks|||What that means is that the foreign key needs to refer back to the primary key in the other table, and only the primary key, when you can help it. In general it is also good practice to make the primary key an integer value (32-bit or 64-bit depending on the number of rows you will need to store), and set them to auto-increment. This holds even if you have unique values in the table. It makes for faster joins.





So:





Orders: OrderID (PK), UserID (FK), ImageID (FK), ...


Users: UserID (PK), ...


Images: ImageID (PK), ....





PK = Primary Key. FK = Foreign Key.





Then when you join, you simply do something similar to the following:





SELECT o.OrderID, i.FileName, u.UserName


FROM


Orders o


INNER JOIN


Images i ON o.ImageID=i.ImageID


INNER JOIN


Users u ON o.UserID=u.UserID;

No comments:

Post a Comment