对表结构的操作(增删改查)
alter table 关键字 修改表的结构
1,rename 对一个已经创建的表,表名进行修改
alter table+表名 rename + 修改后的表名;rename table 要修改的表格名 to 修改后的表名;
例如:把student表名改为stu?alter table student rename stu;
2,change 对一个已经创建的表,表的字段进行修改
alter table + 表名 change+原字段+修改后的字段名,数据类型,显示长度,约束、
例子:alter table student change class age int(2);不加约束时,自动保留主键,清空约束
3,add 给一个已经创建的表,给表添加字段
alter table+表名 add 添加的字段,数据类型,显示长度,约束
例子:alter table student add sex char(3)defult '女';(添加字段并设默认值为女) alter table student add age int(2) first;(添加字段,并放到第一列;默认是插到最后一列);alter table student add age int(2) after class;(添加的字段,放在class字段后面)
4,modify 对一个已经创建的表,对表字段顺序调整替换
alter table + 表名+modify+需要调换的字段名,数据类型+after+列名
例如:把age字段放到name后面,alter table student modify age int(2) after name;
5,drop 对一个已经创建的表,对表字段进行删除
alter table +表名+drop+需要删除的字段名
drop table+表名:删除整个表(包括表结构)
delete from+表名:清空表里的所有数据,表结构还在
6,修改默认的编码格式
alter table+表名 default charset=utf8
7,ddl语句:表的定义语句,对表结构的增删改查
dml语句:表操作语句,对表数据的增删改查
对表数据的操作:增删改查
1,从一个表中查询所有的数据
select*from+表名:*表示所有的字段
2,从一个表中查询指定的字段的数据
select 字段1,字段2 from+表名
例如:查询class name字段中的所有数据:select class,name from student;注意:字段与字段之间要用逗号隔开
3,给一个表中字段取别名 as
例如:select id as ’学号‘,name as’姓名‘,class as’班级‘ from student;
4,从一个表中查询符合某些条件的数据
select*from +表名 where+条件
例子:把student表中的所有女性的所有数据查询出来,select*from student where sex='女';
where条件使用注意:条件中有(=,<,>,<=,>=,!=)
wher+多个条件:条件中有(and,or,in,is null,is not null,between and)
判断一个是否为空:字段名+is null 或者字段名+is not null
例子:查询英语成绩为空人的名字和成绩:select name,english from student where english is null;
查询英语成绩在88到97人的名字和成绩:select name english from student where english between 88 and 97;
查询年龄是20,22,24岁所有人的名字和年龄:select name,age from student where age in(20,22,24);
查询年龄是20或者22或者24岁所有人的名字和年龄:select name,age from student where age=20 or age=22 or age=24;
查询年时是20或者22岁,且是女性的人的名字和年龄:select name,age from student where age=20 or age=22 and sex='女';
5,order by对表中的数据进行排序
asc 默认的升序,从小到大
desc 默认的降序
select+字段,字段+from+表名 order by+列名 asc/desc;
例子:根据年龄进行升序/降序排序:select name,age from student order by age asc/desc;
注意点:1,如果是升序排序,那么asc可以省略,默认的order by是升序排序;
2,order by后面只能接2个升序或者降序的条件,且以先排序的条件为主;
例子:先对班级排序,然后对于年龄进行排序:select id,name,class,age from student order by class desc,age asc;
6,like 模糊匹配
select*from+表名 where +列名 like+匹配的条件;
%:表示匹配0个或者多个字符;_:表示匹配一个字符;
例如:匹配以3开头/中间为ao的/ao结尾的的年龄的所有数据:select*from student where age like '3%'或者'%ao%'或者'%ao';
7,limit m,n 后面加2个数值限制
第一个m:是从第几行开始,取索引值(从0开始)会有偏移值
第二个 n:是取几行的数据,显示多少行数据
例如:1,取头5行数据:limit 0,5;select*from +表名+limit 0,5;
2,取头行年龄最大的3个显示:select id,name,age from student order by age desc limit 0,3;
3,取表中,第二行到第5行的数据,select id,name,age from student limit 1,4;
8,sql 函数 聚合函数
max 最大
min 最小
avg 平均值:平均值+5:select avg(age)+5 from student;
sum 求和
count 统计
distinct 去重复
例如:1,求年龄的最大/最小/平均/总和年龄:select max/min/avg/sum(age) from student;
2,统计有多少条数据:select count(1/id/*)from student;其中用1最常用;通常*不允许使用
3,去除重复:select distinct(sex) from student;
9,group by 分组
例如:显示表中男女最大的年龄分别是多大?
select sex,max(age) from student group by sex;
注意点:聚合函数,只能和group by分组后的字段名同时使用查询,其他的字段名不能和聚合函数同时使用,
查询的字段中有聚合函数时,除聚合函数外,其他查询的字段都是取满足条件的第一个第一条数据
10,having+条件
和where 条件用法一样,但是使用的场景不同,一般having用于group by分组之后;
查询表中每个班级数学成绩最高分,且大于90分,并且最大值字段为别名
1,分组:select *from student group by class;
2,分组,最高分和大于90分:select class,max(math) from student group by class having max(math)>90;
3,分组,最高分和大于90分,别名:select class,max(math) as max from student group by class having max>90;其中的as可不写
注意点:1,group by 后面使用条件判断,只能使用having不能使用where
2,having后面条件只能是select后按的列名或者聚合函数
3,一般情况下,都是聚合函数和group by分组以及having组合使用
11,sql嵌套或者sql子查询
求表中年龄最大的学生的所有信息?
第一步:select max(age) from student; 结果为:34
第二部:select*from student age=34;
第三步:select*from student where age=(select max(age) from student);
注意点:=后面使用()
子sql结果只能是一个值才能使用=
子sql结果得到多个值,那么只能使用in
update ... set ...修改
update+表名+set+字段名=修改值 where+条件;
例如:把zhaoliu的性别改为女性:update student set sex='女' where name='zhaoliu';
注意:字段最好用id号,名字会有出现重复的时候
例如:把学号是1和3的学生年龄都改为27岁:update student set age=27 where id=1 or id=3;
delete删除
delete from+表名;清空所有数据,保留表结构
delete from+表名 where+条件;删除指定的一个数据
例如:删除id是3和4的数据:delete from student where id=3 or id=4;