------------------------------------------------创建bankDB数据库------------------------------------
use master
go
if(exists(select name from sysdatabases where name='bankDB'))--判断是否存在bankDB数据库,如果存在则删除
begin
drop database bankDB
end
GO
create database bankDB
on primary
(
name='bankdb_data',
filename='D:\bank\bankdb_data.mdf',
size=5MB,
filegrowth=15%
)
LOG ON
(
name='bankdb_log',
filename='D:\bank\bankdb_log.ldf',
size=5MB,
filegrowth=15%
)
GO
use bankDB
GO
-----------------------------------------------用户信息表---------------------------------------------
if(exists(select * from sysobjects where name='userinfo'))
begin
drop table userinfo
end
GO
create table userinfo
(
customerID int identity(1,1) ,--顾客编号
customerName varchar(50) not null, --顾客姓名
PID varchar(50) not null, --身份证号
telephone varchar(50) not null, --电话
address varchar(50) --居住地址
)
GO
-----------------------------------------------银行卡信息表-------------------------------------------
if(exists(select * from sysobjects where name='cardinfo'))
begin
drop table cardrinfo
end
GO
create table cardinfo
(
cardID varchar(23) constraint pk_cardID primary key not null ,
curType char(4) NOT NULL, --货币类型
savingType varchar(50) not null, --存款类型 活期/定期/定活两便
openDate datetime not null, --开户日期
openMoney money not null, --开户金额
balance money not null, ---余额
pass char(6) not null, --密码
IsReportLoss varchar(10), --挂失
customerID int not null --顾客编号
)
GO
-----------------------------------------------交易信息表-----------------------------------------------
if(exists(select * from sysobjects where name='transinfo'))
begin
drop table transinfo
end
GO
create table transinfo
(
transDate datetime not null, --交易日期
cardID varchar(23) not null, --卡号
transType varchar(20) not null,--交易类型 /存取/支入
transMoney money not null, --交易金额
remark varchar(50) --备注
)
GO
---------------------------------------------对用户信息表添加约束------------------------------------------
alter table userinfo --添加多个约束时,只需要用一个add
add constraint pk_customerID primary key(customerID), -----------------------------customerID设为主键
constraint ck_PID check(len(PID)=18 or len(PID)=15),--------为PID添加check约束 要求其证号为18位或15位
constraint ck_telephone check(telephone --------------------------对电话号码添加check约束
like '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' or telephone like
'[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9]')
GO
--------------------------------------------对银行卡信息表添加约束------------------------------------------
alter table cardinfo
add constraint ck_cardID check(len(cardid)=23), ------------------------对银行卡号添加check约束
constraint ck_savingtype check(savingtype='活期' or savingtype='定期' or savingtype='定活两便'),
constraint DF_curType default('RMB')for curType, -------------对货币类型添加默认约束,默认为人名币
constraint DF_openDate default(getdate()) for openDate,-----------对开户日期添加默认约束,默认为当前日期
constraint ck_openMoney check(openMoney>=1), -----对开户金额添加check约束,要求开户金额要大于1
constraint ck_balance check(balance>=0), ---------- 对余额添加check约束,要求余额必须大于1
constraint DF_pass default ('888888') for pass, -------------------------------密码默认为 888888
constraint DF_IsReportLoss default '否' for IsReportLoss, ---------------------------------挂失默认为否
constraint fk_customerID foreign key(customerID) references userinfo(customerID)
--------和userinfo表的customerID 设置主、外键关系
GO
---------------------------------------------对交易信息表添加约束------------------------------------------
-----------------------------------------------------------------------------------------------------------
alter table transinfo
add constraint DF_transDate default (getdate()) for transDate, -------对交易日期添加默认约束,默认为当前日期
constraint fk_cardID foreign key(cardID) references cardinfo(cardID),---和cardinfo表cardID设置主外键关系
constraint ck_transMoney check(transMoney>0) ----对交易金额设置check约束,交易金额必须大于0
GO
---------------------------------------------------创建开户的存储过程--------------------------------------
if(exists(select * from sysobjects where name='proc_kaihu'))
begin
drop proc proc_kaihu
end
GO
create proc proc_kaihu
@customerName varchar(50),
@pid varchar(23),
@telephone varchar(50),
@address varchar(50) ,
@openmoney money ,
@inputpass char(6)='888888',
@savingType varchar(20)
as
print'---------------------------------------------------------------------------------'
print'**************************** 欢迎使用中国建设银行!******************************'
print' 您现在使用的是开户业务! '
print'---------------------------------------------------------------------------------'
declare @errorsum int,@cardID varchar(23),@customerID int,@opentime datetime
declare @tempStr varchar(20)
begin transaction--开始事物
set @errorsum=0
--------/* 插入用户信息*/
insert into userinfo(customerName,PID,telephone,address)values(@customerName,@pid,@telephone,@address)
set @errorsum=@errorsum+@@error
--print @errorsum
select @tempStr=rand(datepart(ms,getdate())*100000)--产生随机数
set @cardID='6227'+' '+'0026'+' '+'7023'+' '+substring(@tempStr,3,4)+' '+substring(@tempStr,3,3)
set @errorsum=@errorsum+@@error
----/*获得卡号等信息*/
set @customerID=(select customerID from userinfo where PID=@pid)--获得顾客编号
set @errorsum=@errorsum+@@error
--print @errorsum
insert into cardinfo(cardID,curType, savingType,openDate,openMoney,balance,pass,IsReportLoss,customerID)
values(@cardID,'RMB',@savingType,getdate(),@openmoney,@openmoney,@inputpass,'否',@customerID)
set @errorsum=@errorsum+@@error
--print @errorsum
set @opentime=(select openDate from cardinfo where cardID=@cardID)
set @errorsum=@errorsum+@@error
--print @errorsum
insert into transinfo values(@opentime,@cardID,'存入',@openmoney,'中国建设银行')
set @errorsum=@errorsum+@@error
--print @errorsum
if(@errorsum=0)
begin
print'开户成功!'
print'开户信息如下:'
print'开户日期为:'+convert(varchar(50),@opentime)
print'开户名为:'+@customerName
print'您的身份证号为:'+@pid
print'您的联系电话为:'+@telephone
print'您的卡号为:'+@cardID
print'卡上余额为:'+convert(varchar(50),@openmoney)
select*from transinfo where cardID=@cardID
commit transaction
end
else
begin
print '开户失败!'
rollback transaction
end
GO
------------------------------------------------取款-----------------------------------------------
/* 创建取款的存储过程 */
---------------------------------------------------------------------------------------------------
if(exists(select * from sysobjects where name='proc_qukuan'))
begin
drop proc proc_qukuan
end
GO
create proc proc_qukuan
@cardid varchar(23),
@pass char(6),
@outmoney money
as
print'---------------------------------------------------------------------------------'
print'**************************** 欢迎使用中国建设银行!******************************'
print' 您现在使用的是取款业务! '
print'---------------------------------------------------------------------------------'
declare @errorsum1 int,@balance money,@pass1 char(6)
begin transaction
set @errorsum1=0
if (exists(select * from cardinfo where cardID=@cardid ))
begin
if((select IsReportLoss from cardinfo where cardID=@cardid
没有合适的资源?快使用搜索试试~ 我知道了~
ATM-SQL.zip_atm
共1个文件
sql:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 153 浏览量
2022-09-19
18:19:08
上传
评论
收藏 5KB ZIP 举报
温馨提示
用数据写的ATM 从注册到转账。适合初学者学习使用
资源推荐
资源详情
资源评论
收起资源包目录
ATM-SQL.zip (1个子文件)
ATM-SQL.sql 24KB
共 1 条
- 1
资源评论
朱moyimi
- 粉丝: 64
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功