Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database schema of a like system

I've been thinking about a liking system for my website, and I can't determine the best way to approach it. I'm using MySQL, and right now, I have an activities table, for posts of text, pictures, videos, etc, and a comments table, for comments. You can like both comments and activity posts, so how should I go about with the columns of the like table? I am open to suggestions of changing the whole schema all together for a more coherent design.

like image 917
Wiz Avatar asked Aug 08 '12 01:08

Wiz


People also ask

What are the 3 types of schema in the database?

Schema is of three types: Logical Schema, Physical Schema and view Schema. Logical Schema – It describes the database designed at logical level. Physical Schema – It describes the database designed at physical level. View Schema – It defines the design of the database at the view level.

What is schema in database system?

A database schema is considered the “blueprint” of a database which describes how the data may relate to other tables or other data models. However, the schema does not actually contain data. A sample of data from a database at a single moment in time is known as a database instance.

What is an example of a database schema?

For example, in the Oracle Database product, a schema represents only a part of a database: the tables and other objects are owned by a single user. Note: In SQL, a view is a virtual table that is based on the result-set of a statement. A view contains both rows and columns.

What type of database uses a schema?

A database schema represents the logical configuration of all or part of a relational database. It can exist both as a visual representation and as a set of formulas known as integrity constraints that govern a database.


1 Answers

Just make a table called likes with a structure like this:

`id` int(11) NOT NULL AUTO_INCREMENT,
`userid` int(11) NOT NULL,
`post_type` varchar(10) NOT NULL,
`post_id` int(11) NOT NULL,
`date` datetime NOT NULL,
 PRIMARY KEY (`id`)

Then whenever someone likes an activity post, you would insert the ID of that user, the post_type as activity, and then post_id would be the post ID (obviously).

If someone likes a comment, you would do the same thing but put comment for the post type instead.

Then to get the likes for an activity post:

SELECT * FROM `likes` WHERE post_id = 'id' AND post_type = 'activity'

You would just need to replace 'id' with the ID variable.

The same would be for comments (except you would need to change the post_type of course).

I hope this helps you get an idea on how to go about making a like system for your website :)

like image 89
Nathan Avatar answered Sep 30 '22 14:09

Nathan