I am trying to figure out what the most appropriate table structure would be for related videos. I have a table videos
which contains details about all the videos. I am planning on a new feature of video recommendations which I will be creating manually for now.
I was thinking about storing it in a table column inside the videos table videos.relatedVideos
and store JSON data for related videos ID. However, I want it to be backward compatible as well.
So say if a video with ID = 5
is added as a related video for video with ID = 10
, the table structure would be
| id | videoId | ..... | relatedVideos|
| 1 | 10 | ..... | [5] |
| 2 | 4 | ..... | [3,6,7] |
I also want the video with ID = 10
to act as a related video for video with ID = 5
. One way of doing it would be to create a new entry for #5
on the videos
table when I am creating an entry for #10
| id | videoId | ..... | relatedVideos|
| 1 | 10 | ..... | [5] |
| 2 | 4 | ..... | [3,6,7] |
| 3 | 5 | ..... | [10] |
But I am looking for something more proper, where I can draw many-to-many relationship between the videos.
One option would be to create a table relatedVideos
and create new entry for each and every related video with relation with the master video, but it might result into duplicate records when adding/editing related videos for any particular videos.
|id | videoID | relatedVideoID|
|1 | 5 | 10 |
|2 | 5 | 15 |
|3 | 5 | 13 |
|4 | 13 | 19 |
Right now, what I am doing is, if I am browsing video page #13
I am executing two queries (Select relatedVideoID from relatedVideos where videoID = 13)
and (Select videoID from relatedVideos where relatedVideoID = 13)
. I believe I can do a similar thing with SQL joins or unions, I am yet to explore those options.
I am then merging the two array results to get [19, 5]
(I am only keeping the distinct ID's). This is getting the work done, however, I am really looking forward to implement a more proper solution.
Just looking for some recommendation on what a good approach would be.