Comparing Ways to Store a List in a Database
Sooner or later you need to store a list: tags on a blog post, toppings on a pizza, skills on a profile. Beginners usually reach for one of three approaches. This page compares them using tags on a post as the running example.
Approach 1: a comma-separated text column
| id | title | tags |
|---|---|---|
| 1 | Learning SQL | sql, beginners, databases |
Pros: very quick to set up; easy to display.
Cons:
- Finding all posts tagged "sql" means searching inside text, which can match "nosql" by accident.
- Renaming a tag means editing every post's text.
- The database cannot check that tags are valid or count them easily.
- Removing one tag means rewriting the string.
Approach 2: a separate table (or two)
CREATE TABLE tags (id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE);
CREATE TABLE post_tags (
post_id INTEGER REFERENCES posts(id),
tag_id INTEGER REFERENCES tags(id),
PRIMARY KEY (post_id, tag_id)
);
Pros:
- "All posts tagged sql" is an exact JOIN.
- Renaming a tag changes one row.
- Counting posts per tag is a simple GROUP BY.
- The database stops duplicate tags on the same post.
Cons: more tables and a JOIN to read the list. For beginners, this feels like more work at first.
Approach 3: a JSON column
Many databases can store JSON, a text format for structured data, in a column:
["sql", "beginners", "databases"]
Pros: keeps the list together with the post; many databases offer functions to search inside JSON.
Cons:
- Functions and syntax differ between databases, so examples do not transfer easily.
- Renaming a tag still means editing every post.
- Rules such as "tags must exist in a list" are harder to enforce.
Side by side
| Question | Comma-separated | Separate table | JSON column |
|---|---|---|---|
| Quick to set up | Yes | Medium | Yes |
| Exact searches | Fragile | Yes | Possible, varies by database |
| Rename a tag once | No | Yes | No |
| Count items per tag | Awkward | Easy | Possible, varies |
| Database checks validity | No | Yes | Limited |
| Skills transfer between databases | Yes | Yes | Less so |
Verdict by use case
| Situation | Suggested approach |
|---|---|
| Tags, categories or anything you search, count or rename | Separate table |
| A list you only ever display, never search, such as notes about a photo | JSON column can be fine |
| Quick throwaway prototype | Comma-separated is tolerable, but plan to replace it |
The beginner rule of thumb
If you will ever ask "which rows have this item?", give the items their own table. That one question covers most real cases. Our normalisation self-check asks it as its very first question.
A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.