Best practice for storing tags in a database?

前端 未结 3 1285
情书的邮戳
情书的邮戳 2020-12-23 18:28

I developed a site that uses tags (key words) in order to categorize photographs. Right now, what I have in my MySQL database is a table with the following structure:

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-23 19:25

    You can make a tags table which is just an id and tag with a unique constraint on tag and then photo_tags table which has tag_id and photo_id. Insert a tag into the tags table only if it doesn't already exist.

    Then you will be querying by a pk instead of varchar text comparison when doing queries like how many photos are tagged with a certain tag.

提交回复
热议问题