简体   繁体   中英

Customer reviews and calendar entries, etc in a database

How would things like customer reviews be stored in a database? I cant imagine there would be rows for each item and columns for each review as one product may have 2 reviews and another may have 100+ - id presume they were stored in a separate file for reviews but then surely not one file per item! I dont know enough about storing data to be able to figure this one out by myself!

A similar situation is something like an online calendar - there is all the information about each appointment (time, duration, location, etc) and there can be many of these on each day, every day, for all users! A logical way would be to have a table for each user with all their appointments in, but at the same time that seems illogical because if you have 1000+ users, thats alot of tables!

Basically Id like to know what the common/best practice way is of storing this 'big dynamic data'.

Customer reviews can easily be stored by using two tables in one-to-many relationship.

Suppose you have a table containing products/articles/whatever worth reviewing. Each of them has an unique ID and other attributes.

Table "products"
+-------------------------------------+
| id | name | attribute1 | attribute2 |
+-------------------------------------+

Then you make another table, with its name indicating what it's about. It should contain at least an unique ID and a column for the IDs from the other table. Let's say it will also have an email of the user who submitted the review and (obviously) the review text itself:

Table "products_reviews"
+--------------------------------------------+
| id | product_id | user_email | review_text |
+--------------------------------------------+

So far, so good. Let's assume you're selling apples.

Table "products"
+-------------------------------+
| 1 | 'Apple' | 'green' | '30$' |
+-------------------------------+

Then, two customers come, each one buys one apple worth 30$ and likes it, so they both leave a review.

Table "products_reviews"
+-------------------------------------------------------------------------------+
| 1 | 2 | alice@mail.com | 'I really like these green apples, they are awesome' |
| 2 | 2 | bob@mail.com   | 'These apples rock!'                                 |
+-------------------------------------------------------------------------------+

So now all you have to do is to fetch all the reviews for your apples and be happy about how much your customers like them:

SELECT * 
FROM products_reviews 
INNER JOIN products ON products_reviews.product_id = products.id 
WHERE products.name = 'Apple';

You can now display them under the shopping page for apples (just don't mention they cost 30$).

The same principle applies for things like an online calendar. You have one table with users, and many tables with other stuff - appointments, meetings, etc. which relate to that user.

Keep in mind, however, that things like meetings are better displayed in a many-to-many table, since they are shared by many people (usually). Here's a link that visualizes it very good, and here's a question here on SO with sample code for PHP. Go ahead and test it for yourself.

Cheers :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM