数据库期末作业之银行ATM存取款机系统
--一、建库、建表、建约束
--1、使用SQL创建表
--客户信息表userinfo
--字段名称 说明 备注
--customerID 顾客编号 自动编号(标识列),从1开始,主键
--用序列sequence实现,用其属性:nextval
--customerName 开户名 必填
--PID 身份证号 必填,智能是18位或15位,唯一约束 check约束len()函数
--telephone 联系电话 必填,11位手机号 check约束,’[0-9]’
--address 居住地址
create table userinfo
(
customerID int identity(1,1),
customerName char(10),
PID char(18) ,
telephone char(11),
address char(30)
)
--银行卡信息表cardinfo
--字段名称 说明
--cardID 卡号 必填,主键,
--银行的卡号规则和电话号码一样,一般前8位代表特殊含义,如某综合某支行等,
--假定该行要求其营业厅的卡号格式为10103576**** ***开始,
--每4位号码后有空格,卡号一般是随机产生。
--curType 货币种类 必填,默认为RMB
--savingTate 存款类型 活期/定活两便/定期
--openDate 开户日期 必填,默认为系统当前日期
--openMoney 开户金额 必填,不低于1元
--balance 余额 必填,不低于1元,否则将销户
--pass 密码 必填,6位数字,开户时默认为6个“6”
--IsReportloss 是否挂失 必填,是/否值,默认为“否”
--customerID 顾客编号 外键,必填,表示该卡对应的顾客编号,一位顾客允许办理多张卡号
create table cardinfo
(
cardID char(19) not null,
curType char(10),
savingTate char(10) ,
openDate datetime,
openMoney money,
balance money,
pass char(6),
IsReportloss char(2) ,
customerID int
)
--交易信息表transinfo
--字段名称 说明
--transDate 交易日期 必填,默认为系统当前日期
--cardID 卡号 必填,外键
--transType 交易类型 必填,只能是存入/支取
--transMoney 交易金额 必填,大于0
--remark 备注 可选,其他说明
create table transinfo
(
transDate datetime,
cardID char(19),
transType char(4),
transMoney money,
remark varchar(100)
)
--2、使用SQL语言在每个表上添加约束
--主键约束、外键约束、CHECK约束、默认约束、非空约束
--①
--客户信息表userinfo
--customerID 顾客编号 自动编号(标识列),从1开始,主键
alter table userinfo
add constraint PK_userinfor primary key(customerID)
--customerName 开户名 必填
alter table userinfo
add constraint CK_cn check(customerName is not null)
--PID 身份证号 必填,智能是18位或15位,唯一约束 check约束len()函数
alter table userinfo
add constraint CK_PID check(len(PID)=18 or len(PID)=15 )
alter table userinfo
add constraint CK_pn check(PID is not null)
alter table userinfo
add constraint UK_pid unique(pid)
--telephone 联系电话 必填,11位手机号 check约束,’[0-9]’
alter table userinfo
add constraint CK_tele check(telephone like '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]')
alter table userinfo
add constraint CK_tn check(telephone is not null)
--②
--银行卡信息表cardinfo
--cardID 卡号 必填,主键,
--卡号格式为10103576**** ***开始
alter table cardinfo
ADD constraint CK_Nca check(cardID is not null)
alter table cardinfo
ADD constraint PK_ca primary key(cardID),
constraint CK_car check(cardID like '1010 3576 [0-9][0-9][0-9][0-9] [0-9][0-9][0-9][0-9]')
--curType 货币种类 必填,默认为RMB
alter table cardinfo
add constraint DF_ca default 'RMB' for curType,
constraint CK_Ncur check(curType is not null)
--savingTate 存款类型 活期/定活两便/定期
alter table cardinfo
add constraint CK_st check(savingTate in ('活期','定活两便','定期'))
--openDate 开户日期 必填,默认为系统当前日期
alter table cardinfo
add constraint DF_openDate default getdate() for openDate,
constraint cK_nod check(openDate is not null)
--openMoney 开户金额 必填,不低于1元
alter table cardinfo
add constraint CK_nopenM check(openMoney is not null),
constraint CK_openM check(openMoney>=1)
--balance 余额 必填,不低于1元,否则将销户
alter table cardinfo
add constraint CK_nbalance check(balance is not null),
constraint CK_balance check(balance>=1)
--pass 密码 必填,6位数字,开户时默认为6个“6”
alter table cardinfo
add constraint CK_npass check(pass is not null),
constraint DF_PASS default '666666' for pass,
constraint CK_PASS check(pass like '[0-9][0-9][0-9][0-9][0-9][0-9]')
--IsReportloss 是否挂失 必填,是/否值,默认为“否”
alter table cardinfo
add constraint CK_nIsReportloss check(IsReportloss is not null),
constraint DF_IsReportloss default '否' for IsReportloss,
constraint CK_IsReportloss check(IsReportloss in('是','否'))
--customerID 顾客编号 外键,必填,表示该卡对应的顾客编号,一位顾客允许办理多张卡号
alter table cardinfo
add constraint FK_customerID foreign key(customerID) references userinfo(customerID),
constraint CK_ncustomerID check(customerID is not null)
--③
--交易信息表transinfo
--transDate 交易日期 必填,默认为系统当前日期
--cardID 卡号 必填,外键
--transType 交易类型 必填,只能是存入/支取
--transMoney 交易金额 必填,大于0
--remark 备注 可选,其他说明
alter table transinfo
add constraint CK_Nrd check(transDate is not null),
constraint DF_td default getdate() for transDate,
constraint FK_cardID foreign key (cardID) references cardinfo(cardID),
constraint CK_NcardID check(cardID is not null),
constraint CK_NtransType check(transType is not null),
constraint DF_transType CHECK(transType in('存入','支取')),
constraint CK_NtransMoney check(transMoney is not null),
constraint DF_transMoney CHECK(transMoney>0)
--二、插入测试数据
--使用SQL语言向每个表中插入至少3条记录
--1.向userinfo表中插入3条记录
insert into userinfo(customerName,PID,telephone,address) values
('李白','370404066601021111','15000000000','山东济南'),
('李大白','370404066601022222','15000000111','山东济宁'),
('李小白','370404066601023333','15000000222','山东泰安')
--2.向cardinfo表中插入3条记录
insert into cardinfo(cardID,savingTate,openMoney,balance,pass,customerID) values
('1010 3576 0000 0000','活期',10,2,'000000',1),
('1010 3576 0000 0001','活期',100,20,'000001',2),
('1010 3576 0000 0010','活期',1000,200,'000010',3)
--3.向transinfo表中插入3条记录
insert into transinfo(cardID,transType,transMoney) values
('1010 3576 0000 0000','存入',100),
('1010 3576 0000 0001','存入',100),
('1010 3576 0000 0010','支取',100)
--三、模拟常规业务
--1)修改客户密码
--修改卡号为‘1010 3576 0000 0000’的密码为‘000011’
update cardinfo set pass='000011' where cardID='1010 3576 0000 0000'
--2)办理银行卡挂失
--挂失卡号为‘1010 3576 0000 0000’的卡
update cardinfo set IsReportloss='是' where cardID='1010 3576 0000 0000'
--3)统计银行资金流通余额和盈利结算
--银行资金流通余额=总存入金额-总支取金额
--通过调用存储过程计算流通余额
create proc pro_sum
as
begin
declare @v_sum money,@v_inmoney money,@v_omoney money
select @v_inmoney=(select SUM(transMoney) from transinfo where transType='存入')
select @v_omoney=(select SUM(transMoney) from transinfo where transType='支取')
select @v_sum=@v_inmoney-@v_omoney
select '流通余额'=@v_sum
end
go
exec pro_sum
--盈利结算=总支取金额 * 0.008 – 总存入金额 * 0.003
--通过调用存储过程计算盈利结算
create proc profit
as
begin
declare @v_pro money,@v_inmoney money,@v_omoney money
select @v_inmoney=(select SUM(transMoney) from transinfo where transType='存入')
select @v_omoney=(select SUM(transMoney) from transinfo where transType='支取')
select @v_pro=@v_omoney*0.008 -@v_inmoney * 0.003
select '盈利结算'=@v_pro
end
go
exec profit
--4)查询本周开户的卡号,显示该卡相关信息
select * from cardinfo where (DATEDIFF(DAY,GETDATE(),openDate)<DATEPART(WEEKDAY,openDate))
--5)查询本月交易金额最高的卡号
select distinct cardID from transinfo where transMoney=(select MAX(transMoney) from transinfo)
--6)查询挂失账号的客户信息
select * from userinfo where customerID in(select customerID from cardinfo where IsReportloss='是')
--四、利用视图实现数据查询
--1)为客户提供以下3个视图供其查询该客户数据
--2)提供友好界面,要求各列名称为中文描述
--3)调用创建的视图获得查询结果
--客户基本信息:vw_userInfo
create view vw_userInfo
as select customerID 客户号,customerName 客户名,PID 身份证号,telephone 电话号码,address 地址 from userinfo
select *from vw_userInfo
--银行卡信息:vw_cardInfo
create view vw_cardInfo
as select cardid 银行卡号,curType 货币类型,savingTate 存款类型,openDate 开户日期,openMoney 开户金额,
balance 余额,pass 密码,IsReportloss 是否挂失,customerID 客户号 from cardinfo
select *from vw_cardInfo
--银行卡交易信息:vw_transInfo
create view vw_transInfo
as select transDate 交易日期,cardID 银行卡号,transType 交易类型,transMoney 交易金额,remark 备注 from transinfo
select *from vw_transInfo
--五、用存储过程实现业务处理
--1)完成开户业务
--创建存储过程通过随机数产生卡号
create proc randomcardid
@cardid char(19)output
as
select @cardid='1010 3576 '+substring(convert(char(10),RAND()*10000),0,5)+' '+substring(convert(char(10),RAND()*10000),0,5)
go
--创建开户存储过程
create proc openaccount
@curtype char(10),
@savingTate char(10) ,
@openMoney money,
@customerID int
as
begin
begin tran
if(@openMoney<1)
begin
print'开户金额小于一元!'
rollback tran
end
else
begin
declare @cardid char(19)
exec randomcardid @cardid output
insert into cardinfo values(@cardid,@curtype,@savingTate,default,@openMoney,@openMoney,default,DEFAULT,@customerID )
print'开户成功!'
select *from cardinfo where cardID=@cardid
commit tran
end
end
go
--test:开户金额>=1
exec openaccount 'DOLLAR','活期',2,'1'
--test:开户金额<1
exec openaccount 'DOLLAR','活期',0.1,'1'
--开户金额小于一元!
--2)完成取款或存款业务
alter proc sav_get
@v_cardnumber char(19),
@v_transtype char(4),
@v_money money,
@v_pass char(6),
@remark char(100)
as
begin tran
if exists(select *from cardinfo where cardID=@v_cardnumber)
begin
if(@v_pass!=(select pass from cardinfo where cardID=@v_cardnumber))
begin
raiserror('密码输入错误!',16,1)
rollback tran
end
if(@v_transtype='存入')
begin
update cardinfo set balance=balance+@v_money where cardID=@v_cardnumber
print'存入成功!'
end
else if(@v_transtype='支取' )
begin
update cardinfo set balance=balance-@v_money where cardID=@v_cardnumber
if((select balance from cardinfo where cardID=@v_cardnumber)<0)
begin
print'余额不足!'
rollback tran
end
end
insert into transinfo values(GETDATE(),@v_cardnumber,@v_transtype,@v_money,@remark)
end
commit tran
go
select *from cardinfo
exec sav_get'1010 3576 0000 0000','存入',10,'000011',null
--3)根据卡号打印对账单
create procedure show
@cardnumber char(19)
as
begin
if exists(select*from transinfo where cardID=@cardnumber)
select *from transinfo where cardID=@cardnumber
else
print'卡号不存在!'
end
go
exec show '1010 3576 0000 0000'
--4)查询、统计指定时间段内没有发生交易的账户信息
create proc noexchange
@starttime datetime,
@endtime datetime
as
begin
if (@starttime<@endtime)
select *from cardinfo
where cardID not in(select cardID from transinfo where transDate>=@starttime and transDate<=@endtime)
else
print'开始时间应早于结束时间!'
end
go
exec noexchange'2018-12-18','2018-12-19'
数据库期末作业之银行ATM存取款机系统的更多相关文章
- MySQL银行ATM存取款机系统(需求分析)
银行ATM需求文档 一.E-R图形文 二.开发步骤 1.明确需求--数据库设计--编码实现功能--测试 2.绘制e-r图--绘制数据库模型图--使用三大方式规范数据库结构 三.开发思路 1. 模型图综 ...
- 银行ATM存取款系统(C语言实现)
这里使用的运行工具是DEV C++.老铁们一定要看仔细了.是DEV C++ 仅供借鉴:这个是大一时期写的.大四的时候整理了一下(本人C语言学的也不太好).肯定很多不足和存在漏洞的地方.仅供借鉴.仅供借 ...
- mysql的ATM存取款机系统
##建库 CREATE DATABASE bankDB; ##客户信息表 CREATE TABLE userInfo ( customerID INT PRIMARY KEY AUTO_INCREME ...
- 语言模拟ATM自动取款机系统
C语言实验报告 题目名称:C语言模拟ATM自动取款机系统 C语言模拟实现ATM自动取款机功能:输入密码,余额查询,取款,存款,转账,修改密码,退出功能: 代码实现的功能: 账号及密码输入: ...
- C#实现ATM自动取款机
本篇用C#实现ATM自动取款机的一些功能.面临的第一个问题是:如何把与自动取款机相关的有形的.无形的方面抽象出来.大致如下: (1)关于用户帐号的类:Account(2)关于银行数据库的类:BankD ...
- 实验01——java模拟银行ATM系统
用java写的一个模拟银行系统,比较初级. ATM.java package cn.tedu.yinhang; import java.util.Scanner; /** * @author 赵瑞鑫 ...
- 数据库大作业--由python+flask
这个是项目一来是数据库大作业,另一方面也算是再对falsk和python熟悉下,好久不用会忘很快. 界面相比上一个项目好看很多,不过因为时间紧加上只有我一个人写,所以有很多地方逻辑写的比较繁琐,如果是 ...
- 分享:计算机图形学期末作业!!利用WebGL的第三方库three.js写一个简单的网页版“我的世界小游戏”
这几天一直在忙着期末考试,所以一直没有更新我的博客,今天刚把我的期末作业完成了,心情澎湃,所以晚上不管怎么样,我也要写一篇博客纪念一下我上课都没有听,还是通过强大的度娘完成了我的作业的经历.(当然作业 ...
- 银行ATM机工作流程模拟编程
[编程内容] 编程,模拟一个ATM(Automatic Teller Machine,自动取款机)的工作流程.依据帐户信息:姓名.帐号.密码.余额,完成ATM机功能:登录.显示余额.取款.修改密码. ...
随机推荐
- POJ 2778 DNA Sequence(AC自动机 + 矩阵快速幂)题解
题意:给出m个模式串,要求你构造长度为n(n <= 2000000000)的主串,主串不包含模式串,问这样的主串有几个 思路:因为要不包含模式串,显然又是ac自动机.因为n很大,所以用dp不太好 ...
- AirPods Max 出厂激活是怎么回事
AirPods Max 出厂激活是怎么回事 话说出厂激活是怎么检测出来的 refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问! 原创文 ...
- npm publish & 403 Forbidden
npm publish & 403 Forbidden 403 Forbidden - PUT https://registry.npmjs.org/ https://www.npmjs.co ...
- 使用 js 实现一个简易版的 async 库
使用 js 实现一个简易版的 async 库 具有挑战性的前端面试题 series & parallel 串行,并行 refs https://www.infoq.cn/article/0NU ...
- 如何使用 js 检测页面上全局变量
如何使用 js 检测页面上全局变量 js 检测页面全局变量脚本 <!DOCTYPE html> <html lang="zh-Hans"> <head ...
- qt DateTime 计算时间
qdatetime doc 获取当前时间 QDateTime t1 = QDateTime::currentDateTime(); qDebug() << t1.toString(&quo ...
- 「NGK每日快讯」2021.2.4日NGK公链第93期官方快讯!
- 一款基于 Web 的通用数据管理工具(转载)
一款基于 WEB 的通用数据管控工具 - CloudQuery 前言 前段时间,公司因为业务发展,数据量攀升,老板迫切需要一个工具对数据进行精细化管理,一是确实需要精细化管理:二是因为我们公司小,数据 ...
- EF多个表映射
public class Media // One entity table { public int Id { get; set; } public string Name { get; set; ...
- javascript中的模块系统
目录 简介 CommonJS和Nodejs AMD异步模块加载 CMD ES modules和现代浏览器 在HTML中使用module和要注意的问题 简介 在很久以前,js只是简单的作为浏览器的交互操 ...