电商管理系统的数据库设计思路和数据库代码
电商系统架构设计思路
为了构建一个高效、可扩展和安全的电商系统,以下是一些架构设计思路:
架构风格选择
采用分层架构,将系统划分为表示层、业务逻辑层和数据访问层。
- 表示层:负责与用户进行交互,向用户展示界面,并接收并处理用户输入。
- 业务逻辑层:处理核心业务逻辑,协调各个模块之间的交互。它将表示层的请求传递给合适的模块,并处理相关业务逻辑处理后的结果。
- 数据访问层:负责与数据库或其他数据存储系统进行交互,执行数据库操作。
这种三层架构有助于实现高内聚和低耦合的组件,提高代码的可维护性和可重用性。
用户认证和权限管理
实现用户注册、登录和个人信息管理功能,并对不同用户类型(普通用户、商家、管理员)进行权限管理。
使用哈希算法对密码进行加密存储,并采用安全的身份验证方法,如JWT(JSON Web Token)来验证用户身份和保护敏感信息。
商品和分类管理
设计商品和分类管理模块,可通过管理员账号添加、编辑和删除商品信息。管理员还能够创建不同的商品分类,用于对商品进行分类和展示。
为商品表和分类表提供合适的索引和查询优化,以提高系统的数据库性能。
购物流程和订单管理
允许用户将商品添加到购物车,并在结算时选择收货地址。提供清晰明了的订单页面,使用户能够查看订单信息、支付状态和物流信息。
订单管理需保证订单数据的一致性和可靠性,确保用户购物过程的按序完成。设置订单状态及相关处理机制,比如待支付、已支付、已发货、已签收等状态,并与物流信息进行关联。
支付和物流集成
支持多种支付方式(如支付宝、微信支付)并与支付服务商进行集成。确保支付过程安全可靠,包括支付金额的准确计算和支付状态的实时更新。
与物流公司建立合作,集成物流接口,以便记录和跟踪用户订单的物流信息。用户可以方便地查看物流状态,提供更好的购物体验。
优惠券和积分功能
设计优惠券系统,包括优惠券的发放机制和使用规则。允许用户通过输入优惠码或享受折扣金额来获取购物优惠。
积分系统可鼓励客户参与活动并提升忠诚度。根据购买商品或参与活动的情况,给予用户相应积分。积分可以用于部分订单金额抵扣或兑换特定礼品。
用户评价和公告通知
用户可以对购买的商品进行评价,包括评分和评论内容,以帮助其他用户做出更好的购物决策。展示用户评价和提供排序和过滤功能,改善用户体验。
管理员可以发布公告向用户发送重要通知和促销信息,增强用户参与度和购物满意度。
以上是电商系统架构设计的一些思路,这些设计方案旨在构建一个功能丰富、高效和安全的电商平台,提供良好的用户体验并满足用户需求。
数据库解释
这个数据库包含了以下表及其详细结构:
address(地址)
- id: int,自增主键
- user_id: int,用户ID
- receiver_name: varchar(255),收件人姓名
- receiver_phone: varchar(255),收件人电话
- province: varchar(255),省份
- city: varchar(255),城市
- district: varchar(255),地区
- detail: varchar(255),详细地址
- postal_code: varchar(10),邮编
- is_default: tinyint(1),是否为默认地址
announcement(公告)
- id: int,自增主键
- title: varchar(255),公告标题
- content: text,公告内容
- create_time: datetime,创建时间
- userid: int,用户ID(外键,关联到user表的id字段)
category(商品分类)
- id: int,自增主键
- name: varchar(255),分类名称
coupon(优惠券)
- id: int,自增主键
- user_id: int,用户ID
- code: varchar(255),优惠券代码
- discount: decimal(10, 2),折扣金额
- valid_from: date,有效期起始日期
- valid_to: date,有效期截止日期
favorites(收藏)
- id: int,自增主键
- user_id: int,用户ID
- product_ids: varchar(255),商品ID列表
logistics_company(物流公司)
- id: int,自增主键
- name: varchar(255),物流公司名称
- contact_number: varchar(255),联系电话
- website: varchar(255),公司网站
logistics_info(物流信息)
- id: int,自增主键
- order_id: int,订单ID
- logistics_company_id: int,物流公司ID
- tracking_number: varchar(255),物流追踪号
- status: varchar(255),物流状态
- update_time: datetime,更新时间
loyalty_points(积分)
- id: int,自增主键
- user_id: int,用户ID
- points: int,积分数量
- validity_period: date,有效期限
orders(订单)
- id: int,自增主键
- user_id: int,用户ID
- total_amount: decimal(10, 2),订单总金额
- create_time: datetime,创建时间
- status: varchar(255),订单状态
payment_record(支付记录)
- id: int,自增主键
- order_id: int,订单ID
- payment_method: varchar(255),支付方式
- amount: decimal(10, 2),支付金额
- status: varchar(255),支付状态
- create_time: datetime,创建时间
product(商品)
- id: int,自增主键
- name: varchar(255),商品名称
- description: text,商品描述
- price: decimal(10, 2),商品价格
- stock: int,库存数量
- type: varchar(255),商品类型
- image_url: varchar(255),商品图片链接
product_image(商品图片)
- id: int,自增主键
- product_id: int,商品ID
- image_url: varchar(255),图片链接
review(评论)
- id: int,自增主键
- user_id: int,用户ID
- product_id: int,商品ID
- rating: int,评分
- comment: text,评论内容
- create_time: datetime,创建时间
shopping_cart(购物车)
- id: int,自增主键
- user_id: int,用户ID
- product_id: int,商品ID
- quantity: int,商品数量
user(用户)
- id: int,自增主键
- username: varchar(255),用户名
- password: varchar(255),密码
- email: varchar(255),电子邮件
- phone_number: varchar(255),电话号码
- gender: varchar(255),性别
- avatar: varchar(255),头像链接
- date_of_birth: date,出生日期
- create_time: datetime,创建时间
- balance: decimal(10, 2),余额
- user_type: enum(‘customer’,‘merchant’,‘admin’),用户类型
- salt: varchar(255),密码加密盐值
user_coupon(用户优惠券)
- id: int,自增主键
- user_id: int,用户ID
- coupon_id: int,优惠券ID
- coupon_name: varchar(255),优惠券名称
- quantity: int,优惠券数量
数据库代码
/* Navicat MySQL Data Transfer Source Server : localhost_3306 Source Server Type : MySQL Source Server Version : 80028 Source Host : localhost:3306 Source Schema : e-exam Target Server Type : MySQL Target Server Version : 80028 File Encoding : 65001 Date: 26/06/2023 11:01:33 */ SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for address -- ---------------------------- DROP TABLE IF EXISTS `address`; CREATE TABLE `address` ( `id` int NOT NULL AUTO_INCREMENT, `user_id` int NULL DEFAULT NULL, `receiver_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, `receiver_phone` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, `province` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, `city` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, `district` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, `detail` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, `postal_code` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, `is_default` tinyint(1) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = DYNAMIC; -- ---------------------------- -- Table structure for announcement -- ---------------------------- DROP TABLE IF EXISTS `announcement`; CREATE TABLE `announcement` ( `id` int NOT NULL AUTO_INCREMENT, `title` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, `content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL, `create_time` datetime NULL DEFAULT NULL, `userid` int NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE, INDEX `FK_Announcement_User`(`userid`) USING BTREE, CONSTRAINT `FK_Announcement_User` FOREIGN KEY (`userid`) REFERENCES `user` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = DYNAMIC; -- ---------------------------- -- Table structure for category -- ---------------------------- DROP TABLE IF EXISTS `category`; CREATE TABLE `category` ( `id` int NOT NULL AUTO_INCREMENT, `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = DYNAMIC; -- ---------------------------- -- Table structure for coupon -- ---------------------------- DROP TABLE IF EXISTS `coupon`; CREATE TABLE `coupon` ( `id` int NOT NULL AUTO_INCREMENT, `user_id` int NULL DEFAULT NULL, `code` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, `discount` decimal(10, 2) NULL DEFAULT NULL, `valid_from` date NULL DEFAULT NULL, `valid_to` date NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = DYNAMIC; -- ---------------------------- -- Table structure for favorites -- ---------------------------- DROP TABLE IF EXISTS `favorites`; CREATE TABLE `favorites` ( `id` int NOT NULL AUTO_INCREMENT, `user_id` int NOT NULL, `product_ids` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL, PRIMARY KEY (`id`) USING BTREE, INDEX `FK_Favorites_User`(`user_id`) USING BTREE, CONSTRAINT `FK_Favorites_User` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = DYNAMIC; -- ---------------------------- -- Table structure for logistics_company -- ---------------------------- DROP TABLE IF EXISTS `logistics_company`; CREATE TABLE `logistics_company` ( `id` int NOT NULL AUTO_INCREMENT, `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, `contact_number` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, `website` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = DYNAMIC; -- ---------------------------- -- Table structure for logistics_info -- ---------------------------- DROP TABLE IF EXISTS `logistics_info`; CREATE TABLE `logistics_info` ( `id` int NOT NULL AUTO_INCREMENT, `order_id` int NULL DEFAULT NULL, `logistics_company_id` int NULL DEFAULT NULL, `tracking_number` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, `status` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, `update_time` datetime NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = DYNAMIC; -- ---------------------------- -- Table structure for loyalty_points -- ---------------------------- DROP TABLE IF EXISTS `loyalty_points`; CREATE TABLE `loyalty_points` ( `id` int NOT NULL AUTO_INCREMENT, `user_id` int NULL DEFAULT NULL, `points` int NULL DEFAULT NULL, `validity_period` date NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = DYNAMIC; -- ---------------------------- -- Table structure for orders -- ---------------------------- DROP TABLE IF EXISTS `orders`; CREATE TABLE `orders` ( `id` int NOT NULL AUTO_INCREMENT, `user_id` int NULL DEFAULT NULL, `total_amount` decimal(10, 2) NULL DEFAULT NULL, `create_time` datetime NULL DEFAULT NULL, `status` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = DYNAMIC; -- ---------------------------- -- Table structure for payment_record -- ---------------------------- DROP TABLE IF EXISTS `payment_record`; CREATE TABLE `payment_record` ( `id` int NOT NULL AUTO_INCREMENT, `order_id` int NULL DEFAULT NULL, `payment_method` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, `amount` decimal(10, 2) NULL DEFAULT NULL, `status` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, `create_time` datetime NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = DYNAMIC; -- ---------------------------- -- Table structure for product -- ---------------------------- DROP TABLE IF EXISTS `product`; CREATE TABLE `product` ( `id` int NOT NULL AUTO_INCREMENT, `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, `description` text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL, `price` decimal(10, 2) NULL DEFAULT NULL, `stock` int NULL DEFAULT NULL, `type` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, `image_url` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = DYNAMIC; -- ---------------------------- -- Table structure for product_image -- ---------------------------- DROP TABLE IF EXISTS `product_image`; CREATE TABLE `product_image` ( `id` int NOT NULL AUTO_INCREMENT, `product_id` int NULL DEFAULT NULL, `image_url` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = DYNAMIC; -- ---------------------------- -- Table structure for review -- ---------------------------- DROP TABLE IF EXISTS `review`; CREATE TABLE `review` ( `id` int NOT NULL AUTO_INCREMENT, `user_id` int NULL DEFAULT NULL, `product_id` int NULL DEFAULT NULL, `rating` int NULL DEFAULT NULL, `comment` text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL, `create_time` datetime NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = DYNAMIC; -- ---------------------------- -- Table structure for shopping_cart -- ---------------------------- DROP TABLE IF EXISTS `shopping_cart`; CREATE TABLE `shopping_cart` ( `id` int NOT NULL AUTO_INCREMENT, `user_id` int NULL DEFAULT NULL, `product_id` int NULL DEFAULT NULL, `quantity` int NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = DYNAMIC; -- ---------------------------- -- Table structure for user -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int NOT NULL AUTO_INCREMENT, `username` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, `password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, `email` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, `phone_number` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, `gender` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, `avatar` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, `date_of_birth` date NULL DEFAULT NULL, `create_time` datetime NULL DEFAULT NULL, `balance` decimal(10, 2) NULL DEFAULT 0.00, `user_type` enum('customer','merchant','admin') CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT 'customer', `salt` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = DYNAMIC; INSERT INTO `user` VALUES (1, 'john123', 'ffdb4708de56ccea017dceac9ca26fad', 'john@example.com', '1234567890', 'Male', 'https://tse3-mm.cn.bing.net/th/id/OIP-C.9SyfqXgT17w0WVMmFhsqWgHaEc?w=245&h=180&c=7&r=0&o=5&dpr=1.8&pid=1.7', '1990-01-01', '2023-06-25 16:08:35', 100.00, 'customer', '405538f942a08c0338caf988e0ca4cbc'); -- ---------------------------- -- Table structure for user_coupon -- ---------------------------- DROP TABLE IF EXISTS `user_coupon`; CREATE TABLE `user_coupon` ( `id` int NOT NULL AUTO_INCREMENT, `user_id` int NOT NULL, `coupon_id` int NOT NULL, `coupon_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL, `quantity` int NOT NULL DEFAULT 0, PRIMARY KEY (`id`) USING BTREE, INDEX `fk_user_coupon_coupon`(`coupon_id`) USING BTREE, INDEX `fk_user_coupon_user`(`user_id`) USING BTREE, CONSTRAINT `fk_user_coupon_coupon` FOREIGN KEY (`coupon_id`) REFERENCES `coupon` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `fk_user_coupon_user` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = Dynamic; SET FOREIGN_KEY_CHECKS = 1;
如果大家觉得有用的话,可以关注我下面的微信公众号,极客李华,我会在里面更新更多行业资讯,企业面试内容,编程资源,如何写出可以让大厂面试官眼前一亮的简历等内容,让大家更好学习编程,我的抖音,B站也叫极客李华。大家喜欢也可以关注一下