储存过程:创建大量的测试数据,用这个数据进行相关的测试,简称:造数据(测试岗位使用评率不高,大多开发在使用,一个储存代码写好后可以给其他同时调用)一:了解储存的基本架构create procedure +储存的名称()begin (开始)代码块end (结束)call 储存名称();二:实例create procedure dcs()#创建一个储存begin#开始create table ddc(id int(2)primary key,age int(2));insert into ddc(id,age)values(1,22),(2,20),(3,21);select * from ddc;endcall dcs();#调用储存三:解决限制只能调用执行一次,增强代码的容错率和健壮性drop procedure if exists dcs;#增强代码的容错率和健壮性create procedure dcs()#创建一个储存begindrop table if exists ddc;#如果存在则删除,不存在则不删除create table ddc(in int(2)primary key,age int(2));insert into ddc(id,age)values(1,22),(2,20),(3,21);select * from ddc;endcall dcs();#调用储存四:了解形参和实参,还有做增删改的相关操作drop procedure if exists dcs;#增强代码的容错率和健壮性create procedure dcs(n int)#创建储存定义一个形参n,类型是整数begindrop table if exists ddc;#如果存在则删除,不存在则不删除create table ddc(in int(2)primary key,age int(2));insert into ddc(id,age)values(1,22),(2,20),(3,21);select * from ddc where id=n;n=1select * from ddc where id=1;#查询update ddc set score=100 where id=1;#修改delete from ddc where id=1;#删除endcall dcs(1);查询ddc表内id=1所有数据形参是设定在dcs储存中的,那么通过call调用dcs的时候一定要赋予一个对应形参对应的实参,且只能赋予一个值。五:if的判断drop procedure if exists dcs;#增强代码的容错率和健壮性create procedure dcs(n int)#创建储存定义一个形参n,类型是整数begindrop table if exists ddc;#如果存在则删除,不存在则不删除create table ddc(in int(2)primary key,age int(2));insert into ddc(id,age)values(1,22),(2,20),(3,21);if n=o then#如果满足条件则执行查询所有select * from ddc;else#不满足则查询最小年龄select min(age) from ddc;end if;#结束if判断endcall dcs(1);(不为o)不满足n=0查询最小年龄,(0)则满足n=0显示表ddc所有数据六:if的多分支判断drop procedure if exists dcs;create procedure dcs(n int)begindrop table if exists ddc;create table ddc(id int(4)primary key,score int(4));insert into ddc(id,score)values(1,22),(2,33),(3,44);if n=o thenselect * from ddc;else if n>1 thenselect * from ddc where id=3;elseselect min(score) from ddc;end if;end if;endcall ddc(2);(0)满足则显示表ddc内所有内容,(大于1)显示表ddc内id=3的所有内容,(不为0也不大于1)则显示表ddc分数最小的所有内容七:while循环语句(死循环)drop procedure if exists dcs;create procedure dcs(n int)begindeclare i int default 1;#设置一个变量i 整形给变量赋予值1drop table if exists ddc;create table ddc(id int(4)primary key auto_increment,score int(4));while循环如果条件满足进入循环体,不满足退出循环体,一直满足一直循环(死循环)while n>=1 do#满足条件进入死循环insert into ddc(score)values(i);end while;#结束select * from ddc;endcall dcs(2);当()内为1或者比1小时则显示ddc表内所有内容,当()内的值大于或等于1则进入死循环创建表数据
file:///C:\Users\86155\Documents\Tencent Files\2362864017\Image\C2C\12O4H46)}`YN@0OG1A8C8%Q.JPG
八:设置变量和while循环语句,创造数据drop procedure if exists dcs;create procedure dcs(n int)begindeclare i int default 1;#设置一个变量i 整形给i量赋予值1declare j int default(select min(score) from ddc); #设置一个变量j,整形给变量赋予值为ddc表内最小分数的那个值)drop table if exists ddc;create table ddc(id int(4)primary key auto_increment,score int(4));while循环如果条件满足进入循环体,不满足退出循环体,一直满足一直循环(死循环)while n>=i do#满足条件进入死循环(n=10 i=1 10>1 1+1=2 2+1=3 3+1=4........... 10>=11)insert into ddc(score)values(i);set i=i+1end while;#结束select * from ddc;endcall dcs(10);i=1 当()>=1时进入创建数据,直到i的值等于()内的值,条件是set i=i+1 每创建一行数据i的值都加1,1+1=2 2+1=3 3+1=4。当()<1时,则显示ddc表内所有内容
|
|