数据库设计
用户表单设计
create table user
(
id int unsigned primary key auto_increment comment '用户ID',
username varchar(20) not null comment '用户名',
password varchar(50) not null comment '密码',
nickname varchar(20) default '' comment '昵称',
email varchar(128) default '' comment '邮箱',
user_img varchar(128) default '' null comment '头像',
update_time date not null comment '更新时间',
create_time date not null comment '创建时间',
power tinyint not null default 1 comment '权限'
)default character set utf8mb4 comment '用户表';
由于用户头像的使用到的存储空间是不定的,一般不将用户头像存储在数据库中,所以将用户头像定义成一个URL,这样未来租用阿里云的对象存储服务OSS就可以用URL加载头像。
另外,我们的二手交易平台最终不仅仅要有普通的用户,还会有管理员用户来管理平台的运行。所以用户之间会有权限的差异,所以定义管理权限字段。
书籍表单设计
我们打算做书籍分类功能,所以需要一个记录书籍分类的表单
create table book_type
(
id int unsigned primary key auto_increment comment '类别ID',
name varchar(20) not null comment '类别名'
)default character set utf8mb4 comment '书籍类别';
关于待售书籍,我们设计这样一个表单来存储信息:
create table book
(
id int unsigned primary key auto_increment comment '书籍ID',
name varchar(50) not null comment '书籍名称',
type_id int unsigned not null comment '类别',
price decimal(12, 2) not null comment '价格',
isbn varchar(20) not null comment 'ISBN号',
img varchar(128) null comment '头像',
detail varchar(500) default '' null comment '书籍描述',
release_time date not null comment '上架时间',
seller_id int unsigned not null comment '卖家ID',
purchased int not null comment '是否已被购买'
)default character set utf8mb4 comment '待售书籍';
平台只负责管理待售的书籍,而不会试图去管理用户不再想要售卖的书籍或者已经卖出去的商品,所以需要 purchased
字段来标识。但这样子就是说,用户每挂售一个书籍,就只支持一次交易,这样做是考虑到我们面向的个人卖家和个人买家,而不是专门面向二手书售卖商。
订单表单设计
create table `order`
(
id int unsigned primary key auto_increment comment '订单ID',
user_id int unsigned not null comment '用户ID',
book_id int unsigned not null comment '书籍ID',
pay_time date not null comment '下单时间',
address varchar(100) not null comment '收货地址',
status tinyint not null comment '订单状态'
)default character set utf8mb4 comment '订单';
买家管理自己买某些书的意愿
卖家可以查看自己卖了哪些书,并可以查看有哪些人希望买这本书。
卖家通过书籍ID去查找买家;买家可以通过用户ID查看自己想要买哪些书。
聊天模块的表单设计
create table `connection`
(
id int unsigned primary key auto_increment comment '连接ID',
user1_id int unsigned not null comment '用户1的ID',
user2_id int unsigned not null comment '用户2的ID'
)default character set utf8mb4 comment '聊天连接';
create table `record`
(
connect_id int unsigned not null unique,
sender_id int unsigned not null comment '发送方ID',
reciever_id int unsigned not null comment '接收方ID',
number int unsigned auto_increment comment '记录编号',
type int unsigned not null default 1 comment '记录类型',
content varchar(150) not null comment '记录'
)default character set utf8mb4 comment '聊天记录';
接收方ID和发送方ID来自于connection表单中的用户1的ID或用户2的ID。
也就说,在一个连接中,一个用户要么是发送方要么是接收方。
而number编号,是用来表示发送的顺序的,也就是说编号越小发送得越早。
type是用来解释记录中存储的内容的,比如我们可以这样子规定:
- type=1:content为文字记录
- type=2:content为图片URL
- type=3:content为视频URL
- ...
这样就可以做到存储不同的聊天记录的效果
表单之间的关系
ER图:
erDiagram
USER ||--o{ BOOK : sell
USER ||--o{ BOOK : purchase
USER ||--o{ ORDER : purchase
BOOK ||--|| TYPE : belongTo
USER {
id int
username string
password string
nickname string
email string
userImg url
updateTime date
createTime date
power int
}
BOOK {
id int
name string
price double
isbn string
img url
detail string
releaseTime date
sellerId int
purchased bool
}
ORDER {
id int
buyerId int
bookId int
payTime date
address string
status int
}
TYPE {
id int
name string
}
修改于 4 个月前