Can I make two columns unique to each other? or us

2019-07-09 12:41发布

问题:

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

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?

回答1:

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 )

with the following commands:

sadd 3 1 2 3
sadd 4 1

You can get all the values for key1=3 by using:

smembers 3

You can check if keys1=3,value1=2 exists by using:

sismember 3 2

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.