Advice on structuring or normalizing my MySQL data needed
I'm building a web application and I just received some basic foundation data from the client in a sp开发者_Python百科readsheet. I need to get the data into a MySQL database in a meaningful way so I can effectively query it later. What makes the most sense? Here's the particulars:
There are Departments, Classes *edit (classes are types of products like mens t-shirts, womens jeans etc...) and Vendors (and Products eventually...). 25 Departments, 300 Classes and 3300 Vendors. Departments share Classes as well as Vendors in some cases.
My web application will have Department pages that will display a list of vendors who have products that appear in the chosen department with a link to a vendor specific page. The vendor page will list their products sorted by class.
I could set up 3 or 4 tables I suppose but will that result in a well formed, efficient query when I build my application? Also, will that make for a normalized database that avoids update, delete anomalies?
This could really be more a question of how to select my data rather than how to structure my DB, but I figured I would start with structure and move to the query optimization.
If I assume correctly that Classes are categories of products, and a product can belong to a single class:
+================================+ +================================+
| departments | | vendors |
+----+------+--------------------+ +----+------+--------------------+
| id | name | others | | id | name | others |
+====+======+====================+ +====+======+====================+
+================================+ +================================+
| classes | | products |
+----+------+--------------------+ +----+----------+----------------+
| id | name | others | | id | class_id | name | others |
+====+======+====================+ +====+==========+================+
+================================+ +================================+
| departments_vendors | | classes_departments |
+----+----------+----------------+ +----+---------------+-----------+
| id | department_id | vendor_id | | id | department_id | class_id |
+====+==========+================+ +====+===============+===========+
+================================+
| products_vendors |
+----+------------+--------------+
| id | product_id | vendor_id |
+====+============+==============+
If products can belong to multiple classes, drop the class_id
column and make a new table called classes_products
.
Assuming the above structure, here is a sample query.
Get one department, that department's classes, and products for those classes:
SELECT departments.id AS DepartmentId,
departments.name AS DepartmentName,
classes.id AS ClassId,
classes.name AS ClassName,
products.id AS ProductId,
products.name AS ProductName
FROM departments
LEFT JOIN classes_departments
ON classes_departments.department_id = departments.id
LEFT JOIN classes
ON classes.id = classes_departments.class_id
LEFT JOIN products
ON products.class_id = classes.id
WHERE departments.id = ##
The best way to get a list of vendors who sell products from a class for a department would be to make another table called classes_vendors
精彩评论