现在有一个user用户表,需要往user表中插入1000个登录用户
要求如下:
1、在插入用户前先判断是否存在1000个登录用户,如果存在则统计表中实 际的行数、如若不存在则自动补齐剩余的数据
2、表名为user,存储过程名字随意取,表中字段有id user_name user_pwd verify 格式如下(1,user1,123456,W4E38J),且id、用户名不能重复,verify 验证码字段为随机生成6位数验证码。
解答过程
drop procedure if exists test;
create procedure test (n int)
BEGIN
declare i int(20) default(select count(*) from user);
declare x varchar(20) default'';
declare y varchar(20) default'';
drop table if exists user;
create table user (id int(20) primary key auto_increment,user_name varchar(20), user_pwd int(20) default 123456, verify varchar(20));
insert into user values(1,'user1',123456,'W4E38J');
if i>=n THEN
select count(*) from user;
else
while i<n DO
set i=i+1;
set x=(select concat('user',i));
set y=(select substr(md5(rand()),1,6));
insert into user (user_name,verify) values (x,y);
end while;
end if;
END
call test(1000)
drop table user;
select * from user;