语法:declare 变量名 数据类型 [初始值]
通过:select ...into…或set命令给变量赋值
例题通过sid查询姓名和年龄
createprocedurepro_3(in id int)begindeclarenamevarchar(10);
declare sexx char(10);
select sname,sex into name,sexx from stuinfo where sid=id;
select name,sexx from dual;
end//
#调用pro_3
call pro_3(10)//
#注意:声明的变量名不能和列名(字段名)同名
例题:查找同桌
createprocedurepro_4(in name varchar(32))
begindeclarestuseattinyint;select seat into stuseat from stuinfo where sname=name;
select * from stuinfo where seat=stuseat+1or seat=stuseat-1;
end//
#调用
call pro_4('百强')//
#通过set给变量赋值
createprocedurepro_5(in num1 year,in num2 year,in name varchar(32))
begindeclarenumint;set num=num2-num1; #得到年龄
update stuinfo set age=num where sname=name;
select * from stuinfo where sname = name;
end//
call pro_5(1991,2018,'小力')//
(6)全局变量(用户变量)
全局变量前面必须有一个@,全局变量的数据类型取决于变量的值。如果一个全局变量没有赋值,他的数据类型为null。
set @name='百强'//select * from stuinfo where sname=@name//
createprocedurepro_9(in num int)begincasenumwhen 1 thenselect '杀马特' as '气质';
when 2thenselect'屌丝'as'气质';
when 3thenselect'正常人'as'气质';
when 4thenselect'贵族'as'气质';
elseselect'输入不正确'as'气质';
endcase;
end//
call pro_9(0)//
loop遇到leave退出
createprocedureproc(in num int)begindeclaretotalintdefault 0;
declare i int default0;
sign:loopset total=total+i;
set i=i+1;
if i>=num then
leave sign;# leave=breakendif;
endloop;
select total from dual;
end//
call proc(100)//
#如果没有设置标签名,leave loop
#sign是循环名,用于结束循环,可以自己随意取名字
(4)while循环
#语法:while 条件 do//代码
end while
createprocedurepro_11(in num int)begindeclaretotalintdefault 0;
declare i int default0;
while num>=i doset total=total+i;
set i=i+1;
endwhile;
select total from dual;
end//
call pro_11(100)//
(5)repeat循环
#语法repeat
代码
until 条件 -- 直重复到条件为true才结束endrepeat
createprocedurepro_12(in num int)begindeclaretotalintdefault 0;
declare i int default0;
repeatset total=total+i;
set i=i+1;
until i>num
endrepeat;
select total from dual;
end//
call pro_12(100)//
(6)leave和iterate
leave类似于break,iterate类似于continue
createprocedurepro_13()begindeclareiintdefault 0;
sign:while i<5doset i=i+1;
if(i=3) then
leave sign; -- 类似于break
#iterate sign; -- 类似于continueendif;
select i from dual;
endwhile;
end//
call pro_13()//
#语法:
Create function 函数名(形参) returns 返回的数据类型
begin
//函数体end
#第一步
delimiter //#不带参数的函数
create function myfun() returns varchar(32)
begin
return'千锋python';
end //#调用函数
select myfun()//
#带参数
create function myfun_1(num1 int,num2 int) returns intbegindeclare num intdefault0;set num=num1+num2;
return num;
end //
select myfun_1(100,200)//
#删除函数
drop function myfun_1//
1、首先停止mysql服务进程:
service mysqld stop2.#然后编辑mysql的配置文件my.cnf(如果是windows的话找到my.ini)
vim /etc/my.cnf
3.#找到 [mysqld]这个模块:#在最后面添加一段代码skip-grant-tables ##忽略mysql权限问题,直接登录#然后保存 :wq!退出#启动mysql服务:
service mysqld start
#直接进入mysql数据库:
mysql
#选择mysql数据库
use mysql;
#对user表的root用户进行密码修改
update mysql.user set authentication_string=password('123456') where user='root' and Host = 'localhost';
#特别提醒注意的一点是,新版的mysql数据库下的user表中已经没有Password字段了#而是将加密后的用户密码存储于authentication_string字段#执行刷新
flush privileges;
#exit退出mysql
exit;
#启动服务
service mysqld start