多测师学习课程内容:mysql(第2天)
修改表的结构:查看一个表的结构:命令desc+表名desc(详情)
凡事修改表的结构,都会用到关键字alter table alter:修改
修改一个表的名字:alter table +表名 rename + 新表名 rename:重命名
例子:把表student修改成student3 :alter student rename student3
修改表的备注:alter table +表名 comment ’备注内容‘
修改表字段的备注alter table +表名 modify 字段名称数据类型 comment ’备注内容‘ modify修改
alter table student3 modify id int(3) comment ’编号‘
添加表的字段:alter table + 表名add +字段名称 +数据类型+约束条件(有的话)
指定添加的字段放在第一位:添加表的字段:alter table + 表名add +字段名称 +数据类型+约束条件(有的话) first
指定添加的字段放在某个字段后面:alter table + 表名add +字段名称 +数据类型+约束条件(有的话)after +字段名
删除表中的字段:alter table +表名 drop+字段名称
例子:把学生表3中bb字段删除:alter table sudent3 drop bb, drop删除
修改字段名:alter table +表名 change +原字段现字段名 数据类型那个 +约束(有的话)
例子:把学生表3中的阿哥字段改为age2数据类型int(10)
alter table student3 change age age2 int(10)
总结;添加字段 add
删除字段 drop
修改字段 change
修改备注 modify
单表的增删改查
增:添加数据
删:删除数据
改:更新数据
查:查询数据
添加数据;命令;insert into插入
给所有的字段进行赋值或者给指定的字段进行赋值
insert into +表名 (字段名称1 , 字段名称2, 。。。。。) values(值1,值,2。。。)values赋值
查询:select * from+表名*代表所有from 从。。。。表里 ;从某个表查询所有的数据
没有赋值显示NULL,代表空的意思
给表中所有字段赋值 简便:insert into +表名 values(值1。。值2。。。。)
删除数据delete drop也是删除, drop表的字段,删除表。 delete删除表中的数据 drop table +表名 删除一个表
删除表数据 delete from +表名 +where条件
删除表中所有数据: delete from student3
删除表中指定的数据:delete from where条件 条件:字段名称=字段值
例子 删除字name = zhaoliu 的数据:delete from student3 where name=’zhaoliu‘
面试题(掌握)
假设有一个表,表中有一百万条数据,如何快速的清空表?
delete from速度很慢
最快的方法删除一个大数据量的表,truncate + 表名, truncate student3
update 更新
update +表名 set 字段名=字段值 where+条件
更新某个表,满足某个条件的数据,把某个字段进行更新。
查询:select * from student3 查询表中所有数据 *代表所有
查询表中指定字段对应的所有数据 select 字段名,字段名 from +表名
select name from student3 查询表中name字段的数据
select name,age from student3 查询表中name,age的数据
查询满足某个条件的所有数据
select * from 表名 where +条件
例子:查询班级是1909班学生信息:select * from student3 where class=’1909‘
查询班级是1909班学生的姓名
1查询的内容name
2有哪些条件 class=’1909‘
select name from student where class=’1909‘
where 后面可以使用的条件
1.字段名=字段值
2.不等于,!=就是不等于
select * from student3 where class!=’1909‘ 查询班级不是1909的学生信息
3.> >= < <=,注意用于比较的字段都是int型的内容。
查询年龄大于22的学生信息
select * from student3 where age>22
4.多个条件使用的是and或者是or。
查询班级是1908班,男生的信息。 查询内容:学生的信息。 条件:class=1908,sex=nan。条件1且条件2,and ,条件1and条件2.
select * from student3 where class=’1908‘ and sex=’nan‘
多个条件条件1and条件2and。。。。条件8
条件与条件是或的关系,用or。
查询学生表中班级是1908班,或者性别是男的学生信息。
满足其中一个条件就可以查询出来。
select * from student3 where class=’1908‘ or sex=’nan‘
不满足,查询数据是空
查询性别是男,并且是1908班的学生,或者年龄是24岁的。
select * from student where sex=’nan' and class='1908' or age=24
or两边的条件至少满足一个,就能被查询出来。
查询年龄在18到24之间的学生信息。
select * from student3 where age between 18 and 24
between。。。。and。。。。在。。。范围之间
age2 bettween18 and 24或者 age>=18 and age <=24
in (集合) age in(10,20,30)
not inin 的取反 不在制定集合范围内,满足条件 age not in(10,20,30)
is null为空
is not null 不为空
查询表中性别为空的信息 sex=null错误写法,要写成sex is null,
select * from student3 where sex is null
select * from student where sex in not null
注意点,千万要写成字段=null!!!
like 像
name=‘zhangsan’
where name like ‘zhangsan’ 全完匹配
模糊匹配%
名字是zhang开头的学生信息
whrere name like ‘zhang%’%匹配一个或多个字符
名字是san结尾的学生信息where name like ‘%san’
名字中包含san的学生信息where name like ‘%san%’
select * from student3 where name like ‘zhangsan’
limit 限制
查下表中的前两条数据:select * from student3 limit 2
select * from student limit 0,2
下标 下标是0开始的,第二条数据下标是1,第三条数据下标是2
limit m,nm代表着下标,从第m条开始,n代表数量,要查询的数量
limit 0,5 从第一条数据开始,查询5条数据
limit2,5 从第三条数据开始,查询5条数据
包含第n条数据。
排除:order by
order by asc 正序从小到大
order by desc 倒序从大到小
根据年龄对学生表中所有的数据进行正序排序
order by age asc注意点:正序asc可以省略不写!!!!
select * from student3 order by age asc
查询性别是男,根据年龄进行正序排序查询内容:学生信息条件sex=nan
order by age asc
select * from student3 where sex=‘nan’ order by age asc
聚合函数sum 求和 avg求平均count 统计max最大min最小
1count统计count(*) 或者count(1) 或者count(字段)
查询学生表中一共有多少条数据:
select count(*) from student
select count(1)from student
select count (id)from student
查询学生表中男人的人数:select count(*) from student where sex=“nan”步骤:1从表中查询所有2从条件过滤 3进行统计
2sum()求和,只能作用在int字段上面
查询班上所有学生的年龄总和
select sum(age) from student3
查询班上男生年龄的总和select sum(age) from student where sex='nan'
3max() 求最大也是作用在int型的字段
查询班上学生的最大年龄
select max(age) from student
min求最小 作用在int字段
select min(age) from student
函数后面连着括号不加空格
avg() 平均作用在int型字段
查询班上的平均年龄
select avg(age) from student3
函数都支持加聚合条件
分组group by +分组字段
group by sex 根据性别分组
group by class 根据班级分组
分别查询学生表中男生女生各多少人?
1查询内容 count(*)
条件 group by sex
select count(*) from student3 group by sex
select sex,count(*) from student3 group by sex显示字段
注意点:1聚合行数一般都会和group by分组组合使用!!!
2分组字段可以加到select后面,其他的字段不能加到select后面!!!
查询男女生中最大的年龄
select sex, max(age) from student group by sex
查询年龄大于20岁的学生信息中,男女生最小的年龄?
查询内容:min(age)
条件: group by sex age》20
select sex,min(age) from student where age>20 group by sex
步骤:1查询所有 from student
2条件过滤 where age>20
3分组 group by sex
4查询分组后的数据 select min(age)
先条件过滤,在分组
查询出根据性别分组后,最小年龄大于20的组的年龄
having条件判断 跟where相似
注意点:1having的作用跟where是一样的
2having是用在group by后面,也就是先分组,在进行条件判断。
3having后面可以接聚合函数,where后面不允许接聚合函数。
select sex,min(age) from student group by sex having min(age)>20
页:
[1]