MySQL造假数据,存储过程

建表

CREATE TABLE `t_user` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `c_user_id` varchar(36) NOT NULL DEFAULT '',
 `c_name` varchar(22) NOT NULL DEFAULT '',
 `c_province_id` int(11) NOT NULL,
 `c_city_id` int(11) NOT NULL,
 `create_time` datetime NOT NULL,
 PRIMARY KEY (`id`),
 KEY `idx_user_id` (`c_user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `t_user_memory` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `c_user_id` varchar(36) NOT NULL DEFAULT '',
 `c_name` varchar(22) NOT NULL DEFAULT '',
 `c_province_id` int(11) NOT NULL,
 `c_city_id` int(11) NOT NULL,
 `create_time` datetime NOT NULL,
 PRIMARY KEY (`id`),
 KEY `idx_user_id` (`c_user_id`)
) ENGINE=MEMORY DEFAULT CHARSET=utf8mb4;

存储过程

delimiter $$
CREATE DEFINER=`root`@`%` FUNCTION `randStr`(n INT) RETURNS varchar(255) CHARSET utf8mb4
  DETERMINISTIC
  BEGIN
  DECLARE chars_str varchar(100) DEFAULT 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  DECLARE return_str varchar(255) DEFAULT '' ;
  DECLARE i INT DEFAULT 0;
  WHILE i < n DO
   SET return_str = concat(return_str, substring(chars_str, FLOOR(1 + RAND() * 62), 1));
   SET i = i + 1;
  END WHILE;
  RETURN return_str;
  END$$
delimiter $$
   CREATE DEFINER=`root`@`%` PROCEDURE `add_t_user_memory`(IN n int)
    BEGIN
    DECLARE i INT DEFAULT 1;
        WHILE (i <= 57358) DO
         INSERT INTO t_user_memory (c_user_id, c_name, c_province_id,c_city_id, create_time) VALUES (uuid(), randStr(20), FLOOR(RAND() * 1000), FLOOR(RAND() * 100), NOW());
         SET i = i + 1;
        END WHILE;
        INSERT INTO t_user SELECT * FROM t_user_memory ;
        delete from t_user_memory where 1=1;
    END $$
调用存储过程 ,没找到原因,每次只能存57358 条数据 但是这个是在存储过程中改了,参数传多少都不会影响到里边的生成条数
CALL add_t_user_memory(57358);

多次执行存储过程来累加数据(自己写了个循环的但是没起效,只能手动多次执行)
删除存储过程

drop procedure add_t_user_memory;

过程中可能会出现权限不够的需要grant处理下

你可能感兴趣的:(mysql,sql,数据库)