SQLServer高级查询与T

您所在的位置:网站首页 怎样创建一个仓库库存表 SQLServer高级查询与T

SQLServer高级查询与T

2024-07-17 01:31:09| 来源: 网络整理| 查看: 265

SQLServer高级查询与T-SQL编程实现仓储库存管理系统

SQLServer高级查询与T-SQL编程 SQLServer高级查询与T-SQL编程实现仓储库存管理系统1、语言和环境2、功能需求3、使用DDL创建数据库StorageDB4. 使用DDL分别创建库存物品表(ProductsInfo)及进出库记录表(Recode)结构及约束5. 使用DML向数据表中插入如下数据6. 创建视图用于查询进出库详情,查询字段为货品ID、货品名称、进出库数量、进出库类型、进出库日期5. 编写存储过程用于进出库记录,使用事务。

1、语言和环境

1.实现语言:T—SQL 2.环境要求:SQLServer2012及以上版本。

2、功能需求

某公司需要开发一款仓储库存管理系统,现有数据库概念模型及数据字典

概念模型: 在这里插入图片描述 物理模型: 在这里插入图片描述

数据字典:

属性名说明备注ProductID货品ID主键,自增ProductName货品名称约束:非空Quantity库存数量约束:大于0RecodeID记录编号主键,自增Count进出库数量RecodeType进出库类型0:入库 1:出库RecodeDate进出库日期 3、使用DDL创建数据库StorageDB --1. 使用DDL创建数据库StorageDB create database StorageDB --打开库 use StorageDB 4. 使用DDL分别创建库存物品表(ProductsInfo)及进出库记录表(Recode)结构及约束 --2. 使用DDL分别创建库存物品表(ProductsInfo)及进出库记录表(Recode)结构及约束 create table ProductsInfo( ProductID int identity(1,1) primary key not null, ProductName varchar(50) not null, Quantity int not null ) create table Recode( RecodeID int identity(1,1) primary key not null, ProductID int references ProductsInfo(ProductID),--注意外键关系 Count int not null, RecodeType smallint not null, RecodeDate date not null ) 5. 使用DML向数据表中插入如下数据 --3. 使用DML向数据表中插入如下数据 insert into ProductsInfo values('Dell 电脑',100) insert into ProductsInfo values('雷朋键盘',150) insert into ProductsInfo values('iPhone 手机',200) select * from ProductsInfo insert into Recode values(1,300,0,'2019-08-22') insert into Recode values(1,150,1,'2019-08-25') insert into Recode values(1,50,1,'2019-09-22') insert into Recode values(2,100,0,'2019-08-22') insert into Recode values(2,50,0,'2019-08-25') insert into Recode values(3,300,0,'2019-08-22') insert into Recode values(3,100,1,'2019-08-25') select * from Recode

也可以使用批量插入,两个效果是一样的。

insert into ProductsInfo select 'Dell 电脑' ,100 union select '雷朋键盘' ,150 union select 'iPhone 手机',200 insert into Recode select 1,300,0,'2019-8-22' union select 1,150,1,'2019-8-25' union select 1,50,1,'2019-8-22' union select 2,100,0,'2019-8-22' union select 2,50,0,'2019-8-25' union select 3,300,0,'2019-8-22'union select 3,100,1,'2019-8-25' go 6. 创建视图用于查询进出库详情,查询字段为货品ID、货品名称、进出库数量、进出库类型、进出库日期

视图中的进出库类型类型需要显示【入库/出库】

-- 4. 创建视图用于查询进出库详情,查询字段为货品ID、货品名称、进出库数量、进出库类型、进出库日期 --判断视图是否存在,存在则删除 if exists(select * from sysobjects where name='v_Details') drop view v_Details go create view v_Details as select p.ProductID '货品ID',ProductName '货品名称',Count '进出库数量' ,(case r.RecodeType when 0 then '入库' when 1 then '出库' else 'nill' end )'进出库类型',RecodeDate '进出库日期' from ProductsInfo p join Recode r on p.ProductID=r.ProductID go

这里使用了case when判断是入库还是出库

5. 编写存储过程用于进出库记录,使用事务。

进出库记录存储过程业务流程为:修改库存数->生成进出库记录,库存数小于0无法完成进出库过程

--5. 编写存储过程用于进出库记录并编写5条语句及进行测试,要求使用事务。 if object_id('proc_chuku') is not null drop proc proc_chuku go create proc proc_chuku(@ProductName varchar(20),@churu varchar(20),@Quantity int) as if exists(select * from ProductsInfo where ProductName=@ProductName) begin declare @ProductID int;--货品id declare @Quantity2 int;--库存 declare @sumError int=0;--定义统计错误信息 select @ProductID=ProductID,@Quantity2=Quantity from ProductsInfo where ProductName=@ProductName begin tran if @Quantity2-@Quantity


【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭