Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Aug 2009
    Location
    in .........
    Posts
    263
    Thanks
    183
    Thanked 0 Times in 0 Posts

    What's the best way to make "like" table?

    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?

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,576
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    Ugh.

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

    If so, you should be USING it!!!

    Code:
    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!!
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Mido™ (04-10-2011)

  • #3
    Regular Coder
    Join Date
    Aug 2009
    Location
    in .........
    Posts
    263
    Thanks
    183
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Ugh.

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

    If so, you should be USING it!!!

    Code:
    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?"

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,576
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Mido™ (04-10-2011)

  • #5
    Regular Coder
    Join Date
    Aug 2009
    Location
    in .........
    Posts
    263
    Thanks
    183
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •