投票系统的Mysql唯一索引

发布于 2025-01-07 14:53:24 字数 611 浏览 4 评论 0原文

在用于记录帖子投票的表格中,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黯然#的苍凉 2025-01-14 14:53:24

修改以下进一步信息 为了具体按照您的要求进行,我建议如下。添加源表以进一步规范化

CREATE TABLE vote_sources (
    source_id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
    // 0 will be treated as anonymous, as NULL can have issues on UNIQUE indexes
    user_id INT(11) UNSIGNED NOT NULL REFERENCES users(user_id) ON DELETE SET 0,
    ip VARCHAR(15) NOT NULL,
    dupeCheck VARCHAR(15) NOT NULL,
    PRIMARY KEY(source_id),
    UNIQUE INDEX (dupeCheck)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_general_ci

CREATE TABLE votes
(
    vote_id int(11) NOT NULL AUTO_INCREMENT,
    post_id int(11) REFERENCES posts(post_id) ON DELETE CASCADE,
    source_id int(11) NOT NULL REFERENCES vote_sources(source_id) ON DELETE CASCADE,
    vote ENUM('Up', 'Down'),
    PRIMARY KEY(vote_id),
    UNIQUE INDEX(post_id,source_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_general_ci

然后处理重复检查此触发器,

CREATE TRIGGER dupeSourceCheck
BEFORE INSERT ON vote_sources
FOR EACH ROW SET NEW.dupeCheck = IF(NEW.user_id>0,CAST(NEW.user_id AS CHAR),NEW.ip)

如果​​重复源是详细的,这反过来会导致重复键错误,同时仍然公开数字 user_id 以实现更高效的连接。

Revised following further information To do specifically as you request I would advise the following. Add a source table to further normalise

CREATE TABLE vote_sources (
    source_id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
    // 0 will be treated as anonymous, as NULL can have issues on UNIQUE indexes
    user_id INT(11) UNSIGNED NOT NULL REFERENCES users(user_id) ON DELETE SET 0,
    ip VARCHAR(15) NOT NULL,
    dupeCheck VARCHAR(15) NOT NULL,
    PRIMARY KEY(source_id),
    UNIQUE INDEX (dupeCheck)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_general_ci

CREATE TABLE votes
(
    vote_id int(11) NOT NULL AUTO_INCREMENT,
    post_id int(11) REFERENCES posts(post_id) ON DELETE CASCADE,
    source_id int(11) NOT NULL REFERENCES vote_sources(source_id) ON DELETE CASCADE,
    vote ENUM('Up', 'Down'),
    PRIMARY KEY(vote_id),
    UNIQUE INDEX(post_id,source_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_general_ci

And then to deal with dupe checks this triggere

CREATE TRIGGER dupeSourceCheck
BEFORE INSERT ON vote_sources
FOR EACH ROW SET NEW.dupeCheck = IF(NEW.user_id>0,CAST(NEW.user_id AS CHAR),NEW.ip)

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.

小…红帽 2025-01-14 14:53:24

我建议规范化:创建另一个字段,例如 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.

尝蛊 2025-01-14 14:53:24

你有 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文