I just found 1 question with no answer about this.
I'm using CodeIgniter 4 (php and MySQL).
I have 3 main tables (videos, users and tags).
I need to set up a many to many relationship between these 3 tables, so I created a 4th table that handles that (pivot).
Tables:
Videos (id, Link)
Users (id, name)
Tags (id, name_tag)
pivot (video_id, user_id, tag_id)
CREATE TABLE `videos` (
`id` int(10) NOT NULL,
`link` varchar(250) NOT NULL,
`title` varchar(250) NOT NULL,
`main_description` text NOT NULL,
`aux_description` text NOT NULL,
`owner` varchar(20) NOT NULL,
`owner_link` varchar(250) NOT NULL,
`date` datetime NOT NULL,
`date_added` datetime NOT NULL,
`thumbnail` varchar(255) NOT NULL,
`id_source` int(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `videos` (`id`, `link`, `title`, `main_description`, `aux_description`, `owner`, `owner_link`, `date`, `date_added`, `thumbnail`, `id_source`) VALUES
(1, '362454114', '1', '', '', '', '', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '', 1),
(2, '249436458', '2', '', '', '', '', '2018-01-01 00:00:00', '2020-05-30 00:00:00', '', 1),
(3, '172290099', '3', '', '', '', '', '2016-06-26 00:00:00', '0000-00-00 00:00:00', '', 1),
(4, '213585809', '4', '', '', '', '', '2019-09-25 00:00:00', '2020-05-30 00:00:00', '...', 1),
(5, '395267446', '5', '', '', '', '', '2019-09-25 00:00:00', '2020-05-30 00:00:00', '...', 1),
(6, '383823899', '6', '', '', '', '', '2019-09-25 00:00:00', '2020-05-30 00:00:00', '...', 1);
ALTER TABLE `videos`
ADD PRIMARY KEY (`id`),
ADD KEY `videos_id_source_foreign` (`id_source`);
ALTER TABLE `videos`
MODIFY `id` int(10) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7;
ALTER TABLE `videos`
ADD CONSTRAINT `videos_id_source_foreign` FOREIGN KEY (`id_source`) REFERENCES `sources` (`id`);
COMMIT;
TAGS TABLE /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CREATE TABLE `tags` (
`id` int(10) NOT NULL,
`name` varchar(30) NOT NULL,
`date` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `tags` (`id`, `name`, `date`) VALUES
(0, 'teste1', '2020-05-29 00:00:00'),
(2, 'teste3', '2020-05-28 00:00:00'),
(4, 'teste0', '2020-05-30 00:00:00');
ALTER TABLE `tags`
ADD PRIMARY KEY (`id`);
ALTER TABLE `tags`
MODIFY `id` int(10) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7;
COMMIT;
USERS TABLE /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CREATE TABLE `users` (
`id` int(10) NOT NULL,
`rand_id` int(20) NOT NULL,
`name` varchar(25) NOT NULL,
`email` varchar(50) NOT NULL,
`profile_image` varchar(50) NOT NULL,
`confirmation_link` varchar(125) NOT NULL,
`confirmation_link_date` datetime NOT NULL,
`password` varchar(250) NOT NULL,
`date` datetime NOT NULL,
`active` tinyint(1) NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `users` (`id`, `rand_id`, `name`, `email`, `profile_image`, `confirmation_link`, `confirmation_link_date`, `password`, `date`, `active`) VALUES
(1, 1, 'User 1', 'user1@gmail.com', '', '', '0000-00-00 00:00:00', '', '2020-05-12 00:00:00', 1),
(2, 2, 'User 2', 'user2@gmail.com', '', '', '2020-05-12 00:00:00', '', '2020-05-12 00:00:00', 1),
(3, 3, 'User 3', 'user3@gmail.com', '', '', '2020-05-12 00:00:00', '', '2020-05-12 00:00:00', 1),
(4, 4, 'User 4', 'user4@gmail.com', '', '', '2020-05-12 00:00:00', '', '2020-05-12 00:00:00', 1),
(5, 5, 'User 5', 'user5@gmail.com', '', '', '2020-05-12 00:00:00', '', '2020-05-12 00:00:00', 1);
ALTER TABLE `users`
ADD PRIMARY KEY (`id`);
ALTER TABLE `users`
MODIFY `id` int(10) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
COMMIT;
PIVOT TABLE /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CREATE TABLE `user_video_relation` (
`user_id` int(10) NOT NULL,
`video_id` int(10) NOT NULL,
`date` datetime NOT NULL,
`tag_id` int(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `user_video_relation` (`user_id`, `video_id`, `date`, `tag_id`) VALUES
(1, 1, '2020-05-30 00:00:00', 0),
(2, 2, '2020-05-29 00:00:00', 0),
(3, 3, '2020-05-28 00:00:00', 0),
(4, 4, '2020-05-27 00:00:00', 0),
(5, 5, '2020-05-26 00:00:00', 0),
(1, 6, '2020-05-25 00:00:00', 0),
(2, 1, '2020-05-24 00:00:00', 0),
(3, 2, '2020-05-23 00:00:00', 0),
(4, 3, '2020-05-22 00:00:00', 0),
(5, 4, '2020-05-21 00:00:00', 0),
(1, 5, '2020-05-20 00:00:00', 0);
ALTER TABLE `user_video_relation`
ADD KEY `user_video_relation_user_id_foreign` (`user_id`),
ADD KEY `user_video_relation_video_id_foreign` (`video_id`),
ADD KEY `user_video_relation_tag_id_foreign` (`tag_id`);
ALTER TABLE `user_video_relation`
ADD CONSTRAINT `user_video_relation_tag_id_foreign` FOREIGN KEY (`tag_id`) REFERENCES `tags` (`id`),
ADD CONSTRAINT `user_video_relation_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`),
ADD CONSTRAINT `user_video_relation_video_id_foreign` FOREIGN KEY (`video_id`) REFERENCES `videos` (`id`);
COMMIT;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
I need to search through the table and group the results in the following structure (a object that I named 'Item').
item={
'User_name'=>'John',
'video_link'=>'http://www.youvideo.com/video',
'tags'=>['cool','awesome','TooTooTrain']
}
In the Pivot table I have 3 FK, resulting in a lot of 'duplicated' data, once I need one row for each associated tag, and the user is able to associate how many tags and videos he want. (And now I know why NoSQL DBs exists).
The General Idea is:
Make a 'Feed' page, where I'll show the last 50 added items (the problem is that one single item will be composed by N database entries, so I can't limit the query by this number).
Make a search page, where the user will be able to search all the videos by the Tags or Users.
I THINK (not sure about that) that the only problem so far is to make this 'Item' object.
What is the best approach to make this Query and result it as this 'Item' object?
SQL to recreate my tables:
https://www.writeurl.com/text/j4ssy2fo99pn42qop9k6/87673c6j9psmv9rexvly/qbois6fy15vfpmuswwl1
