Data backup and stored procedure mysql-

[TOC]

Modify the isolation level

修改全局的  
 set global transaction isolation level read committed;
 或者:
 set @@tx_isolation = "asasasasas-read";
 修改局部
 set session transaction isolation level read committed;
 

 @@系统内置变量
 @表示用户自定义的变量

Stored Procedures

What is a stored procedure

是任意的sql语句的组合,被放到某一个存储过程中,类似于一个函数,有一个函数名/参数/还有函数体.

Used to do

其中可以包含任何的sql语句,逻辑处理,事务处理,所有的我们学过的sql语句都可以放到里面

Three data handling

  1. Application only concerned with the business logic, all the data associated with the logic package into mysql

    • Advantages: application to be processed matter becomes less, you can reduce network traffic
  2. Applications should not only deal with business logic, but also to write sql statement

    • Benefits: reduce communication costs, labor costs
    • Disadvantages: increased network transmission, write very complex sql statement
  3. ORM frame by using the object-relational mapping, to automatically generate and execute the sql statement

    • Advantages: no need to write the sql statement, to enhance the development efficiency
    • Disadvantages: not flexible enough, application developers and database completely isolated, can lead to only focus on the development of the upper layer, but do not know the underlying principle
# 语法
create procedure p_name(p_type p_name p_date_type)
begin
sql......
end

# p_type 参数的类型  in 输入   out 输出必须是一个变量,不能是值    inout 即可输出也可输入  
# p_name 参数的名字
# p_data_type  参数的数据类型  如  int  float

Case

delimiter |
create procedure a1(in a int, in b int, out c int) 
begin
set c = a + b;
end |
delimiter ;

# 调用
set @res =0;
call a1(1,1,@res);
select * from @res;

# 删除
drop procedure 名称;

# 查看
show create procedure 名称

# 查看全部  例如 db库下的所有
select name from mysql.proc where db = 库名 and  type = "PROCEDURE";

delimiter |
create procedure transfer2(in aid int,in bid int,in m float,out res int)
begin 
	declare exit handler for sqlexception
	begin
		# 异常处理代码
		set res = 99;
		rollback;
	end;
	
	start transaction;
	update account set money = money - m where id = aid;
	update account set money = moneys + m where id = bid;
	commit;
	set res = 1;
end|
delimiter ;

Backup and Recovery

# 备份
mysqldump.exe   
mysqldump -u用户名 -p密码 数据库 表名1 表名2 .... > 文件路径....
# 注意 第一个表示数据库  后面全都是表名
mysqldump -uroot -p day41 student  > 


#备份多个数据库
mysqldump -uroot -p111 --databases day41 day40  > x3x.sql
#指定 --databases 后导出的文件包含 创建库的语句   而上面的方式不包含

#备份所有数据  
mysqldump -uroot -p111 --all-databases > all.sql


#自动备份
linux crontab 指令可以定时执行某一个指令



# 恢复数据:
没有登录mysql 
mysql < 文件的路径


已经登录了MySQL 
source  文件路径


注意: 如果导出的sql中没有包含选择数据库的语句 需要手动加上 

Guess you like

Origin www.cnblogs.com/raynduan/p/11444766.html