How relation between one record on a table and several records on onother table

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


How relation between one record on a table and several records on onother table



My project is a program like movie collection.
Main table is for storing movie data with fields like name,director,release year and ...
But each record must have another attribute named genre (genres).
Each movie may be categorized by one or several genres (Comedy, Horror, History, Romance,...)
Genre table contain two fields.ID:integer and Genre:vchar
For each movie, some of genre fields can be selected.
How I can do this?
thank you





you have to use sql join for that, but before this you have to set relationship of primary key table and foreign key table
– ershoaib
7 mins ago


sql join





Possible duplicate of How to implement one-to-one, one-to-many and many-to-many relationships while designing tables? You will need a many-to-many relationship. It is not one-to-many because many movies can have the same genre and a movie can have more than one genre.
– Georg Patscheider
4 mins ago






2 Answers
2



The movie table must have some movie_id in order to uniquely identify each movie.
It must also have a column called genre_id (same as that in the genre table) to link the movie with each genre.


movie


movie_id


genre_id


genre



Using the following query, you can choose movie belonging to each genre:


SELECT M.movie_id
,M.movie_name
,G.genre_id
FROM movie M
LEFT JOIN genre G ON M.genre_id = G.genre_id



You need 3 tables to do this work correctly.


Table Moovie :
Moov_ID
Moovie_Name
Moov_DirectorID
...

Table Genres:
Genre_ID
Genre_Name
...


Table Moovie_Genre (Whic will link moovies and genres)
Mg_ID
Mg_MoovieID
Mg_GenreID



Like this you can have a many to many relation.



You can use a string field in your moovie table in order to use comma seperated values to stock genreIDs but, I prefere to have a many to many relation.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

'Series' object is not callable Error / Statsmodels illegal variable name