Well lately I’m making a script application. People can post and share stuff (let’s call them object for the purpose of the question). Thing is, that a user can “bookmark an object” in his profile.
I have a table for users with unique uid (primary key) and a table for objects with a unique oid (primary key). I was thinking of making a new table in MySQL, calling it liked_objects which will have the following:
id – oid – uid
Each time I need to find all the object that user X liked I may run a query
SELECT * FROM liked_objects WHERE uid = userXid
My question is: Is there a most efficient way of doing this?
I’m using PHP 5 and MySQL Database.
Best regards
You don’t need an id column for your
liked_objectstable. Assuming a user can only like each object once, you can just use two columns:uid | oidMake the Primary Key be both columns. Then to unlike an object you just:
DELETE FROM `liked_objects` WHERE uid = 'user_id' AND oid = 'object_id'To like an object you:
INSERT INTO `liked_objects` (`uid`, `oid`) VALUES('user_id', 'object_id')And you can select the liked objects the way you said.