Server database design
mysql database design
- User Information Form Design
User information table, save the user's account id, user name, password, email and other important information
And auxiliary information
Uses: id specified user access to information, and show
Important attributes: mailbox, the mailbox is the user's security guarantees, you can register by mail, reset the password, perform critical operations
Field name | Field Type | Key Properties | Explanation |
---|---|---|---|
uid |
int |
primary key auto_increment |
用户 id |
uname |
varchar |
not null |
用户名 |
password |
varchar |
not null |
密码 |
email |
varchar |
not null & unique key |
用户认证凭证 |
sex |
tinyint |
default null |
性别 |
age |
tinyint |
defalut null |
年龄 |
create_time |
int |
not null |
注册时间 unix 时间戳 |
more_info |
text |
default null |
额外信息 json |
Construction of the table statement
create table `user` (
`uid` int primary key auto_increment COMMENT '用户 id 唯一 自增',
`uname` varchar(64) not null COMMENT '用户名',
`password` varchar(32) not null COMMENT '密码',
`email` varchar(32) not null COMMENT '绑定邮箱',
`sex` tinyint default null COMMENT '性别',
`age` tinyint default null COMMENT '年龄',
`create_time` int not null COMMENT '注册时间',
`more_info` text COMMENT '额外信息 json 格式保存',
unique key (`email`) COMMENT '邮箱 唯一'
) COMMENT = '用户表';
- User Registration Form Design
效验作用
The registration form: for registered users verify the authenticity, the transition is to become a true user information
Here the use of mail service to authenticate a user
Field name | Field Type | Key Properties | Explanation |
---|---|---|---|
email |
varchar |
primary |
注册邮箱 |
auth |
varchar |
not null |
验证码 |
expire_time |
int |
not null |
失效时间 |
Design codes 6
Use expiration time: regular tasks clearing the CAPTCHA failure to ensure that too much junk data table will not have
Construction of the table statement
create table `register` (
`email` varchar(32) primary key COMMENT '验证邮箱',
`auth` varchar(10) not null COMMENT '验证码',
`expire_time` int not null COMMENT '失效时间'
) COMMENT = '用户注册验证表';
- Reset Password Information Form (forget the password for the scene)
Sign up above the data table with the transition table
Same table design, a form can be used in combination, but the reason is that the separate design:
Consider a situation is not reasonable, if A If a registered mail account, the server authentication code is transmitted, so that the table register
will have a record registered in a field which holds codes.
A user is assumed that prior to use codes for authentication, the user B requests the service to retrieve the password, B should enter your mailbox, but the error input A mailbox become a (in this case there is no side effect of B the impact of a is a will again receive a message containing a verification code to reset your password), if the registration and reset the password using the same table, will lead to registration verification code is reset code coverage .
Designed separately to avoid code verification is covered by two different forms api to service (but still less than that A user will receive an email to reset your password)
After resetting your password main use mail verification, we can not confirm user input mailbox is its own mailbox, enter your account number and email id even require the user to reset the password and verify it on the server decide whether to send a message . But still can not use the server for malicious behavior to avoid the operation. Here we do not consider such malicious behavior.
- Friends table
Table Design:
Because the relationship is mutual, this design can reduce data redundancy, data in relation to each table there will be only one.
Field name | Field Type | Key Properties | Explanation |
---|---|---|---|
uid_1 |
int |
not null |
用户 1 |
uid_2 |
int |
not null |
用户 2 |
remark_1_2 |
varchar |
default null |
1 对 2 的备注 |
remark_2_1 |
varchar |
default null |
2 对 1 的备注 |
group_1_2 |
varchar |
default null |
1 对 2 的分组 |
group_2_1 |
varchar |
default null |
2 对 1 的分组 |
Not consider blacklist, delete the one-way friendship will be deleted
uid_1
Anduid_2
group collaboration primary key
Construction of the table statement
create table `friend` (
`uid_1` int not null COMMENT '用户 1',
`uid_2` int not null COMMENT '用户 2',
`remark_1_2` varchar(64) COMMENT '用户1 对 用户2 的备注',
`remark_2_1` varchar(64) COMMENT '用户2 对 用户1 的备注',
`group_1_2` varchar(64) COMMENT '用户1 对 用户2 的分组',
`group_2_1` varchar(64) COMMENT '用户2 对 用户1 的分组',
primary key(`uid_1`, `uid_2`) COMMENT '好友关系 唯一性'
) COMMENT = '好友关系表';
- Friends offline message table
Field name | Field Type | Key Properties | Explanation |
---|---|---|---|
to_uid |
int |
index & not null |
接收者 id 索引 |
from_uid |
int |
not null |
发送者 id |
msg |
text |
not null |
消息 json 格式 |
Friends offline forms used only when the recipient is offline. When the recipient is not online through mysql, it will be pushed directly to the target user
Table offline message to the recipient as the main index design, when the user directly pulling the line all
to_uid
equal用户 id
messages to
Offline message not only to save chat messages, and friend requests containing a message, group chat invitation message
Construction of the table statement
create table `pri_msg` (
`to_uid` int not null COMMENT '接受者 id',
`from_uid` int not null COMMENT '发送者 id',
`msg` text not null COMMENT '消息 json 格式',
index (`to_uid`) COMMENT '建立索引'
) COMMENT '私聊离线消息表';
- Group chat information table
Group chat information table:
Display some basic information about major conservation group chat, group chat easy viewing
Field name | Field Type | Key Properties | Explanation |
---|---|---|---|
gid |
int |
primary key & auto_increment |
群聊 id 主键 自增 |
owner |
int |
not null |
群主 id |
gname |
varchar |
not null |
群聊名称 |
create_time |
int |
not null |
群聊建立时间 |
person_number |
int |
not null |
群人数 |
Construction of the table statement
create table `group_info` (
`gid` int primary key auto_increment COMMENT '群聊 id 自增',
`owner` int not null COMMENT '群主',
`gname` varchar(64) not null COMMENT '群名称',
`create_time` int not null COMMENT '建群时间',
`person_number` int not null COMMENT '群人数'
) COMMENT = '群聊信息表';
/* index (`owner`) */ /* 暂不考虑为 owner 建立 索引 */
- Group Chat user table
Field name | Field Type | Key Properties | Explanation |
---|---|---|---|
gid |
int |
not null |
群聊 id |
uid |
int |
not null |
用户 id |
join_time |
int |
not null |
入群时间 |
remark |
varchar |
not null |
群呢称 |
last_msg_id |
int |
no null |
已读群聊消息的最大 id |
Group Chat user relationship table primary key design
(gid, uid)
,last_msg_id
the role that the user has read the message marking, push unread message on the line when the user (with the following table using the stored group chat message)
Construction of the table statement
create table `group_person` (
`gid` int not null COMMENT '群聊 id',
`uid` int not null COMMENT '用户 id',
`join_time` int not null COMMENT '加群时间',
`remark` varchar(64) COMMENT '群聊备注',
`last_msg_id` int not null COMMENT '已读的当前群聊最后一条消息 id',
primary key(`gid`, `uid`)
) COMMENT = '群聊 用户关系表';
- Group chat messages offline table
For different group chat, group chat each design a form. Facilitating the increment message id
Small token of my name designed to group
group:$gid
$gid
shows the group number
Field name | Field Type | Key Properties | Explanation |
---|---|---|---|
mid |
int |
primary key auto_increment |
消息 id |
from_uid |
int |
not null |
发送者 id |
msg |
text |
not null |
消息 |
Description: Form a group chat messages from the message id is set to increase, a group chat offline messages push, the sender only needs to record and send a message id.
Construction of the table statement
create table `group:gid` (
`mid` int primary key auto_increment COMMENT '消息 id, 自增',
`from_uid` int not null COMMENT '发送者 id',
`msg` text not null COMMENT '群聊消息'
) COMMENT = '群聊离线消息列表';