Fan pays attention to the design of database table

Source: Internet
Author: User

CREATE TABLE relation (id PRIMARY KEY AUTO_INCREMENT, //主键,自增from_user_id big integer, // 用户 A 的 idto_user_id big integer,// 用户 B 的 Idrel_type enum(1,2) //关注数据);

Pull/fans/followers, in the database, the number that is stored is a mapping relationship. For example, the black is 1, the fan/concern is a thing, is 2. So, the key data in a record is:

    • from_user_idWhich user initiated this record?

    • to_user_idWhich user is the recipient of this record?

    • rel_typeWhat did the initiator do to the recipient? Types of Things to save

Examples of scenarios:

User A is concerned about user B

Insert data:

INSERT INTO relation (rel_type, from_user_id, to_user_id) VALUES(2, A.id, B.id)

Number of fans for user A:

select COUNT(*) from relation where rel_type=2 and to_user_id=A.id;

Fan pays attention to the design of database table

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.