Senior
Database schema: how many tables are needed to store messages with tags (one-to-many or many-to-many)?
sobes.tech AI
Answer from AI
To store messages with tags, where one message can have many tags, and one tag can belong to many messages, a many-to-many relationship is used.
This is implemented through three tables:
- messages — the table with messages.
- tags — the table with tags.
- message_tags — an intermediate table that stores pairs (message_id, tag_id), linking messages and tags.
This approach allows for efficient storage and searching of messages by tags and vice versa.