Answer the question
In order to leave comments, you need to log in
Database structure and query to it?
There is a simple database of films. I have a movie, it has a name, a description, actors (may be many) and genres (may be many). I don't know how to make the structure better. I sketched the following structure:
1 table: films
id, name, description
2 table: genres
id, name
3 table: actors
id, name
4 table: films_genres
film(id), genre(id)
5 table: films_actors
film(id), actor (id)
Is this structure correct and how to make a request for a movie.
I understand there will be 3 requests for a movie, its genres and actors?
But how to make requests if I need to get a lot of movies?
Tell me please.
Answer the question
In order to leave comments, you need to log in
It is possible to pull out and one request.
SELECT film.id, film.name, film.descrioption,
actor.id actor_id, actor.name actor_name,
genre.id genre_id, genre.name genre_name
FROM films
LEFT JOIN films_genres ON film.id = films_genres.film_id
LEFT JOIN genre ON genre.id = films_genres.genre_id
LEFT JOIN films_actors ON film.id = films_actors.film_id
LEFT JOIN actor ON actor.id = films_actors.actor_id
WHERE films_actors.actor_id IN (734, 44) -- актеры такие-то
AND films_genres.genre_id = 3 -- жанр такой-то
You can generally store json in the field of the movie. For example, store actors and genres. This can be done if you do not search by actors and genres, but are only needed for display.
And then it all comes down to one table.
And yes, at first I received films, then I received all at once to all films of actors and genres. Then 3 requests.
UPD: again, in any case, you can store everything in json, and if you need to search by genre and actor, then search from the tables that you created. This is necessary if you really need a very fast issue, when there are already a lot of records.
For example, Facebook has a list of dialogs and everything for display is stored in a table conditionally in Conversation and all the data is there, i.e. there are almost no or maybe no queries to other tables. This greatly speeds up the application.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question