Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you structure a database that allows voting similar to how stackoverflow does it?

I'm new to databases and web development but I'm trying my best to learn my way though making my own dynamic website. I'm taking it step by step and am currently designing the data model on paper. I'm wonder how does one structure a database for a website that allows voting like how stackoverflow does it? If there's a table that contains a list of questions, each question that a user creates gets added to this table. There can't simply be a field on this table that counts votes because that would allow a single person to have unlimited votes right? So there should be a key that connects to another table which counts votes and keeps track of users so they can't vote twice, correct? If this is true, this is the part where I get confused. Each answer given can also be voted on. So does that mean that when a user submits an answer, in addition to adding that answer to probably a separate table for answers per question asked, the model must also generate a new table for each answer dynamically during run-time to keep track of all these votes?

Note that I'm not specifically asking about how stackoverflow does it, but how the concept of what the user experiences works.

One thing I'd also like to do is query the activity of a single user, so if all these tables would have to be created dynamically for every piece of submitted data, creating a crap load of tables over time, wouldn't it be really slow to have to parse through every table to check if a particular user submitted any data or voted?

Is there a better way of doing this that someone could explain in laymen terms? No need for specific code... I can probably figure that out later when the time comes. I'm just theorizing right now and building a paper model to work off of later.

EDIT: Oh, I see. I think in excel-like spreadsheets when I'm thinking of database tables, so correct me if my understanding is wrong. So every vote site-wide is on a single table (listed vertically on a spreadsheet), each having a line of data (horizontally) that links the vote to a variety of owners (user and question OR answer)? Is that correct? I say question OR answer because I don't understand a scenario where it would make sense to have them both as a vote attribute(not sure if that's correct terminology) instead of creating two separate vote datas for an answer and a question which are both being voted on. Basically the way I see it, each line represents a vote and there are 3 fields, 1. Value (+1 or -1), 2. From whom (username), 3. To what (question or answer).

like image 616
user1263500 Avatar asked Mar 12 '12 12:03

user1263500


People also ask

Which database is best for voting system?

mysql - Database design for voting module with long-run and high-load capability - Database Administrators Stack Exchange.

What is vote in stackoverflow?

When you vote up, you are moving that content "up" so it will be seen by more people. By default, answers are sorted by number of votes. 1. Upvotes on a question give the asker +10 reputation. Upvotes on an answer give the answerer +10 reputation.


1 Answers

You have to look at all the elements. Basically you have

Questions
Users
Answers
Votes

Users are tied to Questions and answers and votes so you will either need adequate foreign keys to handle this or a child table that connects these. For instance you could have

tblQuestions
    questid
    question
    userid

then

tblAnswer
    Answer
    answerid
    userid
    questid 
    accepted (to flag as accepted answer)

and finally

tblVote
    vote (up or down)
    questid
    answerid
    userid

The user table is fairly straightforward and the fun part happens in the behind the scenes logic. this is obviously a very rough layout and a lot of other things need to be considered and there are dozens of ways to accomplish the table layout.

like image 124
Brian Avatar answered Sep 29 '22 07:09

Brian