database design - How to store complex data structures in MySQL and prevent duplicates? -


mysql 5.5

i writing system storing queries.

every time person submits search, can saved in database. there can additional actions connected search.

i.e. "look rows color=red or color=blue , change color_type 'primary color'"

my problem is, because of number of tables involved, there nothing preventing same query being saved twice.

the essential part of query query itself, rather action attached.

for example, if happens enter same query above using action "change color_hue 90%", action should attached previous search.

as of now, create duplicate search.

of course, easy if saved in single table indexes, it's not.

i have:

search_terms id | search_terms (varchar 255 unique)  searches id | table_to_search  search_groups id | search_id (fk searches.id) | search_terms_id (fk search_terms.id) | search_exclude (bool)  `search_exclude` determines whether match terms or exclude them.  target_fields id | field_name  search_groups_target_fields (linking table many-to-many relationship between search_groups , target_fields) search_group_id (fk1) | target_field_id (fk2) (composite unique index on fk1+fk2) 

so how can enforce uniqueness of these searches, when facts of each search located in 3 different tables?

each search can have many search groups. , each search group can have many fields , vice versa.

i thought of potential change, taking search_id fk out of search groups , putting in linking table, allowing many-to-many searches-to-groups.

search_groups id |  search_terms_id (fk search_terms.id) | search_exclude (bool)  searches_search_groups search_id (fk) | search_group_id (fk) 

but search_groups doesn't different search_terms. except each group still defined component fields. core question of is, how can make sure each group unique when definition partly composed contents of different table? don't think it's possible indexes.


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -