ID for tags in tag systems

前端 未结 3 1445
醉话见心
醉话见心 2021-02-03 15:50

I\'m implementing a tag system similar to StackOverflow tag system. I was thinking about when storing the tags and relating to a question, that relationship will be directly wit

相关标签:
3条回答
  • 2021-02-03 16:09

    Your last sentence in your question seems to answer it. Assuming the tags are stored in a tag table, I would always have an ID column (int or GUID) and the varchar/string column for the tag name. The many-to-many (junction table) that would relate some other entity to one or more tags would have two columns containing the ID's the "other entity" and the tag's ID. It's then easy to edit a tag (to correct a mis-spelling for example) without touching the key. You should get much better performance when using queries that include joins with your junction table and it also means you're normalizing your data better.

    Remember, "the key, the whole key and nothing but the key, so help me codd"! :)

    0 讨论(0)
  • 2021-02-03 16:09

    If you foresee many tags, and are using a relational database, using an ID that the database supports natively (e.g. RID) internally may just give you better performance.

    If that's not a concern: go by simple short tag names. You can give the tags long names which will be displayed in the user interface too where it makes sense (e.g. ask the user for one when creating a new tag). You are more likely to have to edit the long names, which nothing refers to directly, so this is not a problem.

    Aside, if you are using a relational database, it is probably not very difficult to change a tag name together with all its references with a simple query, it may just be a slightly more expensive operation, but it is probably not going to be done frequently enough that you need to optimize for it. And consider that you may have duplicate tags that you will want to merge too, so you might want to be able to do that anyway.

    0 讨论(0)
  • 2021-02-03 16:26

    Have a look at these related earlier SO questions:

    • What is the most efficient way to store tags in a database

    • Database design for tagging

    • How to design a database schema to support tagging with categories

    • Is there an ideal schema for tagging?

    0 讨论(0)
提交回复
热议问题