D
D
Denis Petrenko2021-05-15 10:18:26
Database design
Denis Petrenko, 2021-05-15 10:18:26

How to organize the storage of multiple tags in the database?

Task: There is a post with a tag. Search through a DB on tags leaves through the table of the intermediary.
The basic solution to the problem is the implementation of the form:

CREATE TABLE questions (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  url TEXT NOT NULL,
  PRIMARY KEY (id)
);

CREATE TABLE tags (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  tag VARCHAR(255) NOT NULL,
  PRIMARY KEY (id)
);

CREATE TABLE questions_tags (
  question_id INT UNSIGNED NOT NULL REFERENCES questions,
  tag_id INT UNSIGNED NOT NULL REFERENCES tags
);

But what to do in the case when questions / posts involve storing several tags at once. That is, I understand how to organize the connection between 1m questions and 1m tags, but what if the number of tags is dynamic. How in this case to organize a DB?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2021-05-15
@Termoslik

What's stopping you from adding multiple entries with the same question_id and different tag_ids to the questions_tags table?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question