第二天ci项目规划 数据库设计

第二天ci项目规划 数据库设计

商品 + 用户 + 订单

数据库设计思想和方法

  1. 关于商品品牌 ,分类 ,属性 如何表示 用一个字段 还是再设计一张表 品牌和商品 是一种信息 还是两种信息 一张表一般只保存一种信息
  2. 品牌和商品能否独立分开 二者生命周期是否一致
  3. 表与表之间的关系
    1. 一对一 A中一条记录 在B中对应的最多只有一条 反之亦然
    2. 一对多 A中一条记录在b中对应N条 反之只有一条
    3. 多对多 A中记录在b中对应的有N条 反之有m条
    4. 分类的层级关系
      1. 使用path 自引用
    5. 商品属性信息保存 商品与属性独立存储 attribute表属性与商品表关系 多对多 简化为两个一对多关系 goods_attr另attribute有type需要一张表
    6. 订单设计 一个订单 可包含多种商品 一个商品 可能有多个订单 订单和商品之间多对多 应该在添加一张表表示他们之间关联联系

      drop table if exists attr;

    drop table if exists brand;

    drop table if exists category;

    drop table if exists goods;

    drop table if exists goods_attr;

    drop table if exists goods_type;

    drop table if exists orde_rinfo;

    drop table if exists "order";

    drop table if exists user;

    /==============================================================/
    /* Table: attr /
    /
    ==============================================================*/
    create table attr
    (
    attr_id integer not null auto_increment,
    attr_name varbinary(10) not null,
    input_type integer not null,
    "values" varchar(10) not null,
    type_id integer not null,
    primary key (attr_id)
    )
    engine = MYISAM;

    /==============================================================/
    /* Table: brand /
    /
    ==============================================================*/
    create table brand
    (
    brand_id integer not null auto_increment,
    brand_name varchar(10),
    primary key (brand_id)
    )
    engine = MYISAM;

    /==============================================================/
    /* Table: category /
    /
    ==============================================================*/
    create table category
    (
    cat_id integer not null auto_increment,
    cat_name varchar(10) not null,
    parent_id integer not null,
    primary key (cat_id)
    )
    engine = MYISAM;

    /==============================================================/
    /* Table: goods /
    /
    ==============================================================*/
    create table goods
    (
    goods_id integer not null auto_increment,
    goods_name varchar(10) not null,
    market_price decimal(10,2) not null,
    shop_price decimal(10,2) not null,
    goods_img char(10) not null,
    goods_number integer not null,
    goods_desc text(100) not null,
    brand_id integer not null,
    cat_id integer not null,
    primary key (goods_id)
    )
    engine = MYISAM;

    /==============================================================/
    /* Table: goods_attr /
    /
    ==============================================================*/
    create table goods_attr
    (
    rec_id integer not null auto_increment,
    goods_id integer not null,
    attr_id integer not null,
    value char(10) not null,
    primary key (rec_id)
    )
    engine = MYISAM;

    /==============================================================/
    /* Table: goods_type /
    /
    ==============================================================*/
    create table goods_type
    (
    type_id integer not null auto_increment,
    type_name varbinary(30) not null,
    primary key (type_id)
    )
    engine = MYISAM;

    /==============================================================/
    /* Table: orde_rinfo /
    /
    ==============================================================*/
    create table orde_rinfo
    (
    red_id integer not null auto_increment,
    order_id integer not null,
    goods_id integer not null,
    primary key (red_id)
    )
    engine = MYISAM;

    /==============================================================/
    /* Table: "order" /
    /
    ==============================================================*/
    create table "order"
    (
    order_id integer not null,
    user_id integer,
    order_sn char(10) not null,
    order_tome integer not null,
    total_price decimal(10,2) not null,
    primary key (order_id)
    )
    engine = MYISAM;

    /==============================================================/
    /* Table: user /
    /
    ==============================================================*/
    create table user
    (
    user_id integer not null auto_increment,
    name varchar(10) not null,
    password char(10) not null,
    email varchar(10) not null,
    primary key (user_id)
    )
    engine = MYISAM;

    alter table attr add constraint FK_Reference_5 foreign key (type_id)
    references goods_type (type_id) on delete restrict on update restrict;

    alter table goods add constraint FK_Reference_1 foreign key (brand_id)
    references brand (brand_id) on delete restrict on update restrict;

    alter table goods add constraint FK_Reference_2 foreign key (cat_id)
    references category (cat_id) on delete restrict on update restrict;

    alter table goods_attr add constraint FK_Reference_3 foreign key (goods_id)
    references goods (goods_id) on delete restrict on update restrict;

    alter table goods_attr add constraint FK_Reference_4 foreign key (attr_id)
    references attr (attr_id) on delete restrict on update restrict;

    alter table orde_rinfo add constraint FK_Reference_7 foreign key (goods_id)
    references goods (goods_id) on delete restrict on update restrict;

    alter table orde_rinfo add constraint FK_Reference_8 foreign key (order_id)
    references "order" (order_id) on delete restrict on update restrict;

    alter table "order" add constraint FK_Reference_6 foreign key (user_id)
    references user (user_id) on delete restrict on update restrict;

上一篇:火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法


下一篇:DBA词典:数据库设计常用词汇中英文对照表