1

I'm working on a poll program and am wondering, so far all of the sample I've seen makes use of insert then counting the total votes for each answers. I find it redundant to have a table with a field of answers and a lot of yes, no and maybe

So I am thinking, is it efficient for me to use update instead of insert, for example, if a user vote yes, the yes field will increment? Will it introduce problems with accuracy if users simultaneously vote?

3 Answers 3

2

You should have a answer table, after you will have a relationship with user and answer. You could get stats in adding some information (datetime, etc..)

Sign up to request clarification or add additional context in comments.

Comments

1

I find it redundant to have a table with a field of answers and a lot of yes, no and maybe

you can add some useful info too, i.e. time of the vote, ip address, cookie and such

Will it introduce problems

No

Comments

0

Your database uses transactions, the update will be atomic. There's no problem with accuracy.

UPDATE MyTable SET MyColumn=MyColumn+1 WHERE MyVoteID=123

3 Comments

Well, MtSQL no longer uses MyISAM by default. And ... it was directly answering his question if there would be a problem with "accuracy" if he used an update for a "vote count", which obviously there won't be. The update is atomic in that it can't be interleaved with another one.
What I wanted to emphasize is that transactions are not a solution to all concurrency problems. Lost updates, phantom reads, non repeatable reads, etc. can happen with transactions or not.
I agree that they are not the solution to all problems; absolutely. However, in this case, the update is a fine solution if he doesn't care who cast the vote (which is what his question implies)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.