Avoid storing a comma-separated list in a string as it is generally not recommended.
However, if your queries consistently treat the entire string as a whole without the need for SQL expressions to search for individual elements, then there may be no harm in doing so.
In SQL, it is more efficient to consider each column as a single scalar value rather than an array. Treating a column as divisible sub-parts can complicate and slow down data processing.
When dealing with tags, it is advisable to have a one-to-many tag table where each bookmark can be associated with multiple tags, with one tag per row in the table.
Alternatively, you can use a lookup table for tags and then create a many-to-many table that maps bookmarks to their corresponding tags.
CREATE TABLE Tag (
tag_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
tag VARCHAR(50) NOT NULL
);
CREATE TABLE BookmarkTag (
bookmark_id INT NOT NULL,
tag_id INT NOT NULL,
PRIMARY KEY (bookmark_id, tag_id),
FOREIGN KEY (bookmark_id) REFERENCES Bookmark(bookmark_id),
FOREIGN KEY (tag_id) REFERENCES Tag(tag_id)
);
This approach provides more flexibility:
- No limit on the number of tags per bookmark
- Simplified search for specific tags
- Ease in calculating the number of tags per bookmark
- Effective control over duplicates, allowed tags, removal of a specific tag from all bookmarks using it, etc.
For further insights, refer to my response to Is storing a delimited list in a database column really that bad?