B2C电商设计

sku设计-方案一

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

create table category(
    id int(11) not null primary key auto_increment,
    name varchar(255) not null default '' comment '商品分类名字,默认为空字符串',
    parent_id int(11) not null default 0 comment '商品分类的父级分类,默认为0,代表根目录',
    sort int(11) not null default 0 comment '排序字段,默认为0'
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci comment '商品分类表';

create table product(
    id int(11) not null primary key auto_increment,
    name varchar(255) not null comment '商品名字',
    category_id int(11) not null comment '商品分类表id'
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci comment '商品表';

create table spec_group(
    id int(11) not null primary key auto_increment,
    name varchar(255) not null comment '规格组名字',
    category_id int(11) not null comment '商品分类表id'
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci comment '规格组表';

create table spec(
    id int(11) not null primary key auto_increment,
    name varchar(255) not null comment '规格项名字',
    group_id int(11) not null comment '规格组表id'
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci comment '规格项表';

create table spec_value(
    id int(11) not null primary key auto_increment,
    product_id int(11) not null comment '商品表id,联合主键',
    spec_id int(11) not null comment '规格项表id,联合主键',
    info varchar(255) not null default '' comment '具体规格信息'
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci comment '规格值表';

SET FOREIGN_KEY_CHECKS = 1;

查询的时候使用

select spec_group.name,spec.name,spec_value.info 
from spec_value 
left join spec on spec_value.spec_id = spec.id 
left join spec_group on spec.group_id = spec_group.id 
where product_id=?

数据表关系对照:

-- product 1:n spec_value
-- category 1:n product
-- category 1:n spec_group
-- spec_group 1:n spec
-- spec 1:n spec_value


sku设计-方案二

create table category(
    id int(11) not null primary key auto_increment,
    name varchar(255) not null default '' comment '商品分类名字,默认为空字符串',
    parent_id int(11) not null default 0 comment '商品分类的父级分类,默认为0,代表根目录',
    topic_img_id int(11) comment '图片表id',
    sort int(11) not null default 0 comment '排序字段,默认为0'
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci comment '商品分类表';

create table product(
    id int(11) not null primary key auto_increment,
    name varchar(255) not null comment '商品名字',
    category_id int(11) not null comment '商品分类表id',
    default_sku_id int(11) default null comment '默认的sku(单品)id,如果没有单品,则为NULL'
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci comment '商品表';

create table product_property(
    id int(11) not null primary key auto_increment,
    name varchar(30) DEFAULT '' COMMENT '详情属性名称',
    detail varchar(255) NOT NULL COMMENT '详情属性',
    product_id int(11) not null comment '产品表id',
    delete_time int(11) DEFAULT NULL,
    update_time int(11) DEFAULT NULL
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci comment '产品非销售属性表';

create table sku(
    id int(11) not null primary key auto_increment,
    product_id int(11) not null comment '商品表id',
    price decimal not null comment '单品价格',
    code varchar(255) comment 'sku唯一标示码',
    specs json comment '规格标示,取自sku_specs表',
    stock int(11) comment '库存量'
) comment 'sku表(单品表)';

create table sku_specs(
    id int(11) not null primary key auto_increment,
    product_id int(11) not null comment '商品id',
    sku_id int(11) not null comment 'sku_id(单品id)',
    spec_key_id int(11) not null comment '规格id',
    spec_value_id int(11) not null comment '规格值id'
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci comment 'sku值表';

create table spec_key(
    id int(11) not null primary key auto_increment,
    name varchar(255) not null comment '规格名字',
    category_id int(11) not null comment '商品分类表id',
    is_sale_attr tinyint default 1 comment '是否销售属性'
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci comment '规格表';

create table spec_value(
    id int(11) not null primary key auto_increment,
    spec_key_id int(11) not null comment '规格表id',
    value varchar(255) not null default '' comment '规格值'
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci comment '规格值表';

-- 数据表关系对照:
-- category 1:n product
-- product 1:n sku
-- sku 1:n sku_specs
-- category 1:n spec_key
-- spec_key 1:n spec_value
-- product 1:n product_property

使用ThinkPHP5.0.24来演示操作

<?php
namespace app\api\controller;

class Index {
        public function show()
        {

        }
}

订单设计

posted @ 2020-02-27 13:32  just_c  阅读(246)  评论(0)    收藏  举报