MySQL

参考数据《MySQL必知必会》
数据库是一个以某种有组织的方式存储的数据集合。
数据库DB是通过数据库管理系统DBMS来创建和管理的。
表table是一种结构化的文件,可以用来存储某种特定类型的数据。
模式schema是关于数据库和表的布局及特性的信息。
列column是表中的一个字段。
正确地将数据分解为多个列极为重要。
数据类型datatype定义列可以存储的数据种类。
表中的数据是按行row存储的。
主键primary key 一列或一组列,其值能够唯一区分表中每一行。
主键列不允许null值
SQL是一种专门用来和数据库通信的语言。
数据的所有存储,检索,管理和处理实际上都是由DBMS完成的,MySQL是一种DBMS,即它是一种数据库软件。
MySQL,Oracle,SQL server等数据库都是基于C/S架构的数据库。
熟悉mysql命令行实用程序
MySQL客户端工具:SQLyog navicat DBeaver
为了连接到MySQL,需要提供主机名,端口号,合法的用户名和密码
mysql -uroot -p
show databases;

58615c5e03ea0603058d55e021ac3f34.png  
use bzbh;

fc4d63e3eba1620771b3012ae27ed3fb.png  
show tables;

show column from customers;

318bf2392b8d95b899208317de903446.png  
自动增量
show create database bzbh;

d603a27ba7af584933f144d63858073e.png  
show create table customers;

show grants;

fe74f1fce48d26ffe3bf093ab9d11d6d.png
show errors;

73a896305868ed07d7033baa4775f294.png  
show warnings;

help show;
检索单列
select prod_name from products;

9ba4335849efbdb6f454dbbe241a04cb.png  
SQL语句不区分大小写,但建议关键字大写,其他小写
检索多列
select prod_id,prod_name,prod_price from products;

d07be4616e06d8b299c40364e058368e.png  
检索所有列 *
select * from products;

71cf686af46cb0880b9497b04f8c5831.png  
检索不同的行 distinct
select distinct vend_id from vendors;

205950de0de029a857701b5539ef6e1c.png  
distinct应用于所有列而不仅是前置它的列
限制结果 limit
select prod_name from products limit 3;

085cd8bd477cc78e3a69a0b7435fb805.png  
指定要检索的开始行和行数,第一个数为开始位置,第二个数为要检索的行数,检索出来的第一行为行0而不是行1,
select prod_name from products limit 2,5;

ad1a184421afb1584955a09f1d6e398b.png  
使用完全限定的表名(指定列所属的表)
select products.prod_name from bzbh.products limit 5;

60c4d93a2ef1fbe1eb896cb25e2cfdd5.png  
排序检索数据 order by
order by子句取一个或多个列的名字,据此对输出结果集进行排序
select prod_name,prod_price from products order by prod_price,prod_name;

78a59ec47a8a2b1706a38d7ec4f06d10.png  
需要理解指定多列排序时,仅在多行具有相同的prod_price值时才对产品按prod_name进行排序,如果prod_price列中的所有值都是唯一的,就不会按prod_name排序了
排序方向模式是升序asc,降序要指定使用desc,如果想在多个列上进行降序排序,必须对每个列指定desc关键字
使用order by和limit组合,能够找出一个列的最高或最低的值
select prod_name,prod_price from products order by prod_price desc limit 1;

bf3c2194bcd0a280b69472228a8276d2.png  
过滤数据 where子句
select prod_name,prod_price from products where prod_price = 2.50;

image.png  
where子句操作符:= <> != > < >= <= between...and... not in like % _ and or
select prod_name,prod_price from products where prod_price between 5 and 10;

35a69ca8a478f732e247bdc19f3aee2b.png  
空值检查 is null
select cust_id from customers where cust_email is null;

8b2087391e6a23012d51804a4925e522.png  
select prod_id,prod_name,prod_price from products where vend_id = 1003 and prod_price <= 10;

b3ecfeb825bf607bfbb632c1b2bb10c7.png  
and操作符优先级高于or,复杂的查询条件可以使用括号来明确分组
select prod_id,prod_name,prod_price from products where vend_id in (1002,1003) order by prod_name;

645d71319fc4139f4fcbd577ea08b2f6.png  
select prod_id,prod_name,prod_price from products where vend_id not in (1002,1003) order by prod_name;

1a2469b1c70da700bb6c1707ee1e32ce.png  
select prod_id,prod_name from products where prod_name like 'jet%';

7c70b91b4951d5afa2c7700db12b0ab3.png  
select prod_id,prod_name from products where prod_name like '_ ton anvil';

1d121781e79bb6b4f7c3106965a371f2.png  
不要过度使用通配符
用正则表达式进行搜索
正则表达式的作用是匹配文本,将一个模式(正则表达式)与一个文本串进行比较
MySQL仅支持多数正则表达式实现的一个很小的子集
下面查询prod_name中包含文本1000的所有行
select prod_name from products where prod_name regexp '1000' order by prod_name;

982b1a6de63ec98f34a69843cf62dd07.png  
select prod_name from products where prod_name regexp '.000' order by prod_name;

817083281561896c87c13bebab556227.png  
句点 . 在正则表达式中表示匹配任意一个字符
like匹配整个列值,而regexp在列值内进行匹配
MySQL的正则表达式匹配不区分大小写,如果需要区分,可以使用关键字binary
select prod_name from products where prod_name regexp binary 'JetPack .000' order by prod_name;

7b2e32ad5ca74b0318137ef18b2e6e5f.png

like 和 regexp还是有区别的,下面查询用like查询,是没有返回结果的

3d7fca045044eea0036dd358ddbe4cc8.png

正则表达式条件或 |
select prod_name from products where prod_name regexp '1000|2000' order by prod_name;

0125c9b04e1386f821269af19dccb99a.png  
匹配几个字符之一 [ ]
select prod_name from products where prod_name regexp '[123] Ton' order by prod_name;

6bc1aa3e52dd40d63465e58c5528fa71.png  
事实上,正则表达式[123]Ton 和 [1|2|3]Ton一样
[^123] 取反
[0-9] 范围
如果要匹配特殊字符本身,需要使用\\为前导,例如 \\- \\. \\\
匹配字符类,可以使用预定义字符集
[:alnum:] 匹配字母和数字 等价于 [a-zA-Z0-9]
[:alpha:] 匹配字符 等价于[a-zA-Z]
[:digit:] 匹配任意数字 等价于[0-9]
[:lower:]
[:upper:]
重复元字符:* + ? {n} {n,} {n,m}

863918ad71797127cb369d8226993361.png  

649324194ff8931903fb7509c1c6ee62.png  

MySQL中的正则表达式转义使用两个反斜杠\\
select prod_name from products where prod_name regexp '\\([0-9] sticks?\\)' order by prod_name;

592ec5cf0570675550a8f6fd440a495f.png  

正则表达式的内容以后会专门写一篇笔记

拼接字段 concat()函数

select concat(vend_name,' (', vend_country, ')') from vendors order by vend_name;

使用别名 AS

 

 

SQL支持利用函数来处理数据

文本处理函数:left()  length()  locate()  lower()   ltrim()   right()   rtrim()   soundex()   substring()   upper()

日期和时间处理函数:adddate()   addtime()   curdate()   curtime()   date()   datediff()  date_add()  date_format()   day()   dayofweek()   hour()   minute()    now()   second()    time()   year()  

MySQL的日期格式必须为yyyy-mm-dd

orders表中的order_date的数据类型为datetime,这种类型会存储日期和具体时间

 

 

 

 

查询2005年9月份的所有订单信息

 

 

以上查询有个问题需要考虑,就是你必须清除9月份的结尾是几号

还有一种办法,你不需要知道单月具有有多少天,使用year() 和 month()函数

 

数值处理函数:abs()   cos()  exp()   mod()   pi()   rand()   sin()   sqrt()    tan()

汇总数据

聚合函数:sum()  avg()  max()  min()  count()

 

avg()忽略列值为Null的行

count(*) 计数 不忽略null值  和  count(column) 对特定列具有值的行计数

 

 

 

 使用distinct

 

组合使用聚合函数

 

分组数据  group  by 和 having

分组允许把数据分为多个逻辑组,以便能对每个组进行聚合计算

 

where过滤行,having过滤分组

where在数据分组前进行过滤,having在数据分组后进行过滤

 

子查询

 

 

 

 

 

592ec5cf0570675550a8f6fd440a495f.png592ec5cf0570675550a8f6fd440a495f.png

posted on 2020-10-20 16:20  peter_liuyu  阅读(55)  评论(0编辑  收藏  举报

导航