我可以使两列彼此独有吗?或者在redis中使用复合主键? [英] Can I make two columns unique to each other? or use composite primary key's in redis?
问题描述
我在玩redis,想重新创建一个我在mysql中的表。下面是我用来创建它的mysql命令:
I'm playing around with redis and wanted to recreate a table I have in mysql. Here's the mysql command I use to create it:
CREATE TABLE data_table(
key1 INT SIGNED NOT NULL,
value1 INT SIGNED NOT NULL,
PRIMARY KEY (key1,value1)
);
我的数据基本上是两列,彼此唯一的数字,例如:
My data is basically two columns with numbers that are unique to each other, such as:
3:1
3:2
3:3
4:1
当我玩redis,并尝试创建上述数据时,'3'键不断被最后值I进入。有没有办法让这个工作在redis?
When I play around redis, and try to create the above data, the '3' key just keeps getting replaced by the last value I enter. Is there a way to make this work in redis?
推荐答案
一个更好的模型与Redis来表示你的数据将是使用简单值为1的对象集合(每个key1对象一个)。下面的例子,你可以存储在Redis中:
A better model with Redis to represent your data would be to use simple sets of value1 objects (one per key1 object). Following the example, you can store in Redis:
3 -> set( 1, 2, 3 )
4 -> set( 1 )
:
sadd 3 1 2 3
sadd 4 1
您可以使用以下方法获取key1 = 3的所有值:
You can get all the values for key1=3 by using:
smembers 3
您可以使用以下方法检查keys1 = 3,value1 = 2是否存在:
You can check if keys1=3,value1=2 exists by using:
sismember 3 2
Redis不是关系数据库系统,所以你不应该尝试映射一个关系模型与Redis。相反,您需要考虑如何构造和访问您的数据,就像您使用编程语言中的内存中数据结构。
Redis is not a relational database system, so you should not try to map a relational model with Redis. Instead, you need to think about how to structure and access your data like if you were using in-memory data structures from a programming language.
这篇关于我可以使两列彼此独有吗?或者在redis中使用复合主键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!