mysql数据库备份函数,mysql数据库—函数、数据备份、流程控制

函数

内置函数

日期相关:

006tNbRwly1fxybu63r71j30tr0dlmzs.jpg

字符串相关:

006tNbRwly1fxybu39oedj30o80d576p.jpg

数字相关:

006tNbRwly1fxybu00d0fj30oa0cyabo.jpg

其他函数:

006tNbRwly1fxybtx70i8j30o908h40a.jpg

当然也包括之前学习的聚合函数

自定义函数

语法:

CREATE FUNCTION f_name(paramters)

returns dataType;

return value;

说明:

paramters 只能是in 输入参数 参数名 类型

必须有返回值

不能呢加begin 和end

returns 后面是返回值的类型 这里不加分号

return 后面是要返回的值

案例:

将两数相加

create function addfuntion(a int,b int)

returns int return a + b;

#执行函数

select addfuntion(1,1);

注意:

函数只能返回一个值

函数一般不涉及数据的增删改查 就是一个通用的功能

调用自定义的函数 与调用系统的一致 不需要call 使用select 可获得返回值

函数中不能使用sql语句

就像在java中不能识别sql语句一样

数据备份与恢复

使用mysqldump程序进行备份

mysqldump -u -p db_name [table_name,,,] > fileName.sql

可以选择要备份哪些表 如果不指定代表 全部备份

#示例:

#单库备份

mysqldump -uroot -p123 db1 > db1.sql

mysqldump -uroot -p123 db1 table1 table2 > db1-table1-table2.sql

#多库备份

mysqldump -uroot -p123 --databases db1 db2 mysql db3 > db1_db2_mysql_db3.sql

#备份所有库

mysqldump -uroot -p123 --all-databases > all.sql

使用 mysql 进行恢复

1.退出数据库后

mysql -u -p < filename.sql;

2.不用退出数据库

2.1 创建空数据库

2.2选择数据库

2.3然后使用source filename; 来进行还原

use db1;

source /root/db1.sql

数据库迁移

务必保证在相同版本之间迁移

# mysqldump -h 源IP -uroot -p123 --databases db1 | mysql -h 目标IP -uroot -p456

流程控制

if语句的使用

if 条件 then

语句;

end if;

第二种 if elseif

if 条件 then

语句1;

elseif 条件 then

语句2;

else 语句3;

end if;

案例:编写过程 实现 输入一个整数type 范围 1 - 2 输出 type=1 or type=2 or type=other;

create procedure showType(in type int,out result char(20))

begin

if type = 1 then

set result = "type = 1";

elseif type = 2 then

set result = "type = 2";

else

set result = "type = other";

end if;

end

CASE 语句

大体意思与Swtich一样的 你给我一个值 我对它进行选择 然后执行匹配上的语句

语法:

create procedure caseTest(in type int)

begin

CASE type

when 1 then select "type = 1";

when 2 then select "type = 2";

else select "type = other";

end case;

end

定义变量

declare 变量名 类型 default 值;

例如: declare i int default 0;

WHILE循环

循环输出10次hello mysql

create procedure showHello()

begin

declare i int default 0;

while i < 10 do

select "hello mysql";

set i = i + 1;

end while;

end

LOOP循环的

没有条件 需要自己定义结束语句

语法:

输出十次hello mysql;

create procedure showloop()

begin

declare i int default 0;

aloop: LOOP

select "hello loop";

set i = i + 1;

if i > 9 then leave aloop;

end if;

end LOOP aloop;

end

REPEAT循环

#类似do while

#输出10次hello repeat

create procedure showRepeat()

begin

declare i int default 0;

repeat

select "hello repeat";

set i = i + 1;

until i > 9

end repeat;

end

#输出0-100之间的奇数

create procedure showjishu()

begin

declare i int default 0;

aloop: loop

set i = i + 1;

if i >= 101 then leave aloop; end if;

if i % 2 = 0 then iterate aloop; end if;

select i;

end loop aloop;

end

标签:end,int,数据库,数据备份,mysql,db1,type,select

来源: https://www.cnblogs.com/gaohuayan/p/11197088.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值