DEV Community

Discussion on: Best way to store array type data inside database?

Collapse
smartcodinghub profile image
Oscar

Hmm, did you discard creating another table? What was the reason behind?

Collapse
mittalyashu profile image
Yashu Mittal Author

What do you mean by creating another table?

In the second option I am creating another table name tags to store the connection between the posts and tags.

Collapse
smartcodinghub profile image
Oscar

I'm sorry, I totally missed. I just though that you would storage the array in a varchar (or any lob type) using JSON o separating it with |.

I would go for this three tables: posts, tags and post_tags (Or any naming you like).

posts --> All the posts
tags --> A master table with all the tags
post_tags --> A table with a post_id and tag_id to do the many to many relationship.

Thread Thread
mittalyashu profile image
Yashu Mittal Author

A table with a post_id and tag_id to do the many to many relationship.

You mean a single row of post_tags can contain multiple tags_id column.

Thread Thread
smartcodinghub profile image
Oscar

I mean something like:

posts

post_id other_value
1 something
2 something
3 something

tags

tag_id tag_text
1 tag1
2 tag2
3 tag3

post_tags

post_id tag_id
1 1
1 2
2 3
3 1
3 2
3 3