用戶實體-電商實例數據庫設計及優化學習筆記

一、背景

這是電商實例數據庫結構設計的用戶模型設計部分,主要內容是用戶模塊的數據庫設計,包括用戶的信息、積分、日誌等等。

二、數據庫結構設計

1、用戶模型設計
也就是管理和維護用戶信息,用得最多的就是登錄和註冊兩個功能。
用戶模塊中最基本的用戶信息:用戶名、密碼、手機號
我們通常把用戶稱爲實體,用戶信息稱爲實體的屬性。
完整的用戶信息,也就是用戶實體的屬性:真實姓名、登錄名、密碼、手機號、證件類型和號碼、郵箱、性別、郵編、省、市、區、門牌號、積分、註冊時間、生日、狀態、級別、餘額(郵箱、手機號、登錄名都可以用來標識一個用戶),其中有些是用戶錄入來獲取的,有些是經過計算得出的,比如註冊時間、用戶生日、餘額、積分。
1.1 如何把用戶的屬性存到表中
把所有的信息存到一個表,優點:易於數據存儲。缺點:會發生數據插入、更新、刪除異常和冗餘問題。更新異常:要修改某一行的值時,不得不修改多行數據。

建立用戶登錄表

CREATE TABLE customer_login(
    customer_id INT UNSIGNED AUTO_INCREMENT NOT NULL comment '用戶ID',
    login_name VARCHAR(20) not null comment '用戶登錄名',
    password char(20) not null comment 'md5加密的密碼',
    user_stats TINYINT not null DEFAULT 1 COMMENT '用戶狀態',
    modified_time TIMESTAMP not null DEFAULT CURRENT_TIMESTAMP
    on UPDATE CURRENT_TIMESTAMP COMMENT '最後修改時間',
    PRIMARY key pk_customerid(customer_id)
    )ENGINE = INNODB COMMENT='用戶登錄表'
    modified_time TIMESTAMP not null DEFAULT CURRENT_TIMESTAMP
    on UPDATE CURRENT_TIMESTAMP 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 護照',
    identity_card_no VARCHAR(20)  comment '證件號碼',
    mobile_phone int UNSIGNED comment '手機號',
    customer_email VARCHAR(50) comment '郵箱',
    gender char(1) COMMENT '性別',
    uesr_point int not null DEFAULT 0 COMMENT '用戶積分',
    register_time TIMESTAMP not null comment '註冊時間',
    birthday datetime COMMENT '會員生日',
    customer_level tinyint not null DEFAULT 1 COMMENT
    '會員級別:1 普通會員 2 青銅會員,3 白銀會員,4 黃金會員,5 鑽石會員',
    user_money DECIMAL(8,2) not null DEFAULT 0.00 COMMENT '用戶餘額',
    modidied_time TIMESTAMP not null DEFAULT CURRENT_TIMESTAMP on UPDATE CURRENT_TIMESTAMP COMMENT '最後修改時間',
    PRIMARY key pk_customerinfid(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 '該級別最高積分',
    modidied_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 not null auto_increment COMMENT '自增主鍵ID',
    customer_id int UNSIGNED not null COMMENT 'customer_login表的自增主鍵ID',
    zip SMALLINT not null comment '郵編',
    province SMALLINT not null comment '地區表中省份的id',
    city SMALLINT not null comment '地區表中城市的id',
    district SMALLINT not null comment '地區表中區的id',
    address VARCHAR(200) not null comment '具體地址的門牌號',
    is_default TINYINT not null COMMENT '是否默認',
    modidied_time TIMESTAMP not null DEFAULT CURRENT_TIMESTAMP on UPDATE CURRENT_TIMESTAMP COMMENT '最後修改時間',
    PRIMARY key pk_customeraddrid(customer_addr_id)
    )ENGINE = INNODB COMMENT='用戶地址表'

接下來是日誌表,用來記錄操作信息,如果操作出現了失誤可以用它來進行分析
用戶積分日誌表

CREATE TABLE customer_point_log(
    point_id int UNSIGNED not null auto_increment COMMENT '積分日誌ID',
    customer_id int UNSIGNED not null COMMENT 'customer_login表的自增主鍵ID',
    source tinyint UNSIGNED not null comment '積分來源:0 訂單,1 登錄 ,2 活動',
    refer_numer 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='用戶積分日誌表'

用戶餘額日誌表

    CREATE TABLE customer_balance_log(
    balance_id int UNSIGNED not null auto_increment COMMENT '餘額日誌ID',
    customer_id int UNSIGNED not null COMMENT 'customer_login表的自增主鍵ID',
    source tinyint UNSIGNED not null comment '記錄來源:1 訂單,2 退貨單',
    source_sn int UNSIGNED not null comment '相關單據id',
    create_time TIMESTAMP not null DEFAULT CURRENT_TIMESTAMP comment '記錄生成時間',
    amount DECIMAL(8,2) not null DEFAULT 0.00 comment '變動金額',
    PRIMARY key pk_balanceid(balance_id)
    )ENGINE = INNODB COMMENT='用戶餘額變動表'

用戶登錄日誌表

CREATE TABLE customer_login_log(
    login_id int UNSIGNED not null auto_increment COMMENT '登錄日誌ID',
    customer_id int UNSIGNED not null COMMENT 'customer_login表的自增主鍵ID',
    login_time TIMESTAMP not null comment '用戶登錄時間',
    login_ip int UNSIGNED not null comment '登錄ip',
    login_type tinyint not null comment '登錄類型:0 未成功,1 成功',
    PRIMARY key pk_loginid(login_id)
    )ENGINE = INNODB COMMENT='用戶登錄日誌表'

三、小結

在本模塊中設計了
用戶登錄表、
用戶信息表、
用戶級別信息表、
用戶地址表、
用戶積分日誌表、
用戶登錄日誌表、
用戶餘額日誌表。
設計一個模塊的數據庫,首先要分析其設計的功能,然後列出實體的屬性,結合功能和屬性來設計相關的表。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章