-- 创建数据库
create database python_school charset=utf8;
-- 使用数据库
use python_school;
-- students表
create table students(
-- 整型 无符号 主键 自动增长 非空
id int unsigned primary key auto_increment not null,
-- 变长字符串(20字符) 默认为空
name varchar(20) default '',
-- 小整数整性 无符号 默认为0
age tinyint unsigned default 0,
-- 浮点型(5个数字位,小数点后占两位)如:171.11
height decimal(5,2),
-- 字符串类型存储('男','女','保密') 默认保密
gender enum('男','女','保密') default '保密',
-- 整型 无符号 默认为0
cls_id tinyint unsigned default 0,
-- 比特值类型(1为真,0为假) 默认为 假
is_delete bit default 0
);
-- classes表
create table classes (
-- 整型 无符号 自动增长 主键 非空
id int unsigned auto_increment primary key not null,
-- 变长字符串(30字符) 非空
name varchar(30) not null
);
-- 向students表中插入数据
insert into students values
(0,'周润发',18,180.00,2,1,0),
(0,'胡歌',18,180.00,2,2,1),
(0,'彭于晏',29,185.00,1,1,0),
(0,'刘德华',59,175.00,1,2,1),
(0,'赵又廷',38,160.00,2,1,0),
(0,'邓超',28,150.00,3,2,1),
(0,'王祖贤',18,172.00,2,1,1),
(0,'周杰伦',36,NULL,1,1,0),
(0,'程坤',27,181.00,1,2,0),
(0,'刘亦菲',25,166.00,2,2,0),
(0,'张艺兴',33,162.00,3,3,1),
(0,'林更新',12,180.00,2,4,0),
(0,'古天乐',12,170.00,1,4,0),
(0,'周杰伦',34,176.00,2,5,0);
-- 向classes表中插入数据
insert into classes values (0, "python_01期"), (0, "python_02期");
select * from 表名;
例:
select * from students;
select 列1,列2,... from 表名;
例:
select name,age from students;
select id as 标号, name as 名字, gender as 性别 from students;
-- 如果是单表查询 可以省略表明
select id, name, gender from students;
-- 表名.字段名
select students.id,students.name,students.gender from students;
-- 可以通过 as 给表起别名
select s.id,s.name,s.gender from students as s;
select distinct 列1,... from 表名;
例:
select distinct gender from students;
使用where子句对表中的数据筛选,结果为true的行会出现在结果集中
select * from 表名 where 条件;
例:
select * from students where id=1;
例1:查询编号大于3的学生
select * from students where id > 3;
例2:查询编号不大于4的学生
select * from students where id <= 4;
例3:查询姓名不是“彭于晏”的学生
select * from students where name != '彭于晏';
例4:查询没被删除的学生
select * from students where is_delete=0;
例5:查询编号大于3的女同学
select * from students where id > 3 and gender=0;
例6:查询编号小于4或没被删除的学生
select * from students where id < 4 or is_delete=0;
例7:查询姓黄的学生
select * from students where name like '黄%';
例8:查询姓黄并且名字是一个字的学生
select * from students where name like '黄_';
例9:查询姓黄或叫靖的学生
select * from students where name like '黄%' or name like '%靖';
例10:查询编号是1或3或8的学生
select * from students where id in(1,3,8);
例11:查询编号为3至8的学生
select * from students where id between 3 and 8;
例12:查询学生是3至8的男生
select * from students where id between 3 and 8 and gender=1;
例13:查询没有填写身高的学生
select * from students where height is null;
例14:查询填写了身高的学生
select * from students where height is not null;
例15:查询填写了身高的男生
select * from students where height is not null and gender=1;
为了方便查看数据,可以对数据进行排序
select * from 表名 order by 列1 asc|desc [,列2 asc|desc,...]
例1:查询未删除男生信息,按学号降序
select * from students where gender=1 and is_delete=0 order by id desc;
例2:查询未删除学生信息,按名称升序
select * from students where is_delete=0 order by name;
为了快速得到统计数据,经常会用到如下5个聚合函数
聚合函数只能再select中使用,若与其他函数使用则报错
例1:查询学生总数
select count(*) from students;
例2:查询女生的编号最大值
select max(id) from students where gender=2;
例3:查询未删除的学生最小编号
select min(id) from students where is_delete=0;
例4:查询男生的总年龄
select sum(age) from students where gender=1;
-- 平均年龄
select sum(age)/count(*) from students where gender=1;
例5:查询未删除女生的编号平均值
select avg(id) from students where is_delete=0 and gender=2;
根据gender字段来分组,gender字段的全部值有3个’男’,‘女’,‘保密’,所以分为了3组 当group by单独使用时,只显示出每组的第一条记录, 所以group by单独使用时的实际意义不大
select gender,group_concat(name) from students group by gender;
select gender,group_concat(id) from students group by gender;
1、通过group_concat()的启发,我们既然可以统计出每个分组的某字段的值的集合,那么我们也可以通过集合函数来对这个值的集合做一些操作
select gender,group_concat(age) from students group by gender;
-- 分别统计性别为男/女的人年龄平均值
select gender,avg(age) from students group by gender;
分别统计性别为男/女的人的个数
select gender,count(*) from students group by gender;
select gender,count(*) from students group by gender having count(*)>2;
select gender,count(*) from students group by gender with rollup;
select gender,group_concat(age) from students group by gender with rollup;
当数据量过大时,在一页中查看数据是一件非常麻烦的事情
select * from 表名 limit start,count
例1:查询前3行男生信息
select * from students where gender=1 limit 0,3;
select * from students where is_delete=0 limit (n-1)*m,m
当查询结果的列来源于多张表时,需要将多张表连接成一个大的数据集,再选择合适的列返回
mysql支持三种类型的连接查询,分别为:
select * from 表1 inner或left或right join 表2 on 表1.列 = 表2.列
例1:使用内连接查询班级表与学生表
select * from students inner join classes on students.cls_id = classes.id;
例2:使用左连接查询班级表与学生表
select * from students as s left join classes as c on s.cls_id = c.id;
例3:使用右连接查询班级表与学生表
select * from students as s right join classes as c on s.cls_id = c.id;
例4:查询学生姓名及班级名称
select s.name,c.name from students as s inner join classes as c on s.cls_id
在一个 select 语句中,嵌入了另外一个 select 语句, 那么被嵌入的 select 语句称之为子查询语句
主要查询的对象,第一条 select 语句
查询班级学生的平均身高
select * from students where age > (select avg(age) from students);
select name from classes where id in (select cls_id from students);
select * from students where (height,age) = (select max(height),max(age) from students);
在 某列 上创建索引
1.特点:增加主键之后,主键列自动会被增加索引
2.增加主键[索引]
已有表添加主键
alter table 表名 add primary key(id);
1.特点
2.实施手段
1.创建表的时候指定唯一性
create table xxx(
id int primary key auto_increment,
phone varchar(20) unique,
)
2.对已有表创建索引
create unique index 索引名 on 表名(字段名);
案例:增加唯一索引
create unique index uq_name on sanguo(name);
insert into sanguo values(null,‘赵云’,158,65,‘m’,‘蜀国’);//添加索引就添加不进去了
1.实施手段
1.创建表同时指定普通索引
create table 表名(
id xxx xxxx,
country varchar(30) ,
index(country),
index(字段名),
)
2.对已有表增加普通索引
create index 索引名 on 表名(字段名);
案例:增加普通索引
create index mul_country on sanguo(country);
drop index 索引名称 on 表名;
show index from 表名;
SELECT select_expr [,select_expr,...] [
FROM tb_name
[WHERE 条件判断]
[GROUP BY {col_name | postion} [ASC | DESC], ...]
[HAVING WHERE 条件判断]
[ORDER BY {col_name|expr|postion} [ASC | DESC], ...]
[ LIMIT {[offset,]rowcount | row_count OFFSET offset}]
]
select distinct *
from 表名
where ....
group by ... having ...
order by ...
limit start,count