-- create table 表名(字段名 数据类型 约束条件)-- 创建classes表(id、name)createtable classes (
id intprimarykeyauto_increment,
name varchar(20));-- 创建students表(id, name, age, height, gender)createtable students (
id intprimarykeyauto_increment,
name varchar(20),
age int,
height int,
gender enum('男','女'));
3.修改表
-- 修改表-新增字段 alter -- 修改 -- alter table 表名 add 列名 类型及约束;-- 1、添加birth字段到最后 最后--默认altertable students add birth date;-- 2、添加score字段到第1列 首列--firstaltertable students add score intfirst;-- 3、添加phone字段到name的后面 name的后面 -- after namealtertable students add phone varchar(20)after name;-- 修改表-修改字段: -- 不修改列名: alter table 表名 modify 列名 类型及约束;altertable students modify phone int;-- 修改列名: alter table 表名 change 原名 新名 类型及约束;altertable students change gender sex enum('男','女');
4.删除表
-- 删除字段-- alter table 表名 drop 列名;altertable students drop score;-- 删除表-- drop table 表名;droptable students;
三、数据的增删改查(CRUD)
1.增加
1.1全字段插入
-- insert into 表名 values (...); -- 向classes表中插入一个班级insertinto classes values(0,'Python1班');insertinto classes values(0,'Python2班');insertinto classes values(0,'Python3班');-- 向students表中插入一个学生信息 枚举下标从1开始-- 主键字段 可以用0: 占位符 insertinto students values(0,'张三',18,180,'男');insertinto students values(0,'李四',19,175,'女');insertinto students values(0,'王五',20,170,3);insertinto students values(0,'赵六',21,165,1);
1.2多行插入
insertinto students values(0,'小明',18,180,2),(0,'小月',18,180,2),(0,'小花',29,185,1),(0,'小红',59,175,1),(0,'小张',38,160,2),(0,'小李',38,160,2);
2.修改
-- update 表名 set 列1=值1, 列2=值2, 列3=值3 ... where 条件判断;-- 所有学生的性别修改成男update students set gender='男';-- 将id为3的学生性别修改成女update students set gender=2where id=3;-- 将id为4的学生名字修改成小小update students set name='小小'where id=4;-- 将id为5的学生,名字修改成小小,性别修改成男update students set name='小小', gender='男'where id=5;
3.删除
-- delete from 表名 where 条件;-- 删除名字是张三的学生
delete from students where name='张三';-- 删除id<4的学生
delete from students where id<4;
4.查询
4.1基本查询
-- 查询所有学生信息 *: 所有字段select*from students;-- 查询指定列 -- id, age, nameselect id, name, age from students;-- 可以用as设置别名 -- select 字段1 as 别名1, 字段2 as 别名2 from 数据表名 where ...;select id as 学号, name as 姓名, age as 年龄 from students;-- 消除重复行: distinctselectdistinct gender from students;
4.2条件查询
-- 比较运算符-- select ... from 表名 where ...-- =-- 查询等于18岁的信息select*from students where age=18;-- > -- 查询大于18岁的信息select*from students where age>18;-- <-- 查询小于18岁的信息select*from students where age<18;-- !=-- 查询不等于18岁的信息select*from students where age!=18;-- 逻辑运算符-- and-- 18岁到28之间的所有学生信息select*from students where age>18and age<28;-- 18岁以上的女生信息select*from students where age>18and gender='女';-- or -- 查看18岁以上 或者 身高大于170的男生select*from students where age>18or(height>170and gender='男');-- not -- 不在18岁以上的女生的信息select*from students wherenot(age>18and gender='女');
4.3范围查询
-- in(1, 3, 8)表示在一个非连续的范围内 --离散 -- 查询 年龄为18, 27, 34的同学信息select*from students where age=18or age=27or age=34;select*from students where age in(18,27,34);-- not in 不连续的范围之内-- 查询 年龄不在18, 27, 34的同学信息select*from students where age notin(18,27,34);-- between ... and ... 表示在一个连续的范围内 -- 连续 -- 查询 年龄是18~34之间的同学信息select*from students where age between18and34;-- not between ... and ... 表示不在一个连续的范围内-- 查询 年龄不是18~34之间的同学信息select*from students where age notbetween18and34;
4.4排序查询
-- order by 单个字段(默认:从小到大排序, 省略了asc) asc--从小到大 desc--从大到小-- 查询年龄在18~34之间的男性,年龄从小到大排序select*from students where(age between18and34)and gender='男'orderby age;-- 查询年龄在18~34之间的女性,身高从大到小排序select*from students where(age between18and34)and gender='女'orderby height desc;-- order by 多个字段 -- 往后写就对了-- 查询年龄在18到34岁之间的女性,身高从大到小排序,如果身高相同情况下按照ID大小排序select*from students where(age between18and34)and gender='女'orderby height desc, id desc, age desc;-- 按照年龄从小到大排序,身高从大到小排序select*from students orderby age, height desc;
4.5聚合查询
-- count 总数-- 查询有多少男生,多少女生selectcount(*)as 男生人数 from students where gender='男';selectcount(*)as 女生人数 from students where gender='女';-- 查询学生总数selectcount(*)from students
selectcount(*)as 学生总数 from students;-- max 最大值-- 查询最大的年龄selectmax(age)as 最大年龄 from students;-- 查询男生年龄最大的 selectmax(age)as 男生最大年龄 from students where gender='男';selectmax(age)as 女生最大年龄 from students where gender='女';-- min 最小值-- 查询最小的年龄selectmin(age)as 班级最小年龄 from students;-- sum 求和-- 计算所有男生的年龄和selectsum(age)as 男生年龄和 from students where gender='男';-- avg 平均值-- 计算男生平均年龄selectavg(age)as 男生平均年龄 from students where gender='男';-- round(num, 2) 保留2位小数-- 计算女生平均年龄,保留2位小数selectround(avg(age),2)as 女生平均年龄 from students where gender='女';
4.6分组查询
-- group by -- 按照性别分组select gender,count(*)from students groupby gender;-- 计算每种性别中的人数select gender,count(*)from students groupby gender;-- 查询每种性别中最大的年龄select gender as 性别,max(age)as 最大年龄 from students groupby gender;-- 查询每种性别的平均年龄,并且保存2位小数select gender as 性别,round(avg(age),2)as 平均年龄 from students groupby gender;-- 计算男性的人数select gender,count(*)from students where gender='男'groupby gender;-- group_concat(...)-- 查询同种性别中的学生姓名select gender as 性别, group_concat(name)as 名字 from students groupby gender;-- having-- 查询平均年龄超过30岁的性别,以及姓名 having avg(age)>30select gender, group_concat(name),round(avg(age),2)from students groupby gender havingavg(age)>30;-- 查询性别人数超过2人得性别,并显示其名字select gender, group_concat(name)from students groupby gender havingcount(*)>2;
4.7分页查询
-- limit start【起始下标】, count【个数】 限制查询出来数据的个数 下标从0开始-- 查询前5个数据select*from students limit0,5;-- 查询id=(6~10)的排序 5, 5 select*from students limit5,5;
4.8子查询
-- 查询出高于平均身高的信息select*from students where height>(selectavg(height)from students);-- 查询最高的男生信息 select*from students where height=(selectmax(height)from students where gender='男');