找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手

多测师学习课程内容:mysql(第2天)

[复制链接]
修改表的结构
查看一个表的结构
desc +表名
凡是修改表的结构,关键字
alter table   alter:修改
修改一个表的名字
alter table +表名 rename +新表名
rename:重命名
例子 把表student2修改为student3
alter table student2 rename student3


修改表的备注
alter table +表名comment“备注内容”
修改表字段的备注
alter table +表名 modify +字段名comment "备注内容”
modify修改
例子修改学生表3中id 备注为编号
alter table student3 modify id int(3) comment'编号‘
添加表的字段
alter+table+表名 add+字段名称+数据类型+约束条件


指定添加的字段放在第1位
alter table +表名 add +字段名称+数据类型+约束条件 first


指定添加的字段放在某一个字段后面
alter table +表名 add +字段名称+数据类型+约束条件 after +字段名


删除表中字段
alter table +表名 drop +字段名称
例子把学生表3中bb字段删除
alter table student3 drop bb
drop:删除


修改字段名
alter table +表名 change +原字段名+现字段名+数据类型+约束
例子 把学生表3中age字段修改为age2,数据类型int(10)
alter table student3 change age age2 int(10)


总结;
1. 添加字段add
2. 删除字段drop
3. 修改字段change
4.修改备注modify
5. 查看表结构desc


单表的增删改查
增:添加数据
删:删除数据
改:更新数据
查:查询数据

添加数据
insert into   插入
给所有的字段进行赋值,或者指定的字段进行赋值
insert into +表名(字段名称1,字段名称2,字段名称3.....)values(值1,值2,值3...)
values :赋值

给所有的字段进行赋值

给指定字段赋值
insert into student3(id,name)values(2,'lisi')

没有赋值的字段显示 Null
Null : 空

给表中的所有字段赋值
insert into +表名values(值1,值2,值3....)

删除数据
delete  删除   删除表中的数据
drop   删除   drop删除表的字段,删除表
drop table +表名    #删除一个表
例如 :删除学生表3
drop table student3

删除表数据
delete from +表名 +where条件
删除表中的所有数据
delete from +表名
delete from student3

面试题(掌握)
假设有一个表,表中有100万条数据,如何快速
的清空这个表
delete from    速度很慢
最快的方法,删除100万条数据
truncate +表名
truncate  student3

删除表中指定的数据
delete from +表名  where +条件
条件 :  字段名称=字段值
例子 删除字段name=”zhaoLiu”的数据
delete from student3 where name=”zhaoliu”

更新  update
update  更新  
update +表名 set 字段名=字段值 where +条件
更新某个表,满足某个条件的数据,把某个字段进行更新


查询
select
select * from +表名  从某个表中查询所有的数据
*   所有
from  从。。。表里
例子   查询学生表中所有的数据
select * from student3

查询表中指定字段对应的所有数据
select 字段名,字段名  from  +表名

查询满足某个条件的所有数据
select * from +表名 where +条件
例子
查询班级是1909班学生信息
select * from student3 where class=’1909’

查询班级是1909班学生的姓名
1. 查询的内容  name
2. 有哪些条件  class=”1909”
select name from student3 where class=’1909’

where  后面可以使用的条件
比较的条件
1.  字段名=字段值
2.  不等于  !=
查询班级不是1909班学生的信息
查询的内容: *
条件 :  class!="1909"
select * from student3 where class!="1909"
3  > >= < <=
主要点  比较的字段都是int型的内容

查询年龄大于22岁的学生信息
查询内容  学生信息  *
条件    age2>22
select * from student3 where age2>22

多个条件同时满足,使用 and

查询班级是1908班,男生的信息
查询的内容  学生的信息  *
条件  class='1908' sex='nan'
条件1 且 条件2   and
条件1 and  条件2
class='1908' and sex='nan'
select * from student3 where class='1908'
and sex='nan';
多个条件
条件1 and 条件2 and  条件3 and 条件4

条件和条件之间是或的关系    or  或者
or左右两边的条件至少满足一个,数据就能被查询出来

between ....and.....  在。。。。范围之间
age2 between 18 and 24
age2 >=18 and age2<=24
查询年龄在19-23岁之间的学生信息
select * from student3 where age2
between 19 and 23

in   (集合)  age2  in (10,20,30)
age2=10 or age2=20 or age2=30
not in   in的取反
不在指定集合范围内,满足条件
age2  not  in (10,20,30)

is  null    为空
is not null  不为空
注意点千万不要写成  字段=Null   错误的写法


like   像
name=”zhangsan”
where  name like “zhangsan”
模糊匹配   %
名字是zhang开头的学生信息
where  name like  “zhang%”
% 匹配一个或多个字符
名字是san结尾的学生信息
where  name  like  ”%san“
名字中包含san学生信息
where name  like  “%san%”

limit   限制
下标  下标是从0开始的
第一条数据   下标是0
第二条数据    下标是1
第三条数据    下标是2
limit  m,n
m 代表着下标,从第m+1条开始
n  代表着数量,查询的数量
limit 0,5 代表着从第1条数据开始,查询5条数据

limit 2,5 代表着从第3条数据开始,查询5条数据

排序  order by
order by asc   正序  从小到大
order by desc  倒叙  从大到小
根据年龄对学生表中所有的数据进行正序排序
查询的内容   学生信息
条件   order by  age2  asc
注意点
正序排序  asc可以省略不写
select * from student3 order by age2 asc

查询性别是男,根据年龄进行正序排序,学生信息
查询的内容  学生信息
条件    sex=’nan’
order by age2 asc
select * from student3 where sex=”nan”
order by age2 asc

查询男生信息,根据年龄倒叙排序
select * from student3 where sex="nan" and class=’1909’
order by age2 desc

聚合函数
count()  统计
sum() 求和
avg()  求平均
max()  最大
min()   最小

count()  统计
count(*)  或者 count(1) 或者 count(字段)
查询学生表中一共有多少条数据
select count(*) from student3
select count(1) from student3
select count(id) from student3

查询学生表中男生的人数
select count(*) from student3 where sex='nan'
步骤
1. 从表中查询所有
2. 条件过滤
3. 统计
sum()  求和   作用在int型字段上面
查询班上所有学生的年龄总和
select sum(age2) from student3

查询班上男生年龄的总和
select sum(age2) from student3 where
sex=’nan’
max()  求最大    作用在int型字段
查询班上学生的最大年龄
select max(age2) from student3
min() 求最小  作用在int型字段
查询班上最小的年龄
select min(age2) from student3

avg()  平均   作用在int型字段
查询班上的平均年龄
select avg(age2)  from student3

分组   group by +分组字段
group by sex  根据性别分组
group by class 根据班级分组

select sex,count(*) from student3
group by sex
注意点
1. 聚合函数一般都会和group by 分组
组合使用
2. 分组的字段可以加到select 后面,其他
的字段不能加到select 后面

select sex,min(age2) from student3
where age2>20
group by sex
步骤
1 查询所有数据   from student3
2 条件过滤    where age2>20
3 分组       group by sex
4 查询分组后的数据  select min(age2)

先条件过滤,再分组

having   条件判断
注意点
1. having的作用和where是一样的
2. having用在group by 的后面,
也就是先分组,再进行条件判断
3 having 后面可以接聚合函数,where
后面不允许接聚合函数。




分享至 : QQ空间
收藏

0 个回复

您需要登录后才可以回帖 登录 | 立即注册