【用戶模塊】html
帳戶登陸:郵箱/用戶名/已驗證手機mysql
密碼sql
若是將全部字段都放到一張表裏存儲?數據庫
數據插入異常 只想插入一個值的 因爲須要主鍵信息,插入的一行變成新的一行,和原來的記錄無關。ide
數據更新異常 只想更新一個值的時候 不得不更新多行 文件鎖機制可能形成用戶沒法登陸。 => 能夠把範圍縮小到另外一張表操做。函數
數據刪除異常 只刪一個值 不得不把無關的數據一塊兒刪了編碼
核心的問題就在於 【數據冗餘出錯了!】,加密
函數依賴 function(arg-1) <- function (arg0) <- function(arg1,arg2) spa
因此須要拆分表 知足第三數據庫範式(外鍵),天然知足第一(二維表)、第二範式(主鍵)。設計
拆分表經驗 : 常常用是熱數據 不常常是冷數據 ,把熱數據放一塊兒,單獨修改很爽。
【用戶級別信息表字段】 會員級別 級別積分下限 級別積分上限。
【用戶登錄表字段】登陸名 密碼 用戶狀態
【用戶地址表】省 市 區 郵編 地址
【用戶信息表】用戶姓名 證件類型 證件號碼 手機號 郵箱 性別 積分 註冊時間 生日 會員級別 用戶餘額。
正常設計數據庫表,按照數據流向(閉環核心業務)。
【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 '用戶字典表' ;