存储过程,函数——mysql批量添加随机生成用户信息(生成随机数)

前两天看到了一篇博客,学习到了通过存储过程批量随机生成用户信息的方法,总结了下,在这里分享给大家,同时也做一个学习记录。闲话不多说,进入正题

我这边使用的是mysql数据库,Navicat12工具,,测试用户信息表d_user表结构如下:

CREATE TABLE `d_user`  (
  `id` varchar(36) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '用户主键ID',
  `user_type` tinyint(1) NULL DEFAULT NULL COMMENT '用户类型',
  `user_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_czech_ci NULL DEFAULT NULL COMMENT '用户名',
  `age` tinyint(3) NULL DEFAULT NULL COMMENT '年龄',
  `password` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '密码',
  `mobile` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '手机号',
  `email` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '邮箱',
  `image_url` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '头像地址',
  `last_online_time` datetime(0) NULL DEFAULT NULL COMMENT '最后上线时间',
  `status` tinyint(4) NULL DEFAULT 1 COMMENT '状态  0:禁用   1:正常',
  `salt` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '盐',
  `create_user_id` varchar(36) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '创建人ID',
  `create_by` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '创建人',
  `create_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
  `update_user_id` varchar(36) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '更新人ID',
  `update_by` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '更新人',
  `update_time` datetime(0) NULL DEFAULT NULL COMMENT '更新时间',
  `is_delete` tinyint(2) NULL DEFAULT 0 COMMENT '是否删除 0:不删1:删除',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

SET FOREIGN_KEY_CHECKS = 1;

存储过程,函数——mysql批量添加随机生成用户信息(生成随机数)_第1张图片

下面sql内容需要了解到mysql生成随机数相关函数的用法,不了解的可以先了解下哦。。。

https://www.jb51.net/article/148167.htm   ——   mysql生成指定位数的随机数及批量生成随机数的方法

https://blog.csdn.net/gushaolin/article/details/103541630   ——   mysql生成随机数--rand、round、floor、ceiling函数以及获得指定范围随机数

存储函数

1.随机生成用户名称generate_userName()

CREATE DEFINER=`root`@`::1` FUNCTION `generate_userName`() RETURNS varchar(255) CHARSET utf8 DETERMINISTIC
BEGIN -- 开始
  DECLARE xing VARCHAR (2056) DEFAULT '赵钱孙李周郑王冯陈楮卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢喻柏水窦章云苏潘葛奚范彭郎鲁韦昌马苗凤花方俞任袁柳酆鲍史唐费廉岑薛雷贺倪汤滕殷罗毕郝邬安常乐于时傅皮齐康伍余元卜顾孟平黄和穆萧尹姚邵湛汪祁毛禹狄米贝明臧计伏成戴谈宋茅庞熊纪舒屈项祝董梁杜阮蓝闽席季麻强贾路娄危江童颜郭梅盛林刁锺徐丘骆高夏蔡田樊胡凌霍虞万支柯昝管卢莫经裘缪干解应宗丁宣贲邓郁单杭洪包诸左石崔吉钮龚程嵇邢滑裴陆荣翁'; -- 定义姓
  DECLARE ming VARCHAR (2056) DEFAULT '嘉懿煜城懿轩烨伟苑博伟泽熠彤鸿煊博涛烨霖烨华煜祺智宸正豪昊然明杰诚立轩立辉峻熙弘文熠彤鸿煊烨霖哲瀚鑫鹏致远俊驰雨泽烨磊晟睿天佑文昊修洁黎昕远航旭尧鸿涛伟祺轩越泽浩宇瑾瑜皓轩擎苍擎宇志泽睿渊楷瑞轩弘文哲瀚雨泽鑫磊梦琪忆之桃慕青问兰尔岚元香初夏沛菡傲珊曼文乐菱痴珊恨玉惜文香寒新柔语蓉海安夜蓉涵柏水桃醉蓝春儿语琴从彤傲晴语兰又菱碧彤元霜怜梦紫寒妙彤曼易南莲紫翠雨寒易烟如萱若南寻真晓亦向珊慕灵以蕊寻雁映易雪柳孤岚笑霜海云凝天沛珊寒云冰旋宛儿绿真盼儿晓霜碧凡夏菡曼香若烟半梦雅绿冰蓝灵槐平安书翠翠风香巧代云梦曼幼翠友巧听寒梦柏醉易访旋亦玉凌萱访卉怀亦笑蓝春翠靖柏夜蕾冰夏梦松书雪乐枫念薇靖雁寻春恨山从寒忆香觅波静曼凡旋以亦念露芷蕾千兰新波代真新蕾雁玉冷卉紫山千琴恨天傲芙盼山怀蝶冰兰山柏翠萱乐丹翠柔谷山之瑶冰露尔珍谷雪乐萱涵菡海莲傲蕾青槐冬儿易梦惜雪宛海之柔夏青亦瑶妙菡春竹修杰伟诚建辉晋鹏天磊绍辉泽洋明轩健柏煊昊强伟宸博超君浩子骞明辉鹏涛炎彬鹤轩越彬风华靖琪明诚高格光华国源宇晗昱涵润翰飞翰海昊乾浩博和安弘博鸿朗华奥华灿嘉慕坚秉建明金鑫锦程瑾瑜鹏经赋景同靖琪君昊俊明季同开济凯安康成乐语力勤良哲理群茂彦敏博明达朋义彭泽鹏举濮存溥心璞瑜浦泽奇邃祥荣轩'; -- 定义名
  DECLARE I_xing INT DEFAULT LENGTH(xing) / 3;    -- 定义姓的下标
  DECLARE I_ming INT DEFAULT LENGTH(ming) / 3;    -- 定义名的下标
  DECLARE return_str VARCHAR (2056) DEFAULT '';   -- 定义默认的返回值
  SET return_str = CONCAT(
    return_str,
    SUBSTRING(xing, FLOOR(1 + RAND() * I_xing), 1)    -- 截取xing集合中的随机一个下标,取一位(字符下标从1开始)
  );
  SET return_str = CONCAT(
    return_str,
    SUBSTRING(ming, FLOOR(1 + RAND() * I_ming), 1)
  );
  IF RAND() > 0.400           -- 如果随机函数值大于0.4.则添加第3个名字
  THEN SET return_str = CONCAT(
    return_str,
    SUBSTRING(ming, FLOOR(1 + RAND() * I_ming), 1)
  );
  END IF;     -- 结束if语句
  RETURN return_str;      -- 返回执行结果
END

2.随机生成手机号码generate_phone()

CREATE DEFINER=`root`@`::1` FUNCTION `generate_phone`() RETURNS char(11) CHARSET utf8
    DETERMINISTIC
BEGIN
  DECLARE head VARCHAR (100) DEFAULT '000,182,150,136,152,158,183';
  DECLARE content CHAR(10) DEFAULT '0123456789';
  DECLARE phone CHAR(11) DEFAULT SUBSTRING(head, 1+ (FLOOR(1 + (RAND() * 3)) * 4), 3);
  DECLARE i INT DEFAULT 1;
  DECLARE len INT DEFAULT LENGTH(content);
  WHILE
    i < 9 DO SET i = i + 1;
    SET phone = CONCAT(
      phone,
      SUBSTRING(content, FLOOR(1 + RAND() * len), 1)
    );
  END WHILE;
  RETURN phone;
END

3.随机生成163.com类型的邮箱generate_163email()

CREATE DEFINER=`root`@`::1` FUNCTION `generate_163email`() RETURNS char(20) CHARSET utf8
    DETERMINISTIC
BEGIN
  DECLARE head VARCHAR (100) DEFAULT '000,182,150,136,152,158,183';
  DECLARE content CHAR(10) DEFAULT '0123456789';
  DECLARE phone CHAR(11) DEFAULT SUBSTRING(head, 1+ (FLOOR(1 + (RAND() * 3)) * 4), 3);#定义手机号变量且长度为11
  DECLARE email CHAR(20); #定义邮箱变量且长度为20
  DECLARE i INT DEFAULT 1;
  DECLARE len INT DEFAULT LENGTH(content);
  WHILE
    i < 9 DO SET i = i + 1;
    SET phone = CONCAT(phone, SUBSTRING(content, FLOOR(1 + RAND() * len), 1));
  END WHILE;
	set email = CONCAT(phone,'@163.com');
  RETURN email;
END

4.指定邮箱类型随机生成generate_email(emailType)

CREATE DEFINER=`root`@`::1` FUNCTION `generate_email`(emailType VARCHAR(36)) RETURNS char(100) CHARSET utf8
    DETERMINISTIC
BEGIN
  DECLARE head VARCHAR (100) DEFAULT '000,182,150,136,152,158,183';
  DECLARE content CHAR(10) DEFAULT '0123456789';
  DECLARE phone CHAR(11) DEFAULT SUBSTRING(head, 1+ (FLOOR(1 + (RAND() * 3)) * 4), 3);#定义手机号变量且长度为11
  DECLARE email CHAR(100); #定义邮箱变量,且长度充足
  DECLARE i INT DEFAULT 1;
  DECLARE len INT DEFAULT LENGTH(content);
  WHILE
    i < 9 DO SET i = i + 1;
    SET phone = CONCAT(phone, SUBSTRING(content, FLOOR(1 + RAND() * len), 1));
  END WHILE;
	set email = CONCAT(phone,emailType); -- 随机手机号与邮箱后缀拼接(如:入参 emailType = @163.com )
  RETURN email;
END

存储过程

mysql批量添加随机生成用户信息batchCreateUsers()

CREATE DEFINER=`root`@`localhost` PROCEDURE `batchCreateUsers`(num INT)
BEGIN
    DECLARE count INT DEFAULT 0;
    DECLARE i INT DEFAULT 0;
    set @exesql = concat("insert into d_user(id, user_type,user_name,age,password,mobile,email,image_url,last_online_time,status,salt,create_user_id,create_by,create_time,update_user_id,update_by,update_time) values ");
    set @exedata = "";
    while count < num do 
            set @exedata = concat(@exedata, ",('",MD5(generate_userName()), "','", 0,"','", generate_userName(),"','", floor(RAND()*37+18), "','", MD5(generate_userName()), "','", generate_phone(), "','", generate_163email(), "','", "http://test.com", "','", current_timestamp(), "','", 0, "','", MD5(generate_userName()), "','", MD5("test"), "','","test", "','", current_timestamp(), "','", MD5("test"), "','", "test", "','", current_timestamp(), "')");
            set count = count + 1;
            set i = i + 1;
            if i % 1000 = 0
            then 
                    set @exedata = SUBSTRING(@exedata, 2);
                    set @exesql = concat("insert into d_user(id, user_type,user_name,age,password,mobile,email,image_url,last_online_time,status,salt,create_user_id,create_by,create_time,update_user_id,update_by,update_time) values ", @exedata);
                    prepare stmt from @exesql;
                    execute stmt;
                    DEALLOCATE prepare stmt;
                    set @exedata = "";
            end if;
    end while;

    if length(@exedata)>0 
    then 
            set @exedata = SUBSTRING(@exedata, 2);
            set @exesql = concat("insert into d_user(id, user_type,user_name,age,password,mobile,email,image_url,last_online_time,status,salt,create_user_id,create_by,create_time,update_user_id,update_by,update_time) values ", @exedata);
            prepare stmt from @exesql;
            execute stmt;
            DEALLOCATE prepare stmt;
    end if;
END

到这里大体部分就已经结束了,,

对于存储过程,存储函数,我这里也附上我参考学习的链接,大家一起做一个回顾学习,如果对以上内容有什么想法,欢迎大家留言,感谢。

参考学习链接:

https://blog.csdn.net/yanluandai1985/article/details/83656374?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task   ——   从头开始学MySQL-------存储过程与存储函数(1)

https://blog.csdn.net/tojohnonly/article/details/70738629   ——   SQL存储过程使用介绍

https://www.runoob.com/w3cnote/mysql-stored-procedure.html   ——   MySQL 存储过程

https://blog.csdn.net/Me_ZH/article/details/78880483   ——   mysql存储过程,生成随机字符串

https://blog.csdn.net/mengzuchao/article/details/80686792   ——   mysql 存储函数

https://blog.csdn.net/ch18255112191/article/details/48294451?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task   ——   MYSQL存储过程和存储函数入门

借鉴链接:

https://blog.csdn.net/tanglei6636/article/details/95317216   ——   【MySQL】生成随机手机号、随机时间方法

https://www.cnblogs.com/lisongyu/p/10369915.html   ——   mysql生成20万条数据(连表插入)

你可能感兴趣的:(数据库,存储过程)