天天看点

新零售mysql设计 订单表 订单详情表

作者:陈业贵

文章目录

  • ​​sql​​
  • ​​订单表​​
  • ​​数据​​
  • ​​订单详情表​​
  • ​​数据:​​
  • ​​订单号与流水号有什么不同?​​
  • ​​订单表(解析)​​
  • ​​id int unsigned PRIMARY KEY AUTO_INCREMENT COMMENT "主键",​​
  • ​​`code` varchar(200) not null COMMENT "流水号",​​
  • ​​type tinyint unsigned not null COMMENT "订单类型:1实体销售,2网络销售",​​
  • ​​shop_id int unsigned COMMENT "零售店id",​​
  • ​​customer_id int unsigned COMMENT "会员id",​​
  • ​​voucher_id int unsigned COMMENT "购物券id",​​
  • ​​amount decimal(10,2) unsigned not null COMMENT "总金额",​​
  • ​​payment_type tinyint unsigned not null COMMENT "支付方式:1借记卡,信用卡,3微信,4支付宝,5现金",​​
  • ​​`status` tinyint unsigned not null COMMENT "状态:1未付款,2已付款,3已发货,4已签收",​​
  • ​​postage decimal(10,2) unsigned COMMENT "邮费",​​
  • ​​weight int unsigned COMMENT "重量(克)",​​
  • ​​create_time timestamp not null default now() COMMENT "创建时间",​​
  • ​​订单详情:​​
  • ​​PRIMARY KEY(order_id,sku_id)为什么?​​
  • ​​order_id int unsigned not null COMMENT "订单id",​​
  • ​​sku_id int unsigned not null COMMENT "商品id",​​

订单与商品表的关系:

一对多的关系。一个订单可以包括多个商品

sql

订单表

create table t_order(
id int unsigned PRIMARY KEY AUTO_INCREMENT COMMENT "主键",
    `code` varchar(200) not null COMMENT "流水号",
    type tinyint unsigned not null COMMENT "订单类型:1实体销售,2网络销售",
    shop_id int unsigned COMMENT "零售店id",
    customer_id  int unsigned COMMENT "会员id",
    amount decimal(10,2) unsigned not null COMMENT "总金额",
    payment_type tinyint unsigned not null COMMENT "支付方式:1借记卡,信用卡,3微信,4支付宝,5现金",
    `status` tinyint unsigned not null COMMENT "状态:1未付款,2已付款,3已发货,4已签收",
    postage decimal(10,2) unsigned COMMENT "邮费",
    weight int unsigned COMMENT "重量(克)",
    voucher_id int unsigned COMMENT "购物券id",
    create_time timestamp not null default now() COMMENT "创建时间",
    INDEX idx_code(`code`),
    INDEX idx_customer_id(customer_id),
    INDEX idx_status(`status`),
    INDEX idx_create_time(create_time),
    INDEX idx_type(type),
    INDEX idx_shop_id(shop_id),
    UNIQUE unq_code(`code`)
) COMMENT="订单表";      

数据

新零售mysql设计 订单表 订单详情表

订单详情表

CREATE TABLE t_order_detail(
    order_id int unsigned not null COMMENT "订单id",
    sku_id int unsigned not null COMMENT "商品id",
    price decimal(10,2) unsigned not null COMMENT "原价格",
    actual_price decimal(10,2) unsigned not null COMMENT "实际购买价格",
    num int unsigned not null COMMENT "购买数量",
    PRIMARY KEY(order_id,sku_id)
    
) COMMENT="订单详情表";      

数据:

新零售mysql设计 订单表 订单详情表

订单号与流水号有什么不同?

订单号是跟业务相关的,流水号是跟交易相关的,订单里面一定保存了金额,支付的时候用到流水号,所以两个使用的场景不一样,数据库id没有任何意义,只是一个记录的唯一标识而已

订单表(解析)

create table t_order(
id int unsigned PRIMARY KEY AUTO_INCREMENT COMMENT "主键",
    `code` varchar(200) not null COMMENT "流水号",
    type tinyint unsigned not null COMMENT "订单类型:1实体销售,2网络销售",
    shop_id int unsigned COMMENT "零售店id",
    customer_id  int unsigned COMMENT "会员id",
    amount decimal(10,2) unsigned not null COMMENT "总金额",
    payment_type tinyint unsigned not null COMMENT "支付方式:1借记卡,信用卡,3微信,4支付宝,5现金",
    `status` tinyint unsigned not null COMMENT "状态:1未付款,2已付款,3已发货,4已签收",
    postage decimal(10,2) unsigned COMMENT "邮费",
    weight int unsigned COMMENT "重量(克)",
    voucher_id int unsigned COMMENT "购物券id",
    create_time timestamp not null default now() COMMENT "创建时间",
    INDEX idx_code(`code`),
    INDEX idx_customer_id(customer_id),
    INDEX idx_status(`status`),
    INDEX idx_create_time(create_time),
    INDEX idx_type(type),
    INDEX idx_shop_id(shop_id),
    UNIQUE unq_code(`code`)
) COMMENT="订单表";      

id int unsigned PRIMARY KEY AUTO_INCREMENT COMMENT “主键”,

第一:id要自增(AUTO_INCREMENT),作用是什么呢?

1)增加记录时,可以不用指定id字段,不用担心主键重复问题。

2)数据库自动编号,速度快,而且是增量增长,按顺序存放,对于检索非常有利;

3)数字型,占用索引空间小,范围查找与排序友好,在程序中传递也方便;

为什么要设置主键,主键作用是什么?

主键是能确定一条记录的唯一标识,主键字段必须唯一,必须非空,一个表中只能有一个主键,主键可以包含一个或多个字段。

打个比方,一条记录包括身份正号,姓名,年龄,学校,国籍,性别等。身份证号是唯一能确定你这个人的,其他都可能有重复,所以,身份证号是主键。

设置主键的作用是唯一性。非空的。说明了自增的id不会重复的。

comment是什么?

代表一种注释。别人看懂这是什么字段或者表或者数据库

为什么要用int unsigned类型呢?

因为id是不是整数的。用unsigned是因为某种商品的数量是不是不能是负数呢.UNSIGNED的范围类型就是0~255

​code​

​ varchar(200) not null COMMENT “流水号”,

记录交易的。(应用场景:支付)

type tinyint unsigned not null COMMENT “订单类型:1实体销售,2网络销售”,

网上的交易叫做网络销售。实体销售是现实生活中去超市买的(看得见摸的着的)

shop_id int unsigned COMMENT “零售店id”,

customer_id int unsigned COMMENT “会员id”,

voucher_id int unsigned COMMENT “购物券id”,

你说对吧。买东西的时候产生订单的过程中。买过程中(产生订单)是不是得出现你是去那家网上商店买的。是不是会员。有没有购物券对吧.

amount decimal(10,2) unsigned not null COMMENT “总金额”,

订单的总金额多少钱是不是需要显示,当然需要对把,

payment_type tinyint unsigned not null COMMENT “支付方式:1借记卡,信用卡,3微信,4支付宝,5现金”,

​status​

​ tinyint unsigned not null COMMENT “状态:1未付款,2已付款,3已发货,4已签收”,

postage decimal(10,2) unsigned COMMENT “邮费”,

weight int unsigned COMMENT “重量(克)”,

create_time timestamp not null default now() COMMENT “创建时间”,

你看啊。订单是不是需要支付。是不是的有一个字段是显示支付的状态的,如果是网络销售的是不是得出邮费。或者商家出。商品的重量是不是也得有。创建时候也得有对吧

INDEX idx_code(`code`),
    INDEX idx_customer_id(customer_id),
    INDEX idx_status(`status`),
    INDEX idx_create_time(create_time),
    INDEX idx_type(type),
    INDEX idx_shop_id(shop_id),
    UNIQUE unq_code(`code`)      

为什么需要加索引?

因为某些字段需要快一点搜索对吧.

订单详情:

CREATE TABLE t_order_detail(
    order_id int unsigned not null COMMENT "订单id",
    sku_id int unsigned not null COMMENT "商品id",
    price decimal(10,2) unsigned not null COMMENT "原价格",
    actual_price decimal(10,2) unsigned not null COMMENT "实际购买价格",
    num int unsigned not null COMMENT "购买数量",
    PRIMARY KEY(order_id,sku_id)
    
) COMMENT="订单详情表";      

PRIMARY KEY(order_id,sku_id)为什么?

order_id int unsigned not null COMMENT “订单id”,

sku_id int unsigned not null COMMENT “商品id”,