一.基础

1.mysql的常用数据类型

整数:int

小整数:tinyint

小数:decimal(5,2)小数2位,整数3位

字符串:varchar

日期时间:daytime

2.数据库中的元素

数据库—–database

表—-table

字段(列)—field

记录(行)—record

二.常用操作

1.创建表

  • 语法:create table 表名(字段名 字段类型, 字段名 字段类型)

  • -- 创建表 d,字段要求如下: 
    -- id : 数 据 类 型 为 
    -- int unsigned( 无 符 号 整 数 ) ) ,primary key( 主键),auto_increment(自增长); 
    -- name 姓名:数据类型为 varchar(字符串)长度为 10,not null(非空),
    -- age 年龄:数据类型为 int(整数),default(默认值)30;
    create TABLE d (
    id int UNSIGNED PRIMARY key auto_increment,
    name varchar(10) not null,
    age int DEFAULT 30);
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13

    ### 字段的约束

    #### 1.主键 (primary key)

    - 主键的值不能重复

    - 自增长,auto_increment,值会系统自动维护,自动增长

    - ```sql
    -- 如果不指定字段,主键自增长字段的值可以用占位符,0或者null
    INSERT into d VALUES (0, '康熙', 30);
    INSERT into d VALUES (NULL, '溥仪', 50);

2.非空(not null)

  • 这个字段必须有值,如果没有值,insert插入会失败

3.唯一 (unique)

  • 字段的约束为唯一,表示字段的值不能重复

4.默认值 (default)

  • 当一个字段有默认值约束,插入数据时,如果指定了值,那么默认值无效,如果没有指定值,会使用默认值

2.插入数据

  • 语法 : insert into 表名 values (值, 值,值);

  • insert into c values (1, '白起', 25);
    
    1
    2
    3
    4
    5
    6

    - `指定字段插入,语法:insert into 表名 (字段名, 字段名) values (值, 值);`

    - ```sql
    -- 例 2:表 c 插入一条记录,只设置 id 和姓名 name
    INSERT into c (id, name) values (3, '曹操');

插入多条记录

  • 多条insert语句,用分号隔开
1
2
3
4
-- 例 3:表 c 插入三条记录,写三条 insert 语句, 语句之间用分号隔开
INSERT into c values (5, '周瑜', 50);
insert into c (id, name) values (6, '鲁肃');
INSERT into c (name) values ('诸葛亮');
  • 一条insert插入多条记录
  • 语法:insert into 表名 values (值,值),(值, 值),(值,值);
1
2
3
4
-- 例 4:表 c 插入多条记录,用一条 insert 语句, 数据之间用逗号隔开
insert into c values (10, '张三', 10),
(11, '李四', 20),
(12, '王五', 30);
  • 一条insert指定字段插入多条记录
  • 语法:insert into 表名 (字段名, 字段名) values (值, 值), (值, 值), (值, 值)
1
2
3
4
-- 例 4:表 c 插入多条记录,用一条 insert 语句, 数据之间用逗号隔开
insert into c (id, name) values (13, '光绪'),
(14, '康熙'),
(15, '雍正');

3.select查询表

  • select * 或者select 字段名 控制了查询返回什么样的字段(列)
  • where 条件 控制了查询返回什么样的记录(行)

查询所有字段

语法: select * from 表名;

  • 查询表的所有字段
1
2
-- 查询表c的所有字段
select * from c;

指定字段名查询

语法:select 字段名, 字段名 from 表名;

1
2
-- 查询表c的id字段
select id from c;

字段的别名

  • 通过 字段名 as 别名 的语法,可以给字段起一个别名,别名可以是中文
  • as可以省略
  • 字段名 as 别名 和 字段名 别名 结果是一样的
1
2
3
4
5
-- 通过as 给字段起一个别名
select card as 身份证, name as 姓名,sex as 性别 from students;

-- 别名的as可以省略
select card 身份证, name 姓名,sex 性别 from students;

表的别名

  • 通过 表名 as 别名 给表起一个别名
  • as可以省略
1
2
3
4
5
-- 通过as 给表students起一个别名
select * from students as stu;

-- 可以省略as
select * from students stu;

3.distinct过滤重复记录

  • 通过select distinct 字段名, 字段名 from 表名 来过滤select查询结果中的重复记录
1
SELECT DISTINCT sex, class from students;

4.where子句

  • where 后面跟一个条件,实现有选择的查询
  • select * from 表名 where 条件
1
2
-- 例 1:查询 students 表中学号 studentNo 等于’001’ 的记录
select * from students where studentNo = '001';
1
2
-- 例 2:查询 students 表中年龄 age 等于 30 的姓名 name,班级 class
select name, class from students where age = 30;

比较运算符

  • =等于
  • < 小于
  • <= 小于等于
  • >大于
  • >=大于等于
  • !=和<>不等于

7.逻辑运算符

  • and与
    • 条件1 and 条件2
    • 两个条件必须都满足
  • or或
    • 条件1 or 条件2
    • 两个条件只要有一个满足即可
  • not非
    • not 条件
    • 条件成立,not以后就不成立,条件不成立,not以后就成立
1
2
3
4
5
6
7
8
9
10
11
-- 例 1:查询 age 年龄小于 30,并且 sex 性别为’女’的同学记录
SELECT * from students where age < 30 and sex = '女';

-- 例 2:查询 sex 性别为’女’或者 class 班级为'1 班'的学生记录
SELECT * from students where sex = '女' or class = '1班';

-- 例 3:查询 hometown 老家非’天津’的学生记录
SELECT * from students where not hometown = '天津';

-- 例 3:查询 hometown 老家’天津’的学生记录
SELECT * from students where not hometown != '天津';

8.模糊查询

  • like实现模糊查询
  • %代表任意多个字符
  • _代表任意一个字符
  • 字段名 like ‘字符%’
    • 指定字符开始,后面任意多个字符
1
2
3
4
5
-- 例 1:查询 name 姓名中以’孙’开头的学生记录
SELECT * from students where name like '孙%';

-- 例 2:查询 name 姓名以’孙’开头,且名只有一个字的学生记录
SELECT * from students where name like '孙_';
1
2
3
4
5
-- 例 3:查询 name 为任意姓,名叫’乔’的学生记录
SELECT * from students where name like '%乔';

-- 查询 name 姓名有’白’子的学生记录
SELECT * from students where name like '%白%';

9.范围查找

  • in (值, 值, 值)
    • 非连续范围查找
  • between 开始值 and 结束值
    • 连续范围查找,包含开始值 包含 结束值
1
2
3
4
5
6
7
-- 例 1:查询 hometown 家乡是’北京’或’上海’或’广东’的学生记录
SELECT * from students where hometown = '北京' or hometown = '上海' or hometown = '广东';
SELECT * from students where hometown in ('北京', '上海', '广东');

-- 例 2:查询 age 年龄为 25 至 30 的学生记录
SELECT * from students where age >= 25 and age <= 30;
SELECT * from students where age BETWEEN 25 and 30;

10.空判断

  • null不是0,也不是’’,null在SQL里面代表空,什么也没有
  • null不能用比较运算符的判断
  • is null —是否为null
  • is not null —是否不为null
    • 不能用 字段名 = null 字段名 != null这些都是错误的
1
2
3
4
5
-- 例 1:查询 card 身份证为 null 的学生记录
SELECT * from students where card is null;

-- 例 2:查询 card 身份证非 null 的学生记录
SELECT * from students where card is not null;

12.order by排序

  • order by 字段名 [asc/desc]
    • asc代表从小到大,升序,asc可以省略
    • desc代表从大到小,不可以省略
1
2
3
4
5
6
-- 例 1:查询所有学生记录,按 age 年龄从小到大排序

select * from students order by age asc;
select * from students order by age;
-- 例 2:查询所有学生记录,按 age 年龄从大到小排序
select * from students order by age desc;
  • 两个字段排序的例子
1
2
3
-- 例 2:查询所有学生记录,按 age 年龄从大到小排序,
-- 年龄相同时,再按 studentNo 学号从小到大排序
SELECT * from students ORDER BY age desc, studentNo;
  • 当一条select语句出现了where和order by
    • select * from 表名 where 条件 order by 字段1,字段2;
    • 一定要把where写在order by前面

13.聚合函数

count求select返回的记录总数

  • count(字段名)
1
2
3
4
5
6
7
8
9
10
-- 查询学生总数(查询stuents表有多少记录)
select count(*) from students;
select count(name) from students;
select count(DISTINCT class) from students;
select count(DISTINCT sex) from students;
-- 查询女同学数量
SELECT count(name) from students where sex = '女';
SELECT count(*) from students where sex = '女';
SELECT count(sex) from students where sex = '女';

max查询最大值

  • max(字段名)
  • 查询指定字段里的最大值
1
2
3
4
5
6
7
8
9
-- 查询students中的最大年龄
SELECT max(age) from students;

-- 查询students中的女生最大年龄
SELECT max(age) from students where sex = '女';


-- 查询students中的'1班'最大年龄
SELECT max(age) from students where class = '1班';

聚合函数不能用到where后面的条件里

min查询最小值

  • min(字段名)
  • 查询指定字段的最小值
1
2
3
4
5
6
7
8
9
-- 查询students中的最小年龄
SELECT min(age) from students;

-- 查询students中的女生最小年龄
SELECT min(age) from students where sex = '女';


-- 查询students中的'1班'最小年龄
SELECT min(age) from students where class = '1班';

sum求和

  • sum(字段名)
  • 指定字段的值求和
1
2
3
4
5
6
7
8
9
-- 查询students中的年龄总和
SELECT sum(age) from students;

-- 查询students中的女生年龄总和
SELECT sum(age) from students where sex = '女';


-- 查询students中的'1班'年龄总和
SELECT sum(age) from students where class = '1班';

avg求平均数

  • avg(字段名)
  • 指定字段的平均值
1
2
3
4
5
6
7
8
-- 查询students中的年龄总和
SELECT sum(age) from students;

-- 查询students中的女生年龄总和
SELECT sum(age) from students where sex = '女';

-- 查询students中的'1班'年龄总和
SELECT sum(age) from students where class = '1班';
  • avg的字段中如果有null,null不做为分母计算平均
1
2
3
create table aa (age int, name varchar(10));
insert into aa values (10, 'a'), (20, 'b'), (null, 'c');
select avg(age) from aa;-- 结果为15,而不是10

14.数据分组

  • group by 字段名
  • select 聚合函数 from 表名 where 条件 group by 字段
  • select 聚合函数 from 表名 group by 字段
  • group by就是配合聚合函数使用的
1
2
3
4
5
-- 分别查询男女同学的数量
SELECT count(*) from students where sex = '男';
SELECT count(*) from students where sex = '女';

select sex, count(*) from students group by sex;
  • group by的例子
1
2
3
-- 分别查询各个年龄段的同学数量

select age, count(*) from students group by age;
  • where与group by
1
2
3
-- 分别查询'1班'不同性别学生数量

select sex, count(*) from students where class = '1班' group by sex;
  • where和group by 和order by的顺序
    • select * from 表名 where 条件 group by 字段 order by 字段

15.分组聚合之后的数据筛选

  • having子句
  • 总是出现在group by之后
  • select * from 表名 group by 字段 having 条件
1
2
3
4
5
6
7
-- 用where查询男生总数
-- where先筛选复合条件的记录,然后在聚合统计
SELECT count(*) from students where sex = '男';

-- 用having查询男生总数
-- having先分组聚合统计,在统计的结果中筛选
SELECT count(*) from students GROUP BY sex HAVING sex = '男';

having配合聚合函数的使用

  • where后面条件不能使用聚合函数, having可以使用聚合函数
1
2
-- 求班级人数大于3人的班级名字
select class from students GROUP BY class HAVING count(*) > 3;

16.having与where筛选的区别

  • where是对标的原始数据进行筛选
  • having是对group by之后已经分过组的数据进行筛选
  • having可以使用聚合函数, where不能用聚合函数

17.limit显示指定的记录数

  • select * from 表名 where 条件 group by 字段 order by 字段 limit start, count
  • limit总是出现在select语句的最后,
  • start代表开始行号,行号从0开始编号
  • count代表要显示多少行
  • 省略start,默认从0开始,从第一行开始
1
2
3
4
5
6
-- 查询前三行记录
SELECT * from students limit 0, 3;
SELECT * from students limit 3;

-- 查询从第4条记录开始的三条记录
SELECT * from students limit 3, 3;
  • 当有where或者group by或者order by, limit总是出现在最后
1
2
3
4
5
-- 查询年龄最大同学的name
select name from students ORDER BY age desc limit 1;

-- 查询年龄最小的女同学信息
SELECT * from students where sex = '女' ORDER BY age LIMIT 1;

18.数据分页显示

  • m 每页显示多少条记录
  • n,第n页
  • (n - 1) * m, m
  • 把计算结果写到limit后面
1
2
3
4
-- 每页显示4条记录,第3页的结果
select * from students limit 8, 4;
-- 每页显示4条记录,第2页的结果
select * from students limit 4, 4;
  • 已知每页记录数,求一张表需要几页显示完
    • 求总页数
    • 总页数 / 每页的记录数
    • 如果结果是整数,那么就是总页数,如果结果有小数,那么就在结果的整数上+1

4.update修改数据

  • 语法:update 表名 set 字段=值, 字段=值 where 条件
    • 如果没有where 条件代表修改表中所有的记录
1
2
-- 例 1:修改表 c,所有人的年龄(age 字段)改为 50
update c set age = 50;
  • 带有条件的update语句
1
2
-- 例 2:修改表 c,id 为 3 的记录,姓名(name 字段)改为 ‘狄仁杰‘,年龄(age 字段)改为 20
update c set name = '狄仁杰', age = 20 where id = 3;

5.delete删除记录

  • 语法:'delete from 表名 where 条件'

  • -- 例 1:删除表 c 中 id 为 6 的记录
    DELETE from c where id = 6;
    -- 删除所有记录
    DELETE from c;
    -- 例 2:删除 class 为’1 班’,并且 age 大于 30 的学生记录
    DELETE from students where class = '1班' and age > 30;
    
    1
    2
    3
    4
    5
    6
    7
    8

    ## 6.truncate table删除表的数据

    - truncate table 表名

    - ```sql
    -- 删除表c中所有的记录
    truncate table c;

delete与truncate对比

  • 速度上,truncate>delete
  • 删除部分数据用delete+where
  • 保留表但将所有数据删除,自增长字段恢复从1开始,用truncate

7.删除表

  • 语法一: drop table 表名
1
2
-- 删除表a
drop table a;
  • 语法二: drop table if exists 表名
1
2
-- 如果表a存在,就删除表a,如果不存在,什么也不做
DROP table if EXISTS a;