create database if not exists name1; // 有就跳过,没有就创建 good
1 2 3
create database if not exists name1 charset=字符集; // 指定字符集 create database if not exists test3 charset=utf8;
注意关键字
1 2 3
create database if not exists `create` charset=utf8; // `create`包裹起来 // 注意点:如果数据库的名称是SQL的关键字或者是一些特殊字符中!@这个时候就需要用反引号括起米
查看数据库
1
show databases;
查看数据库全局默认编码
1
show variables like 'character_set_%';
查看一个数据库编码
1
show create database name;
| test1 | CREATE DATABASE test1 /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci / /!80016 DEFAULT ENCRYPTION=’N’ */ |
数据库的删除
删除数据库
1 2 3
drop database name; // 没有这个数据库会报错 // Can't drop database 'name'; database doesn't exist
1 2
drop database if exists name; // 没有这个数据库不会报错,跳过
数据库的修改和查看
修改数据库的字符集
1 2
alter database name charset=字符集; // alter database test1 charset=gbk;
查看数据库
1 2
show create database name; // 查看字符集 show databases; // 查看数据库
表的增删改查
注意 : 需要制定一个数据库
1
use name;
查看数据库中的表
1
show tables;
创建表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
create table name( 字段名称 数据类型, 字段名称 数据类型, 字段名称 数据类型, ); // 没这个表会成功,有这个表会失败 create table stu( id int, name text ); // 修复 create table if not exists person( id int, name text );
查看制定表的结构
1
desc name;
删除表
制定一个表
1
use name;
删除表
1 2
drop table name;// 没有就报错 drop table if exists name; // 兼容
修改表
修改表名
1 2
rename table 原始名称 to 新名称; rename table stu to person;
添加字段
1 2 3 4
alter table 表名 add 新增字段名称 类型 [位置]; alter table person add age int;// 默认字段放在后面 alter table person add score float first; // first 放到最前面 alter table person add phone int after name; // name 后面
alter table person add age int;
alter table person add score float first;
alter table person add phone int after name;
删除字段
1 2
alter table 表名 drop 字段名称; alter table person drop phone;
修改字段
1.修改数据类型
1 2
alter table 表名 modify 需要修改的字段名称 新的数据类型; alter table person modify score double;
2.修改字段名称和数据类型
1 2
alter table 表名 change 原始字段名称 新字段名称 新的数据类型; alter table person change age address text;
存储引擎
MySQL中有三种存储引擎,分别是:
MyISAM:安全性低,但不支持事务和外键,适合频繁插入和查询的应用
InnoDB(默认):安全性高,支持事务和外键,适合对安全性,数据完整性要求较高的应用
Memory:访问速度极快,但不会永久存储数据,适合对读写速度要求较高的应用
1 2 3 4
create table stu( id int, name text )engine=引擎名称;
不同引擎的本质
数据库的本质就是文件,只要创建一个数据库,就会创建文件夹
创建一张表,就会在指定数据库目录创建一个文件,这个文件会保存结构
引擎
内容
存储文件
InnoDB
创建表就会自动创建一个文件
.ibd 保存了这张表的结构
ibdata1,ibdata2
MyISAM
自动创建三个文件
.sdi 表的结构
.MYD 表中存储的数据
.MYI 表中索引
.MYD文件
Memory
创建.sdi文件,保存结构信息
将数据保存在内存中
内存
修改表的存储引擎
1
alter table 表明 engine=名称;
插入和更新数据
插入数据
1 2 3 4 5 6 7
INSERT INTO table_name ( field1, field2,...fieldN ) VALUES ( value1, value2,...valueN ); // INSERT INTO person ( score,id,name,address ) VALUES ( 1.1,1,"nihao","china");
查看数据
1 2 3 4
SELECT column_name,column_name FROM table_name [WHERE Clause] [LIMIT N][ OFFSET M]
更新数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
UPDATE table_name SET field1=new-value1, field2=new-value2 [WHERE Clause] // // 不指定条件就更新整张表 update person set score=2.2323;
// 指定更新 更新满足条件的 update person set score=99.2 whereid=12;
// 可以多个条件指定 update person set score=99.2 whereid=12 AND name="nihao2"; update person set score=99.2 whereid=12 OR name="nihao";
// 更新多个字段 update person set score=991.2,address='new address'whereid=12 OR name="nihao";
查询表数据
全部
1
select * from 表名 [where]
指定字段
1
select 字段1,字段2 from 表名 [where]
where 支持的运算符
1 2 3 4 5 6 7
=(等于)、!=(不等于)、《〉(不等于)、〈(小于)、〈=(小于等于)、>(大于)、>=(大于等于); IN (set);固定的范围值 PETWEEN•••AND:值在什么范围 IS NULL:(为空) IS NOT NULL (不为空 AND:与 OR;或 NOT:非
比如
1
select * from stu where score in (77, 88)
删除表数据
删除数据
1 2
delete from 表名 [where 条件]; // select name from person whereid>5;
// enum(v1,v2,...) create table person( id int, gender enum('男","女') )
集合类型
几个固定值中的几个
mysql也是通过整数的形式实现的
mysql集合类型按照2(n)实现的 方便位运算
1 2 3 4 5 6 7
// set(v1,v2,v3...) create table person id int, hobby set( '篮球','足球','高尔夫球','排球') )
insert into person values (1,'篮球,足球,高尔夫球')
日期类型
时间需要单引号扩起来
1 2 3 4 5 6 7 8 9 10
create table person ( id int, filedl DATE, filed2 TIME, filed3 DATETIME ) // 报错,时间需要单引号扩起来 insert into person values (1, 2020-02-02, 14:18:23, 2020-02-02 14:18:23); // 完美 insert into person values (1, '2020-02-02', '14:18:23', '2020-02-02 14:18:23');
布尔类型
mysql boolean类型也是整型实现的,0假1真,底层本质是mysql是使用c c++实现,所以非0就是真
boolean 真假
数值类型
数据完整性-主键
什么是数据的完整性 保证保存到数据库中的数据都是正确的。
如何保证数据完整性? 数据的完整性可以分为三类:实体完整性、域完整性、参照完整性
实体完整性
什么是实体? 表中的一行数据就是一个实体 (entity)
如何保证实体完整性? 保证实体完整性就是保证每一行数据的唯一性
实体完整性的约束类型
主键约束 (primary key)
唯一约束(unique)
自动增长列 (auto_ increment)
主键约束 (primary key) 主键用于唯一标识表中的每一条数据,和现实生活中的身份证很像
1 2 3 4 5 6 7
create table person ( id int, name varchar (20) ); // 下面的不会报错,但是会有问题,没有唯一性 insert into person values (1," Inj"); insert into person values (1," Inj");
有效的
1 2 3 4 5 6 7 8 9 10 11 12 13
create table person ( id int primary key, name varchar (20) ) // 下面的会报错,唯一性问题 insert into person values (1," Inj") insert into person values (1," Inj") // 或者 create table person ( id int, name varchar (20), primary key(id) ) ;
create table person( name varchar(20), age int, primary key(name,age) ) insert into person values (" Inj",1) insert into person values (" Inj",1)
数据完整性-唯一约束
唯一约束保证某个字段的值永远不重复
1 2 3 4 5 6 7 8 9 10 11 12 13 14
create table person( name varchar(20) unique, age int ) insert into person values (" Inj",1) insert into person values (" Inj",1)
// 多个unique create table person( name varchar(20) unique, age int unique ) insert into person values (" Inj",1) insert into person values (" Inj",1)
唯一约束和主键约束的异同
同:被约束的字段的取值不能重复
异:主键在一张表中只有一个,唯一约束可以有多个
唯一约束可以为空,主键不能为空
数据完整性-自动增长约束
自动增长约束的作用是让某个字段的取值从1开始递增,从而保证实体完整性
必须是主键才可以自动增长
1 2 3 4 5 6 7
createtable person ( id int auto_increment primary key, name varchar (20) ); // 下面的会报错,唯一性问题 insertinto person values (null," Inj"); insertinto person values (default," Inj");
default 或者null 就自动关增长
在企业开发中如何选择主键呢?最少性和稳定性
最少性:能用一个字段作为主键,就不要使用多个字段
稳定性:能用不被操作(修改)的字段作为主键,就不要使用会被操作的字段作为主键
一般情况会定义id字段,并且字段为整形,自动增长,设置为主键
实体完整性
没有这些的时候 添加这些
如何修改约束
1.如何修改主键约束
1 2 3 4 5 6 7 8 9
altertable 表名 addprimary key(字段); // createtable person ( id int, name varchar (20) ); // insertinto person values (1," Inj") insertinto person values (1," Inj")
2.如何修改唯一约束
1 2 3 4 5 6 7 8 9
altertable 表名 addunique(字段); // createtable person2 ( id int, name varchar (20) ); // insertinto person values (1," Inj") insertinto person values (1," Inj")
3.如何修改自动增长约束
1 2 3 4 5 6 7 8 9 10
altertable 表名 addprimary key(字段); altertable 表名 modify 字段 数据类型 auto_increment; // createtable person2 ( id int, name varchar (20) ); // insertinto person values (null," Inj") insertinto person values (default," Inj")
createtable person ( id int, name varchar (20) notnull ); insertinto person values (1," Inj"); insertinto person values (null,null);
默认值
1 2 3 4 5 6 7
createtable person ( id int, name varchar (20) default'这是默认值' ); insertinto person values (1," Inj"); insertinto person values (null,null); insertinto person values (null,default);
注意点null,也不会使用默认值,使用default才会使用
表与表之间的关系
1.参照完整性
参照完整性又称引用完整性,主要用于保证多表之间引用关系的正确性
2.为什么要创建多张表?
示例:定义一张表保存2个学生3门课程的成绩
如果将所有的数据都放到一张表中,会出现大量元余数据(学生信息保存)
所以为了降低数据库的体积,提升数据库的效率,我们需要根据自身需求对表进行拆分
3.什么时候会出现冗余数据
表与表之间的关系可以分为三种 一对一、一对多、多对多
3.1 一对一
一般不用拆分
id
name
gender
mateId
1
A
1
2
2
B
0
1
3
C
1
4
4
D
0
3
3.2 一对多关系
拆
一个人有多辆车,一个班有多个学生,一个人有多门成绩
3.3 多对多关系
拆
一个学生有多个老师,一个老师有多个学生
参照完整性
如何保证参照完整性?
默认情况下表与表之间是独立存在的,不会相互影响
也正是因为如此,默认情况下也不会检查表与表之间的依赖关系
所以为了保证表与表之间参照完整性,我们可以通过’外键’来保证参照完整性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
createtable stu( id int auto_increment primary key, name varchar(20), gender enum('男','女') )
createtable grade( id int auto_increment primary key, km varchar(20), score double, uid int )
insertinto stu values (null,'zs','男') insertinto stu values (null,'ls','女')
alter table 从表名称 add foreign key(外键字段名称) references 主表名称(主表主键名称);
create table grade( id int auto_increment primary key, km varchar (20), score double, uid int ); // alter table grade add foreign key(uid) references stu(id); insert into grade values (null,"语文',100,3);
createtable grade2( id int auto_increment primarykey, km varchar (20), score double, uid int, foreign key (uid) references stu (id) ondeletesetnull )// 主表删除,从表**引用置空**, insertinto grade values (null,"语文',100,3); update stu set id=3 where id=1;// 失败
create table grade3( id int auto_increment primarykey, km varchar (20), score double, uid int, foreign key (uid) references stu (id) on update cascade )// 主表删除,从表引用置空, insert into grade3 values (null,"语文',100,3); delete from stu where id=1;
select*from stu orderby id DESC,score asc; // id 一样,按照score升序
聚合函数
1.统计计算
1 2 3 4 5 6 7 8 9 10 11 12 13 14
count() 统计 selectcount(*) from stu
sum() 求和 selectsum(id) from stu
avg()求平均值 selectAVG(id) from stu
max() 获取最大值 selectmax(id) from stu
min()获取最小值 selectmin(id) from stu
2.数值类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
rand()生成随机数 select rand() from dual; select*from stu orderby rand()
round()四舍五入 select round(1.2) from dual;
ceil() 向上取整 selectceil(1.2) from dual;
floor() 向下取整 selectfloor(1.2) from dual;
truncate() 截取小数位 selecttruncate(1.234567,2) from dual;
3.字符串类
1 2 3 4 5 6 7 8 9 10 11 12 13 14
ucase():#转换为大写 select ucase('hello') from dual;
lcase();#转换为小写 select lcase('HELLO') from dual;
left();#从左边指定位置开始截取到 selectleft('1234567890',3) from dual; //123
right();#从右边指定位置开始截取 selectright('1234567890',3) from dual; //890
substring();#从指定位置开始截取指定个字符 selectsubstring('1234567890',3,5) from dual;//34567,第三个开始,截取5个
数据分组
数据分组 group by
1 2
select 分组字段|| 聚合函数 from 表名 groupby 分组字段:
1 2
统计表中一共有多少stuId SELECT stuId FROM rel
1 2
统计表中一共有多少stuId,根据stuId分组 SELECT stuId FROM rel GROUPBY stuId
1 2
每个id有多少人 SELECT stuId,COUNT(*) FROM rel GROUPBY stuId
注意点
在对数据进行分组的时候,select 后面必须是分组字段或者聚合函数,否则就只会返回第一条数据
1 2 3 4 5
elect city from stu group by city;
select name from stu group by city;
select city, group_concat(name) from stu group by city;
条件查询 having
having和where很像都是用来做条件查询的
但是where是去数据库中查询符合条件的数据,而having是去结果集中查询符合条件的数据
1 2 3 4
select * from stu where id=1 // 从数据库查找
select name from stu where id=1// 有结果
1 2 3 4
select * from stu having id=1 // 从select * from stu 的结果集查找
select name from stu having id=1// 没有结果
1 2 3 4 5 6 7
需求:select city from stu group by city:
需求:select city, avg (score) from stu group by city;
需求:select city, avg (score) as average from stu group by city;
需求:select city, avg (score) as average from stu group by city having average>=60
数据分页
1.分页 limit:
1
select 字段 from 表 limit 索引,个数;
查询选项
select[查询选项]字段名称 from 表名:
1 2 3
select [查询选项] 字段名称 from 表名: all 显示所有查询出来的数据[默认] distinct 去除结果集中重复的数据之后再显示
1 2
select all stuId from rel select distinct stuId from rel
注意
如果是通过distinct来对结果集中重复的数据进行去重,那么只有所有列的数据都相同才会去重
1 2 3
select distinct * from rel // 全部 select distinct stuId from rel // stuId 去重
1 2 3 4
完整的查询语句
select[查询选项]宁段名称 [from 表名〕[where 条件〕 Lorder by 排序〕[group by 分组] [having 条件] [limit分页]
多表查询
1.多表查询
多表查询只需要在单表查询基础上增加一张表即可
1
select * from 表名1,表名2;
注意点:
默认情况下多表查询的结果是笛卡尔集
2.union
在纵向上将多张表的结果结合起来返回给我们
1
select * from 表名1 union select * from 表名2
注意
使用union进行多表查询,返回的结果集的表头的名称是第一张表的名称
使用union进行多表查询,必须保证多张表查询的字段个数一致
使用union进行多表查询,默认情况下会自动去重
使用union进行多表查询,如果不想自动去重,那么可以在union后面加上all
1 2
select * from 表名1 union all select * from 表名2 // 不去重
表的连接
1.表的连接查询
将多张表中’关联的字段’’连接’在一起查询我们称之为’表的连接查询’
大白话:查询多张表中满足条件的数据
1
select * from stu, grade where stu.id = grade. stuld:
1.1内连接 inner join
在内连接中只会返回满足条件的数据
在进行多表查询的时候,如果想查询指定的字段,那么必须在字段名称前面加上表名才行
1 2 3
select * from 表名1 inner join 表名2 on 条件; select * from stu inner join grade on stu. id = grade. stuld; select stu.id,stu.name,grade.scorefrom stu inner join grade on stu. id = grade. stuld;
1.2外连接
1.2.1左外连接 left join
在左外连接中,左边的表是不看条件的,无论条件是否满足,都会返回左边表中所有的数据
在左外连接中,只有右边的表会看条件,对于右边的表而言,只有满足条件才会返回对应的数据
1 2 3
#select stu.id, stu.name, grade. score from stu left join grade on stu.id=grade.stuld: 在以上的查询语句中stu表在左边,grade表在右边 所以stu表不看条件,只有grade表看条件
1.2.2右外连接 right join
在右外连接中,左边的表是不看条件的,无论条件是否满足,都会返回左边表中所有的数据
在右外连接中,只有左边的表会看条件,对于左边的表而言,只有满足条件才会返回对应的数据 /
1 2 3 4
#select stu。 id, stu。 name, grade。 score from stu right join grade on stu。 id = grade。 stuld; 在以上的查询语句中stu表在左边,grade表在右边
所以stu表看条件,只有grade表不看条件
1.3交叉连接 Cross join
如果没有指定条件,那么返回笛卡尔集
如果指定了条件,那么就等价于内连接
1.4全连接 full join (MySQL不支持全连接)
自然连接
1.自然连接 (natural)
自然连接是用来简化’内连接和外连接’的
如果多张表需要判断的条件字段名称一致,那么不用编写条件,自然连接会自动判断
1.1自然内连接
1 2 3 4
select * from 表名1 inner join 表名2 on 条件; select * from stu inner join grade on stu.id=grade.stuId; stu 改为 stuId select * from stu naturl join grade;
CREATEVIEW person_view ASSELECT name, city FROM person WHERE score >=60;
SELECT*FROM person_view
1 2
SELECT name, score FROM Stu INNERJOIN grade on stu.stuld = grade.stuId; createview stu_view asSELECT name, score FROM Stu INNERJOIN grade on stu.stuld = grade.stuId;
SELECT name, city, score FROM person WHERE score >=60; CREATEVIEW person_view ASSELECT name, city, score FROM person WHERE Score >=60; // 随便编辑 INSERTINTO person_view values('it666','台湾',33)
如果想让插入和更新的数据必须符合创建视图的条件,那么就可以在创建视图的时候添加限制条件
1 2 3 4 5
SELECT name, city, score FROM person WHERE score >=60; CREATEVIEW person_view ASSELECT name, city, score FROM person WHERE Score >=60withcheck option; // 下面的会失败,不符合条件 INSERTINTO person_view values('it666','台湾',33)
SELECT city FROM person GROUPBY city; SELECT city, avg(score) as avgScore FROM person GROUPBY city; CREATEVIEW person_view AsSELECT city, avg(score) as avgScore FROM person GROUPBY city;
select*from person_view; // 更新平均分将会失败 UPDATE person_View set avgScore=11WHERE city='北京';
set@stuId=2; set@stuName=''; select*from stu where id=@stuId; select name into@stuNamefrom stu where id=@stuId; select@stuNamefrom dual;
3.局部变量
局部变量只能在存储过程和函数中定义,所以也称之为存储过程变量
default 可以设置默认值
1 2 3 4 5 6 7 8 9 10
CREATEPROCEDURE show_stu() BEGIN declare stuId2 int; declare stuName varchar(255); set stuId2 =2; select name into stuName from stu where id=@stuId; SELECT*FROM stu WHERE id=stuId; select stuName from dual; END call show_stu();
Mysql 储存过程深入
1.存储过程参数:
MySQL存储过程中的参数分为:
in 输入参数[默认]
out 输出参数
inout 输入输出参数
示例一:输入参数
外界传递给我们的参数
1 2 3 4 5
CREATEPROCEDURE show_stu_by_id(in stuId int) BEGIN
SELECT*FROM stu WHERE id=stuId; END
示例二:输出参数
存储过程中返回给外界的参数
MySQL存储过程中不能使用return返回值,需要通过参数来向外返回值
1 2 3 4 5 6 7 8
CREATEPROCEDURE show_stu_by_id(in stuId int,out stuName varchar(255)) BEGIN SELECT name into stuName FROM stu WHERE id=stuId; END
createfunction fun_add(a int,b int) returnsintDETERMINISTIC begin
declare sum intdefault0; set sum =a+b; return sum;
end;
1 2 3 4 5 6
createfunction check_stu(stuId int) returnsvarchar(255) DETERMINISTIC begin declare stuName varchar(255) default''; select name into stuName from stu where id= stuId; return stuName; end
条件语句
1.IF语
1 2 3 4 5 6 7
if 条件表达式 then ... elseif 条件表达式 then ... else ... end if;
示例一:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
createfunction fun_test(age int) returnsvarchar(255) DETERMINISTIC begin
declareresultvarchar(255) default''; if age>=18then setresult='成年人';
begin declareresultvarchar(255) default''; if score<0|| score >100then setresult='没有这个分数'; elseif score <60then setresult='不及格'; elseif score <80then setresult='良好'; else setresult='优秀'; end if; returnresult; end;
// 使用 SELECT fun_test2(2) from DUAL;
2.CASE语句
1 2 3 4 5 6 7
case
when 条件表达式 then ... when 条件表达式 then ... endcase:
1 2 3 4 5 6 7 8 9 10 11 12
createfunction check_stu(score int) returnsvarchar(255) DETERMINISTIC begin declareresultvarchar(255) default''; case
when score=100then result='keeping'; when score=0then result='not keeping'; endcase: returnresult; end
3.循环语句
1 2 3
while 条件表达式 do ... end while;
示例:1+ n的和/ 1+ 2+3+4+5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
createfunction fun_test6(num int) returnsintDETERMINISTIC begin declare sum intdefault0; declare currentIndex intdefault1; while currentIndex <= num do set sum=sum+currentIndex; set currentIndex=1+currentIndex;
end while;
return sum; end;
// 使用 select fun_test6(4) from DUAL;
repeat 循环
1 2 3
repeat ... until 条件表达式 end repeat;
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
createfunction fun_test6(num int) returnsintDETERMINISTIC begin declare sum intdefault0; declare currentIndex intdefault1; repeat set sum=sum+currentIndex; set currentIndex=1+currentIndex; until currentIndex > num end repeat;
return sum; end;
// 使用 select fun_test6(4) from DUAL;
批量数据处理
需求:往数据库里存储一万条数据实现方案:
1.写一万条insert into语句
2.将insert into语句封装到存储过程或者函数中
将来怎么使用?
是配合其它SQL语句使用,还是单独使用
单独使用–存储过程
配合其它SQL语句使用一-自定义函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14
createprocedure add_status(num int) begin DECLARE currentId intDEFAULT0; DECLARE currentAge intDEFAULT0; DECLARE currentName VARCHAR(255) DEFAULT''; while currentId<num do set currentId=currentId+1; set currentAge=FLOOR(RAND()*30); set currentName= concat('it',currentAge); INSERTINTO stu VALUES(currentId,currentName,currentAge); end WHILE; end; // 调用 call CALL add_status(10000);
createprocedure add_status3(num int) begin DECLARE currentId intDEFAULT0; DECLARE currentAge intDEFAULT0; DECLARE currentName VARCHAR(255) DEFAULT''; set autocommit=0; while currentId<num do set currentId=currentId+1; set currentAge=FLOOR(RAND()*30); set currentName= concat('it',currentAge); INSERTINTO stu VALUES(currentId,currentName,currentAge); end WHILE; commit; end; // 调用 call CALL add_status3(10000);
set autocommit=0; commit;
等到循环结束以后再去执行sql语句
注意点:
只要在循环前面加上set autocommit = 0;,在循环后面加上commit;
那么就不会生成一条插入语句就执行一条插入语句了
会等到所有的插入语句都生成之后,再统一的解析,统一的编译,统一的执行
优化方式2
预处理不能使用局部变量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
createprocedure add_status4(num int) begin set@currentId=0; set@currentAge=0; set@currentName=''; prepare test from'INSERT INTO stu VALUES(?,?,?);'; while @currentId<num do set@currentId=@currentId+1; set@currentAge=FLOOR(RAND()*30); set@currentName= concat('it',@currentAge); execute test using@currentId,@currentName,@currentAge; end WHILE; end; // 调用 call CALL add_status4(10000);
两者结合
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
createprocedure add_status4(num int) begin set@currentId=0; set@currentAge=0; set@currentName=''; prepare test from'INSERT INTO stu VALUES(?,?,?);'; set autocommit=0; while @currentId<num do set@currentId=@currentId+1; set@currentAge=FLOOR(RAND()*30); set@currentName= concat('it',@currentAge); execute test using@currentId,@currentName,@currentAge; end WHILE; commit; end; // 调用 call CALL add_status4(10000);