超市進銷存系統 數據庫設計報告

超市進銷存系統 數據庫設計報告

注意:本設計報告省略了需求分析部分、實體說明、聯繫說明、索引說明等,重點是數據庫的實現過程以及sql語言的編寫以及其他一些我認爲的重點展示
另外:本系統前期主要使用了軟件PowerDesigner,從需求分析到模型設計、約束條件、視圖、業務規則等,都是用的該軟件。

產品簡介

超市進銷存系統主要爲商品的進貨上架、銷售收銀、倉庫存儲提供線上管理的功能。

目標客戶:中小型超市

客戶的業務需求:

  1. 改變傳統的人工管理,實現日常管理信息化;

  2. 通過對庫存和銷售信息的快速查詢和處理,提高商品的採購的速度和科學性;

  3. 提升超市管理水平,降低經營成本,提高工作效率。

本系統的總體框架
這裏寫圖片描述


數據模型

BPM模型
這裏寫圖片描述
CDM 模型
這裏寫圖片描述
PDM模型
這裏寫圖片描述

部分存儲器、觸發器、函數的設計

本人負責的是關於存貨模塊的存儲器、觸發器、函數設計,這裏舉了幾個例子:

  • 存儲器設計1、
/*具有“採購員”職稱的員工加薪15%,“理貨員”加薪10%,“收銀員”加薪5%,“經理”加薪20%*/
create procedure proc_raise
as
declare cur cursor for select workerNum,job from t_user
declare @increment decimal(2,1)
declare @num int
declare @tjob varchar(50)
open cur
fetch next from cur into @num,@tjob
while(@@fetch_status=0)
begin
if @tjob='採購員' set @increment=0.15
else if @tjob='理貨員'set @increment=0.1
else if @tjob='收銀員' set @increment=0.05
else if @tjob='經理' set @increment=0.2
update t_user
set salary=salary*(1+@increment)
where workerNum=@num
fetch next from cur into @num,@tjob
end
close cur
deallocate cur
go

測試結果:
未執行存儲器之前的員工表:
這裏寫圖片描述

執行加薪功能的的存儲器之後:

execute proc_raise

這裏寫圖片描述

  • 存儲器設計2、
/*清空庫存信息表:t_inventory鍾庫存量爲0的商品信息*/
create procedure proc_amount0
as
declare cur cursor for select goodsNum,amount from t_inventory
declare @gnum int
declare @gamount int
open cur
fetch next from cur into @gnum,@gamount
while(@@fetch_status=0)
begin
if @gamount=0
delete from t_inventory
where goodsNum=@gnum
fetch next from cur into @gnum,@gamount
end
close cur
deallocate cur

測試結果:
未執行存儲器之前的t_inventory:
這裏寫圖片描述

執行存儲器:

execute proc_amount0

這裏寫圖片描述

  • 用戶自定義函數設計1、
/*輸入商品編號,在t_goodsOn表上將該商品標註爲' 促銷-買一送一'*/

create function dbo.func_num_cuxiao(@gnum int)
returns varchar(50)
as
begin
declare @gname varchar(50);
declare @gprice int;
select @gname=goodsName from t_goodsOn where t_goodsOn.goodsNum=@gnum;
select @gprice=price from t_goodsOn where t_goodsOn.goodsNum=@gnum;
set @gname=@gname+'促銷-買一送一';
return @gname
end

declare @t varchar(50)
execute @t= dbo.func_num_cuxiao "01";
update t_goodsOn
set goodsName=@t where goodsNum=01 ;
select *from t_goodsOn

測試結果:
這裏寫圖片描述

  • 用戶自定義函數設計2、
/*對於庫存量小於10的商品,備註remark裏面添加“庫存緊張!!!”的字段*/
create function dbo.func_less(@gnum int)
returns varchar(50)
as 
begin
declare @mark varchar(50)
set @mark='庫存緊張!!!'+(select remark from t_inventory where goodsNum=@gnum)
return @mark
end

create procedure proc_tmp
as
declare cur cursor for select goodsNum from t_inventory
declare @gnum int
declare @ta int 
declare @t varchar(50)
open cur
fetch next from cur into @gnum
while(@@fetch_status=0)
begin
select @ta=amount from t_inventory where goodsNum=@gnum
if @ta<10
begin
execute @t= dbo.func_less @gnum ;
update t_inventory
set remark=@t where goodsNum=@gnum ;
end
fetch next from cur into @gnum
end
close cur
deallocate cur

結果測試:
原來的庫存信息表t_inventory:
這裏寫圖片描述

execute proc_tmp

執行之後:
這裏寫圖片描述

  • 觸發器設計1、
/*觸發器:t_goodsBuy商品進價變,t_incentory的同商品的進價也變,售價+(現進價-原進價),t_goodsSale商品售價也變*/
create trigger tri_alter_cost
on dbo.t_goodsBuy
after insert
as
begin 
declare @num int;
declare cur cursor for select goodsNum from inserted;
open cur;
fetch from cur into @num;
while @@FETCH_STATUS=0
begin
update t_inventory
set cost=(select cost from inserted where goodsNum=@num)
where goodsNum=@num;
update t_inventory
set price=price+(select cost from inserted where goodsNum=@num)-(select cost from t_inventory where goodsNum=@num)
where goodsNum=@num;
update t_goodsSale
set price=price+(select cost from inserted where goodsNum=@num)-(select cost from t_inventory where goodsNum=@num)
where goodsNum=@num;
fetch next from cur into @num;
end
close cur;
end

結果測試:
測試結果:
原來的庫存表t_inventory:
這裏寫圖片描述
原來的銷售表t_goodsSale:
這裏寫圖片描述

/*在採購員進貨,增加t_goodsBuy之後:*/
insert into t_goodsBuy
values('01','口香糖','03','10','150','盒','綠箭公司','2016/8/23'),
      ('02','方便麪','01','10','10','箱','康師傅公司','2016/8/23'),
      ('03','可樂','02','15','12','箱','可口可樂公司','2016/8/23'),
      ('04','餅乾','04','11','15','箱','樂事公司','2016/8/23');

t_inventory庫存表:
這裏寫圖片描述
t_goodsSale銷售表:
這裏寫圖片描述

執行成功的SQL源碼:

  1. 建表、創建索引

/*==============================================================*/
/* Table: t_buyer                                               */
/*==============================================================*/
create table t_buyer (
   workerNum            int                  not null,
   workerName           varchar(50)          not null,
)
go

alter table t_buyer
   add constraint PK_T_BUYER primary key nonclustered (workerNum)
go

/*==============================================================*/
/* Index: Index_workerNum                                       */
/*==============================================================*/
create index Index_workerNum on t_buyer (
workerNum ASC
)
go

/*==============================================================*/
/* Index: Index_workerName                                      */
/*==============================================================*/
create index Index_workerName on t_buyer (
workerName  ASC
)
go

/*==============================================================*/
/* Table: t_cashier                                             */
/*==============================================================*/
create table t_cashier (
   workerNum            int                  not null,
   workerName           varchar(50)          not null,
   workTime             varchar(5)           not null
)
go

alter table t_cashier
   add constraint PK_T_CASHIER primary key nonclustered (workerNum)
go

/*==============================================================*/
/* Index: Index_workerNum                                       */
/*==============================================================*/
create index Index_workerNum on t_cashier (
workerNum ASC
)
go

/*==============================================================*/
/* Index: Index_workerTime                                      */
/*==============================================================*/
create index Index_workerTime on t_cashier (
workTime ASC
)
go

/*==============================================================*/
/* Table: t_goodsBuy                                            */
/*==============================================================*/
drop  table t_goodsBuy 
create table t_goodsBuy (
   goodsNum             int                  not null,
   goodsName            varchar(50)          not null,
   workerNum            int                  not null,
   cost                 int                  not null,
   amount               int                  not null,
   units                varchar(50)          not null,
   suppler              varchar(50)          not null,
   date                 varchar(50)          not null
)
go

alter table t_goodsBuy
   add constraint PK_T_GOODSBUY primary key nonclustered ()
go

/*==============================================================*/
/* Index: Index_goodsName                                       */
/*==============================================================*/
create index Index_goodsName on t_goodsBuy (
goodsName ASC
)
go

/*==============================================================*/
/* Index: Index_date                                            */
/*==============================================================*/
create index Index_date on t_goodsBuy (
date ASC
)
go

/*==============================================================*/
/* Index: Index_wokerNum                                        */
/*==============================================================*/
create index Index_wokerNum on t_goodsBuy (
workerNum ASC
)
go



/*==============================================================*/
/* Table: t_goodsOn                                             */
/*==============================================================*/
create table t_goodsOn (
   goodsNum             int                  not null,
   goodsName            varchar(50)          not null,
   cost                 int                  not null,
   prcie                int                  not null,
   amount               int                  not null,
   units                varchar(50)          not null,
   remark               varchar(300)         null,
   workerNum            int                  not null
)
go

alter table t_goodsOn
   add constraint PK_T_GOODSON primary key nonclustered (goodsNum, workerNum)
go

/*==============================================================*/
/* Index: Index_amount                                          */
/*==============================================================*/
create index Index_amount on t_goodsOn (
amount ASC
)
go

/*==============================================================*/
/* Table: t_goodsOnOut                                          */
/*==============================================================*/
create table t_goodsOnOut (
   workerNum            int                  not null,
   goodsNum             int                  not null,
   goodsName            varchar(50)           not null,
   amount               int                  not null
)
go

alter table t_goodsOnOut
   add constraint PK_T_GOODSONOUT primary key nonclustered (workerNum, goodsNum, t_g_workerNum)
go

/*==============================================================*/
/* Index: Index_workerNum                                       */
/*==============================================================*/
create index Index_workerNum on t_goodsOnOut (
workerNum ASC
)
go

/*==============================================================*/
/* Index: Index_goodsNum                                        */
/*==============================================================*/
create index Index_goodsNum on t_goodsOnOut (
goodsNum ASC
)
go

/*==============================================================*/
/* Table: t_goodsSale                                           */
/*==============================================================*/
drop table t_goodsSale
create table t_goodsSale (
   workerNum            int                  not null,
   goodsNum             int                  not null,
   goodsName            varchar(50)          not null,
   prcie                int                  not null,
   amount               int                  not null,
   units                varchar(50)          not null
)
go

alter table t_goodsSale
   add constraint PK_T_GOODSSALE primary key nonclustered (goodsNum)
go

/*==============================================================*/
/* Index: Index_workerNum                                       */
/*==============================================================*/
create index Index_workerNum on t_goodsSale (
workerNum ASC
)
go

/*==============================================================*/
/* Index: Index_profit                                          */
/*==============================================================*/
--create index Index_profit on t_goodsSale (
--profit ASC
--)
--go

/*==============================================================*/
/* Index: Index_amount                                          */
/*==============================================================*/
create index Index_amount on t_goodsSale (
amount ASC
)
go

/*==============================================================*/
/* Table: t_goosClass                                           */
/*==============================================================*/
drop table t_goodsClass
create table t_goodsClass (
   classNum             int                  not null,
   className            varchar(50)          not null,
   goodsNum             int                  not null,
    goodsName            varchar(50)          not null,
   workerNum            int                  not null

)
go

alter table t_goodsClass
   add constraint PK_T_GOOSCLASS primary key nonclustered (goodsNum)
go

/*==============================================================*/
/* Index: Index_goodsName                                       */
/*==============================================================*/
create index Index_goodsName on t_goodsClass (
goodsName ASC
)
go

/*==============================================================*/
/* Index: Index_className                                       */
/*==============================================================*/
create index Index_className on t_goodsClass (
className ASC
)
go

/*==============================================================*/
/* Table: t_inventory                                           */
/*==============================================================*/
create table t_inventory (
   goodsNum             int                  not null,
   goodsName            varchar(50)          not null,
   className            varchar(10)             not null,
   amount               int                  not null,
   cost                 int                  not null,
   prcie                int                  not null,
   units                varchar(50)          not null,
   remark               varchar(300)         null,
)
go

alter table t_inventory
   add constraint PK_T_INVENTORY primary key nonclustered (goodsNum, workerNum)
go

/*==============================================================*/
/* Index: Index_className                                     */
/*==============================================================*/
create index Index_className on t_inventory (
className ASC
)
go

/*==============================================================*/
/* Index: Index_amount                                          */
/*==============================================================*/
create index Index_amount on t_inventory (
amount ASC
)
go

/*==============================================================*/
/* Table: t_manager                                             */
/*==============================================================*/
create table t_manager (
   workerNum            int                  not null,
   workerName           varchar(50)          not null
)
go

alter table t_manager
   add constraint PK_T_MANAGER primary key nonclustered (workerNum)
go

/*==============================================================*/
/* Index: Index_workerNum                                       */
/*==============================================================*/
create index Index_workerNum on t_manager (
workerNum ASC
)
go

/*==============================================================*/
/* Index: Index_workerName                                      */
/*==============================================================*/
create index Index_workerName on t_manager (
workerName ASC
)
go

/*==============================================================*/
/* Table: t_tallyClerk                                          */
/*==============================================================*/
create table t_tallyClerk (
   workerNum            int                  not null,
   workerName           varchar(50)          not null
)
go

alter table t_tallyClerk
   add constraint PK_T_TALLYCLERK primary key nonclustered (workerNum)
go

/*==============================================================*/
/* Index: Index_workerNum                                       */
/*==============================================================*/
create index Index_workerNum on t_tallyClerk (
workerNum ASC
)
go

/*==============================================================*/
/* Index: Index_workerName                                      */
/*==============================================================*/
create index Index_workerName on t_tallyClerk (
workerName  ASC
)
go

/*==============================================================*/
/* Table: t_user                                                */
/*==============================================================*/
drop table t_user
create table t_user (
   workerNum            int                  not null,
   workerName           varchar(50)          not null,
   workerSex            varchar(5) check(workerSex in('男','女'))  not null,
   workerAge            int check(workerAge>=18 and workerAge<=60) not null,
   workTime             varchar(5)           not null,
   job                  varchar(50) check(job in('採購員','收銀員','理貨員','經理')) not null,
   salary               int                  not null
)
go

alter table t_user
   add constraint PK_T_USER primary key nonclustered (workerNum)
go

/*==============================================================*/
/* Index: Index_workerNum                                       */
/*==============================================================*/
create index Index_workerNum on t_user (
workerNum ASC
)
go

/*==============================================================*/
/* Index: Index_workerName                                      */
/*==============================================================*/
create index Index_salary on t_user (
salary DESC
)
go
  1. 創建視圖

說明:視圖的作用包括能夠簡化數據的操作、提供數據庫的安全性等等,所以因爲需要適時查看上架商品數量、庫存數量,以及時補足,同時,查看銷售量靠前的商品,優化銷售方案等原因,設計了以下三個視圖。

/*==============================================================*/
/* View: View_goodsOnAmount 查詢上架商品數量視圖                */
/*==============================================================*/
create view lihuoyuan.View_goodsOnAmount as
select
goodsNum,
goodsName,
amount
from
t_goodsOn
with check option
go

/*==============================================================*/
/* View: View_goodsSale  查詢商品銷售數量視圖                   */
/*==============================================================*/
create view View_goodsSale as
select
goodsNum,
goodsName,
amount
from
t_goodsSale
go

/*==============================================================*/
/* View: View_inventoryAmount  查詢商品庫存量視圖               */
/*==============================================================*/
create view caigouyuan.View_inventoryAmount as
select
goodsNum,
goodsName,
amount
from
t_inventory
with check option
go


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