班费管理系统数据库的设计与实现

班费管理系统数据库的设计与实现

班费管理系统
一、项目功能
模块 基本功能
系统管理 1、管理员登录
2、学生用户登录
3、退出系统
用户管理 1、用户信息的显示
2、按用户名查询用户信息
3、添加管理员信息
4、修改密码
5、删除用户
院部管理 1、院部信息的显示
2、修改删除院部
3、添加新的院部信息
4、删除院部信息
班级管理 1、班级信息的显示
2、按院部查询班级信息
3、修改删除班级
4、添加新的班级信息
5、删除班级信息
学生管理 1、学生信息的显示
2、按学号、班级、院部查看学生信息
3、修改学生信息
4、添加新的学生信息
5、批量导入学生信息
6、删除学生信息
学生班费管理 1、显示所有学生班费信息
2、按学号,姓名,日期分别查询学生班费信息
3、学生班费信息导出
班费管理 1、显示所有班费信息
2、按日期、姓名、学号、班级等查询班费信息
3、添加班费信息
4、班费的管理:查看、添加、删除、修改
5、班费信息的修改
6、删除班费信息
实时监控 选择正在上交班费的班级和院系进行实时监控

(1)学生管理模块功能需求:
增加漏掉或者新来的的学生信息:学号,姓名,交纳班费金额,交纳日期等,以便记录所有的学生资料。
对学生信息不是很准确的同学进行信息修改,便于更准确的记录所有的学生信息。
对已经不在校或者不存在的学生的信息进行删除。
查询所有的学生信息,了解班级有哪些学生。
显示当前班费所有信息:班级,班费管理员学号,所收班费总额,班费总支出,班费当前余额。
管理员可以录入、修改、删除学生信息,也可以对学生信息进行查询。
(2)管理员管理模块功能需求:
管理员可以修改管理员用户和密码。
(3)缴纳管理模块功能需求:
记录所有交纳的班费以及个人交纳班费的情况,进一步的了解所有同学班费交纳情况。
管理员可以查询、录入、删除、修改缴纳信息。
(4)支出管理模块功能需求:
对所有的班费支出进行记录输入数据库,加以保存,以便以后能方便查询班费的开支。
管理员可以查询、录入、删除、修改支出信息。
(5)借用管理模块功能需求:
管理员可以查询、录入、删除、修改借用信息。
(6)报销管理模块功能需求:
管理员可以查询、录入、删除、修改报销信息。
班费管理系统的系统结构图

班费管理系统的系统结构图
二、数据库设计
1、概念结构设计
在这里插入图片描述
在这里插入图片描述
学生E-R图
在这里插入图片描述
用户E-R图
在这里插入图片描述
班级E-R图
在这里插入图片描述
班费E-R图
在这里插入图片描述
班费明细E-R图
在这里插入图片描述
报销E-R图
在这里插入图片描述
借用E-R图
在这里插入图片描述
局部E-R图
在这里插入图片描述
整体E-R图
在这里插入图片描述在这里插入图片描述
2、逻辑结构设计
学生表(学号,班级编号,姓名,性别,电话,邮箱)
用户表(用户号,用户名,密码,类型)
班级表(班级编号,班级名称,专业,学院)
班费表(班费编号,班级编号,上交日期,上交人数,上交数目,剩余数目)
班费明细表(明细号,班费编号,学号,姓名,上交数目)
报销表(报销号,班费编号,报销人,报销日期,报销数目)
借用表(借用号,班费编号,借用人,借用日期,借用数目,归还日期,归还数目,还清状态)
3、物理结构设计
(1)student(学生表)
列名 数据类型 长度 约束 说明
Studentno Char 11 主键 学号
Classno Char 20 班级编号
Sname Char 8 姓名
Brithday date 年龄
Sex enum 2 性别
Phone Varchar 12 电话
Email Varchar 20 邮箱

(2)user(用户表)
列名 数据类型 长度 约束 说明
Userno Char 20 主键 用户号
Username Char 8 用户名
Pwd Char 11 密码
Type Char 8 用户类型

(3)class(班级表)
列名 数据类型 长度 约束 说明
Classno Char 20 主键 班级编号
Classname Char 20 班级名称
Major Char 20 专业
Academy Char 20 学院

(4)classfee(班费表)
列名 数据类型 长度 约束 说明
Classfeeno Char 20 主键 班费编号
Classno Char 20 班级编号
Handday date 上交日期
Handrenshu Int 10 上交人数
Handsumamount Decimal(10,2) 10 上交总数目
surplusamount Decimal(10,2) 10 剩余数目

(5)lineitem(班费明细表)
列名 数据类型 长度 约束 说明
Detailno Char 20 主键 明细号
Classfeeno Char 20 班费编号
Studentno Char 11 学号
Sname Char 8 姓名
Handamount Decimal(10,2) 10 上交数目

(6)reimburse(报销表)
列名 数据类型 长度 约束 说明
Reimburseno Char 20 主键 报销号
Classfeeno Char 20 班费编号
rname Char 8 报销人
rtime date 报销日期
ramount Decimal(10,2) 10 报销数目

(7)loan(借用表)
列名 数据类型 长度 约束 说明
loanno Char 20 主键 借用号
Classfeeno Char 20 班费编号
lname Char 8 借用人
ltime date 借用日期
lamount Decimal(10,2) 10 借用数目
duedate date 归还日期
dueamount Decimal(10,2) 10 归还数目
duestatus char 10 还清状态
三、数据库的实现
1、数据库
数据库名 对象定义SQL语句 说明
bfglxt Create database bfglxt; 创建一个数据库,数据库名为bfglxt
2、表
表名 对象定义SQL语句 说明
student Create table if not exists student
(
Studentno char(11) not null comment ‘学号’,
Classno char(6) not null comment ‘班级编号’,
Sname char(8) not null comment ‘姓名’,
Birthday date not null comment ‘年龄’,
Sex enum(‘男’,‘女’) default ‘男’ comment ‘性别’,
Phone varchar(12) not null comment ‘电话’,
Email varchar(20) not null comment ‘邮箱’,
Primary key (studentno)
); 创建学生表,表名为student
user Create table if not exists user
(
Userno char(20) not null comment ‘用户号’,
Username char(8) not null comment ‘用户名’,
Pwd char(11) not null comment ‘密码’,
Type char(8) not null comment ‘用户类型’,
Primary key (Userno)
); 创建用户表,表名为user
class Create table if not exists class
(
Classno Char(20) not null comment ‘班级编号’,
Classname Char(20) not null comment ‘班级名称’,
Major Char(20) not null comment ‘专业’,
Academy Char(20) not null comment ‘学院’,
Primary key (Classno)
); 创建班级表,表名为class
classfee Create table if not exists classfee
(
Classfeeno Char(20) not null comment ‘班费编号’,
Classno Char(20) not null comment ‘班级编号’,
Handday date not null comment ‘上交日期’,
Handrenshu int(10) not null comment ‘上交人数’,
Handsumamount Decimal(10,2) not null comment ‘上交总数目’,
surplusamount Decimal(10,2) not null comment ‘剩余数目’,
Primary key (Classfeeno)
); 创建班费表,表名为classfee
lineitem Create table if not exists lineitem
(
Detailno Char(20) not null comment ‘明细号’,
Classfeeno Char(20) not null comment ‘班费编号’,
Studentno Char(11) not null comment ‘学号’,
Sname Char(8) not null comment ‘姓名’,
Handamount Decimal(10,2) not null comment ‘上交数目’,
Primary key (Detailno)
); 创建班费明细表,表名为lineitem
reimburse Create table if not exists reimburse
(
Reimburseno Char(20) not null comment ‘报销号’,
Classfeeno Char(20) not null comment ‘班费编号’,
Rname Char(8) not null comment ‘报销人’,
Rtime date not null comment ‘报销日期’,
ramount Decimal(10,2) not null comment ‘报销数目’,
Primary key (Reimburseno)
); 创建报销表,表名为reimburse
loan Create table if not exists loan
(
loanno Char(20) not null comment ‘借用号’,
classfeeno Char(20) not null comment ‘班费编号’,
lname Char(8) not null comment ‘借用人’,
ltime date not null comment ‘借用日期’,
lamount Decimal(10,2) not null comment ‘借用数目’,
duedate date not null comment ‘归还日期’,
dueamount Decimal(10,2) not null comment ‘归还数目’,
duestatus char(10) not null comment ‘还清状态’,
Primary key (loanno)
); 创建借用表,表名为loan
3、操作表中的数据
操作类型 数据操作SQL语句 说明
插入数据 student:
insert into student values(‘1714154114’,‘10002’,‘韩敏杰’,‘1997-12-31’,‘女’,‘11111111111’,‘[email protected]’);
insert into student values(‘1714154116’,‘10002’,‘余杰’,‘1998-05-28’,‘女’,‘22222222222’,‘[email protected]’);
insert into student values(‘1714154118’,‘10002’,‘赵莹莹’,‘1998-12-31’,‘女’,‘33333333333’,‘[email protected]’);
insert into student values(‘1714154129’,‘10002’,‘刘苗苗’,‘1999-12-31’,‘女’,‘44444444444’,‘[email protected]’);
insert into student values(‘1714154136’,‘10002’,‘王燃燃’,‘1999-12-31’,‘女’,‘55555555555’,‘[email protected]’);
user:
insert into user values(‘韩敏杰’,‘123456’,1);
insert into user values(‘余杰’,‘123456’,1);
insert into user values(‘赵莹莹’,‘123456’,1);
insert into user values(‘刘苗苗’,‘123456’,2);
insert into user values(‘王燃燃’,‘123456’,1);
class:
insert into class values(‘10001’,‘网络工程2017-1’,‘网络工程’,‘信息工程学院’);
insert into class values(‘10002’,‘网络工程软件开发2017-1’,‘网络工程软件开发’,‘信息工程学院’);
insert into class values(‘10003’,‘物联网工程2017-1’,‘物联网工程’,‘信息工程学院’);
insert into class values(‘20001’,‘商务英语2017-1’,‘商务英语’,‘外国语学院’);
insert into class values(‘20002’,‘商务英语2017-2’,‘商务英语’,‘外国语学院’);
insert into class values(‘30001’,‘物流管理2017-1’,‘物流管理’,‘工商管理学院’);
insert into class values(‘30002’,‘物流管理2017-2’,‘物流管理’,‘工商管理学院’);
classfee:
insert into classfee values(‘20191201001’, ‘10001’,‘2019-12-11’,‘1’,‘110.25’,‘100.25’);
insert into classfee values(‘20191201002’, ‘10002’,‘2019-12-10’,‘5’,‘250’,‘100’);
insert into classfee values(‘20191201003’, ‘10003’,‘2019-12-13’,‘15’,‘120.5’,‘12.05’);
insert into classfee values(‘20191201004’, ‘10001’,‘2019-12-15’,‘40’,‘100.0’,‘90’);
insert into classfee values(‘20191201005’, ‘10002’,‘2019-12-14’,‘41’,‘150.35’,‘130.5’);
lineitem:
insert into lineitem values(‘201911001’,‘20191201002’,‘1714154114’,‘韩敏杰’,‘5’);
insert into lineitem values(‘201911002’,‘20191201002’,‘1714154116’,‘余杰’,‘5’);
insert into lineitem values(‘201911003’,‘20191201002’,‘1714154118’,‘赵莹莹’,‘5’);
insert into lineitem values(‘201911004’,‘20191201002’,‘1714154129’,‘刘苗苗’,‘5’);
insert into lineitem values(‘201911005’,‘20191201002’,‘1714154136’,‘王燃燃’,‘5’);
reimburse:
insert into reimburse values(‘r-201912120001’,‘20191201002’,‘韩敏杰’,‘2019-12-12’.‘5.9’);
insert into reimburse values(‘r-201912120002’,‘20191201003’,‘韩敏杰’,‘2019-12-13’,‘6’);
insert into reimburse values(‘r-201912120003’,‘20191201004’,‘韩敏杰’,‘2019-12-14’,‘5.8’);
insert into reimburse values(‘r-201912120004’,‘20191201005’,‘韩敏杰’,‘2019-12-15’,‘7.8’);
insert into reimburse values(‘r-201912120005’,‘20191201006’,‘余杰’,‘2019-12-12’,‘9’);
insert into reimburse values(‘r-201912120006’,‘20191201007’,‘王燃燃’,‘2019-12-12’,‘8’);
loan:
insert into loan values(‘l-201912150001’,‘20191201002’,‘余杰’,‘2019-12-15’,‘5’,‘2019-12-31’,‘5.9’,‘YES’);
insert into loan values(‘l-201912150002’,‘20191201002’,‘韩敏杰’,‘2019-12-15’,‘6’,‘2019-12-31’,‘5’,‘NO’);
insert into loan values(‘l-201912150003’,‘20191201002’,‘余杰’,‘2019-12-15’,‘15’,‘2020-01-01’,‘0’,‘NO’);
insert into loan values(‘l-201912150004’,‘20191201002’,‘余杰’,‘2019-12-15’,‘12’,‘2019-12-31’,‘0’,‘NO’);
修改数据 student:
update student set phone=‘123456789114’ where studentno=‘1714154114’ && classno=‘10002’;
user:
update user set pwd=‘123114’ where username=‘韩敏杰’;
class:
update class set classname=‘网络工程(软件开发方向)’ where classno=‘10002’;
classfee:
update classfee set handrenshu=‘6’ where classfeeno=‘20191201002’ and classno=‘10002’;
lineitem:
update lineitem set handamount=‘6’ where detailno=‘201911001’ and classfeeno=‘20191201002’;
reimburse:
update reimburse set ramount=‘6’ where reimbuseno=‘r-201912120001’;
loan:
update loan set lamount=‘26’ where loanno=‘l-201912120001’;
删除数据 student:
delete from student where studentno=‘1714154114’ ;
user:
delete from user where username=‘韩敏杰’;
class:
delete from class where classno=‘10002’;
classfee:
delete from classfee where classfeeno=‘20191201002’ and classno=‘10002’;
lineitem:
delete from lineitem where detailno=‘201911001’ and classfeeno=‘20191201002’;
reimburse:
delete from reimburse where reimbuseno=‘r-201912120001’;
loan:
delete from loan where loanno=‘l-201912120001’;
查询数据 student:
select * from student where sname like “%杰”;
user:
select count() as ‘用户总数’ from user;
class:
select classname from student,class where studentno=‘1714154129’ and class.classno=student.classno;
classfee:
select classfeeno ,handsumamount,handday from classfee where handsumamount >20;
lineitem:
select lineitem.sname,student.sex,handamount from student,classfee,lineitem where student.studentno=lineitem.studentno and classfee.classfeeno=lineitem.classfeeno and detailno=‘201911001’;
reimburse:
select rname,reimburseno,ramount from reimburse where rtime=“2019-12-12” and ramount<8;
loan:
select loanno,lname,duedate from loan where duestatus=“YES” and duedate=“2019-12-31”; student:
查询student表中姓名(sname)最后1个字为杰的学生信息。
user:
统计用户总数。
class:
查询学号为“1714154129”的班级名称
classfee:
查询classfee上交数目高于20元的班费编号(classfeeno )、上交数目(handamount)和上交日期(handday)。
lineitem:
查询姓名,性别,上交日期和上交数目,当班费明细号为‘201911001’
reimburse:
查询报销日期2019.12.12且报销数目小于8元的姓名,报销号,报销数目
loan:
查询还清状态为YES并且归还日期为2019-12-31的借用号,借用人和归还日期
4、视图
视图名 对象定义SQL语句 说明
student_v1 create view student_v1
as
select studentno as ‘学号’ ,sname as ‘姓名’,birthday as ‘年龄’,sex as ‘性别’,phone as ‘电话’,email as ‘邮箱’
from student
where sex=‘女’
with check option; 创建视图student_v1,包含所有女学生的学号、姓名、年龄、性别、电话和邮箱,字段名用中文表示,同时要求对视图的修改也符合
上述条件。
classfee_v2 create view classfee_v2
as
select classfee.classfeeno,handday,handrenshu,handamount,class.classname
from classfee,class
where classfee.classno=class.classno; 创建视图classfee_v2,包含班费编号、班级名称、上交日期、上交人数和上交数目。
lineitem_v3 create view lineitem_v3
as
select student.sname,phone,handday,handamount
from student,classfee,lineitem
where student.studentno=lineitem.studentno and classfee.classfeeno=lineitem.classfeeno; 创建视图lineitem_v3,包含姓名、电话、上交日期和上交数目。
5、索引
索引名 对象定义SQL语句 说明
I_em_ind create index S_em_ind on student(email desc); 对student表中的email列按降序创建普通索引I_em_ind。
C_sp_ind create index C_sp_ind on account(sname,phone); 对student表中的sname和phone列创建复合索引C_sp_ind。
U_sa_ind create unique index U_sa_ind on product(name(2)); 对student表中的sname列前2个字创建唯一性索引U_sa_ind。
U_ut_ind alter table user drop primary key;(如果有主建先删除主键)
alter table user add primary key(userno),add unique index U_ut_ind(username); 对user表中的userno列创建主键索引,type列添加一个唯一索引U_ut_ind。
C_hh_ind alter table classfee drop primary key;(如果有主建先删除主键)
alter table classfee add primary key(classfeeno,classno),add index c_hh_ind(handrenshu,handsumamount); 对classfee表中的classfeeno和classno列创建主键索引,handrenshu和handsumamount列添加一个复合索引C_hh_ind。
U_sn_ind alter table student drop primary key;(如果有主建先删除主键)
alter table student add primary key(studentno),add unique u_sn_ind(sname); 对student表中的studentno列创建主键索引,sname列创建唯一索引U_sn_ind。
6、存储过程
存储过程名 对象定义SQL语句 说明
p_compare drop procedure p_compare;删除存储过程
delimiter //
create procedure p_compare(in a char(20),in b char(20),out result int)
begin
declare t1 Decimal(10,2);
declare t2 Decimal(10,2);
select handsumamount into t1 from classfee where classfeeno=a;
select handsumamount into t2 from classfee where classfeeno=b;
if
t1>t2 then
set result=0;
elseif
t1<t2 then
set result=1;
end if;
end //
delimiter ;
调用存储过程:
call p_compare(20191201001,20191201002,@result);
select @result; 创建存储过程p_compare,比较两个班费的上交数目,如前者比后者高就输出0,否则输出1.调用该存储过程比较 20191201001和20191201002两个班费的班费上交总数目,输出结果 。
P_handamount drop procedure P_handamount;删除存储过程
delimiter //
create procedure P_handsumamount(in classno char(8),in year int,out sumamount decimal(10,2))
begin
select sum(handsumamount) into sumamount from classfee
where classno=classno and year(handday)=year;
end //
delimiter ;
调用存储过程:
call p_handsumamount (‘10002’,‘2019’,@sumamount);
select @sumamount; 创建存储过程p_handamount,返回给定班级,给定班费的上交总金额。调用存储过程,返回班级编号为‘10002’,2019年的上交总金额。
p_bj drop procedure p_bj;删除存储过程
delimiter //
create procedure p_bj(in p_loanno char(20),out result char(8))
begin
select if(lamount=dueamount,‘YES’,‘No’) from loan where loanno=p_loanno;
end //
delimiter ;
调用存储过程:
call p_bj(‘l-201912150001’,@result);
call p_bj(‘l-201912150002’,@result);
call p_bj(‘l-201912150003’,@result); 创建一个存储过程p_bj,比较某同学的借用数目和归还数目,如果相同则返回YES,若不是则返回NO。调用该存储过程分别“l-201912150001”、“l-201912150002”和“l-201912150003”的数目,输出结果。
p_a() drop procedure proc_cursor;删除存储过程
delimiter //
create procedure proc_cursor()
begin
declare p_classno char(20) default ’ ';
declare p_classname char(20) default ’ ';
declare pro_cursor cursor
for select classno,classname from class;
declare continue handler for not found set @dovar=1;#定义处理程序
set @dovar=0;
open pro_cursor;
fetch_Loop:LOOP
fetch pro_cursor into p_classno,p_classname;
if @dovar=1 then
leave fetch_Loop;
else
select p_classno,p_classname;
end IF;
end LOOP fetch_Loop;
close pro_cursor;
select @dovar;
end //
delimiter ;
调用存储过程:
call proc_cursor(); 创建存储过程,利用循环语句控制fetch语句来检索并显示游标pro_cursor中可用的数据,其中游标pro_cursor指向的是从班级表中查询班级编号(classno)和班级名称(classname)的记录集。
7、存储函数
存储函数名 对象定义SQL语句 说明
F_NUM() drop function F_NUM;删除存储函数
delimiter //
create function F_NUM()
returns int
begin
return (select count(userno) from user);
end //
delimiter ;
调用存储函数:
select F_NUM(); 创建一个存储函数F_NUM(),返回用户总数,并调用该函数输出结果。
func_bj drop function func_bj;删除存储函数
方法一:
delimiter //
create function func_bj(f_loanno char(20))
returns char(8)
begin
declare num1 Decimal(10,2);
declare num2 Decimal(10,2);
declare result char(8);
select lamount into num1 from loan where loanno=f_loanno;
select dueamount into num2 from loan where loanno=f_loanno;
if num1!=num2 then set result=‘NO’;
else set result=‘YES’;
end if;
return result;
end //
delimiter ;
方法二:
delimiter //
create function func_bj(f_loanno char(30))
returns char(8)
begin
return (select if(lamount=dueamount,‘YES’,‘No’) from loan where loanno=f_loanno) ;
end //
delimiter ;
调用存储函数:
select func_bj(‘l-201912150001’);
select func_bj(‘l-201912150002’);
select func_bj(‘l-201912150003’); 创建一个存储函数,比较某同学的借用数目和归还数目,如果相同则返回YES,若不是则返回NO。调用该存储过程分别“l-201912150001”、“l-201912150002”和“l-201912150003”的数目,输出结果。
F_handsumamount drop function F_handsumamount;删除存储函数
delimiter //
create function F_handsumamount(classno char(8),year int)
returns decimal(10,2)
begin
declare sumamount decimal(10,2);
select sum(handsumamount) into sumamount from classfee
where classno=classno and year(handday)=year;
return sumamount;
end //
delimiter ;
调用存储函数:
select F_handsumamount(‘10002’,‘2019’); 创建存储函数F_handsumamount,返回给定班级,给定班费的上交总金额。调用存储过程,返回班级编号为‘10002’,2019年的上交总金额。
8、触发器
触发器名 对象定义SQL语句 说明
t_ll drop trigger t_ll;删除触发器
delimiter //
create trigger t_ll after insert
on loan for each row
begin
declare a Decimal(10,2);
declare b Decimal(10,2);
declare c Decimal(10,2);
select handsumamount,lamount,dueamount into a,b,c from loan,classfee where loan.classfeeno=new.classfeeno and loanno=new. loanno and loan.classfeeno=classfee.classfeeno;
update classfee set surplusamount=a-b+c
where classfee.classfeeno=new.classfeeno;
end //
delimiter ;
使用触发器语句:
select
from lineitem;
select* from classfee;
insert into loan values(‘l-201912150006’,‘20191201002’,‘余杰’,‘2019-12-15’,‘5’,‘2019-12-31’,‘5.9’,‘YES’);
insert into loan values(‘l-201912150008’,‘20191201002’,‘王燃燃’,‘2019-12-10’,‘8’,‘2019-12-31’,‘5’,‘NO’);
select* from lineitem;
select* from classfee; 创建触发器t_ll,实现当向loan表插入一行数据时,根据班费编号对classfee表的剩余数目进行修改,上交总数目减去借用数目加上归还数目。
t_l drop trigger t_l;删除触发器
方法一:
delimiter //
create trigger t_l after update
on lineitem for each row
begin
declare a decimal(10,2);
select Handamount into a from lineitem
where Detailno=old.Detailno;
update classfee set Handsumamount=ahandrenshu where classfeeno=old.classfeeno;
end //
delimiter ;
方法二:
delimiter //
create trigger t_l after update
on lineitem for each row
begin
update classfee set Handsumamount=new.Handamount
handrenshu
where classfeeno=old.classfeeno;
end //
delimiter ;
使用触发器语句:
select * from lineitem;
update lineitem set Handamount=10 where detailno=“201911001”;
update lineitem set Handamount=12 where detailno=“201911003”;
select * from lineitem; 创建一个触发器,当修改lineitem表中商品的上交数目(Handamount)时,则触发器将修改classfee表中对应商品的上交总数目(Handsumamount),令其上交总数目等于上交总人数乘以上交数目。
t_sl drop trigger t_sl;删除触发器
delimiter //
create trigger t_sl after delete on student for each row
begin
delete from lineitem where studentno=old.studentno;
end //
delimiter ;
使用触发器语句:
select* from student where studentno=‘1714154114’;
delete from student where studentno=‘1714154114’;
select* from student where studentno=‘1714154114’; 创建触发器t_sl,在student表中当删除学生信息时将lineitem表中与该客户有关的数据同时全部删除。
t_cl drop trigger t_cl;删除触发器
delimiter //
create trigger t_cl after delete on classfee for each row
begin
delete from lineitem where classfeeno=old.classfeeno;
end //
delimiter ;
使用触发器语句:
select* from lineitem;
delete from classfee where classfeeno=‘20191201001’;
select* from lineitem; 创建触发器 t_cl,在classfee表中当删除班费信息时将lineitem表中与该订单有关的数据同时全部删除。
9、事件
事件名 对象定义SQL语句 说明
dellclassfee_no drop event dellclassfee_no;删除事务
方法一:
delimiter //
create event dellclassfee_no on schedule every 1 year
starts now()
do
begin
declare o_cno int(11);
declare o_handday datetime;
declare exist boolean default true;
declare num cursor for
select classfeeno,handday from classfee;
declare continue handler for not found set exist=false;
open num;
fetch num into o_cno,o_handday;
while exist do
if year(now())-year(o_handday)>10 then
delete from classfee where classfeeno=o_cno;
delete from lineitem where classfeeno=o_cno;
end if;
fetch num into o_cno,o_handday;
end while;
close num;
end //
delimiter ;
方法二:
delimiter //
create event dellclassfee_no on schedule at now()
do
begin
delete classfee.,lineitem. from classfee,lineitem
where year(now())-year(handday)>10 and classfee.classfeeno=lineitem.classfeeno;
end //
delimiter ;
检查事务是否实现语句:
select * from classfee;
select * from lineitem; 创建事件,每年清理一次班费表和班费明细表,将班费日期超过10年的班费信息和班费明细信息删除。

10、事务(在存储过程中使用事务)
存储过程名 对象定义SQL语句 说明
P_sno_del drop procedure P_sno_del;删除存储过程
delimiter //
create procedure P_sno_del(in sno_no char(11))
begin
start transaction;
delete from student where studentno=sno_no;
delete from lineitem where studentno=sno_no;
commit;
end //
delimiter ;
验证语句:
select * from lineitem;
call P_sno_del(‘1714154114’);
select * from lineitem; 创建存储过程P_sno_del,在存储过程中使用事务,实现同时删除给定用户(studentno)在student表和lineitem表中的数据。提示:studentno作为该存储过程的输入参数。
P_lc_upd drop procedure P_lc_upd;删除存储过程
方法一:
delimiter //
create procedure P_lc_upd(in l_loanno char(20),in l_classfeeno char(20),in l_lname char(8),in l_ltime date,in l_lamount Decimal(10,2),in l_duedate date,in l_dueamount Decimal(10,2),in l_dueststus char(10))
Begin
declare a int;
start transaction;
insert into loan values
(l_loanno,l_classfeeno,l_lname,l_ltime,l_lamount,
l_duedate,l_dueamount,l_dueststus);
select count() into a from classfee where classfee.classfeeno=l_classfeeno;
commit;
else
rollback;
end if;
end //
delimiter ;
方法二:
delimiter //
create procedure P_lc_upd(in a char(20),b char(20),c char(8),d date,e Decimal(10,2),f date,g Decimal(10,2),h char(10))
Begin
start transaction;
insert into loan values(a,b,c,d,e,f,g,h);
update classfee set surplusamount=handsumamount-e+g where classfee.classfeeno=b;
commit;
end //
delimiter ;
验证语句:
select
from loan;
select* from classfee;
call P_lc_upd(‘l-201912150014’,‘20191201002’,‘赵莹莹’,‘2019-12-20’,‘10’,‘2019-12-22’,‘0’,‘NO’);
select* from loan;
select* from classfee;

创建存储过程P_lc_upd,在存储过程中使用事务,实现当向loan表中插入一行数据时,根据班费编号对classfee表的剩余数目进行修改,上交总数目减去借用数目加上归还数目。提示:该存储过程有8个输入参数,分别为loan表新增记录的列值。
11、数据库用户及权限分配
用户名 定义用户SQL语句 权限分配SQL语句 说明
Up00001 create user ‘Up00001’@‘localhost’ identified by ‘123456’; grant select on bfglxt.* to ‘Up00001’@‘localhost’;
grant update(phone,email) on bfglxt.student to ‘Up00001’@‘localhost’;
grant update(pwd) on bfglxt.user to ‘Up00001’@‘localhost’; 创建用户Up00001,并授予用户Up00001对bfglxt库中所有表有select操作权限和对bfglxt库中student表的电话列和邮箱列有UPDATE权限,和对user表中密码的修改权限
Ub00001 create user ‘Ub00001’@‘localhost’ identified by ‘123456’; grant all on bfglxt.student,user,
lineitem to ‘Ub00001’@‘localhost’; 创建用户Ub00001,并授予用户Ub00001对bfglxt库中student,user,lineitem表所有权限
Uy00001 create user ‘Uy00001’@‘localhost’ identified by ‘123456’; grant all on bfglxt.student,user,classfee,
lineitem,reimburse,loan to ‘Uy00001’@‘localhost’ with grant option;
grant create routine on bfglxt.* to ‘Ub00001’@‘localhost’ with grant option; 创建用户Uy00001,并授予用户Uy00001对bfglxt库中除了class表的所有权限,并允许其将该权限授予其他用户。

扫描二维码关注公众号,回复: 12627172 查看本文章

授予用户Uy00001为bfglxt数据库创建存储过程和存储函数权限,并允许其将该权限授予其他用户。
Ux00001 create user ‘Ux00001’@‘localhost’ identified by ‘123456’; grant all on . to ‘Ux00001’@‘localhost’ with grant option; 创建用户Ux00001,密码为123456,并授予用户Ux00001对所有库都 有所有操作权限,并允许其将该权限授予其他用户。
12、备份与恢复
操作类型 对应操作的SQL语句 说明
备份 mysqldump -u root -p bfglxt>D:/bfglxt_backup.sql
mysqldump -u root -p bfglxt student>D:/bfglxt_student.sql 使用mysqldump命令备份数据库bfglxt中的所有表,将其备份到D:\,备份文件名为bfglxt_backup.sql。
使用mysqldump命令备份数据库bfglxt中的student表,将其备份到D:\,备份文件名为bfglxt_student.sql。
恢复 mysql -u root -p bfglxt<D:\bfglxt_backup.sql
drop database bfglxt;
create database bfglxt;
use bfglxt;
source D:/bfglxt_backup.sql;
delete from students;source D:/bfglxt_student.sql; 使用mysql命令将备份文件bfglxt_backup.sql恢复到数据库bfglxt中。
使用source命令将备份文件bfglxt_backup.sql恢复到数据库bfglxt中。
删除student表的数据,用source命令恢复。

猜你喜欢

转载自blog.csdn.net/weixin_46098270/article/details/114264518