双字段唯一索引
问题描述:
我目前使用的是mongodb,但我也习惯了MySQL。双字段唯一索引
我在想,我们可以有双重字段索引吗?
例如想象该表:
Firstname | LastName
============+=================
Bob | Bob
Bob | B
John | Bob
甲索引,其中,我们可以有数据库实施唯一与First Name
和Last Name
为一体两者(wihout具有有一个开销作为第三字段作为两个组合或作为一个散列或东西
所以,如果我们要进入{ 'firstname: 'bob', 'lastname': 'bob'}
它不会插入。
MongoDB中能做到这一点?
答
你可以通过一个复合索引在MongoDB中做到这一点,就像这样:
db.col.ensureIndex({Firstname:1, LastName:1}, {unique:true})
答
您可以在mysql中添加多列索引。例如:
alter table users add unique index(firstname, lastname);
UPDATE:
这就是所谓的复合唯一约束
答
MongoDB为您提供了复合键和唯一索引的功能秒。
http://www.mongodb.org/display/DOCS/Indexes#Indexes-unique%3Atrue
在这里,有一个例子,你在找什么:
db.things.ensureIndex({姓:1,姓氏您可以通过以下链接找到有关此信息: 1},{unique:true});
让我知道这是否对你有帮助。
请添加downvote的原因 – 2012-03-15 12:38:00