当前位置:网站首页>MySQL database combat (1)
MySQL database combat (1)
2022-08-03 10:50:00 【wrdoct】

一、数据库概述
(1)sql、DB、DBMS分别是什么,他们的关系?
DB:DataBase(数据库,数据库实际上在硬盘上以文件的形式存在).
DBMS:DataBase Management System(数据库管理系统,常见的有:MySQL, Oracle, DB2, Sybase, SqlServer…).
SQL:结构化查询语言,是一门标准通用的语言.标准的sql适合于所有的数据库产品.SQL属于高级语言.SQL语句在执行的时候,实际上内部也会先进行编译,然后再执行sql.(sql语句的编译由DBMS完成).
DBMS负责执行sql语句,通过执行sql语句来操作DB当中的数据.
DBMS - (执行)->SQL -(操作)->DB
(2)什么是表?
表:table
是数据库的基本组成单元,所有的数据都以表格的形式组织,目的是可读性强.
【注】一个表包括行和列:
行:被称为数据/记录(data)
列:被称为字段(column)
(3)每一个字段应该包括哪些属性?
字段名、数据类型、相关的约束.
【注】数据类型:int、varchar、bigint…
(4)学习MySQL主要还是学习通用的SQL语句,包括增删改查,那么SQL怎么分类?
DQL(数据查询语言):查询语句,凡是select语句都是DQL.
DML(数据操作语言):insert、delete、update,对表当中的数据进行增删改.
DDL(数据定义语言):create、drop、alter,对表结构的增删改.
TCL(事务控制语言):commit提交事务,rollback回滚事务.
DCL(数据控制语言):grant授权、revoke撤销权限等.
(5)导入数据:
第一步:登录mysql数据库管理系统
命令:mysql -uroot -p
第二步:查看有哪些数据库
命令:show databases;
【注】这个不是SQL语句,属于MySQL的命令.
第三步:创建属于我们自己的数据库
命令:create database start;
【注】这个不是SQL语句,属于MySQL的命令.
第四步:使用start数据
命令:use start;
【注】这个不是SQL语句,属于MySQL的命令.
第五步:查看当前使用的数据库中有哪些表?
命令:show tables;
【注】这个不是SQL语句,属于MySQL的命令.
第六步:初始化数据
命令:source sql脚本;
【注】sql脚本:一个文件,以sql结尾,The file to write a lot ofsql语句.使用source命令执行sql脚本.
(6)删除数据库 :drop database start;
(7)查看表结构:desc 表名;
(8)常用命令:
select database(); #查看当前使用的是哪个数据库
select version(); #查看mysql的版本号
\c #结束一条语句
exit #退出mysql
show create table 表名; #查看创建表的语句
二、简单查询、条件查询、模糊查询
(1)简单的查询语句(DQL):
语法格式:
select 字段名1,字段名2,......from 表名;
select 字段名1,字段名2,... as ... from 表名;
【注】任何一条sql语句以“;”结尾.
sql语句不区分大小写.
标准sqlStatement requires a string(汉字)(varchar)使用单引号.
查询所有字段:select * from 表名;
(2)条件查询
条件查询需要用到where语句,where必须放到from语句表的后面.
语法格式:
select 字段名1,字段名2,......from 表名 where 条件;
【注】条件:between ... and ...; 必须左小右大,可以使用在数字方面,也可以使用在字符串方面,为左闭右开.
【注】在数据库中,NULL不是一个值,代表什么也没有,为空(空不是一个值,不能用等号衡量,必须使用 is null 或者 is not null).
【注】当运算符的优先级不确定的时候加小括号.
【注】in 等同于 or .(inThe end of the specific value is the,不是区间)
(3)模糊查询like
在模糊查询中,必须掌握两个特殊的符号:一个是%,一个是_.% 代表任意多个字符,_代表任意1个字符.
【注】在%和_With escape characters\,表示一个普通的%和_.
三、排序数据(升序、降序)
(1)默认是升序.若要指定:asc表示升序,desc表示降序.
select ... from ... order by ... asc, ... desc;
【注】越靠前的字段越能起到主导作用.只有当前面的字段无法完成排序的时候,Will start at the back of the field to sort.
select ... from ... order by 1; #表示按照第1The order of columns of sorts
select ... from ... order by 4; #表示按照第4The order of columns of sorts
四、分组函数/聚合函数/多行处理函数
(1)分组函数一共5个.又叫:多行处理函数(输入多行输出一行).
count 计数
sum 求和
avg 平均值
max 最大值
min 最小值
【注】所有分组函数都是对“某一组”数据进行操作的.
【注】分组函数自动忽略NULL.
(2)单行处理函数:输入一行输出一行.
【注】数据库中只要有NULL参与运算,最终结果一定是NULL.
ifnull(可能为NULL的数据, 被当做什么来处理); #属于单行处理函数
(3)Matters needing attention when using the grouping function:
分组函数不可以直接使用在where子句当中.
因为group by是在where执行之后才会执行,而Group functions must be exercised only after the group.
即:If the group functions directly use inwhere子句当中,Representative haven't group performed grouping function.
(4)count(*)和count(具体的某个字段)有什么区别?count(*):不是统计某个字段中数据的个数,而是统计总记录条数(With the field it doesn't matter);count(具体的某个字段):Said the statistics in this field不为NULL的数据总数量.
(5)分组函数也能组合起来使用.
五、分组查询
(1)group by 和 havinggroup by :According to a field or certain fields进行分组;having:After the grouping data再次过滤.
select ... from 表名 group by ... having 条件; #效率较低
select ... from 表名 where 条件 group by ...; #效率较高(能使用where尽量使用where)
【注】分组函数一般都会和group by联合使用,这也是为什么它被称为分组函数的原因.
并且任何一个分组函数都是在group by语句执行结束之后才会执行的.
当一条sql语句没有group by的话,整张表的数据会自成一组.
(2)规则:当一条sql语句中出现group by时,select后面只能跟分组函数和参与分组的字段.
(3)多个字段联合分组:
select ... from ... group by 字段1, 字段2;
(4)一个完整的DQL语句:
Function of the written order and execution order:
select 5
...
from 1
...
where 2
...
group by 3
...
having 4
...
order by 6
...
六、关于查询结果集的去重
select distinct 字段 from 表名; # distinct 关键字
【注】distinct 只能出现在所有字段的最前面,Said to unite behind all of the fields are to heavy.
七、连接查询
(1)在实际开发中,大部分情况下都不是从单表中查询数据,Are generally more table联合查询取出最终的结果.
(2)连接查询的分类:
According to the division s:SQL92、SQL99.
根据表的连接方式来划分:
内连接:等值连接、非等值连接
外连接:左外/左连接、右外/右连接
全连接:
(3)笛卡尔积现象:当两张表进行连接查询时,没有过滤/条件限制,Eventually join query result would beThe product of the two tables to record the number.
【注】避免笛卡尔积现象:加条件进行过滤.
但是不会减少记录的匹配次数,只不过显示的是有效记录(Won't improve the execution efficiency).
【注】表的别名:执行效率高,可读性好.
(4)内连接之等值连接:
最大的特点:条件是等量关系.
#SQL92 (太老,不用了)
select e.ename, d.dname from emp e, dept d where e.deptno = d.deptno;
#SQL99 (常用的) (Table join condition andwhereSeparation of data filter conditions,结构清晰)
#语法: ...A join B on 连接条件 where ...
select e.ename, d.dname from emp e join dept d on e.deptno = d.deptno;
select e.ename, d.dname from emp e inner join dept d on e.deptno = d.deptno; #inner可以省略,With the words of readability is better
(5)内连接之非等值连接:
最大的特点:条件是非等量关系.
(6)自连接:
最大的特点:一张表看做两张表,自己连接自己.
(7)外连接:
什么是外连接?和内连接的区别?
内连接:
假设A表和B表进行连接,使用内连接的话,凡是A表和B表能够匹配上的记录查询出来,这就是内连接.
A、B两张表没有主副之分,两张表是平等的.
外连接:
假设A表和B表进行连接,使用外连接的话,A、B两张表中有一张表是主表,一张表是副表,
The main query of the main table data,捎带着查询副表,
When the side tables of the data in the table data is not and matching,副表自动模拟出NULL与之匹配.
外连接的分类?
左外/左连接:On the left side of this form is the main table;
右外/右连接:The right of this table is the main table.
左连接有右连接的写法,右连接也会有对应的左连接的写法.
#outer可以省略
select 表名 from ... left join ... on ...; #左连接 fromAt the back of the is on the left side of the 主表
select 表名 from ... left outer join ... on ...; #左连接 fromAt the back of the is on the left side of the 主表
select 表名 from ... right join ... on ...; #右连接 joinAt the back of the is the right of the 主表
select 表名 from ... right outer join ... on ...; #右连接 joinAt the back of the is the right of the 主表
【注】To distinguish the internal connection and external connection is the main difference betweenleft和right.
【注】外连接最重要的特点是:主表的数据无条件的全部查询出来.
(8)全连接
(9)三张表的连接查询
...
A
join
B
join
C
on
...
【注】navicat工具箱:可以更方便的操作MySQL.
边栏推荐
猜你喜欢
随机推荐
build --repot
如何改变sys_guid() 返回值类型
Advanced use of MySQL database
安全研究员:大量Solana钱包被盗
【LeetCode—第2题 两数之和 代码详解 】附有源码,可直接复制
Skills required to be a good architect: How to draw a system architecture that everyone will love?What's the secret?Come and open this article to see it!...
ScrollView嵌套RecyclerView滚动冲突
QT with OpenGL(Shadow Mapping)(面光源篇)
VL53L0X V2 laser ranging sensor collects distance data serial output
Web Server 设置缓存响应字段的一些推荐方案
微信多开批处理(自动获取安装路径)
LeetCode_二分搜索_简单_367.有效的完全平方数
synchronized
pixel手机升系统
numpy
创建C UDR时,指定的HANDLESNULLS的作用是什么?
This article understands the process from RS485 sensor to IoT gateway to cloud platform
如何通过DBeaver 连接 TDengine?
训练双塔检索模型,可以不用query-doc样本了?明星机构联合发文
Leecode-SQL 1667. 修复表中的名字









