mysql的ATM存取款机系统

##建库
CREATE DATABASE bankDB;
##客户信息表
CREATE TABLE userInfo
(
customerID INT PRIMARY KEY AUTO_INCREMENT NOT NULL,
customerName CHAR NOT NULL,
PID CHAR(18) NOT NULL,
telephone CHAR(11) NOT NULL,
address VARCHAR(128)
)


##创建银行信息表
CREATE TABLE cardInfo
(
cardID CHAR PRIMARY KEY NOT NULL,            
curID VARCHAR(19) NOT NULL,
savingID INT NOT NULL,
openDate TIMESTAMP NOT NULL,
openMoney DECIMAL NOT NULL,
balance DECIMAL NOT NULL, 
`password` VARCHAR(6) NOT NULL,
IsReportLoss BIT NOT NULL,
customerID INT NOT NULL
)
##交易信息表结构 (tradeInfo)

CREATE TABLE tradeInfo
(
transDate TIMESTAMP,
cardID    CHAR NOT NULL,
transType CHAR NOT NULL,
transMoney DECIMAL NOT NULL,
remark    TEXT
)
##存款类型表结构(deposit)

CREATE TABLE deposit
(
savingID INT PRIMARY KEY AUTO_INCREMENT NOT NULL,
savingName VARCHAR(32) NOT NULL,
descrip    VARCHAR(32)
)

##建立userInfo的约束
##身份证,唯一约束
ALTER TABLE userInfo 
ADD CONSTRAINT id_PID CHECK (len(PID)=18 OR len(PID)=15),
ADD CONSTRAINT UQ_PID UNIQUE(PID);


##`cardinfo`的约束
##,必填,默认为RMB,
##savingID,外键.openDate,默认为系统当前日期 这个不用建 因为 TIMESTAMP已经是当前日期

##openMoney,不低于1元
##balance,不低于1元
##password,6位数字,开户时默认为6个“8”
##IsReportLoss,是/否值,默认为“否”
##customerID,外键
ALTER TABLE cardinfo ALTER COLUMN curID SET DEFAULT'RMB'; 

ALTER TABLE cardinfo
ADD CONSTRAINT ck_openMoney CHECK(openMoney>=1)

ALTER TABLE cardinfo
ADD CONSTRAINT ck_balance CHECK(balance>=1)

ALTER TABLE cardinfo ALTER COLUMN `password` SET DEFAULT'888888';
##否为0,是为1
ALTER TABLE cardinfo ALTER COLUMN IsReportLoss SET DEFAULT '0';

ALTER TABLE cardinfo
ADD CONSTRAINT fk_deposit_cardinfo_savingID FOREIGN KEY(savingID) REFERENCES `deposit`(`savingID`)

ALTER TABLE cardinfo
ADD CONSTRAINT FK_customerID FOREIGN KEY(customerID) REFERENCES userInfo(customerID)


##交易信息表结构 (tradeInfo)的约束
##默认为系统当前日期 不用建 因为类型已经是系统当前的日期了
##cardID外键,必填
ALTER TABLE tradeInfo ADD CONSTRAINT fk_cardinfo_tradeInfo_cardID FOREIGN KEY (cardID) REFERENCES cardinfo(cardID);

##添加数据
INSERT INTO userinfo(customerName,PID,telephone,address)
VALUES('张四龙','123456789012345','01067898978','北京海淀'),
('张艺龙','321245678912345678','0478-44443333','山东一条街'),
('张三龙','123456789032145','2222-63598978','河南'),
('张二龙','56789123453212461','010-44442222','河北')



INSERT INTO `deposit`(`savingName`,`descrip`)
VALUES('活期','钱生钱!!'),
('死期','钱死钱!!'),
('活期','钱生钱!!'),
('死期','钱生死钱!!')


INSERT INTO `cardinfo`(`cardID`,`savingID`,`openMoney`,`balance`,`customerID`)
VALUES('7418529631012345',1,5,200,2),
('8529631234785623',2,4,100,1),
('7415987563214756',3,3,50,3),
('2599774315533222',4,2,30,4)



INSERT INTO`tradeinfo`(`transDate`,`cardID`,`transType`,`transMoney`,`remark`)
VALUES('2008-6-15 11:14:52','7418529631012345','支取',1000,''),
('2017-6-15 11:14:52','8529631234785623','支取',1000,'很好'),
('2012-6-15 11:14:52','7415987563214756','存入',450,'非常好'),
('2010-6-15 11:14:52','2599774315533222','存入',780,'vary好')

#模拟常规业务
#1.修改
UPDATE `cardinfo` SET `password` = '123456' WHERE `cardID` = '2599774315533222';
UPDATE `cardinfo` SET `password` = '123123' WHERE `cardID` = '7415987563214756';
##2.办理银行卡挂失
UPDATE `cardinfo` SET `IsReportLoss` = 1 WHERE `cardID` = '7415987563214756';
##3.统计银行总存入的金额和总支取金额
SELECT `tradeinfo`.`transType`AS 资金流向, SUM(`transMoney`) FROM tradeInfo
GROUP BY `tradeinfo`.`transType`

##4.查看本周开户信息
SELECT `cardID`AS 卡号,userInfo.`customerName`AS 姓名,curID AS 货币,`savingName` AS 存款类型,openDate AS 开户日期,openMoney AS 开户金额,balance AS 存款余额,IsReportLoss AS 账户状态
FROM cardinfo,userInfo,`deposit`
WHERE WEEK(NOW())
AND IsReportLoss = 1
GROUP BY cardID
##5.查询本月交易金额最高的卡号
SELECT DISTINCT cardid FROM `tradeinfo`
WHERE `transMoney` =(
SELECT MAX(transMoney) FROM tradeinfo
WHERE MONTH(NOW())
)
##6.查询挂失的客户
SELECT `customerName` AS 客户姓名, telephone AS 联系电话
FROM userinfo
WHERE `customerID` = (
SELECT `customerID` FROM `cardinfo`
WHERE `IsReportLoss` = 1
)

## 7.催款提醒
SELECT `customerName` AS 客户姓名, `telephone` AS 联系电话,balance AS 存款余额
FROM userinfo
INNER JOIN cardinfo ON `userinfo`.`customerID` = cardinfo.`customerID`
WHERE `balance`<200
##月末也就是30天后
AND DAY(`openDate`)>=30

######创建、使用客户友好信息视图
CREATE VIEW view_userInfo
AS
SELECT * FROM userInfo
SELECT * FROM view_userInfo

CREATE VIEW view_cardinfo
AS 
SELECT `cardID`AS 卡号,`curID` AS 货币种类,`savingID`AS 存款类型,`openDate`AS 开户日期,`openMoney`AS 开户金额,`balance`AS 余额,`password`AS 密码,`IsReportLoss`AS 是否挂失,`customerID`AS 客户编号
FROM `cardinfo`
SELECT * FROM view_cardinfo

CREATE VIEW view_tradeinfo
AS 
SELECT `transDate`AS 交易日期,`cardID`AS 卡号,`transType`AS 交易类型,`transMoney`AS 交易金额,`remark`AS 备注
FROM `tradeinfo`
SELECT * FROM view_tradeinfo


######使用事务模拟完成存款或取款业务

DELIMITER $$
CREATE PROCEDURE  usp_add_and_getMoney()
BEGIN
##设置全局错误总数默认为0
   DECLARE t_error INTEGER DEFAULT 0;
   ##设置如果有sqlexception就将全局错误总数为1
   DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=1;
   ##关闭事务默认提交
   SET autocommit=0;
   ##开始事务
   START TRANSACTION;
    ##存款
    UPDATE cardInfo SET balance=balance+5000 WHERE cardID='7418529631012345';
    ##1.取款
    UPDATE cardInfo SET balance=balance-50 WHERE cardID='8529631234785623';
    IF t_error!=0 THEN
    ##中途发生错误,则回滚事物
    ROLLBACK;
    ELSE 
    ##提交事物
    COMMIT;
    ##开启提交事物
    END IF; 
    SET autocommit=1;
END $$
DELIMITER ;
## 查看事务
CALL usp_add_and_getMoney();
SELECT * FROM cardInfo

 

posted @ 2018-06-15 18:45  胜过这首歌  阅读(5999)  评论(0编辑  收藏  举报
AmazingCounters.com