montainyang 发表于 2019-8-27 17:58:42

使用存储过程造百万级测试数据

来源微信公众号:IT测试前沿https://mp.weixin.qq.com/mp/homepage?__biz=MzA3NDU2MDYzNw==&hid=6&sn=cbebcf2a2e486445af3d56b9bd8ba0df

1.        首先我们先创建一张表
创建一张表以存放测试数据,该表包含四个字段:唯一自增量的主键id、姓名name、手机号mobile、身份证号idcode。

-- ----------------------------
-- Table structure for `user_test`
-- ----------------------------
DROP TABLE IF EXISTS `user_test`;
CREATE TABLE `user_test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(10) DEFAULT NULL,
`mobile` varchar(11) DEFAULT NULL,
`idcode` varchar(18) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;



2.        创建一个增加一条记录的存储过程并调用
DROP PROCEDURE IF EXISTS proInsert;
Create procedure proInsert()
Begin
Insert into user_test(name,mobile,idcode) values('姓名1','13900001111','123456789012345678');
End

Call proInsert ();



3.        在原有基础上增加循环语句
DROP PROCEDURE IF EXISTS proInsert;
Create procedure proInsert()
Begin
        #定义一个初始变量
        Declare i int;
        Declare _name varchar(10) default        1;
        Declare _mobile varchar(11) default 1;
        Declare _idcode varchar(18) default 1;
        #设置变量值为1
        Set i=1;

        While i<=10 do
                #mysql的连接函数
                Set _name=concat('姓名_',i);
                #name加后缀
                Set _mobile =1380000000+i;
                #mysql 随机函数,生成0-1的小数
                Set _idcode=123456789010000000+i;
                Insert into user_test(name,mobile,idcode) values(_name,_mobile,_idcode);
                Set i=i+1;
        End while;
End


4.        使用call调用存储过程,或者运行函数。
Call proInsert();



5.        增加后的数据




6.        存储过程的一些语法:
a)        删除存储过程命令:
        DROP {PROCEDURE | FUNCTION} 名称;
b)        创建存储过程:
        create procedure 名称(参数,.....)
                 begin
                   过程体;
                   过程体;
                 end//
c)        参数:
                in|out|inout 参数名称 类型(长度)
d)        在sql语句中给变量赋值:
        into
e)        在过程体外声明变量:
        @变量名
f)        重新制定sql语句的结束符:
        delimiter //
g)        例子:获取5条文章记录
        create procedure getNews()
                begin
                        select * from news limit 5;
                end//
h)        例子:获取n条文章记录
        create procedure getNewsN(in n int(5))
                begin
                        select * from news limit n;
                end//
i)        例子:获取某栏目下文章的条数。
        create procedure getNewsByType(in fid int,out num int)
                begin
                        select count(*) into num from news where fcid=fid;
                end//
j)        声明变量:
        declare 变量名 类型(长度) default 默认值;
k)        给变量赋值:
        set 变量名=值;





页: [1]
查看完整版本: 使用存储过程造百万级测试数据