I am using a table to store votes of different users on different polls.
Table has following structure.
id | poll_id | opt | ip_address id : auto increment poll_id : (STRING UNIQUE) unique for a particular poll opt : (STRING) option selected by user ip_address: (STRING UNIQUE) ip address of user
This is my query
INSERT OR REPLACE INTO tbl_poll(id,poll_id,opt,ip_addr) VALUES (null,'$poll_id','$opt','$ip_addr')
(Here $poll_id, $opt and $ip_addr are php variables which holds the respective values)
Now, the scenario is like this,
User ‘A’ votes for option 2 of poll_id ‘mypoll’. Query works perfectly. (Does insert)
User ‘A’ changes mind and votes for option 5 of poll_id ‘mypoll’. Query works perfectly. (Does replace)
But if User ‘A’ votes for option 4 of poll_id ‘yourpoll’. Query fails (It does a replace) but it should insert a new record with poll_id ‘yourpoll’
I think, it considers the unqiue constraint of ip_address only but not the poll_id
From your description on the desired functionality, it would seem that you want
poll_idandip_addressto be a unique pair or compound unique.