1. 程式人生 > >Mysql模擬銀行資料庫系統

Mysql模擬銀行資料庫系統

E-R圖
在這裡插入圖片描述
在這裡插入圖片描述
#建立銀行存取款系統資料庫 如果存在先刪除在建立
DROP DATABASE IF EXISTS bankDB;
CREATE DATABASE IF NOT EXISTS bankDB;

#建立普通使用者bankMaster並授權
GRANT ALL ON bankDB TO [email protected] IDENTIFIED BY ‘1234’;

#建立銀行客戶表
DROP TABLE IF EXISTS userInfo;
CREATE TABLE IF NOT EXISTS userInfo(
customerID INT(8) PRIMARY KEY AUTO_INCREMENT COMMENT’客戶編號’,
customerName VARCHAR(10) NOT NULL COMMENT’開戶姓名’,
PID CHAR(18) NOT NULL COMMENT’省份證編號’,
telephone CHAR(11) NOT NULL COMMENT ‘手機號碼’,
address VARCHAR(50) COMMENT’地址’
)ENGINE=INNODB CHARSET=utf8 COMMENT=‘客戶表’;

#建立銀行卡資訊表
DROP TABLE IF EXISTS cardInfo;
CREATE TABLE IF NOT EXISTS cardInfo(
cardID VARCHAR(19) NOT NULL PRIMARY KEY COMMENT’銀行卡卡號’,
curID VARCHAR(10) NOT NULL DEFAULT’RMB’ COMMENT’幣種’,
savingID INT(2) NOT NULL COMMENT’存款型別 ‘,
openDate TIMESTAMP NOT NULL COMMENT’開卡日期’,
openmoney DECIMAL NOT NULL COMMENT ‘開卡金額’,
balance DECIMAL NOT NULL COMMENT ‘賬戶餘額’,
password

VARCHAR(6) NOT NULL DEFAULT’888888’ COMMENT’密碼’,
IsReportLoss BIT NOT NULL DEFAULT 0 COMMENT ‘是否掛失’,
customerID INT(8) NOT NULL COMMENT’客戶編號’
#CONSTRAINT FK_customerID FOREIGN KEY(customerID) references userInfo(customerID)
)ENGINE=INNODB CHARSET=utf8 COMMENT=‘銀行卡資訊表’;
ALTER TABLE cardinfo MODIFY openmoney DECIMAL(9,2);
ALTER TABLE cardinfo MODIFY balance DECIMAL(9,2);
#建立外來鍵
ALTER TABLE cardInfo
ADD CONSTRAINT FK_customerID FOREIGN KEY(customerID) REFERENCES userINfo(customerID);
ALTER TABLE cardInfo
ADD CONSTRAINT FK_savingID FOREIGN KEY(savingID) REFERENCES deposit(savingID);
#刪除外來鍵
#alter table cardInfo drop foreign key FK_savingID;

#交易資訊表
DROP TABLE IF EXISTS tradeInfo;
CREATE TABLE IF NOT EXISTS tradeInfo(
transDate TIMESTAMP NOT NULL COMMENT’交易日期’,
cardID CHAR(19) NOT NULL COMMENT’卡號’,
transType CHAR(10) NOT NULL COMMENT’交易型別’,
transMonry DECIMAL NOT NULL COMMENT’交易金額’,
remark TEXT COMMENT’交易備註’,
CONSTRAINT FK_cardID FOREIGN KEY(cardID) REFERENCES cardInfo(cardID)
)ENGINE=INNODB CHARSET=utf8 COMMENT=‘交易資訊表’;

#存款型別表
DROP TABLE IF EXISTS deposit;
CREATE TABLE IF NOT EXISTS deposit(
savingID INT(2) PRIMARY KEY COMMENT’存款型別 ‘,
savingName VARCHAR(20) NOT NULL COMMENT’存款型別名稱’,
curID VARCHAR(10) NOT NULL DEFAULT’RMB’ COMMENT’存款幣種’
)ENGINE=INNODB CHARSET=utf8 COMMENT=‘存款型別表’;

#銀行客戶表資料
INSERT INTO userInfo(customerID,customerName,PID,telephone,address)
VALUES(1,‘丁六’,‘567891321242345618’,‘13645667783’,‘北京西城區’),
(2,‘王五’,‘56789132124234567X’,‘13642345112’,‘河北石家莊市’),
(3,‘張三’,‘567891321242345789’,‘13567893246’,‘北京海定區’),
(4,‘丁一’,‘123451321242345618’,‘13056434411’,‘河南新鄉’),
(5,‘李四’,‘678911321242345618’,‘13318877954’,‘山東濟南市’);

#存款型別表資料
INSERT INTO deposit(savingID,savingName)
VALUES(1,“定期一年”),(2,“定期二年”),(3,‘活期’);

#銀行卡資訊資料
INSERT INTO cardInfo(cardID,savingID,openDate,openmoney,balance,password,IsReportLoss,customerID)
VALUES(‘1010357612121004’,1,NOW(),1,1001,‘888888’,0,1),
(‘1010357612121130’,1,NOW(),1,1,‘888888’,0,2),
(‘1010357612125678’,3,NOW(),1000,6100,‘123456’,0,3),
(‘1010357612128284’,3,NOW(),1000,1001,‘888888’,0,4),
(‘1010357612123333’,1,NOW(),1,1501,‘123123’,1,5);
UPDATE cardINfo SET cardID=‘1010357612121134’ WHERE cardID=‘1010357612123333’;
#交易資訊表資料
INSERT INTO tradeInfo(transDate,cardID,transType,transMonry,remark)
VALUES(NOW(),‘1010357612125678’,‘支取’,900,NOW()),
(NOW(),‘1010357612121130’,‘存入’,300,NOW()),
(NOW(),‘1010357612121004’,‘存入’,1000,NOW()),
(NOW(),‘1010357612121130’,‘存入’,1900,NOW()),
(NOW(),‘1010357612121134’,‘存入’,5000,NOW()),
(NOW(),‘1010357612121134’,‘存入’,500,NOW()),
(NOW(),‘1010357612121134’,‘支取’,2000,NOW()),
(NOW(),‘1010357612125678’,‘存入’,2000,NOW()),
(NOW(),‘1010357612121134’,‘支取’,2000,NOW()),
(NOW(),‘1010357612125678’,‘存入’,2000,NOW()),
(NOW(),‘1010357612125678’,‘存入’,2000,NOW());
#update tradeinfo set transType=‘支取’ where cardID='1010357612121134
#delete from tradeinfo;

#模擬常規業務
#(1)修改客戶密碼:張三、李四
UPDATE cardInfo SET password=123456 WHERE cardID=‘1010357612125678’;
UPDATE cardInfo SET password=123123 WHERE cardID=‘1010357612121134’;
#(2)辦理銀行卡掛失 :李四
UPDATE cardInfo SET IsReportLoss=1 WHERE cardID=‘1010357612121134’;
SELECT c.cardID ‘卡號’,c.curID ‘貨幣’, d.savingName ‘儲存種類’, c.openmoney ‘開戶金額’, c.balance‘餘額’,
c.password ‘密碼’,c.IsReportLoss‘是否掛失’,u.customerName‘客戶姓名’
FROM cardInfo c
INNER JOIN userInfo u
ON c.customerID=u.customerID
INNER JOIN deposit d
ON c.savingID=d.savingID;
#統計銀行總存入金額和總支出金額
SELECT transType 資金流向,SUM(transMonry) 總金額 FROM tradeinfo
WHERE transType='支取’OR transType=‘存入’
GROUP BY transType;

#查詢本週開戶資訊
SELECT c.cardID ‘卡號’, u.customerName ‘姓名’,c.curID ‘貨幣’,d.savingName ‘存款型別’,
c.openDate ‘開戶日期’, c.openmoney ‘開好戶金額’, c.balance ‘存款餘額’, c.IsReportLoss‘賬戶狀態’
FROM userinfo u
INNER JOIN cardinfo c
ON u.customerID=c.customerID
INNER JOIN deposit d
ON c.savingID=d.savingID
WHERE WEEK(c.openDate)=WEEK(NOW());

#查詢本月交易金額最高的卡號:存款、取款
SELECT cardID FROM tradeinfo WHERE transMonry=(
SELECT MAX(transMonry) FROM tradeInfo WHERE transType=‘支出’ OR transType=‘存入’
AND MONTH(transDate)= MONTH(NOW())
);

#查詢掛失客戶
SELECT u.customerName ‘客戶姓名’,u.telephone ‘聯絡電話’,c.cardID ‘卡號’,c.IsReportLoss ‘1是/2否掛失’
FROM userinfo u
INNER JOIN cardinfo c
ON c.customerID=u.customerID
WHERE c.IsReportLoss= b’1’;

#催款提醒業務
SELECT u.customerName ‘客戶姓名’,u.telephone ‘聯絡電話’,c.balance ‘存款餘額’
FROM userinfo u
INNER JOIN cardinfo c
ON u.customerID=c.customerID
WHERE c.balance<200;

#建立使用者檢視
DROP VIEW IF EXISTS view_userInfo;
CREATE VIEW view_userInfo
AS
SELECT customerID ‘客戶編號’, customerName ‘開戶名’,PID ‘身份證號’,telephone ‘電話號碼’,address ‘居住地址’
FROM userinfo u;
#建立銀行卡檢視
DROP VIEW IF EXISTS view_cardinfo;
CREATE VIEW view_cardinfo
AS
SELECT cardID ‘卡號’,customerName ‘客戶名’,c.savingID ‘貨幣種類’,savingName ‘存款型別’,openDate ‘開戶日期’,
balance’餘額’, password‘密碼’,IsReportLoss’是否掛失’
FROM userinfo u
INNER JOIN cardinfo c
ON u.customerID=c.customerID
INNER JOIN deposit d
ON c.savingID=d.savingID;
#建立交易記錄檢視
DROP VIEW IF EXISTS view_transInfo;
CREATE VIEW view_transInfo
AS
SELECT transDate ‘交易日期’,transType ‘交易型別’,cardID ‘卡號’, transMonry ‘交易金額’, remark ‘備註’
FROM tradeinfo;
#使用事物完成轉賬
#模擬從‘1010357612121134’賬戶中轉賬出300元給卡號為’1010357612125678‘的賬戶 李四轉給丁一300
#先關閉自動提交
SET autocommit=0;
#開啟事物
START TRANSACTION;
UPDATE cardinfo SET balance=balance-300 WHERE cardID=‘1010357612121134’;
UPDATE cardinfo SET balance=balance+300 WHERE cardID=‘1010357612125678’;
#提交事物 永久儲存
COMMENT;
#遇到問題回滾資料回到初始狀態
ROLLBACK;
#還原自動提交
SET autocommit=1;