银行ATM存取款机系统MySQL数据库
建表
1 CREATE TABLE cardInfo 2 ( 3 cardid CHAR(16)not NULL PRIMARY key, 4 `password` CHAR(6)not NULL , 5 curID INT(4)not NULL, 6 savingID INT(11)not NULL, 7 openDate TIMESTAMP not NULL, 8 openMoney DECIMAL(20) not NULL, 9 balance DECIMAL(20) not NULL, 10 IsReportLoss bit not NULL, 11 customerID INT(4)not NULL 12 ) 13 ALTER TABLE cardinfo ADD CONSTRAINT Fk_cuid FOREIGN KEY(customerID) REFERENCES userinfo(customerID); 14 ALTER TABLE cardinfo ADD CONSTRAINT FK_saving FOREIGN KEY(savingID) REFERENCES deposit(savingID); 15 16 17 #drop table if exists cardinfo; 18 19 20 CREATE TABLE userInfo 21 ( 22 customerID INT(4) PRIMARY KEY not NULL, 23 customerName CHAR(8) not NULL, 24 PID CHAR(18) not NULL, 25 telephone CHAR(20) not NULL, 26 address VARCHAR(50) 27 ) 28 29 30 CREATE TABLE tradeInfo 31 ( 32 cardID CHAR(16) not NULL, 33 tradeDate TIMESTAMP not NULL, 34 tradeMoney DECIMAL(20)not NULL, 35 tradeType char(4)not NULL, 36 remark text 37 ) 38 ALTER TABLE tradeinfo ADD CONSTRAINT FK_cid FOREIGN KEY(cardid) REFERENCES cardinfo(cardid); 39 40 CREATE TABLE deposit 41 ( 42 savingID INT(4) NOT NULL PRIMARY KEY, 43 savingName VARCHAR(20) NOT NULL, 44 descrip VARCHAR(50) 45 )
测试数据:
1 #存款类型 2 INSERT INTO deposit (savingName,descrip) VALUES ('活期','按存款日结算利息'); 3 INSERT INTO deposit (savingName,descrip) VALUES ('定期一年','存款期是1年'); 4 INSERT INTO deposit (savingName,descrip) VALUES ('定期二年','存款期是2年'); 5 INSERT INTO deposit (savingName,descrip) VALUES ('定期三年','存款期是3年'); 6 INSERT INTO deposit (savingName) VALUES ('定活两便'); 7 INSERT INTO deposit (savingName) VALUES ('通知'); 8 INSERT INTO deposit (savingName,descrip) VALUES ('零存整取一年','存款期是1年'); 9 INSERT INTO deposit (savingName,descrip) VALUES ('零存整取二年','存款期是2年'); 10 INSERT INTO deposit (savingName,descrip) VALUES ('零存整取三年','存款期是3年'); 11 INSERT INTO deposit (savingName,descrip) VALUES ('存本取息五年','按月支取利息'); 12 SELECT * FROM deposit; 13 14 INSERT INTO userInfo(customerName,PID,telephone,address ) 15 VALUES('张三','123456789012345','010-67898978','北京海淀'); 16 INSERT INTO cardInfo(cardID,savingID,openMoney,balance,customerID) 17 VALUES('1010357612345678',1,1000,1000,1); 18 19 INSERT INTO userInfo(customerName,PID,telephone) 20 VALUES('李四','321245678912345678','0478-44443333'); 21 INSERT INTO cardInfo(cardID,savingID,openMoney,balance,customerID) 22 VALUES('1010357612121134',2,1,1,2); 23 24 INSERT INTO userInfo(customerName,PID,telephone) 25 VALUES('王五','567891234532124670','010-44443333'); 26 INSERT INTO cardInfo(cardID,savingID,openMoney,balance,customerID) 27 VALUES('1010357612121130',2,1,1,3); 28 29 INSERT INTO userInfo(customerName,PID,telephone) 30 VALUES('丁六','567891321242345618','0752-43345543'); 31 INSERT INTO cardInfo(cardID,savingID,openMoney,balance,customerID) 32 VALUES('1010357612121004',2,1,1,4); 33 34 SELECT * FROM userInfo; 35 SELECT * FROM cardInfo; 36 37 38 /* 39 张三的卡号(1010 3576 1234 5678)取款900元,李四的卡号(1010 3576 1212 1134)存款5000元,要求保存交易记录,以便客户查询和银行业务统计。 40 说明:当存钱或取钱(如300元)时候,会往交易信息表(tradeInfo)中添加一条交易记录, 41 同时应更新银行卡信息表(cardInfo)中的现有余额(如增加或减少300元) 42 43 */ 44 SELECT * FROM cardInfo; 45 SELECT * FROM tradeInfo; 46 /*--------------交易信息表插入交易记录--------------------------*/ 47 INSERT INTO tradeInfo(tradeType,cardID,tradeMoney) 48 VALUES('支取','1010357612345678',900); 49 /*-------------更新银行卡信息表中的现有余额-------------------*/ 50 UPDATE cardInfo SET balance=balance-900 WHERE cardID='1010357612345678'; 51 52 53 /*--------------交易信息表插入交易记录--------------------------*/ 54 INSERT INTO tradeInfo(tradeType,cardID,tradeMoney) 55 VALUES('存入','1010357612121134',5000); 56 /*-------------更新银行卡信息表中的现有余额-------------------*/ 57 UPDATE cardInfo SET balance=balance+5000 WHERE cardID='1010357612121134'; 58 59 60 /*--------检查测试数据是否正确---------*/ 61 SELECT * FROM cardInfo; 62 SELECT * FROM tradeInfo;
实现功能:
1 SELECT * from userinfo; 2 SELECT * from cardinfo; 3 SELECT * from deposit; 4 SELECT * from tradeinfo; 5 6 #修改张三的密码为:123456 李四的密码为123123 7 UPDATE cardinfo SET `PASSWORD`=123456 WHERE customerID=1; 8 UPDATE cardinfo SET `PASSWORD`=123123 WHERE customerID=4; 9 10 #办理银行卡挂失 11 SELECT cardid 卡号,curID 货币,savingName 储蓄种类, openDate 开户日期, openMoney 开户金额,balance 余额,`password` 密码,IF(IsReportLoss=0,'未挂失','已挂失') 是否挂失,customerName 客户姓名 12 FROM cardinfo,userinfo,deposit WHERE cardinfo.customerID=userinfo.customerID and cardinfo.savingID=deposit.savingID;/*若全为未挂失则自己修改几条*/ 13 14 #统计银行总存入和总支出 15 SELECT tradetype 资金流向,SUM(trademoney) 总金额 FROM tradeinfo GROUP BY tradetype; 16 17 #查询本周开户信息 18 SELECT cardid 卡号,customerName 客户姓名 ,curID 货币,savingName 储蓄种类, openMoney 开户金额,balance 余额,IF(IsReportLoss=0,'未挂失','已挂失') 账户状态 19 FROM cardinfo,userinfo,deposit WHERE cardinfo.customerID=userinfo.customerID and cardinfo.savingID=deposit.savingID AND WEEK(NOW())=WEEK(opendate); 20 21 #查询本月交易金额最高的卡号 22 SELECT cardID from tradeinfo WHERE tradeMoney IN(SELECT MAX(tradeMoney) from tradeinfo WHERE MONTH(tradeDate)=MONTH(NOW())) 23 24 #查询挂失客户 25 SELECT customerName 客户姓名,telephone 联系电话 from userinfo WHERE customerID in 26 (SELECT customerID from cardinfo WHERE IsReportLoss=1); 27 28 #查询客户余额<200则提醒缴费 29 SELECT customerName 客户名称,telephone 联系电话,balance 存款余额 FROM userinfo,cardinfo WHERE userinfo.customerID=cardinfo.customerID AND balance<200; 30 31 #银行卡视图 32 DROP VIEW IF EXISTS vw_cardinfo; 33 CREATE VIEW vw_cardInfo 34 AS 35 (SELECT cardid 卡号,customerName 客户姓名, curID 货币,savingName 储蓄种类, openDate 开户日期, openMoney 开户金额,balance 余额,`password` 密码,IF(IsReportLoss=0,'未挂失','已挂失') 是否挂失 36 FROM cardinfo,userinfo,deposit WHERE cardinfo.customerID=userinfo.customerID and cardinfo.savingID=deposit.savingID); 37 38 #银行卡交易记录视图 39 CREATE VIEW vw_transInfo 40 AS 41 (SELECT tradeDate 交易日期,tradeType 交易类型,cardinfo.cardid 卡号,tradeMoney 交易金额,remark 备注 FROM tradeinfo,cardinfo WHERE tradeinfo.cardID=cardinfo.cardid);