下一篇:多表连接查询
1.sql语言中的注释
- -- 行注释
- /* 注释内容 */
2.mysql的常用数据类型
3.数据库中的元素
- 数据库-----database
- 表----table
- 字段(列)---field
- 记录(行)---record
4.创建表
- 语法:create table 表名(字段名 字段类型, 字段名 字段类型)
-- 例 1:创建表 a,字段要求:name(姓名),数据类型:varchar(字符串),长度为 10
CREATE TABLE a(name varchar(10));
- 创建两个字段的表
-- 例 2:创建表 b,字段要求: name(姓名),数据类型为 varchar(字符串),长度为 10;
-- height(身高),数据类型为 decimal(小数),一共 5 位, 其中 3 位整数,2 位 小数。
CREATE TABLE b(
name varchar(10),
height DECIMAL(5, 2)
);
- 创建三个字段的表
-- 例 3:创建表 c,字段要求如下:
-- id:数据类型为 int (整数);
-- name 姓名:数据类型为 varchar(字符串)长度为 20,
-- age 年龄:数据类型为 tinyint unsigned(无符号小整数);
CREATE TABLE c(
id int,
name VARCHAR(20),
age TINYINT UNSIGNED
);
5.插入数据
- 语法 : insert into 表名 values (值, 值,值);
-- 往表c插入一条记录
INSERT into c VALUES (0, '张飞', 30);
- 指定字段插入,语法:insert into 表名 (字段名, 字段名) values (值, 值);
-- 例 2:表 c 插入一条记录,只设置 id 和姓名 name
INSERT into c (id, name) values (3, '曹操');
-- 例 2:表 c 插入一条记录,只设置 id 和姓名 age
INSERT into c (id, age) values (4, 100);
6.插入多条记录
- 多条insert语句,用分号隔开
-- 例 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 (值,值),(值, 值),(值,值);
-- 例 4:表 c 插入多条记录,用一条 insert 语句, 数据之间用逗号隔开
insert into c values (10, '张三', 10),
(11, '李四', 20),
(12, '王五', 30);
- 一条insert指定字段插入多条记录
- 语法:insert into 表名 (字段名, 字段名) values (值, 值), (值, 值), (值, 值)
-- 例 4:表 c 插入多条记录,用一条 insert 语句, 数据之间用逗号隔开
insert into c (id, name) values (13, '光绪'),
(14, '康熙'),
(15, '雍正');
7.select查询表
查询所有字段
-
语法: select * from 表名;
-
查询表的所有字段
-- 查询表c的所有字段
select * from c;
指定字段名查询
- 语法:select 字段名, 字段名 from 表名;
-- 查询表c的id字段
select id from c;
-- 查询表c的id和age字段
select id, age from c;
-- 查询表c的所有字段,但顺序自定义
select name, id, age from c;
8.update修改数据
- 语法:update 表名 set 字段=值, 字段=值 where 条件
- 如果没有where 条件代表修改表中所有的记录
-- 例 1:修改表 c,所有人的年龄(age 字段)改为 50
update c set age = 50;
- 带有条件的update语句
-- 例 2:修改表 c,
-- id 为 3 的记录,
-- 姓名(name 字段)改为 ‘狄仁杰‘,年龄(age 字段)改为 20
update c set name = '狄仁杰', age = 20 where id = 3;
- 另外的例子
-- 修改name为刘备的记录为李白
update c set name = '李白' where name = '刘备';
-- id大于10的记录,长一岁
update c set age = age + 1 where id > 10;
9.delete删除记录
- 语法:'delete from 表名 where 条件'
-- 例 1:删除表 c 中 id 为 6 的记录
DELETE from c where id = 6;
-- 例 1:删除表 c 中 name 为 诸葛亮 的记录
DELETE from c where name = '诸葛亮';
-- 删除年龄大于50的记录
DELETE from c where age > 50;
-- 删除id小于3的记录
delete from c where id < 3;
-- 删除所有记录
DELETE from c;
10.truncate table删除表的数据
- truncate table 表名
-- 删除表c中所有的记录
truncate table c;
11.小结
- 增
- insert
- 删
- delete
- 改
- update
- 查
- select
12.删除表
- 语法一: drop table 表名
-- 删除表a
drop table a;
- 语法二: drop table if exists 表名
-- 如果表a存在,就删除表a,如果不存在,什么也不做
DROP table if EXISTS a;
-- 如果表b存在,就删除表b,如果不存在,什么也不做
DROP table if EXISTS b;
13.字段的约束
14.主键
- 主键的值不能重复
- 自增长,auto_increment
- 值会系统自动维护,自动增长
-- 例 1:创建表 d,字段要求如下:
-- id : 数 据 类 型 为
-- int unsigned( 无 符 号 整 数 ) ) ,primary key( 主键),auto_increment(自增长);
-- name 姓名:数据类型为 varchar(字符串)长度为 10;
-- age 年龄:数据类型为 int(整数);
create TABLE d (
id int UNSIGNED PRIMARY key auto_increment,
name varchar(10),
age int);
INSERT into d (name, age) values ('张飞', 30);
-- 插入的时候指定了id的值
INSERT into d (id, name, age) values (6, '曹操', 30);
-- 不指定id的值
INSERT into d (name, age) values ('周瑜', 30);
select * from d;
-- 如果不指定字段,主键自增长字段的值可以用占位符,0或者null
INSERT into d VALUES (0, '康熙', 30);
INSERT into d VALUES (NULL, '溥仪', 50);
15.非空
- 非空not null
- 这个字段必须有值,如果没有值,insert插入会失败
-- 例 1:创建表 e,字段要求如下:
-- id:数据类型为 int unsigned (无符号整数);
-- name 姓名:数据类型为 varchar(字符串)长度为 10,not null(非空),
-- age 年龄:数据类型为 int(整数);
CREATE table e (
id int UNSIGNED,
name varchar(10) not null,
age int);
insert into e values (1, '张三', 20);
insert into e (id, age) values (1, 20);
select * from e;
16.唯一
- 唯一unique
- 字段的约束为唯一,表示字段的值不能重复
-- 例 1:创建表 f,字段要求如下:
-- id:数据类型为 int (整数);
-- name 姓名:数据类型为 varchar(字符串)长度为 10,unique(唯一);
-- age 年龄:数据类型为 int(整数);
CREATE TABLE f (
id int,
name varchar(10) UNIQUE,
age int);
insert into f VALUES (1, '张三', 20);
insert into f VALUES (2, '李四', 20);
select * from f;
17.默认值
- default 值
- 当一个字段有默认值约束,插入数据时,如果指定了值,那么默认值无效,如果没有指定值,会使用默认值
-- 例 1:创建表 g,
-- 字段要求如下:
-- id:数据类型为 int (整数);
-- name 姓名:数据类型为 varchar(字符串)长度为 10;
-- age 年龄:数据类型为 int(整数),default(默认值)30;
CREATE TABLE g(
id int,
name varchar(10),
age int DEFAULT 30);
insert into g values (1, '张三', 20);
-- 插入的时候不指定age的值
insert into g (id, name) values (2, '李四');
select * from g;
18.字段的别名
- 通过 字段名 as 别名 的语法,可以给字段起一个别名,别名可以是中文
- as可以省略
- 字段名 as 别名 和 字段名 别名 结果是一样的
-- 通过as 给字段起一个别名
select card as 身份证, name as 姓名,sex as 性别 from students;
-- 别名的as可以省略
select card 身份证, name 姓名,sex 性别 from students;
19.表的别名
- 通过 表名 as 别名 给表起一个别名
- as可以省略
-- 通过as 给表students起一个别名
select * from students as stu;
-- 可以省略as
select * from students stu;
20.distinct过滤重复记录
- 通过select distinct 字段名, 字段名 from 表名 来过滤select查询结果中的重复记录
SELECT DISTINCT sex, class from students;
21.where子句
- where 后面跟一个条件,实现有选择的查询
- select * from 表名 where 条件
-- 例 1:查询 students 表中学号 studentNo 等于’001’ 的记录
select * from students where studentNo = '001';
-- 例 2:查询 students 表中年龄 age 等于 30 的姓名 name,班级 class
select name, class from students where age = 30;
22.select查询的基本规律
- select * 或者select 字段名 控制了查询返回什么样的字段(列)
- where 条件 控制了查询返回什么样的记录(行)
23.比较运算符
- =等于
- < 小于
- <= 小于等于
>
大于>=
大于等于- !=和<>不等于
-- 例 1:查询 students 表中 name(姓名)等于’小乔’学生的 age(年龄)
select age from students where name = '小乔';
-- 例 2:查询 students 表中 30 岁以下的学生记录
SELECT * from students where age < 30;
-- 例 2:查询 students 表中 30 岁和30岁以下的学生记录
SELECT * from students where age <= 30;
-- 查询家乡不在'北京'的学生记录
select * from students where hometown != '北京';
select * from students where hometown <> '北京';
24.逻辑运算符
- and与
- 条件1 and 条件2
- 两个条件必须都满足
- or或
- 条件1 or 条件2
- 两个条件只要有一个满足即可
- not非
- not 条件
- 条件成立,not以后就不成立,条件不成立,not以后就成立
-- 例 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 != '天津';
25.模糊查询
- like实现模糊查询
- %代表任意多个字符
- _代表任意一个字符
- 字段名 like '字符%'
- 指定字符开始,后面任意多个字符
-- 例 1:查询 name 姓名中以’孙’开头的学生记录
SELECT * from students where name like '孙%';
-- 例 2:查询 name 姓名以’孙’开头,且名只有一个字的学生记录
SELECT * from students where name like '孙_';
-- 例 3:查询 name 为任意姓,名叫’乔’的学生记录
SELECT * from students where name like '%乔';
-- 查询 name 姓名有’白’子的学生记录
SELECT * from students where name like '%白%';
26.范围查找
- in (值, 值, 值)
- 非连续范围查找
- between 开始值 and 结束值
- 连续范围查找,包含开始值 包含 结束值
-- 例 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;
27.空判断
- null不是0,也不是'',null在SQL里面代表空,什么也没有
- null不能用比较运算符的判断
- is null ---是否为null
- is not null ---是否不为null
- 不能用 字段名 = null 字段名 != null这些都是错误的
-- 例 1:查询 card 身份证为 null 的学生记录
SELECT * from students where card is null;
-- 例 2:查询 card 身份证非 null 的学生记录
SELECT * from students where card is not null;
28.where子句可以用到update和delete语句后面
-- 例 1:修改 age 为 25,并且 name 为’孙尚香’的学生 class 为’2 班’
update students set class = '2班' where age = 25 and name = '孙尚香';
-- 例 2:删除 class 为’1 班’,并且 age 大于 30 的学生记录
DELETE from students where class = '1班' and age > 30;
29.order by排序
- order by 字段名 [asc/desc]
- asc代表从小到大,升序,asc可以省略
- desc代表从大到小,不可以省略
-- 例 1:查询所有学生记录,按 age 年龄从小到大排序
select * from students order by age asc;
select * from students order by age;
-- 例 2:查询所有学生记录,按 age 年龄从大到小排序
select * from students order by age desc;
- 两个字段排序的例子
-- 例 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前面
30.聚合函数
count求select返回的记录总数
- count(字段名)
-- 查询学生总数(查询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(字段名)
- 查询指定字段里的最大值
-- 查询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(字段名)
- 查询指定字段的最小值
-- 查询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(字段名)
- 指定字段的值求和
-- 查询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(字段名)
- 指定字段的平均值
-- 查询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不做为分母计算平均
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
31.数据分组
- group by 字段名
- select 聚合函数 from 表名 where 条件 group by 字段
- select 聚合函数 from 表名 group by 字段
- group by就是配合聚合函数使用的
-- 分别查询男女同学的数量
SELECT count(*) from students where sex = '男';
SELECT count(*) from students where sex = '女';
select sex, count(*) from students group by sex;
- group by的例子
-- 分别查询各个年龄段的同学数量
select age, count(*) from students group by age;
- where与group by
-- 分别查询'1班'不同性别学生数量
select sex, count(*) from students where class = '1班' group by sex;
where和group by 和order by的顺序
- select * from 表名 where 条件 group by 字段 order by 字段
32.分组聚合之后的数据筛选
- having子句
- 总是出现在group by之后
- select * from 表名 group by 字段 having 条件
-- 用where查询男生总数
-- where先筛选复合条件的记录,然后在聚合统计
SELECT count(*) from students where sex = '男';
-- 用having查询男生总数
-- having先分组聚合统计,在统计的结果中筛选
SELECT count(*) from students GROUP BY sex HAVING sex = '男';
33.having配合聚合函数的使用
- where后面条件不能使用聚合函数, having可以使用聚合函数
-- 求班级人数大于3人的班级名字
select class from students GROUP BY class HAVING count(*) > 3;
34.having与where筛选的区别
- where是对标的原始数据进行筛选
- having是对group by之后已经分过组的数据进行筛选
- having可以使用聚合函数, where不能用聚合函数
35.limit显示指定的记录数
- select * from 表名 where 条件 group by 字段 order by 字段 limit start, count
- limit总是出现在select语句的最后,
- start代表开始行号,行号从0开始编号
- count代表要显示多少行
- 省略start,默认从0开始,从第一行开始
-- 查询前三行记录
SELECT * from students limit 0, 3;
SELECT * from students limit 3;
-- 查询从第4条记录开始的三条记录
SELECT * from students limit 3, 3;
- 当有where或者group by或者order by, limit总是出现在最后
-- 查询年龄最大同学的name
select name from students ORDER BY age desc limit 1;
-- 查询年龄最小的女同学信息
SELECT * from students where sex = '女' ORDER BY age LIMIT 1;
36.数据分页显示
- m 每页显示多少条记录
- n,第n页
- (n - 1) * m, m
- 把计算结果写到limit后面
-- 每页显示4条记录,第3页的结果
select * from students limit 8, 4;
-- 每页显示4条记录,第2页的结果
select * from students limit 4, 4;
- 已知每页记录数,求一张表需要几页显示完
- 求总页数
- 总页数 / 每页的记录数
- 如果结果是整数,那么就是总页数,如果结果有小数,那么就在结果的整数上+1