简体   繁体   中英

Database Design best solution

i have a problem about db design. I am doing a blog application and i have 3 tables which names are Record , Category and Tags . In this case There are a lot of record and one record can related with N categories. And one category can relate with N tags. For example;

Record 1--->N Category and Category 1-->N Tag

What is the best db design in this scenario ?? Could you suggest any solution ?

Finally in the end of the project i want to do this,

 BlogRecord 
 ................
 ................
 Categories : Asp.net , JQuery
 Tags : Life Cyle,rooting,jquery selector,jQuery ajax

This is a pretty basic example of database architecture. You have a record , which has a one-to-many relationship with both tags and categories . (If I understand right)

One To Many

If you really need one-to-many relationships (I doubt that in this actually the case, however, see below), there is really one one sensible way to design this. When you have a one-to-many relationship, you have the "many" point to the "one". Your tables should look like this:

record:
   id, info, date_created, other_stuff

category:
   id, record_id, name, date_created, other_stuff

tag:
   id, record_id, info, other stuff

Pretty simple, both tag and category has a column called record_id which give the id of the record it belongs to.

One-to-many has a limitation. The 'many' can only point to 'one'. Just like the name. So if you have a one-to-many relationship where the records is 'one' and the category is the 'many', each category can only have ONE record in it. That doesn't really make sense for a category.

I think you might actually be looking for a many-to-many relationship for some of these:

Many to Many

Ok, lets say you actually want a many-to-many relation between records and tags. Like the tag 'cool' can be attached to two different records, but each record can have many tag. Or maybe a record can be in many categories and a category can have many record. This seems like it would make more sense, and would be a 'many-to-many' relationship. It is a little harder, but still easy.

record:
     id, category_id, info, date_created, other_stuff

tag:
     id, info, other stuff

Notice I got rid of record_id on the tag, that is because it can attach to many different records, not just one. To do this, we need a whole table to map the many-to-many relationships:

records_to_tags
     record_id, tag_id

Now you fill your records-to-tags table with the relationships. You might have data like this:

record:
     1, "I'm the first record", "yay", 10/12/2014, asdf
     2, "I'm the second record", "woot", 10/13/2014, asdf

tags:
     1, "cool post"
     2, "awesome"

records-to-tags
     1, 1
     1, 2
     2, 2

You can see here that both records have the 'cool post' tag, but only the second one has the 'awesome' tag.

You can learn more about database relationships at this link: http://code.tutsplus.com/tutorials/

Why re-invent the wheel? See the Wordpress Database Design . They've implemented this scenario using the following 3 tables and it's a very scalable design IMO.

  1. wp_terms
  2. wp_term_relationships
  3. wp_term_taxonomy

wp_terms - The categories for both posts and links and the tags for posts are found within the wp_terms table.

wp_term_relationships - Posts are associated with categories and tags from the wp_terms table and this association is maintained in the wp_term_relationships table. The association of links to their respective categories are also kept in this table.

wp_term_taxonomy - This table describes the taxonomy ( category , link, or tag ) for the entries in the wp_terms table.

在此输入图像描述

It sounds to me like a many-to-many relationship as many blog records can have an asp.net category and asp.net can have many blog records. Same with Tags, so you'll probably need one or two pivot tables between the three (total of 5 tables).

在此输入图像描述

i think my best solution is in above picture

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