I am trying to build a little system that would assign specific tags to an item, or a person to be precise. So, I have a list of persons and a list of tags. I need to assign 3 specific tags to each person (that correspond to 3 different skills this person might have).
In a nutshell, the output would look like this :
Person 1 | webdesign, ux, jquery
Person 2 | blogging, photography, wordpress
Person 3 | graphic-design, 3d, inventor
...
For now, those lists are stored in two different tables :
persons
-------
person_id
person_name
tags
-------
tag_id
tag_name
My main goal is to avoid repetition and to simply assign 3 existing tags to an existing person.
Could you give me a few hints on how to implement this? I know that a three-table design is common for a tagging system, but is it relevant in my situation?
Thanks for your help.
If you want to ensure that you don’t have any duplicates and to be able to add
Ntags to a person, then to properly implement a normalized design you would need a third table to link thetagsto eachpersonTo guarantee uniqueness, you can either use a composite primary key, or add a
unique indexto the table including both columns.See an example of the above in this SQL Fiddle.
If you need to enforce the 3 tag limit at the database level, you can add a third column to the
persons_2_tagstable (tag_numberfor example) that is anenumwith values of1, 2, 3and add that to yourunique index. Insert logic would need to be handled at the application level, but would be enforced by theindex.