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 多表联合查询

交叉连接查询

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

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

相关推荐
wefg135 分钟前
【MySQL】事务
数据库·mysql
黑白极客42 分钟前
mysql的高可用性
数据库·mysql
爱笑鱼1 小时前
Handler(一):post 之后,Runnable 到底在哪个线程执行?
android
木木子222 小时前
[特殊字符] 音乐播放器——状态驱动的多媒体控制
android·开发语言·华为·php·harmonyos
雨白2 小时前
C 语言基础:结构体、联合体与枚举
android
heimeiyingwang3 小时前
【架构实战】MySQL索引优化:从慢查询到毫秒响应
数据库·mysql·架构
阿巴斯甜3 小时前
Android 代码混淆
android
渣渣灰飞4 小时前
MySQL 系统学习 第二阶段 第四章:DCL(Data Control Language)第二节:权限管理与角色(Role)
数据库·学习·mysql
宠友信息5 小时前
多端即时通讯源码技术实践,Redis路由、MySQL持久化与Socket接入
spring boot·websocket·mysql·uni-app
渣渣灰飞5 小时前
MySQL 系统学习 第四阶段:MySQL 高级 第一节:索引(Index)
数据库·学习·mysql