鲜花网站项目(一)————项目需求分析以及数据表结构关系

项目需求分析

  1. 用户浏览花品功能
  2. 用户选购花品添加购物车,以及购买功能
  3. 用户管理自身相关信息
  4. 管理员账户管理用户信息以及花品信息功能

创建表结构关系

user表:具有普通用户和管理员用户

create table user(
       user_id int auto_increment primary key,
      username varchar(20) not null,
       password varchar(20) not null,
       role varchar(8) not null
      );

我认为user表和userdetail表具有主键一对一关联关系,userdetail的userdetail_id是user表的user_id主键的外键

userdetail表:是user表的详细相关信息

 create table userdetail(
        userdetail_id int not null,
        nickname varchar(30) not null,
        phone varchar(11) not null,
        email varchar(35) not null,
        created_at datetime not null
         );

 alter table userdetail add foreign key(userdetail_id) references user(user_id);

flower表:是flower的相关信息

create table flower(
         flower_id int auto_increment primary key,
         flower_name varchar(35) not null,
         flower_price double not null,
         flower_picture varchar(20) not null,
         flower_number int(5) not null,
         flower_info varchar(200)
        );

 catalog表:catalog表是flower的种类表

 create table catalog(
         catalog_id int auto_increment primary key,
         catalog_name varchar(20) not null
         );

 我认为catalog和flower具有多对多关联关系,so i create a table 展示这种关系

create table catalog_flower(
         catalog_id int ,
         flower_id int
         );
alter table catalog_flower add foreign key(flower_id) references flower(flower_id);
alter table catalog_flower add foreign key(catalog_id) references catalog(catalog_id);

我认为一个用户可以有多个购物车记录,ok,一个flower对应一个user应该只有一条购物车记录,因为很明显,正常情况下,一个用户的购物车里面不应该有重复的花品存在,当添加重复的花品时,我们采取的实际是更新购物车的花品数量的方式 。但是,我们存在很多user,所以一个user一个flower, 很多 user 就会有 很多 重复的 flower ,flower 对 购物车 为 一对多的关系。

这次我做的购物车并没有用网上所说的把购物车存到session或者是cookie中,那样浏览器重启or cookie清除都是导致数据丢失,虽然网上还有一种方法就是先存到session,等user登陆后提交到数据库中,但是这样的话就意味着用户可以直接先加入购物车而不需要在加入购物车之前先进行进行登陆,这个我觉得有点不太能接受,所以我是if你想要加入购物车,请先进行登陆,登陆后加入购物车的操作是直接将数据存到数据库中,因此这个表的结构如下

create table shopping_cart(
         shoppingcart_id int auto_increment primary key,
         user_id int not null,
         flower_id int not null,
         flower_name varchar(35) not null,
         flower_number int(5) not null,
         flower_price double not null
         );
alter table shopping_cart add foreign key(user_id) references user(user_id);
alter table shopping_cart add foreign key(flower_id) references flower(flower_id);

 当我们提交购物车后,会进入到订单界面,so,我来做一个订单表,同上,一个user有很多订单,一个flower有很多订单记录

create table shopping_order(
             shoppingorder_id int auto_increment primary key,
             user_id int not null,
             flower_id int not null,
             flower_name varchar(35) not null,
             flower_number int(5) not null,
             flower_price double not null,
             flower_sumprice double not null
              );    
alter table shopping_order add foreign key(flower_id) references flower(flower_id);
alter table shopping_order add foreign key(user_id) references user(user_id);

 确认订单,产生消费记录,一个user会有很多消费记录 so,

create table consume_record(
         consumerecord_id int auto_increment primary key,
         user_id int not null,
         created_at datetime not null
         );

 消费记录表里面是有着消费详情的 一个user有很多消费记录详情,一个flower有很多消费记录详情,一个consume_record有很多消费记录详情

create table counsumerecord_detail(
         record_detail int primary key auto_increment,
         user_id int not null,
         flower_id int not null,
         flower_name varchar(35) not null,
         flower_number int not null,
         flower_price double not null,
         flower_numprice double not null,
         consumerecord_id int not null
         );
alter table counsumerecord_detail add foreign key(user_id) references user(user_id);
alter table counsumerecord_detail add foreign key(flower_id) references flower(flower_id);
alter table counsumerecord_detail add foreign key(consumerecord_id) references consume_record(consumerecord_id);

 

为了能够做到数据的一致性,我想我们应该在消费记录详情表中设置一个触发器,当我们向consumerecord_detail表中插入一条新的纪录时候,那么flower表中相应的flower的数量自动减少,我想这个应该是通过触发器来实现的,但是的话,我们如果设置了触发器上层代码就显得很无力,而且,设置触发器需要直接写入值而不是从VIEW层传递过来的数据,so,这种做法想法很好,but,我不能实现

posted @ 2017-03-12 12:33  我们不该这样的  阅读(724)  评论(0编辑  收藏  举报