Best way to store tags in a sql server table?

前端 未结 3 556
一向
一向 2021-02-07 07:06

What\'s the best way to store tags for a record? Just use a varchar field? What about when selecting rows that contains tag x? Use the like operator?

thanks!

3条回答
  •  臣服心动
    2021-02-07 07:41

    No, it is generally a bad idea to put multiple pieces of data in a single field. Instead, use a separate Tags table (perhaps with just a TagID and TagName) and then, for each record, indicate the TagID associated with it. If a record is associated with multiple tags, you will have duplicate records with the only difference being TagID.

    The advantage here is that you can easily query by tag, by record, and maintain the Tags table separately (i.e. what if a tag name changes?).

提交回复
热议问题