美文网首页开发随笔
SpringBoot点餐随笔-数据库

SpringBoot点餐随笔-数据库

作者: 8813d76fee36 | 来源:发表于2017-09-29 17:57 被阅读48次

边听边记,以后再整理

表设计(MySQL)

  • 商品表 product_info
    1、商品id product_id使用varchar类型,不自增。id自增受int类型最大值限制。
    2、商品单价 product_price 使用decimal类型,涉及价钱计算使用该类型。
    3、创建时间 create_time 使用timestamp类型。设置默认值为当前时间
    default current_timestamp
    4、更新时间 update_time 使用timestamp类型。设置默认值为当前时间,并让数据库自动设置更新时间为操作的当前时间:
    default current_timestamp on update current_timestamp

注意:只有使用MySQL 5.7及以上版本才允许使用 default current_timestamp 设置默认值为当前时间。

 create table `product_info`(
    `product_id` varchar(32) not null,
    `product_name` varchar(64) not null comment '商品名称',
    `product_price` decimal(8,2) not null comment '商品单价',
    `product_stock` int not null comment '库存',
    `product_description` varchar(64) comment '描述',
    `product_icon` varchar(512) comment '小图',
    `category_type` int not null comment '类目编号',
    `create_time` timestamp not null default current_timestamp comment '创建时间',
    `update_time` timestamp not null default current_timestamp on update current_timestamp comment '修改时间',
    primary key(`product_id`)
) comment '商品表';
  • 类目表 product_category
    1、类目数量不多,id可以使用int自增
    `category_id` int not null auto_increment
    2、在商品表中,会利用类目编号category_type到类目表中与表中相应字段对应查询数据,为提升性能可以给该字段加约束索引。
    unique key `uqe_category_type` (`category_type`)
create table `product_category`(
    `category_id` int not null auto_increment,
    `category_name` varchar(64) not null comment '类目名称',
    `category_type` int not null comment '类目编号',
    `create_time` timestamp not null default current_timestamp comment '创建时间',
    `update_time` timestamp not null default current_timestamp on update current_timestamp comment '修改时间',
    primary key (`category_id`),
    unique key `uqe_category_type` (`category_type`)
) comment '类目表';
  • 订单总表 order_master
    1、订单状态 order_status 状态值使用tinyint即可。
    2、订单可能经常使用用户openid查询,所以给openid加索引。
    key `idx_buyer_openid` (`buyer_openid`)
create table `order_master`(
    `order_id` varchar(32) not null,
    `buyer_name` varchar(32) not null comment '买家名字',
    `buyer_phone` varchar(32) not null comment '买家电话',
    `buyer_address` varchar(128) not null comment '买家地址',
    `buyer_openid` varchar(64) not null comment '买家微信openid',
    `order_amount` decimal(8,2) not null comment '订单总金额',
    `order_status` tinyint(3) not null default '0' comment '订单状态,默认0新下单',
    `pay_status` tinyint(3) not null default '0' comment '支付状态,默认0未支付',
    `create_time` timestamp not null default current_timestamp comment '创建时间',
    `update_time` timestamp not null default current_timestamp on update current_timestamp comment '修改时间',
    primary key (`order_id`),
    key `idx_buyer_openid` (`buyer_openid`)
) comment '订单主表';
  • 订单详情表
    1、查询订单详情会用到订单id,所以给订单id加索引
    key `idx_order_id` (`order_id`)
create table `order_detail`(
    `detail_id` varchar(32) not null,
    `order_id` varchar(32) not null,
    `product_id` varchar(32) not null,
    `product_name` varchar(64) not null comment '商品名称',
    `product_price` decimal(8,2) not null comment '商品价格',
    `product_quantity` int not null comment '商品数量',
    `product_icon` varchar(512) comment '商品图片',
    `create_time` timestamp not null default current_timestamp comment '创建时间',
    `update_time` timestamp not null default current_timestamp on update current_timestamp comment '修改时间',
    primary key (`detail_id`),
    key `idx_order_id` (`order_id`)
) comment '订单详情表';

创建库

新建数据库,编码选择utf8mb4 - utf8mb4_general_ci
该编码可以存储emoji表情。

相关文章

网友评论

    本文标题:SpringBoot点餐随笔-数据库

    本文链接:https://www.haomeiwen.com/subject/omgiextx.html