...

View Full Version : What's the best way to make "like" table?



Mido™
04-09-2011, 10:54 PM
I want to make a table for inserting the "likes" that users do on the comments, the problem I want to make it show how many likes per comment and who is like it and to show the name with the id to make the other could click on his name to see his profile.

I did male a table for it like this:

CREATE TABLE likes (
id INT NOT NULL AUTO_INCREMENT,
postId INT NOT NULL,
users TEXT NOT NULL ,
PRIMARY KEY(id)
)DEFAULT CHARACTER SET utf8

and I save the users names in the column users like this (Mido||Seven||Zoro)
so are any one have any idea about the best way to make this?

Old Pedant
04-10-2011, 06:18 AM
Ugh.

Don't you already *HAVE* a USERS table in your database??

If so, you should be USING it!!!



CREATE TABLE likes (
postid INT NOT NULL REFERENCES posts(id),
userid INT NOT NULL REFERENCES users(id)
);

And that's it. You could have an auto_increment field, but what for? It will never be used. It will just waste space and slow things down.

You could also add indexes for both postid and userid, depending on how many entries you expect to have in the "likes" table.

NORMALIZE NORMALIZE NORMALIZE!!

Mido™
04-10-2011, 06:50 PM
Ugh.

Don't you already *HAVE* a USERS table in your database??

If so, you should be USING it!!!



CREATE TABLE likes (
postid INT NOT NULL REFERENCES posts(id),
userid INT NOT NULL REFERENCES users(id)
);

And that's it. You could have an auto_increment field, but what for? It will never be used. It will just waste space and slow things down.

You could also add indexes for both postid and userid, depending on how many entries you expect to have in the "likes" table.

NORMALIZE NORMALIZE NORMALIZE!!


hahaha, thanks for your reply and you are right about the auto_increment column, but about users table I surely will use it but I don't know how to use it with the other way "storing the user's names in text field" .
by using your way I see I would add a new row for every "like" sent by a user on a comment, so the table rows will be very much "are you see it won't make a problem and will be better than storing all name in one big text column?"

Old Pedant
04-10-2011, 08:38 PM
As I said, read up on NORMALIZATION.

Yes, I am sure.

*IF* you want one record per LIKES, then this is exactly what you need.

If you only wanted a *count* of LIKES, then you don't need this table, at all.

Mido™
04-10-2011, 11:07 PM
Thanks man, at least you make me read about database Normalization more and know more about it.
and I think now I'll use your way :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum