简体   繁体   中英

Optimal design for a database table to search for attributes

Hi I hope it's ok that I write this question here. I'm currently outlining a data structure that will sit in a database where there are movies, and each movie has a lot of descriptors.

I want to be able to search through the entire database and find movie X that has attribute, Y, Z and doesn't have A, B, C.

What I'm thinking is to store the descriptors/attributes like this:

Movie ID | Attribute | Has_Attribute
1        | Action    | 0
1        | Adventure | 1
1        | Comedy    | 1
2        | Action    | 1

Is this the best way to store all the attributes for a record?

Presumably for every subsequent call, I would search where Action == 0 AND Comedy == 1 ... n == n_has_attribute to begin to narrow down the search.

In the designing table, you do not need to store the attributes that do not exist. You need just to record the attributes that a movie has. Hence, your design would be like:

Movie ID | Attribute
1        | Adventure 
1        | Comedy   
2        | Action    

Moreover, if the number of attributes is not too many, you can define them as a column in the table which has a binary value:

Movie Id | Adventure | Comedy | Action 
1        | 1         | 1      | 0
2        | 0         | 0      | 1

Therefore, to choose a better data structure, you need to clarify more the space of the problem in terms of the number of attributes, number of movies.

In addition, if you need to store the data in a decision tree, the breaking point of the nodes will be the attributes and it is more like to the second tabling architecture than the first design.

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