How to model tags in the database?

前端 未结 3 918
萌比男神i
萌比男神i 2021-01-04 20:35

I have an existing webapp and want to add a tag feature so that users can tag existing objects. The question is should I add a tag column to each object? or should I normali

相关标签:
3条回答
  • 2021-01-04 20:50

    Yes, you should normalize it. The 'tag column' is either going to support only one tag per record, or is going to have hideous search performance.

    0 讨论(0)
  • 2021-01-04 20:56

    Definitely normalize. A table for tags, a table for your existing objects, and a table of links between them.

    0 讨论(0)
  • 2021-01-04 20:57

    Do you foresee users needing to associate more than one tag with an object?

    If not, add the TAG_ID fk to the OBJECT table. Otherwise, you'd need three tables in total to correctly model a many-to-many relationship:

    OBJECT

    • OBJECT_ID (pk)

    OBJECT_TAG_XREF

    • OBJECT_ID (pk, fk to OBJECT)
    • TAG_ID (pk, fk to TAG)

    TAG

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