MySQL进阶

⼀、MySQL 函数

数据分析的基础

  1. 排序

  2. max

  3. min

  4. 汇总

  5. count

  6. sum

  7. avg

  8. 数制

  9. ⼆进制

  10. 0 0

  11. 1 1

  12. 2 10

  13. 3 11

  14. 4 100

  15. ⼋进制

  16. ⼗进制

  17. ⼗六进制

  18. 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

相关推荐
2501_916008893 小时前
Web 前端开发常用工具推荐与团队实践分享
android·前端·ios·小程序·uni-app·iphone·webview
我科绝伦(Huanhuan Zhou)4 小时前
MySQL一键升级脚本(5.7-8.0)
android·mysql·adb
怪兽20145 小时前
Android View, SurfaceView, GLSurfaceView 的区别
android·面试
龚礼鹏6 小时前
android 图像显示框架二——流程分析
android
玩机达人886 小时前
2025年新版ADB工具箱下载+驱动+ADB指令集+fastboot刷机ROOT工具
adb
消失的旧时光-19436 小时前
kmp需要技能
android·设计模式·kotlin
帅得不敢出门7 小时前
Linux服务器编译android报no space left on device导致失败的定位解决
android·linux·服务器
雨白8 小时前
协程间的通信管道 —— Kotlin Channel 详解
android·kotlin
TimeFine9 小时前
kotlin协程 容易被忽视的CompletableDeferred
android
czhc114007566311 小时前
Linux1023 mysql 修改密码等
android·mysql·adb