开发者

Multiple unique columns but only unique combinations

I've run into a problem with MySQL unique indicies. What I want to do is have something like this:

user_id  group_id
1        1
1        2
1        3
2        1
2        4

Where there can be multiple instances of the same user ID, as well as multiple instances of group IDs, but only allow unique combinations of the two. I'm using this with an ON DUPLICATE KEY UPDATE MySQL query, and I'm wondering what I need to do to the indexes on the table. Currently, I have this:

PRIMARY id
UNIQUE  user_id, group_id

But... a开发者_如何学Python) is this enough and b) am I doing it right to get what I'm looking for?

Thanks for any help

James


You almost got it. If you use MyISAM engine, you can do this:

CREATE TABLE your_table(
id int unsigned not null auto_increment,
group_id int unsigned,
primary key(group_id, id)
) ENGINE = MyISAM;

Try it and see the behavior, it'll increment the id the way you specified in your example and you'll have unique constraint too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜