简体   繁体   中英

MySQL Database; best schema

I have a question about a database schema for a certain website where people can sell and buy stuff.

When someone adds an ad to the database, he will choose the category and fills out the filters that belong to the selected category, so anyone can easily find his ad by browsing to a category and choosing filters like:

[bluetooth] yes

[camera] 3 - 5 megapixels.

Et cetera

What is the best way to store these filters in the database? This is what I have so far:

Schema http://img98.imageshack.us/img98/5089/database2.jpg

This is just an idea, but it will never send the ads back with value for filter "megapixels" between 3 and 5, since filter_value is of type VARCHAR .

I'd do with tables for:

  • ads
  • filter names (category is just one of filters)
  • filter values
  • association between ad and filter values

Store it any way you like but for efficient querying based on these filters set up Solr and use faceted searching.

The design works pretty fine according to me, I am curious as to why you have included an additional id field in the join table, eg between ads and categories. I think you could do without them. The design on the whole is pretty good because it lets you make changes to the base tables eg ads later on.

Another thing What is filter_value? and why cant it go in the filters table?

Have you considered an additional table, filter_value_list with columns id, filter_id, and value? This table would contain a list of VARCHAR answers that would be supplied to the user in the form of a drop down for each filter linked to the category of the ad that is being placed.

For the "Bluetooth" filter, the possible values would be "Yes" and "No". For the "Camera" filter, the possible values might be "Less than 1 megapixel", "1 or 2 megapixels", "3 to 5 megapixels", "5 to 10 megapixels", "More than 10 megapixels" (we're planning for future technology here).

The advantages:

  1. User selects, rather than types, their answer.

  2. Each filter is guaranteed to be understood by your system.

  3. Consistency across all advertisements.

  4. The possible value list can also be used in an "add/remove filters" sidebar like you see on many sites these days.

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