目录
一、按关键字排序
1.使用ORDER BY语句来实现排序
2.排序可针对一个或多个字段
3.ASC:升序,默认排序方式
4.DESC:降序
6.ORDER BY的语法结构
select column1,column2... from 库名 order by column1,column2,... asc|desc;
mysql> create database tanwenlong;
mysql> create table test(xuehao char(10) not null,nianling int(3) not null,xingming char(36) not null,chengji varchar(3) not null);
mysql> insert into test values(201001,17,'zhangsan',60),(201002,18,'lisi',70),(201003,19,'wangwu',80),(201004,18,'zhaoliu',88),(201005,17,'lilei',55);
默认升序
mysql> select chengji from test order by chengji;
降序
mysql> select chengji from test order by chengji desc;
加入条件的排序
mysql> select xingming,chengji from test where chengji>=60 order by chengji desc;
多条件的排序
mysql> select * from test order by nianling desc,chengji desc;
二、对结果进行分组
1.使用GROUP BY语句来实现分组
2.通常结合聚会函数一起使用
3.可以按一个或多个字段对结果进行分组
4.GROUP BY的语法结构
select column_name,aggregate_function(column_name) from table_name where column_name operator value GROUP BY column_name;
mysql> select count(xingming),chengji from test where chengji>=60 group by nianling;
+-----------------+---------+
| count(xingming) | chengji |
+-----------------+---------+
| 1 | 60 |
| 2 | 70 |
| 1 | 80 |
+-----------------+---------+
3 rows in set (0.01 sec)
mysql> select count(xingming),chengji from test where chengji>=50 group by nianling order by count(xingming) desc;
+-----------------+---------+
| count(xingming) | chengji |
+-----------------+---------+
| 2 | 70 |
| 2 | 60 |
| 1 | 80 |
+-----------------+---------+
3 rows in set (0.00 sec)
三、限制结果条目
1.只返回SELECT查询结果的第一行或前几行
2.使用LIMIT语句限制条目
3.LIMIT语法结构
select column1,column2,... from table_name limit[offset,] number;
mysql> select * from test;
+--------+----------+----------+---------+
| xuehao | nianling | xingming | chengji |
+--------+----------+----------+---------+
| 201001 | 17 | zhangsan | 60 |
| 201002 | 18 | lisi | 70 |
| 201003 | 19 | wangwu | 80 |
| 201004 | 18 | zhaoliu | 88 |
| 201005 | 17 | lilei | 55 |
| 201006 | 18 | lili | 90 |
+--------+----------+----------+---------+
6 rows in set (0.00 sec)
前三个
mysql> select * from test limit 3;
+--------+----------+----------+---------+
| xuehao | nianling | xingming | chengji |
+--------+----------+----------+---------+
| 201001 | 17 | zhangsan | 60 |
| 201002 | 18 | lisi | 70 |
| 201003 | 19 | wangwu | 80 |
+--------+----------+----------+---------+
3 rows in set (0.00 sec)
第一个数字:位置偏移量是从零开始,第二个数字:返回记录行的最大数目
第三到第五
mysql> select * from test limit 2,3;
+--------+----------+----------+---------+
| xuehao | nianling | xingming | chengji |
+--------+----------+----------+---------+
| 201003 | 19 | wangwu | 80 |
| 201004 | 18 | zhaoliu | 88 |
| 201005 | 17 | lilei | 55 |
+--------+----------+----------+---------+
3 rows in set (0.00 sec)
四、设置别名
1.使用AS语句设置别名,关键字AS可省略
2.设置别名时,保证不能与库中其他表或字段名称冲突
3.别名的语法结构
select column_name AS alias_name from table_name;
select column_name(s) from table_name AS alias_name;
举例
原字段或表名不会被改变
mysql> select xuehao as 学号,nianling as 年龄,xingming as 姓名,chengji as 成绩 from test;
+--------+--------+----------+--------+
| 学号 | 年龄 | 姓名 | 成绩 |
+--------+--------+----------+--------+
| 201001 | 17 | zhangsan | 60 |
| 201002 | 18 | lisi | 70 |
| 201003 | 19 | wangwu | 80 |
| 201004 | 18 | zhaoliu | 88 |
| 201005 | 17 | lilei | 55 |
| 201006 | 18 | lili | 90 |
+--------+--------+----------+--------+
6 rows in set (0.00 sec)
as 可以省略,表也可以设置别名,结果和上述一样
mysql> select xuehao 学号,nianling 年龄,xingming 姓名,chengji 成绩 from test;
mysql> select cj.xuehao 学号,cj.nianling 年龄,cj.xingming 姓名,cj.chengji 成绩 from test as cj;
as的用法
mysql> select count(*) as number from test;
+--------+
| number |
+--------+
| 6 |
+--------+
1 row in set (0.00 sec)
mysql> select p.xuehao,p.xingming from test as p limit 2;
+--------+----------+
| xuehao | xingming |
+--------+----------+
| 201001 | zhangsan |
| 201002 | lisi |
+--------+----------+
2 rows in set (0.00 sec)
as作为连接语句,复制用法
mysql> create table abc as select * from test;
Query OK, 6 rows affected (0.01 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> select * from abc;
+--------+----------+----------+---------+
| xuehao | nianling | xingming | chengji |
+--------+----------+----------+---------+
| 201001 | 17 | zhangsan | 60 |
| 201002 | 18 | lisi | 70 |
| 201003 | 19 | wangwu | 80 |
| 201004 | 18 | zhaoliu | 88 |
| 201005 | 17 | lilei | 55 |
| 201006 | 18 | lili | 90 |
+--------+----------+----------+---------+
6 rows in set (0.00 sec)
五、通配符
1.用于替换字符串中的部分字符
2.通常配合LIKE一起使用,并协同WHERE完成查询
3.常用通配符
%表示零个,一个或多个
_表示单个字符
mysql> select xingming,chengji from test where xingming like '%n';
+----------+---------+
| xingming | chengji |
+----------+---------+
| zhangsan | 60 |
+----------+---------+
1 row in set (0.00 sec)
mysql> select * from test where xingming like 'z%';
mysql> select * from test where xingming like 'zhang_an';
mysql> select * from test where xingming like 'l_s_';
两者结合
mysql> select * from test where xingming like '%sa_';
六、子查询
1.也称作内查询或者嵌套查询
2.先于主查询被执行,其结果将作为外层主查询的条件
3.在增删改查中都可以使用子查询
4.支持多层嵌套
5.IN语句是用来判断某个值是否在给定的结果集中
用法:
mysql> select xuehao,xingming,chengji from test where chengji in (select chengji from test where chengji>=60);
+--------+----------+--------+
| xuehao | xingming | chengji|
+--------+----------+--------+
| 201001 | zhangsan | 60 |
| 201002 | lisi | 70 |
| 201003 | wangwu | 80 |
| 201004 | zhaoliu | 88 |
| 201006 | lili | 90 |
+--------+----------+--------+
5 rows in set (0.00 sec)
降序:
mysql> select xuehao,xingming,chengji from test where chengji in (select chengji from test where chengji>=60 ) order by chengji desc;
mysql> create table source as select * from test;
mysql> delete from source;
mysql> select * from source;
Empty set (0.00 sec)
mysql> insert into source select * from test where chengji in (select chengji from test where chengji>=80);
mysql> select * from source;
+--------+----------+----------+---------+
| xuehao | nianling | xingming | chengji |
+--------+----------+----------+---------+
| 201003 | 19 | wangwu | 80 |
| 201004 | 18 | zhaoliu | 88 |
| 201006 | 18 | lili | 90 |
+--------+----------+----------+---------+
3 rows in set (0.00 sec)
修改:
mysql> alter table source add column num int(3); #添加一列num
mysql> desc source; #查表的结构
mysql> update source set num=101 where chengji in (select chengji from test where chengji >=80);
mysql> select * from source;
+--------+----------+----------+---------+------+
| xuehao | nianling | xingming | chengji | num |
+--------+----------+----------+---------+------+
| 201003 | 19 | wangwu | 80 | 101 |
| 201004 | 18 | zhaoliu | 88 | 101 |
| 201006 | 18 | lili | 90 | 101 |
+--------+----------+----------+---------+------+
3 rows in set (0.00 sec)
mysql> select * from (select * from test where chengji>=75)as a;
mysql> select * from (select * from test where chengji>=75)a;
mysql> select * from (select * from test where chengji>=75)a order by chengji desc;
mysql> delete from test where chengji in(select chengji from (select * from test where chengji >=75)a);
mysql> select * from test;
+--------+----------+----------+---------+
| xuehao | nianling | xingming | chengji |
+--------+----------+----------+---------+
| 201001 | 17 | zhangsan | 60 |
| 201002 | 18 | lisi | 70 |
| 201005 | 17 | tianqi | 55 |
+--------+----------+----------+---------+
3 rows in set (0.00 sec)
mysql> insert into source values(201008,18,'zhangsan',89,102);
后面条件为真则执行前面的
mysql> select count(num) from source where exists(select num from source where xingming='zhangsan');
七、NULL值
1.表示缺失的值
2.与数字0或者空白(spaces)是不同的
3.使用IS NULL或IS NOT NULL进行判断
4.NULL值和空值的区别
- 空值长度为0,不占空间;NULL值的长度为NULL,占用空间
- IS NULL无法判断空值
- 空值使用"=“或者”<>"来处理
- COUNT () 计算时,NULL会忽略,空值会加入计算
mysql> select * from student;
+----+----------+-------+
| id | name | score |
+----+----------+-------+
| 1 | xiaoming | 100 |
| 2 | xiaohong | 60 |
| 3 | xiaobai | 100 |
| 4 | lisi | 40 |
| 5 | zhangsan | 60 |
| 6 | xiaoliu | 70 |
| 7 | wangwu | 80 |
| 8 | lili | NULL |
+----+----------+-------+
8 rows in set (0.00 sec)
## count()计算时,空值NULL会忽略不计
mysql> select count(score) from student;
+--------------+
| count(score) |
+--------------+
| 7 |
+--------------+
1 row in set (0.00 sec)
## 查询student表中,score字段空值
mysql> select * from student where score is null;
+----+------+-------+
| id | name | score |
+----+------+-------+
| 8 | lili | NULL |
+----+------+-------+
1 row in set (0.00 sec)
## 查询student表中,score字段不是空值
mysql> select * from student where score is not null;
+----+----------+-------+
| id | name | score |
+----+----------+-------+
| 1 | xiaoming | 100 |
| 2 | xiaohong | 60 |
| 3 | xiaobai | 100 |
| 4 | lisi | 40 |
| 5 | zhangsan | 60 |
| 6 | xiaoliu | 70 |
| 7 | wangwu | 80 |
+----+----------+-------+
7 rows in set (0.00 sec)
八、正则表达式
1.根据指定的匹配模式匹配记录中符合要求的特殊字符
2.使用REGEXP关键字指定匹配模式
常用匹配模式
字符 | 说明 |
---|---|
^ | 匹配开始字符 |
$ | 匹配结束字符 |
. | 匹配任意单个字符 |
* | 匹配任意个前面的字符 |
+ | 匹配前面字符至少1次 |
p1|p2 | 匹配p1或p2 |
[…] | 匹配字符集中括号内的任何字符 |
[^…] | 匹配不在括号内的任何字符 |
{n} | 匹配前面的字符串n次 |
{n,m} | 匹配前面的字符串至少n次,至多m次 |
mysql> select * from test where xingming regexp '^z'; 以什么开头
mysql> select * from test where xingming regexp 'i$'; 以什么结尾
mysql> select * from test where xingming regexp 'zha';
mysql> select * from test where xingming regexp 'zha.gsan'; 匹配单个字符
mysql> select * from test where xingming regexp '^[z|l]'; 以z或l开头
mysql> select * from test where xingming regexp '^[^z|l]'; 不以z或l开头
mysql> select * from test where xingming regexp 'e{3,5}';
mysql> select * from test where xingming regexp 'ee*';
mysql> select * from test where xingming regexp '^[d-f]';
九、运算符
1.算术运算符
MySQL支持的算术运算符
+ | 加法 |
---|---|
- | 减法 |
* | 乘法 |
/ | 除法 |
% | 取余数 |
mysql> select 2+3,3-1,4*5,5/4,7%3;
+-----+-----+-----+--------+------+
| 2+3 | 3-1 | 4*5 | 5/4 | 7%3 |
+-----+-----+-----+--------+------+
| 5 | 2 | 20 | 1.2500 | 1 |
+-----+-----+-----+--------+------+
1 row in set (0.01 sec)
mysql> create table suanshu as select 2+3,3-1,4*5,5/4,7%3;
mysql> desc suanshu;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| 2+3 | int(3) | NO | | 0 | |
| 3-1 | int(3) | NO | | 0 | |
| 4*5 | int(3) | NO | | 0 | |
| 5/4 | decimal(5,4) | YES | | NULL | |
| 7%3 | int(1) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
2.比较运算符
1.等于运算符
用来判断数字、字符串和表达式是否相等的,如果相等则返回 1,如果不相等则返回 0。其中字符的比较是根据 ASCII 码来判断的。
mysql> select 1=1,1=3,'a'='a','a'='z';
+-----+-----+---------+---------+
| 1=1 | 1=3 | 'a'='a' | 'a'='z' |
+-----+-----+---------+---------+
| 1 | 0 | 1 | 0 |
+-----+-----+---------+---------+
1 row in set (0.00 sec)
2.不等于运算符
不等于号有两种写法,分别是<>或者!=,用于针对数字、字符串和表达式不相等的比较。如果不相等则返回 1,如果相等则返回 0。
需要注意的是不等于运算符不能用于判断 NULL。
mysql> select 1!=1, 'a'!='ab',1<>2,'a'<>null;
+------+-----------+------+-----------+
| 1!=1 | 'a'!='ab' | 1<>2 | 'a'<>null |
+------+-----------+------+-----------+
| 0 | 1 | 1 | NULL |
+------+-----------+------+-----------+
3.大于、大于等于、小于、小于等于运算符
mysql> select 'a'>'z',2<1,3>1;
+---------+-----+-----+
| 'a'>'z' | 2<1 | 3>1 |
+---------+-----+-----+
| 0 | 0 | 1 |
+---------+-----+-----+
1 row in set (0.01 sec)
4.IS NULL、IS NOT NULL
IS NULL 判断一个值是否为 NULL,如果为 NULL 返回 1,否则返回 0。
IS NOT NULL 判断一个值是否不为 NULL,如果不为 NULL 返回 1,否则返回 0。
mysql> select 1 is null ,'a' is not null ,null is null,null is not null;
+-----------+-----------------+--------------+------------------+
| 1 is null | 'a' is not null | null is null | null is not null |
+-----------+-----------------+--------------+------------------+
| 0 | 1 | 1 | 0 |
+-----------+-----------------+--------------+------------------+
5.between and
用于判断一个值是否落在某两个值之间
mysql> select 's' between 'a' and 'z','a' between 'v' and 'z';
+-------------------------+-------------------------+
| 's' between 'a' and 'z' | 'a' between 'v' and 'z' |
+-------------------------+-------------------------+
| 1 | 0 |
+-------------------------+-------------------------+
6.least、greatest
LEAST:当有两个或者多个参数时,返回其中的最小值。如果其中一个值为 NULL,则返回结果就为 NULL。
GREATEST:当有两个或者多个参数时,返回其中的最大值。如果其中一个值为 NULL, 则返回结果就为 NULL。
mysql> select least(11,43,112),greatest(11,43,112),least('f','s','r'),greatest('t','s','p');
+------------------+---------------------+--------------------+-----------------------+
| least(11,43,112) | greatest(11,43,112) | least('f','s','r') | greatest('t','s','p') |
+------------------+---------------------+--------------------+-----------------------+
| 11 | 112 | f | t |
+------------------+---------------------+--------------------+-----------------------+
1 row in set (0.00 sec)
7.in、not in
IN 判断一个值是否在对应的列表中,如果是返回 1,否则返回 0。
NOT IN 判断一个值是否不在对应的列表中,如果不是返回 1,否则返回 0。
mysql> select 's' in('a','s',1),1 in (1,2,'a'),3 not in (1,2,3);
+-------------------+----------------+------------------+
| 's' in('a','s',1) | 1 in (1,2,'a') | 3 not in (1,2,3) |
+-------------------+----------------+------------------+
| 1 | 1 | 0 |
+-------------------+----------------+------------------+
8.like、not like
LIKE 用来匹配字符串,如果匹配成功则返回 1,反之返回 0;NOT LIKE 正好跟 LIKE 相反。
LIKE 支持两种通配符:’%’ 用于匹配任意数目的字符,而’_’只能匹配一个字符。
mysql> select 'abc' like 'a%','abc' like 'a__';
+-----------------+------------------+
| 'abc' like 'a%' | 'abc' like 'a__' |
+-----------------+------------------+
| 1 | 1 |
+-----------------+------------------+
1 row in set (0.00 sec)
3.逻辑运算符
1.又称为布尔运算符
2.用来判断表达式的真假
3.常用的逻辑运算符
运算符 | 说明 |
---|---|
NOT或! | 逻辑非 |
AND或&& | 逻辑与 |
OR或|| | 逻辑或 |
XOR | 逻辑异或 |
逻辑非
mysql> select not 2,!3,not 0,!(4-4);
+-------+----+-------+--------+
| not 2 | !3 | not 0 | !(4-4) |
+-------+----+-------+--------+
| 0 | 0 | 1 | 1 |
+-------+----+-------+--------+
1 row in set (0.00 sec)
逻辑或
mysql> select 2 and 3,4 && 0,0 && NULL,1 and NULL;
+---------+--------+-----------+------------+
| 2 and 3 | 4 && 0 | 0 && NULL | 1 and NULL |
+---------+--------+-----------+------------+
| 1 | 0 | 0 | NULL |
+---------+--------+-----------+------------+
1 row in set (0.00 sec)
4.位运算符
1.对二进制进行计算的运算符
2.常用的位运算符
运算符 | 说明 |
---|---|
& | 按位与 |
| | 按位或 |
~ | 按位取反 |
^ | 按位异或 |
<< | 按位左移 |
>> | 按位右移 |
mysql> select 4&5,4|5,4&~3,3^4,2<<2,2>>1;
+-----+-----+------+-----+------+------+
| 4&5 | 4|5 | 4&~3 | 3^4 | 2<<2 | 2>>1 |
+-----+-----+------+-----+------+------+
| 4 | 5 | 4 | 7 | 8 | 1 |
+-----+-----+------+-----+------+------+
1 row in set (0.00 sec)
十、连接查询
MySQL的连接查询,通常都是将来自两个或多个表的行结合起来,基于这些表之间的共同字段,进行数据的拼接。首先,要确定一个主表作为结果集,然后将其他表的行有选择性的连接到选定的主表结果集上。使用较多的连接查询包括:内连接,左连接和右连接。
1.外连接:分为左连接和右连接。
左外连接就是以左表为准,去匹配右表,左表有多少条数据,结果就是多少条数据。左表有的数据正常显示,右表没有的数据就用NULL显示。
mysql> create table chengji(id int(3) not null primary key auto_increment,age int(3) not null,name varchar(64) not null,scrore decimal(4,1));
mysql> insert into chengji(age,name,scrore) values(17,'zhangsan',60),(18,'lisi',70),(18,'wangwu',80),(17,'zhaoliu',95),(19,'tianqi',55);
mysql> select * from chengji;
+----+-----+----------+--------+
| id | age | name | scrore |
+----+-----+----------+--------+
| 1 | 17 | zhangsan | 60.0 |
| 2 | 18 | lisi | 70.0 |
| 3 | 18 | wangwu | 80.0 |
| 4 | 17 | zhaoliu | 95.0 |
| 5 | 19 | tianqi | 55.0 |
+----+-----+----------+--------+
5 rows in set (0.00 sec)
mysql> create table sushe(id int(3) not null primary key,num int(3) not null);
mysql> insert into sushe values(1,305),(2,306),(3,307),(8,308),(9,309),(10,310);
mysql> select * from sushe;
+----+-----+
| id | num |
+----+-----+
| 1 | 305 |
| 2 | 306 |
| 3 | 307 |
| 8 | 308 |
| 9 | 309 |
| 10 | 310 |
+----+-----+
6 rows in set (0.00 sec)
2.内连接
mysql> select * from chengji c inner join sushe s on c.id=s.id;
+----+-----+----------+--------+----+-----+
| id | age | name | scrore | id | num |
+----+-----+----------+--------+----+-----+
| 1 | 17 | zhangsan | 60.0 | 1 | 305 |
| 2 | 18 | lisi | 70.0 | 2 | 306 |
| 3 | 18 | wangwu | 80.0 | 3 | 307 |
+----+-----+----------+--------+----+-----+
3 rows in set (0.00 sec)
左连接
mysql> select * from chengji c left join sushe s on c.id=s.id;
+----+-----+----------+--------+------+------+
| id | age | name | scrore | id | num |
+----+-----+----------+--------+------+------+
| 1 | 17 | zhangsan | 60.0 | 1 | 305 |
| 2 | 18 | lisi | 70.0 | 2 | 306 |
| 3 | 18 | wangwu | 80.0 | 3 | 307 |
| 4 | 17 | zhaoliu | 95.0 | NULL | NULL |
| 5 | 19 | tianqi | 55.0 | NULL | NULL |
+----+-----+----------+--------+------+------+
5 rows in set (0.00 sec)
右连接
mysql> select * from chengji c right join sushe s on c.id=s.id;
+------+------+----------+--------+----+-----+
| id | age | name | scrore | id | num |
+------+------+----------+--------+----+-----+
| 1 | 17 | zhangsan | 60.0 | 1 | 305 |
| 2 | 18 | lisi | 70.0 | 2 | 306 |
| 3 | 18 | wangwu | 80.0 | 3 | 307 |
| NULL | NULL | NULL | NULL | 8 | 308 |
| NULL | NULL | NULL | NULL | 9 | 309 |
| NULL | NULL | NULL | NULL | 10 | 310 |
+------+------+----------+--------+----+-----+
6 rows in set (0.00 sec)
十一、MySQL数据库函数
MySQL数据库函数
常用的函数分类
1.常用的数学函数
常用的数学函数 | 返回值 |
---|---|
abs(x) | 返回x的绝对值 |
rand() | 返回0到1的随机数 |
mod(x,y) | 返回x除以y以后的余数 |
power(x,y) | 返回x的y次方 |
round(x) | 返回离x最近的整数 |
round(x,y) | 保留x的y位小数四舍五入的值 |
sqrt(x) | 返回x的平方根 |
truncate(x,y) | 返回数字x截断为y位小数的值 |
ceil(x) | 返回大于或等于x的最小整数 |
floor(x) | 返回小于等于x的最大整数 |
greatest(x1,x2…) | 返回集合中最大的值 |
least(x1,x2…) | 返回集合中最小的值 |
2.常用的聚合函数
常用的聚合函数 | 返回值 |
---|---|
avg() | 返回指定列的平均值 |
count() | 返回指定列中非NULL值的个数 |
min() | 返回指定列的最小值 |
max() | 返回指定列的最大值 |
sum() | 返回指定列的所有值之和 |
3.常用的字符串函数
常用的字符串函数 | 返回值 |
---|---|
length(x) | 返回字符串x的长度 |
trim() | 返回去除指定格式的值 |
concat(x,y) | 将提供的参数x和y拼接成一个字符串 |
upper(x) | 将字符串x的所有字母变成大写字母 |
lower(x) | 将字符串x的所有字母变成小写字母 |
left(x,y) | 返回字符串x 的前y个字符 |
right(x,y) | 返回字符串x的后y个字符 |
repeat(x,y) | 将字符串x重复y次 |
space(x) | 返回x个空格 |
replace(x,y,z) | 将字符串z替代字符串x中的y字符串 |
strcmp(x,y) | 比较x和y,返回的值可以为-1,0,1 |
substring(x,y,z) | 获取从字符串x中的第y个位置开始长度为z的字符串 |
reverse(x) | 将字符串x反转 |
4.常用的日期时间函数
常用的日期时间函数 | 返回值 |
---|---|
curdate() | 返回当前时间的年月日 |
curtime() | 返回当前时间的时分秒 |
now() | 返回当前时间的日期和时间 |
month(x) | 返回日期x中的月份值 |
week(x) | 返回日期x是年度第几个星期 |
hour(x) | 返回x中的小时值 |
minute(x) | 返回x中的分钟值 |
second(x) | 返回s中的秒钟值 |
dayofweek(x) | 返回x是星期几,1是星期日,2是星期一 |
dayofmonth(x) | 计算日期x是本月的第几天 |
dayofyear(x) | 计算日期x是本年的第几天 |
十二、存储过程
1.简介:
- 从 5.0 版本才开始支持
- 是一组为了完成特定功能的SQL语句集合
- 比传统SQL速度更快,执行效率更高
- 存储过程的优点
1.执行一次后,会将生成的二进制代码驻留缓冲区,提高执行效率
2.SQL语句加上控制语句的集合,灵活性高
3.在服务器端存储,客户端调用时,降低网络负载
4.可多次重复被调用,可随时修改,不影响客户端调用
5.可完成所有的数据库操作,也可控制数据库的信息访问权限 - 为什么要用存储过程?
1.减轻网络负载
2.增加安全性
2.创建存储过程
使用CREATE PROCEDURE语句创建存储过程
创建存储过程的语法结构
CREATE PROCEDURE <过程名> (过程参数[...])<过程体> [过程参数[...]] 格式 [IN|OUT|INOUT] <参数名> <类型>
3.参数分为
输入参数:IN
输出参数:OUT
输入/输出参数:INOUT
4.存储过程的主体部分,被称为过程体
5.以BEGIN开始,以END结束,若只有一条SQL语句
6.以DELIMITER开始和结束
7.存储过程
mysql> delimiter $$
mysql> create procedure a()
-> begin
-> select * from chengji limit 3;
-> end $$
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> call a();
+----+-----+----------+--------+------+
| id | age | name | scrore | num |
+----+-----+----------+--------+------+
| 1 | 17 | zhangsan | 60.0 | NULL |
| 2 | 18 | lisi | 70.0 | NULL |
| 3 | 18 | wangwu | 80.0 | NULL |
+----+-----+----------+--------+------+
3 rows in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter $$
mysql> create procedure p(in num1 int,out num2 int,inout num3 int)
-> begin
-> select num1,num2,num3;
-> set num1=10,num2=20,num3=30;
-> select num1,num2,num3;
-> end $$
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> call p(@num1,@num2,@num3);
+------+------+------+
| num1 | num2 | num3 |
+------+------+------+
| 1 | NULL | 3 |
+------+------+------+
1 row in set (0.00 sec)
+------+------+------+
| num1 | num2 | num3 |
+------+------+------+
| 10 | 20 | 30 |
+------+------+------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
in和inout参数会将全局变量的值传入存储过程中,而out参数不会将全局变量的值传入存储过程中,在全局过程使用中,参数值in、out、inout都会发生改变
mysql> select @num1,@num2,@num3;
+-------+-------+-------+
| @num1 | @num2 | @num3 |
+-------+-------+-------+
| 1 | 20 | 30 |
+-------+-------+-------+
1 row in set (0.00 sec)
调用完存储过程后,发现in参数不会对全局变量的值引起变化,而out和inout参数调用完存储过程后,会对全局变量的值产生变化,会将存储过程引用后的值赋值给全局变量,in参数赋值类型可以是变量还有定值,而out和inout参数赋值类型必须是变量
8.修改存储过程
1、存储过程的修改分为特征修改和内容修改
2、特征修改的方法
ALTER PROCEDURE <过程名> [<特征>...]
3、内容修改可先删除原有存储过程,之后再创建方法
9.删除存储过程
1、删除存储过程的语法
DROP {
PROCEDURE | FUNCTION } [ IF EXISTS ] <过程名>
2、删除的过程
mysql> drop procedure a;
Query OK, 0 rows affected (0.00 sec)
mysql> call a();
ERROR 1305 (42000): PROCEDURE score.a does not exist