子在川上曰,逝者如斯夫。

回顾

安***r> 解压版安装mysql zip版本

  1. 解压缩,进入解压目录
  2. cmd> bin\mysqld --initialize --console 初始化
  3. 添加my.ini 配置文件
[mysqld]
character-set-server=utf8mb4
  1. cmd> bin\mysqld install [服务名] 会把mysql安装为windows系统服务
  2. cmd> bin\mysql -u用户名 -p
  3. mysql> alter user ‘root’@‘localhost’ identified by ‘新密码’;

SQL语句

1. DDL 数据定义语言

create database 数据库名;
create table 表名(列定义); (重点)
drop database 数据库名;
drop table 表名;
alter table... (添加列, 修改列, 删除列, 重命名列8.0才有)
alter user 用户
create -- 创建xx定义, drop -- 删除xx定义, alter -- 修改xx定义

添加列
语法:alter table 表名 add 列名 数据类型;
例如:给student新增一个age列

alter table stduent add age tinyint unsigned;

修改列
语法:alter table 表名 modify 列名 新类型;
例如要修改列的长度定义原来varchar(10)

alter table student modify name varchar(20);

删除列
语法:alter table 表名 drop 列名;

重命名列
语法:alter table 表名 rename column 旧列名 to 新列名;

2. DML (数据操控语言) (重点)

insert

语法1:

insert into 表名(...) values(...);  插入一行

语法2:

insert into 表名(...) values(...), (...), (...), (...);  插入多行
create table student2(
    id int primary key,
    name varchar(20),
    sex char(1)
);

语法3:从表1查询,把查询结果插入表2

insert into2 select * from1;

如果两张表结构不一样,可以在select后加具体的列名,以便和新表的列相匹配
例如:

create table student3(
    id int primary key,
    name varchar(20)
);
insert into student3 select id,name from student;

load data

可以把外部文本文件的内容导入到数据库表中
语法:load data infile ‘文件路径\文件名字’ into table 表名;

create table hero(
    id int primary key,
    name varchar(10),
    loc varchar(10),
    sex char(1),
    birth int,
    death int,
    power int
);

要让load data命令生效,必须修改设置:

[mysqld]
character-set-server=utf8mb4
secure-file-priv=

其中secure-file-priv默认是null值,表示不允许加载文件
可以改为具体目录名,表示只能从这个目录加载文件
如果改为"",表示可以从任意目录加载文件

例如:加载之前heroes.txt,把数据存入hero:

load data infile 'e:\\heroes.txt' into table hero;

如果文件中的列分隔符是, 不是默认\t 键,需要用 fields TERMINATED BY来指定分隔符

load data infile 'e:\\person.txt' into table person fields TERMINATED BY ',';

source

source 文件路径/文件名
  • 其文件内容必须是合法的sql语句
  • 与load的区别:不用引号,建议使用/分隔路径,文件编码与操作系统编码一致(gbk)

update 更新

语法:

update 表名 set 列名=新值 where 条件;
update person set sex='男';             // 把所有记录性别都改为男
update person set sex='男' where id=1;  // 只修改id=1的性别为男

delete 删除

语法:

delete from 表名; // 删除表中所有记录(危险操作)
delete from 表名 where 条件; // 删除满足条件的记录

select 查询

语法:

select 列名... fromwhere 条件 group by 分组条件 having 分组筛选条件 order by 排序条件 limit;

条件 where

= 等值匹配
!= 不等值匹配
> 大于
< 小于
>= 大于等于
<= 小于等于

逻辑运算符组合多个条件
逻辑与(两个条件同时成立) and 例如:

select * from hero where sex='女' and loc='建业'; 

逻辑或(两个条件有一个成立,结果就是真) or

select * from hero where name='小乔' or id=200;
逻辑非 (条件取反) not 

列 between 值1 and 值2 等价于 列 >= 值1 and 列 <= 值2, 注意小值要在前面,包含边界的值
列 in (值1,值2,… 值n) 等价于 列=值1 or 列=值2 … or 列=值n 注意值列表的长度
like 模糊查询 其中匹配通配符 % 表示匹配0~多个任意字符
通配符 _ 表示匹配1个任意字符
例如:

select * from hero where power between 85 and 90;
select * from hero where power >= 85 and power <=90;
not in
not like
not between ... and

排序条件

排序条件:列名 升降序 如果升降序关键字省略,默认是asc
升序-> 由小到大 asc
降序-> 由大到小 desc

select * from hero order by power desc limit 10;

多列排序: 排序条件1, 排序条件2 …
先按照条件1排序,条件1中取值相同的,再按照条件2排序

限制返回结果个数

limit m;    // 最多返回m个结果
limit n,m;  // 最多返回m个结果,n代表起始下标,下标从0开始

经常用来实现分页应用,假设每页10条

第一页 limit 0,10;
第二页 limit 10,10;
第三页 limit 20,10;

分组条件

select count(*),max(sal),min(sal),sum(sal),avg(sal),deptno from emp group by deptno;
count(*)  表示求每组的个数
max() 求最大值
min() 求最小值
sum() 求和
avg() 求平均值

分组之后,

  • select子句中只能出现分组条件列和组函数,其他列不能出现在select中,
  • order by 子句中只能出现分组条件列和组函数,其他列不能出现在order by中,
    例如:
    select deptno,max(sal),ename from emp group by deptno; // ename不符合刚才的规定
    select deptno,max(sal) from emp order by ename; // 错误的

having 也是过滤

where > group by > having > select > order by > limit // sql语句的执行顺序
select count(*), deptno from emp where count(*) >=5 group by deptno; // 因为where先执行,这时候还没有分组,不知道个数,错误
select count(*), deptno from emp group by deptno having count(*)>=5;

有时候筛选条件既可以写在where 上,也可以写在having (优先采用where)

select count(*), deptno from emp where deptno=10 or deptno=30 group by deptno;
select count(*), deptno from emp group by deptno having deptno=10 or deptno=30;

多列分组 (了解)

多个列取值都相同的分为一组

group by1,列2 ...
select count(*),deptno,job from emp group by job,deptno;

多列分组时,列的顺序不影响结果

多表结构和连接查询

select ... from1 inner join2 on 连接条件 
    where group by having order by limit;
select empno,ename,sal,emp.deptno,dept.deptno,dname,loc from emp inner join dept on emp.deptno = dept.deptno;

表1 表1别名

select empno,ename,sal,e.deptno,d.deptno,dname,loc 
    from emp e inner join dept d on e.deptno = d.deptno;

±------±-------±--------±-------+ ±-------±-----------±---------+
| empno | ename | sal | deptno | | deptno | dname | loc |
±------±-------±--------±-------+ ±-------±-----------±---------+
| 7369 | SMITH | 800.00 | 20 | | 20 | RESEARCH | DALLAS |
| 7499 | ALLEN | 1600.00 | 30 | | 30 | SALES | CHICAGO |
| 7521 | WARD | 1250.00 | 30 | | 30 | SALES | CHICAGO |
| 7566 | JONES | 2975.00 | 20 | | 20 | RESEARCH | DALLAS |
| 7654 | MARTIN | 1250.00 | 30 | | 30 | SALES | CHICAGO |
| 7698 | BLAKE | 2850.00 | 30 | | 30 | SALES | CHICAGO |
| 7782 | CLARK | 2450.00 | 10 | | 10 | ACCOUNTING | NEW YORK |
| 7788 | SCOTT | 3000.00 | 20 | | 20 | RESEARCH | DALLAS |
| 7839 | KING | 5000.00 | 10 | | 10 | ACCOUNTING | NEW YORK |
| 7844 | TURNER | 1500.00 | 30 | | 30 | SALES | CHICAGO |
| 7876 | ADAMS | 1100.00 | 20 | | 20 | RESEARCH | DALLAS |
| 7900 | JAMES | 950.00 | 30 | | 30 | SALES | CHICAGO |
| 7902 | FORD | 3000.00 | 20 | | 20 | RESEARCH | DALLAS |
| 7934 | MILLER | 1300.00 | 10 | | 10 | ACCOUNTING | NEW YORK |
±------±-------±--------±-------+

±-------±-----------±---------+
| deptno | dname | loc |
±-------±-----------±---------+
| 10 | ACCOUNTING | NEW YORK |
| 20 | RESEARCH | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
±-------±-----------±---------+

几种连接查询

1 inner join2 on 连接条件  (内连接:两张表的记录必须完全满足连接条件,才会出现在最后结果中)1 left outer join2 on 连接条件  (左外连接)1 right outer join2 on 连接条件  (右外连接)
select empno, ename, e.deptno, d.deptno, d.dname, d.loc 
    from emp e left outer join dept d on d.deptno=e.deptno;

left outer join 位于连接左侧的表,不管是否连接到了记录,都会出现在结果中
符合连接条件的记录,和内连接效果一样
不符合连接条件的记录,对应另一张表的列都是null值

right outer join 位于连接右侧的表,不管是否连接到了记录,都会出现在结果中

outer可以省略

连接查询的等价写法

  • 内连接的等价写法 (重要)
select ... from1,2 where 连接条件;
select e.empno,e.ename,e.deptno,d.deptno,d.dname from emp e, dept d where e.deptno=d.deptno;
  • mysql 独有的 (了解)
select ... from1 inner|left join2 using(deptno); // 两张表的连接列名要相同
select e.empno,e.ename,e.deptno,d.deptno,d.dname from emp e inner join dept d using(deptno);

常用函数

select count(*) from emp; // 求整张表的行数
select max(sal) from emp; // 求整张表的工资最大值

Bit Functions 位运算函数
Comparison operators 比较运算符
Control flow functions 流程控制
Date and Time Functions 日期函数

    year() 截取年份
    month()
    date()
    date_add(日期 时间间隔); 其中时间间隔的语法:interval n 单位
    select empno,ename,date_add(hiredate, interval 1 month ),hiredate from emp; 加一个月
    select empno,ename,date_add(hiredate, interval 3 day ),hiredate from emp;3SELECT EXTRACT(DAY_MINUTE FROM '2009-07-02 13:02:03'); 提取日期中的从天到分钟的部分
    select now() 获取当前时间

Encryption Functions 加密
Information Functions
Logical operators 逻辑运算符
Miscellaneous Functions 剩余的函数
Numeric Functions 数学函数

    rand() 生成一个从[0.0 ~ 1.0) 之间的随机小数, 小于1.0
    floor() 舍去小数
    round() 四舍五入

String Functions 字符串函数

    left(字符串, n)  n代表从左边要截取的字符
    lower()
    upper()
    substr(字符串,下标, 长度) 下标从1开始
    求字符串长度的例子:select * from hero where char_length(name)=4;

导出数据

  1. cmd > mysqldump -uroot -p 库名 >> 要保存的文件.sql (source的逆操作, 内部是sql语句)
  2. 把表中的数据保存到文本文件中 (load data infile的逆操作)
    select * from 表 into outfile ‘文件路径\文件名’