Storage 002 电商数据库设计

【用户模块】

账户登录:邮箱/用户名/已验证手机

密码

 

 

 

如果将所有字段都放到一张表里存储?

数据插入异常        只想插入一个值的  由于需要主键信息,插入的一行变成新的一行,和原来的记录无关。

数据更新异常  只想更新一个值的时候 不得不更新多行   文件锁机制可能造成用户无法登录。 => 可以把范围缩小到另一张表操作。

数据删除异常  只删一个值    不得不把无关的数据一起删了

 

核心的问题就在于   【数据冗余出错了!】,

 

函数依赖   function(arg-1)   <- function (arg0)   <- function(arg1,arg2)      

所以需要拆分表 满足第三数据库范式(外键),自然满足第一(二维表)、第二范式(主键)。

 

拆分表经验 :  经常用是热数据  不经常是冷数据 ,把热数据放一起,单独修改很爽。

【用户级别信息表字段】 会员级别  级别积分下限 级别积分上限。

【用户登陆表字段】登录名 密码 用户状态

【用户地址表】省 市 区 邮编 地址

【用户信息表】用户姓名  证件类型 证件号码 手机号 邮箱 性别  积分  注册时间  生日 会员级别 用户余额。

 

正常设计数据库表,按照数据流向(闭环核心业务)。   

【1用户】登录 =》浏览【2分类】+浏览【3商品】=》加入【4购物车】=》结算【5订单】+【6收货地址】=》【7支付】

 

=======================================================================

https://dev.mysql.com/doc/refman/5.7/en/numeric-type-overview.html

varchar(M)    M指的的是字符数   根据UTF-8编码 字节数为 3 * M

int(M)          M要配合 zerofill(默认unsigned),前面用0填充 。   要表示status,一般 tinyint 就够了 可以表示 -127~128 ,unsigned 255。

 

2^8 = 256   2^1 = 2     2^32=42亿    

 

create table customer_login(
    customer_id int unsigned AUTO_INCREMENT NOT NULL comment '用户ID',      -- int unsigned     不需要负数选择无符号 存储42亿   
    login_name varchar(20) not null comment '用户登录名',            -- varchar(20)  20个字符   utf8   20x3 = 60个字节 
    password char(32) not null comment 'md5加密密码',                       -- char(32)      经过md5加密就是32字节字符串
    user_stats  tinyint not null default 1 comment '用户状态',         -- 表示 -128~127
    modified_time timestamp not null default current_timestamp
    on update current_timestamp comment '最后修改时间'              --  使用 mysql自带更新修改时间功能 , 程序上方便很多

    primary key pk_customerid(customer_id)
)engine = innodb comment='用户登陆表'
;

 

 

create table customer_inf(
    customer_inf_id int unsigned AUTO_INCREMENT not null comment '自增主键ID',
    customer_id int unsigned not null comment 'customer_login表的自增id',
    customer_name varchar(20) not null comment '用户真实姓名',
    identity_card_type tinyint not null default 1 comment '证件类型:1 身份证 ,2 军官证 3 护照',
    mobile_phone int unsigned comment '手机号',
customer_mail varchar(50) comment '邮箱',
gender char(1) comment '性别', -- utf8编码 3字节 1个字符
user_point int not null default 0 commnet '用户积分',
register_time timestamp not null comment '注册时间',
   birthday datetime comment '会员生日',-- datetime 1000-01-01 9999-12-31
   customer_level tinyint not null default 1
comment '会员级别:1普通会员,2青铜会员,3白银会员,4黄金会员,5钻石会员',
   user_money decimal(8,2) not null default 0.00 comment '用户余额',
 modified_time timestamp not null default CURRENT_TIMESTAMP
   on update current_timestamp comment '最后修改时间',
   primary key pk_customerinfid commentfid(customer_inf_id)
   )engine
=innodb comment '用户信息表' ;

 

create table customer_level_inf(
    customer_level tinyint not null auto_increment comment '用户级别自增ID ' ,
    level_name varchar(10) not null comment '会员级别名称',
    min_point int unsigned not null default 0 comment '级别最低分',
    max_point int unsigned not null default 0 comment '级别最高分',
    modified_time timestamp not null default current_timestamp
    on update  current_timestamp comment '最后修改时间'
   primary key pk_levelid(customer_level) 
)engine = innodb comment '用户级别信息表'
;

 

create table customer_addr(
    customer_addr_id int unsigned AUTO_INCREMENT not null comment '自增ID',
    customer_id int unsigned not null comment 'customer_login表的自增ID',
    zip smallint not null comment '邮编',
    province smallint not null comment '地区表中省份id',    --  smallint 2^16 = 65536  16/8 = 2个字节
    city smallint not null comment '地区表中市id',
    district smallint not null comment '地区表中区id',
    address varchar(200) not null comment '具体地址门牌号',
    is_default tinyint not null comment '是否默认',
    modified_time timestamp not null default current_timestamp
    on update current_timestamp comment '最后修改时间',
    primary key pk_customeraddid(customer_addr_id)
)enginer=innodb comment '用户地址表'
;
create table customer_point_log(
    point_id int unsigned not null auto_increment comment '积分日志ID',
    customer_id int unsigned not null comment '用户id',
    source tinyint unsigned not null comment '积分来源:0订单 1登陆 2活动',
    refer_number int unsigned not null default 0 comment '积分来源相关编号',
    change_point smallint not null  default 0 comment '变更积分数',
    create_time timestamp not null comment '积分日志产生时间',
    primary key pk_pointid(point_id)
)engine = innodb comment '用户字典表'
;

 

posted @ 2018-07-22 11:31  chenhui7373  阅读(236)  评论(0编辑  收藏  举报