当前位置:网站首页>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()方法
边栏推荐
猜你喜欢
With strong network, China mobile to calculate excitation surging energy network construction
This article understands the process from RS485 sensor to IoT gateway to cloud platform
VL53L0X V2 laser ranging sensor collects distance data serial output
Regulation action for one hundred days during the summer, more than 700 traffic safety hidden dangers were thrown out
Mysql OCP 27题
2022T电梯修理考试题及答案
苏州大学:从PostgreSQL到TDengine
以网强算,中国移动算网建设激发澎湃能量
创建C UDR时,指定的HANDLESNULLS的作用是什么?
C# Color颜色RGB对照表、颜色选择器
随机推荐
Mysql OCP 72 questions
从餐桌到太空,孙宇晨的“星辰大海”
出色的移动端用户验证
使用 Scrapy 框架对重复的 url 无法获取数据,dont_filter=True
Scrapy + Selenium implements simulated login and obtains dynamic page loading data
QSplitter(分离部件)
oracle计算同、环比
Leecode-SQL 1527. 模糊查询匹配(模糊查询用法)
流水线设计的方法和作用「建议收藏」
像用户体验设计师一样思考
Leecode-SQL 1484. 按日期分组销售产品
OS层面包重组失败过高,数据库层面gc lost 频繁
DOM对象能干什么?
混合型界面:对话式UI的未来
【学习笔记之菜Dog学C】通讯录
OPENCV学习DAY7
LeetCode_二分搜索_简单_367.有效的完全平方数
Mysql OCP 75 questions
Scapy的介绍(一)「建议收藏」
孙宇晨式“溢价逻辑”:不局限眼前,为全人类的“星辰大海”大胆下注