In a table for recording votes on posts as
CREATE TABLE votes
(
vote_id int(11) NOT NULL AUTO_INCREMENT,
post_id int(11) REFERENCES posts(post_id) ON DELETE CASCADE,
user_id int(11) REFERENCES users(user_id) ON DELETE SET NULL,
vote ENUM('Up', 'Down'),
ip varchar(255),
UNIQUE INDEX (on which???)
PRIMARY KEY(vote_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_general_ci
How to add UNIQUE INDEX to avoid duplicate voting? If the voter is user, UNIQUE INDEX should apply to (post_id, user_id); and if not a user, UNIQUE INDEX should apply to (post_id, ip).
In fact, I need to have UNIQUE INDEX only for (post_id, user_id) OR (post_id, ip); but NOT both.
I recommend to normalize: Create another field like
vote_src VARCHAR(15), that gets either the user or the IP from your code and create a unique index on this.