MySQL进阶

⼀、MySQL 函数

数据分析的基础

1. 排序

1. max

2. min

2. 汇总

1. count

2. sum

3. avg

3. 数制

1. ⼆进制

1. 0 0

2. 1 1

3. 2 10

4. 3 11

5. 4 100

6.

2. ⼋进制

3. ⼗进制

4. ⼗六进制

5. AA 27

1、聚合函数

只有 select ⼦句和 having ⼦句、order by ⼦句中能使⽤聚合函数,where ⼦句不能使⽤聚合函数。当使⽤聚合查询以后,不能使⽤where条件,如果要添加条件,就使⽤having

mysql> create table 学⽣表(序号 int,姓名 char(16),年

龄 int,班级 int);

Query OK, 0 rows affected (0.01 sec)

#新建⼀个学⽣表,表头有:序号,数字形式;姓名,⽂本字符串形式;年龄,数字形式;班级,数字形式。

mysql> desc 学⽣表;

mysql> select avg(年龄) from 学⽣表;

mysql> select avg(年龄) from 学⽣表 group by 班级;

mysql> select avg(年龄) 平均年龄,班级 from 学⽣表group by 班级;

mysql> select avg(年龄),班级 from 学⽣表 group by 班级;

mysql> select min(年龄) 最⼩年龄,班级 from 学⽣表group by 班级;

mysql> select count(*) 总⼈数,班级 from 学⽣表 groupby 班级;

  • MySQL ⼦语句查询

⼦查询是指⼀个查询语句嵌套在另⼀个查询语句内部的查询;

该查询语句可以嵌套在⼀个 SELECT、SELECT...INTO、INSERT...INTO 等语句中。

在执⾏查询时,⾸先会执⾏⼦查询中的语句,再将返回的结果作为外层查询的过滤条件。

在⼦查询中通常可以使⽤⽐较运算符和 IN、EXISTS、ANY、ALL 等关键字。

例如:select * from class where cid=(select classid from student where sname='张三');

mysql> create table class(cid int(4) not null primary key, cname varchar(20));

mysql> create table student (sid int(4) not nullprimary key, sname varchar (20), sage int (2),classid int (4) not null);

mysql> select * from class where cid=(select classid from student where sname='张三');

mysql> select * from class where cid>(select classid from student where sname='张三');

2、exists 关键字的⼦查询

mysql> select * from class where cid=(select

classid from student where sname='张三');

+------+-------+

| cid | cname |

+------+-------+

| 1001 | Java |

+------+-------+

1 row in set (0.00 sec)

#查询张三同学所在班级的信息

mysql> select * from class where cid>(select

classid from student where sname='张三');

+------+---------+

| cid | cname |

+------+---------+

| 1002 | C++ |

| 1003 | Python |

| 1004 | PHP |

| 1005 | Android |

+------+---------+

4 rows in set (0.03 sec)

# 查询⽐张三同学所在班级编号还⼤的班级的信息

2、exists 关键字的⼦查询

exists 关键字后⾯的参数可以是任意⼀个⼦查询, 它不产⽣任何数据只返回 true 或 false。⽽当返回值为 true 时外层查询才会执⾏。

相当于内层句⼦是⼀个判断句式。

mysql> select * from class where exists (select * from student where sname='王五');

  • MySQL 多表联合查询
  1. 交叉连接查询

交叉连接返回的结果是被连接的两个表中所有数据⾏的笛卡尔积;

mysql> select * from student cross join class;

mysql> select sname,cname from student inner join class on student.classid=class.cid;

  • MySQL 授权

mysql> grant all on *.* to haha@'192.168.100.%' identified by '123456'; # 为haha⽤户进⾏授权

mysql> show grants for haha@'192.168.100.%'; # 查看haha的权限

mysql> drop user haha@'192.168.100.%'; # 删除⽤户

  • MySQL 触发器

1、概念

触发器是⼀种特殊的存储过程,它在插⼊,删除或修改特定表中的数据时触发执⾏,它⽐数据库本身标准的功能有更精细和更复杂的数据控制能⼒

1. 例如在某⼀个时间触发什么事情

2. 例如不允许股票价格的升幅⼀次超过%10

3. 审计功能,某⼀个⼈登录会记录所有的操作

#查看数据库内的所有表

mysql> show tables;

mysql> create trigger deltable after delete on class for each row delete from student;

#查看触发器

mysql> show triggers\G;

#查看当前数据库下的所有数据表

mysql> show tables;

#删除class表中cid为1005的整⾏数据,也就是执⾏之前设置的触发动作

mysql> delete from class where cid=1005;

#因为触发器执⾏,所以student表的内容在class表的删除动作后被清空

mysql> select * from student;

#删除触发器

mysql> drop trigger deltable;

  • MySQL 基本优化操作

[root@localhost ~]# vim /etc/my.cnf

[mysqld]

skip-name-resolve #添加该⾏,表示本机跳过MySQL密码验证

skip-grant-tables #添加该⾏,表示登录时,忽略所有的⽤户认证信息,包括⽤户名、密码和权限。

[root@localhost ~]# systemctl restart mysqld.service

[root@localhost ~]# mysql #免密时,直接使⽤MySQL命令登录

mysql> use yh; #查看存储引擎时,必须要切换到某个数据库

mysql> show table status where name='class' \G;

#也可查看单个数据表的存储引擎

mysql> create table haha(id int) engine=MyISAM;

#创建表haha,并添加数字形式的表头id,指定haha表的存储引擎为MyISAM

mysql> show table status where name='haha' \G;

#查看haha表的存储引擎及其他状态信息

mysql> alter table haha engine=InnoDB; #修改已存在的数据表haha的存储引擎

[root@localhost ~]# vim /etc/my.cnf

[mysqld]

default-storage-engine=InnoDB #添加此⾏,当以后再创建表时,存储引擎将改为InnoDB

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值