一、已知表stringcontent
其中字段:
id 自增长
content varchar(20)
向该表插入指定个数的,随机的字符串
drop table if exists stringcontent;create table stringcontent(id int primary key auto_increment,content varchar(20));CREATE PROCEDURE test_randstr_insert(IN insertCount INT)BEGINDECLARE i INT DEFAULT 1;DECLARE str VARCHAR(26) DEFAULT 'abcdefghijklmnopqrstuvwxyz';DECLARE startIndex INT;#代表初始索引DECLARE len INT;#代表截取的字符长度WHILE i<=insertcount DOSET startIndex=FLOOR(RAND()*26+1);#代表初始索引,随机范围1-26SET len=FLOOR(RAND()*(20-startIndex+1)+1);#代表截取长度,随机范围1-(20-startIndex+1)INSERT INTO stringcontent(content) VALUES(SUBSTR(str,startIndex,len));SET i=i+1;END WHILE;endCALL test_randstr_insert(10);select * from stringcontent;
