当前位置:网站首页>Advanced use of MySQL database
Advanced use of MySQL database
2022-08-03 10:31:00 【丁Jiaxiong】
24. MySQLAdvanced database usage
文章目录
24.1 条件查询
24.1.1 聚合函数

作用
- Treat the current table as a group for statistics
24.1.2 group 分组查询
group by
将查询结果按照1个或多个字段进行分组,字段值相同的为一组
concat Data in the same group can be queried
知识要点
group by
- 将查询结果按照1个或多个字段进行分组,字段值相同的为一组
group by + group_concat()
- 根据分组结果,使用group_concat()来放置每一个分组中某字段的集合
group by + 聚合函数
group by + having
group by + with rollup
- Add a new row to the data table,record the result of the operation(一般是汇总结果)
24.1.3 limit 分页(限制)查询
24.1.4 连接查询(数据来自多张表)
内连接
- The result of the query is the data matched by the two tables that meet the conditions

外连接
左外连接
- 查询的结果为两个表匹配到的数据和左表特有的数据(对于右表中不存在的数据使用null填充)
右外连接
- 查询的结果为两个表匹配到的数据和右表特有的数据(对于左表中不存在的数据使用null填充)

自连接
Only one table needs to be used
- 加快查询速度,Reduce data table footprint

24.1.5 子查询
Treat the result of one query as a condition of another query
分类
- 标量子查询
- 列子查询
- 行子查询
24.2 外键

24.3 视图
One can put complexitySQLA virtual table that encapsulates the functionality of a statement
24.3.1 优点
- 方便操作
- 减少复杂sql语句
- 增强可读性
- 增强复用性
24.3.2 视图的使用
创建
查看
使用
24.4 事务
A series executed as a basic unit of workSQL语句的操作,要么完全地执行,要么完全地不执行.
24.4.1 四大特性 ACID
A 原子性
- 一个事务必须被视为一个不可分割的最小工作单元,整个事务中的所有操作要么全部提交成功,要么全部失败回滚
C 一致性
- 数据库总是从一个一致性的状态转换到另一个一致性的状态
I 隔离性
- 一个事务所做的修改在最终提交之前,对其他事务是不可见的
D 持久性
- 一旦事务提交,则其所做的修改会永久保存到数据库
24.4.2 事务的使用
开启
- begin
- start transaction
提交
- commit
回滚
- rollback
24.5 索引
Quickly find clues in your data
24.5.1 本质
- 一种特殊的文件,包含着对数据表里所有记录的位置信息
24.5.2 使用
查看
创建
删除
24.5.3 优缺点
优点
- 加快数据的查询速度
缺点
- Creating indexes wastes time and takes up disk space,And it will take more and more time as the amount of data increases
24.5.4 使用原则
- Avoid using too many indexes for tables with frequent data updates
- Tables with a small amount of data do not need to use indexes
- Tables with large amounts of data and infrequent data changes can use indexes
24.6 设计范式
Different specification requirements are called different paradigms
各种范式呈递次规范,越高的范式数据库冗余越小
24.6.1 划分
第一范式 1NF
- 字段的原子性,That is, a field cannot be divided into several other fields
第二范式 2NF
满足1NF 的基础上
- 表必须有一个主键
- 非主键字段必须完全依赖于主键,而不能只依赖于主键的一部分
第三范式 3NF
满足2NF 的基础上
- 非主键字段必须直接依赖于主键,不能存在传递依赖
巴斯-科德范式 BCNF
第四范式 4NF
第五范式 5NF
24.7 E-R模型
实体-关系模型
- Describes the structural model for storing data in a database
24.7.1 表现形式
- 实体 矩形
- 属性 椭圆
- 关系 菱形
24.7.2 三种关系
- 一对一
- 一对多
- 多对多
24.8 Python 连接MySQL数据库
pymysql 模块
使用步骤
- 导包
- 创建连接对象
- 获取游标对象
- 增删改查
- 关闭游标和连接
24.9 SQL语句参数化
24.9.1 SQL 注入
- 用户提交带有恶意的数据与SQL语句进行字符串方式的拼接,从而影响SQL语句的语义,Eventually a data leak occurs
24.9.2 防止SQL注入
24.9.3 参数化
- SQL语句中的参数使用%s来占位
- 将SQL语句中%s占位所需要的参数存在一个列表中,把参数列表传递给execute()方法
边栏推荐
猜你喜欢
随机推荐
GBase 8c与openGauss是什么关系?
投稿有礼,双社区签约博主名额等你赢!
C language two-dimensional array is called with one-dimensional array
Scrapy + Selenium 实现模拟登录,获取页面动态加载数据
Mysql OCP 74 questions
消费者认可度较高 地理标志农产品为啥“香”
从餐桌到太空,孙宇晨的“星辰大海”
Regulation action for one hundred days during the summer, more than 700 traffic safety hidden dangers were thrown out
Scapy的介绍(一)「建议收藏」
go泛型使用方法
训练双塔检索模型,可以不用query-doc样本了?明星机构联合发文
DOM0、DOM2、DOM3 事件
跨域问题的分析
免费的mysql数据库管理工具_易语言快速导入MySQL数据库
历史拉链数据处理有人做过吗
VL53L0X V2激光测距传感器 采集距离数据串口输出
报告:想学AI的学生数量已涨200%,老师都不够用了
select statement in go
被审稿人吐槽没有novelty!深度学习方向怎么找创新点?
数字藏品和ICP















