投票系统的Mysql唯一索引
在用于记录帖子投票的表格中,
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
如何添加UNIQUE INDEX
以避免重复投票?如果投票者是用户,则 UNIQUE INDEX 应应用于 (post_id, user_id);如果不是用户,则 UNIQUE INDEX 应应用于 (post_id, ip)。
事实上,我只需要 (post_id, user_id) OR (post_id, ip); 拥有 UNIQUE INDEX
但不能两者兼而有之。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
修改以下进一步信息 为了具体按照您的要求进行,我建议如下。添加源表以进一步规范化
然后处理重复检查此触发器,
如果重复源是详细的,这反过来会导致重复键错误,同时仍然公开数字 user_id 以实现更高效的连接。
Revised following further information To do specifically as you request I would advise the following. Add a source table to further normalise
And then to deal with dupe checks this triggere
Which in turn should cause a duplicate key error if a duplicate source is detailed, while still exposing a numeric user_id for more efficient joins.
我建议规范化:创建另一个字段,例如
vote_src VARCHAR(15)
,它从代码中获取用户或 IP,并为此创建唯一索引。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.你有 PHPMyAdmin 吗?在那里这很容易做到。您想要一个字段作为主键还是真的只想要一个唯一索引?如果您有 PHPMyAdmin 作为选项,您可以转到结构选项卡,在底部您将看到一个索引表,其中可以选择添加索引。点击“开始”,然后沃拉!你有你的索引。
Do you have PHPMyAdmin? This is very easy to do there. Do you want a field to be a PRIMARY KEY or do you truly want just a UNIQUE index? If you have PHPMyAdmin as an option you can go to your structure tab and at the bottom you will see an indexes table with a choice to add an index. Click 'GO' and walla! You have your index.