Redis key-value database nosql data modeling (3) ------ How to store master-slave table data one-to-many relationship

                                                                                        Author: QQ 14588019 WonderfulLife

customers table (main table)

cust_id cust_name mobile address
3892045 Yang Fan 13500138000 1027709, Block C, Yihe Sunshine Building, Dongtucheng Road, Chaoyang District, Beijing Tian Ye 13600138000 Orders table (from the table),
South 7th High-tech Road, Nanshan District, Shenzhen City, Guangdong Province ord_id cust_id ord_date state 1035 3892045 2018-04-2018 Received 1039 3892045 2018-04-22 In delivery 1065 3892045 2018-04-22 In delivery 1033 1027709 2018-04-23 Out of stock







1044 1027709 2018-02-19 Signed

Create 1st customer information
cust_id     cust_name   mobile       address
Block C, Yihe Sunshine Building, Dongtucheng Road, Chaoyang District, Beijing, 3892045 Yang Fan 13500138000
			
MULTI
HMSET customer:3892045:hset cust_id 3892045 cust_name "Yang Fan" mobile "13500138000" address "Block C, Yihe Sunshine Building, Dongtucheng Road, Chaoyang District, Beijing"
LPUSH customer:list 3892045 # The set of customer numbers saved in the queue is used for paging
EXEC
---------------------------------------------------------------------------------------------
Create 1st order message
ord_id   cust_id   ord_date   state
1035 3892045 2018-04-22 Signed

MULTI
HMSET order:1035:hset ord_id  1035  cust_id 3892045  ord_date "2018-04-22"  state "已签收"
SADD customer:3892045:child:set 1035 # Use the collection to store the primary key of the secondary table, that is, to find the secondary table through the primary key of the primary table.
LPUSH order:list 1035 # The set of order numbers saved in the queue is used for paging
EXEC

Create 2nd Order Information
ord_id   cust_id   ord_date   state
1039 3892045 2018-04-22 In delivery

MULTI
HMSET order:1039:hset ord_id  1039  cust_id 3892045  ord_date "2018-04-22"  state "配送中"
SADD customer:3892045:child:set 1039 # Use the collection to store the primary key of the secondary table, that is, to find the secondary table through the primary key of the primary table
LPUSH order:list 1039 # The set of order numbers saved in the queue is used for paging
EXEC

Create Article 3 Order Information
ord_id   cust_id   ord_date   state
1065 3892045 2018-04-22 In delivery

MULTI
HMSET order:1065:hset ord_id  1065  cust_id 3892045  ord_date "2018-04-22"  state "配送中"
SADD customer:3892045:child:set 1065 # Use the collection to store the primary key of the secondary table, that is, to find the secondary table through the primary key of the primary table
LPUSH order:list 1065 # The set of order numbers saved in the queue is used for paging
EXEC

===================================================================================================
Create 2nd customer information
cust_id     cust_name   mobile       address
1027709 Tianye 13600138000 Gaoxin South 7th Road, Nanshan District, Shenzhen City, Guangdong Province
			
MULTI
HMSET customer:1027709:hset cust_id 1027709 cust_name "Tian Ye" mobile "13600138000" address "South 7th Gaoxin Road, Nanshan District, Shenzhen City, Guangdong Province"
LPUSH customer:list 1027709 # The set of customer numbers saved in the queue is used for paging
EXEC
---------------------------------------------------------------------------------------------
Create 1st order message
ord_id   cust_id   ord_date   state
1033 1027709 2018-04-23 Out of stock

MULTI
HMSET order:1033:hset ord_id  1033  cust_id 1027709  ord_date "2018-04-23"  state "已出库"
SADD customer:1027709:child:set 1033 # Use the collection to store the primary key of the secondary table, that is, to find the secondary table through the primary key of the primary table.
LPUSH order:list 1033 # The set of order numbers saved in the queue is used for paging
EXEC

Create 2nd Order Information
ord_id   cust_id   ord_date   state
1044 1027709 2018-02-19 Signed

MULTI
HMSET order:1044:hset ord_id  1044  cust_id 1027709  ord_date "2018-02-19"  state "已签收"
SADD customer:1027709:child:set 1044 # Use the collection to store the primary key of the secondary table, that is, to find the secondary table through the primary key of the primary table
LPUSH order:list 1044 # The set of order numbers saved in the queue is used for paging
EXEC

****************************************************************************************************

Display information for customer number (3892045)
127.0.0.1:6379> HGETALL customer:3892045:hset
127.0.0.1:6379> LLEN customer:list # Returns the total number of records  
127.0.0.1:6379> LRANGE customer:list 0 20 # This is the primary key of 20 records on page 1  
127.0.0.1:6379> LRANGE customer:list 21 40 # This is the primary key of 20 records on page 2  
127.0.0.1:6379> LRANGE customer:list 41 60 # This is the primary key of 20 records on page 3
Show detailed primary key collection
127.0.0.1:6379> SMEMBERS customer:3892045:child:set
Show details
127.0.0.1:6379> HGETALL order:1035:hset
127.0.0.1:6379> HGETALL order:1039:hset
127.0.0.1:6379> HGETALL order:1065:hset

After this article is explained, it may not be published commercially without permission!

Supplementary explanation NoSql many-to-many relationship modeling only needs to add another set in "adding details" to store the other party's primary key in the set contained in a primary key in "details", which is almost the same as this article, No more opening explanation!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325865540&siteId=291194637