CREATE DEFINER=`root`@`%`FUNCTION`rand_string`(n INT) RETURNSvarchar(255) CHARSET utf8mb4 DETERMINISTIC BEGIN DECLARE chars_str varchar(100) DEFAULT'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; DECLARE return_str varchar(255) DEFAULT'' ; DECLARE i INTDEFAULT0; WHILE i < n DO SET return_str = concat(return_str, substring(chars_str, FLOOR(1 + RAND() * 62), 1)); SET i = i + 1; ENDWHILE; RETURN return_str; END;
CREATE DEFINER=`root`@`%`PROCEDURE`insert_student`(IN n int) BEGIN DECLARE i INTDEFAULT1; WHILE (i <= n) DO INSERTINTO student (name, cid) VALUES (rand_string(8), FLOOR(RAND() * 100)); SET i = i + 1; ENDWHILE; END;