YOU'VE MADE A BRAVE DECISION, WELCOME.

每一个不曾起舞的日子都是对生命的辜负。

学习笔记:MySQL-2

三、事务与存储过程

事务的概念,会开启、提交和回滚事务
事务的四种隔离级别
创建存储过程
调用、查看、修改和删除存储过程


1.事务管理

– 开启事务
start transaction;
– 提交事务
commit;
– 取消事务(回滚)
rollback;
–创建表account,插入数据
create table account(
id int primary key auto_increment,
name varchar(40),
money float
);
insert into account(name,money) values('a',1000),('b',2000),('c',3000);
–利用事务实现转账功能,首先开启事务,然后执行语句,提交事务
start transaction;
update account set money=money-100 where name='a';
update account set money=money+100 where name='b';
commit;
–事务的提交,通过这个命令查看mysql提交方式(若为1,表示自动提交,为0,就要手动提交)
select @@autocommit;
–若事务的提交方式为手动提交
set @@autocommit = 0; (设置手动提交)
start transaction;
update account set money=money+100 where name='a';
update account set money=money-100 where name='b';
–现在执行select * from account 可以看到转账成功,若此时退出数据库重新登录,会看到各账户余额没有改变,所以一定要用commit语句提交事务,否则会失败
(事务的回滚,别忘记设置为手动提交的模式)
start transaction;
update account set money=money-100 where name='a';
update account set money=money+100 where name='b';
–若此时a不想转账给b,可以使用事务的回滚
rollback;
–事务的隔离级别
read uncommitted;
read committed;
repeatable read;
serializable;


2.存储过程

–创建查看student表的存储过程
–创建student表
create table student(
id int not null primary key auto_increment,
name varchar(4),
grade float
)ENGINE=InnoDB default character set utf8;
–将mysql的结束符设置为//
delimiter //;
create procedure Proc()
begin
select * from student;
end //
delimiter ; (将mysql的结束符设置为;)
call Proc();(调用该存储过程)
–变量的使用,mysql中变量不用事前申明,在用的时候直接用“@变量名”使用就可以
set @number=100; -- 或set @num:=1;
–定义条件和处理程序
—声明光标
DECLARE * cursor_name* CURSOR FOR select_statement;
—光标OPEN语句
OPEN cursor_name;
—光标FETCH语句
FETCH cursor_name INTO var_name [, var_name] ...
—光标CLOSE语句
CLOSE cursor_name;


3.调用存储过程

–定义存储过程
delimiter //
create procedure proc1(in name varchar(4),out num int)
begin
select count(*) into num from student where name=name;
end//
delimiter ;
–调用存储过程
call proc1("tom",@num)(查找名为tom学生人数)
–查看结果
select @num;
–查看存储过程
show procedure status like 'p%'; (获得以p开头的存储过程信息)
–修改存储过程
alter {procedure|function} sp_name[characteristic...]
–删除存储过程

drop procedure proc1;

四、视图

如何创建视图
查看、修改、更新、删除视图


1.视图的基本操作

–在单表上创建视图,重新创建student表,插入数据
create table student(
id int not null primary key auto_increment,
name varchar(10) not null,
math float,
chinese float
);
insert into student(name,math,chinese) values
('howie1',66,77),
('howie2',66,77),
('howie3',66,77);
–开始创建视图
create view stu_view as select math,chinese,math+chinese from student;
–创建自定义字段名称的视图
create view stu_view2(math,chin,sum) as select math,chinese,math+chinese from student;
–在多表上创建视图,创建表stu_info,插入数据
create table stu_info(
id int not null primary key auto_increment,
class varchar(10) not null,
addr varchar(100)
);
insert into stu_info(class,addr) values
('1','anhui'),
('2','fujian'),
('3','guangdong');
–创建视图stu_class
create view stu_class(id,name,class) as
select student.id,student.name,stu_info.class from
student,stu_info where student.id=stu_info.id;
–查看视图
desc stu_class;
show table status like 'stu_class'\G
show create view stu_class\G
–修改视图
create or replace view stu_view as select * from student;
alter view stu_view as select chinese from student;
–更新视图
update stu_view set chinese=100;
insert into student values(null,'haha',100,100);
delete from stu_view2 where math=100;
–删除视图
drop view if exists stu_view2;


五、数据库的导入导出

导入导出数据库的数据
导入导出数据库SQL
备份数据库


1.导入导出数据库的数据

–导出整个数据库的数据
mysqldump -u root -p RUNOOB > database_name.txt

–备份所有数据库
mysqldump -u root -p --all-databases > database_dump.txt