show databases; ------- 查看当前目录的数据文件夹
use 文件名字; ------- 切换数据文件夹
1.插入数据
insert into 表的名字(表的字段) values (对应的值);
insert into student(xh, xm, xb) values('2017082802002', '张三', 0);
插入多条数据
insert student (xh, xm, xb)
values
('2017082802003', '张1', 1),
('2017082802004', '张2', 0),
('2017082802005', '张3', 0),
('2017082802006', '张4', 1),
('2017082802007', '张5', 0),
('2017082802008', '张6', 1);
2.数据查询
select 要查询的字段 from 要查询的表 where 要查询的条件;
// where子句:用来筛选 from 子句中指定的操作所产生的行
// group by子句:用来分组 where 子句的输出
// having子句:用来从分组的结果中筛选行
// order by子句:用来排序【desc降序,默认是asc升序可以不写】
select <列名>
from <表名>
[where <查询条件表达式>]
[group by <分组条件表达式>]
[having <筛选条件表达式>]
[order by <排序的列名>[asc或desc]]
[limit offSet,pageNum [offSet开始索引,pageNum显示条数] ]
select *
from student
where Sname like "%阳%"
group by Sno
having Sage > 20
order by Sno desc;
例如:
//在student学生表中查询所有字段,* 代表所有字段
select * form student;
//在student学生表中查找考生号,学号,姓名的字段
select ksh,xh,xm from student;
//在学生表中查找学号为2017001和考生号为995201314的数据
select ksh,xh,xm from student where xh='2017001' and ksh='995201314';
//在学生表中查找学号为2017001或考生号为995201314的数据
select ksh,xh,xm from student where xh='2017001' or ksh='995201314';
like(模糊查找)
select * from student where xm like "%张"; //以张结尾
select * from student where xm like "张%"; //以张开始
select * from student where xm like "%张%"; //包含张
between a and b(在a和b两个数之间)
select * from student where Sage between 20 and 25; //查询学生表的年龄在20~25之间(包括20和25)
not between a and b(不在a和b两个数之间)
select * from student where Sage not between 20 and 25; //查询学生表的年龄不在20~25之间(20和25也不包括)
in(特定的集合)(是在特定的集合里)
select * from student where Sdepartment in("计算机系","法学系"); //查询学生表中计算机系和法学系的学生
not in(特定的集合)(不在特定的集合里)
//查询学生表中除计算机系和法学系外的其他系学生信息
select * from student where Sdepartment not in("计算机系","法学系");
order by (默认按照升序对记录进行排序)
//将满足条件的数据查询出来再按xh升序排序,再按xm升序排序【asc表示升序排列可省略】
select *
from student
where xm like "%张%"
order by xh asc,xm asc;
//将满足条件的数据查询出来再按xh降序排序,再按xm降序排序【desc表示降序排列不可省略】
select *
from student
where xm like "%张%"
order by xh desc,xm desc;
is null(当列的值是 null)
select * from score where grade is null; //查询成绩表里面是否有成绩为空的数据
is not null(当列的值不为 null)
select * from score where grade is not null; //查询成绩表里面的成绩为非空的数据
group by(根据(by)一定的规则进行分组(Group))
select Sno,avg(grade) from score group by Sno; //查询每个学生的学号和平均成绩并按照学号进行分组
having(对分组后的结果进行条件限制;也可以直接限制但不能加聚合函数)
//查询学号和平均值根据学号且平均值大于70的进行分组
select Sno,avg(grade)
from score
group by Sno
having avg(grade) > 70;
//或【注意:不能加聚合函数】
select * from score having grade >= 70;
limit (对查询的显示结果限制数目)
select Sname,Sage from student order by sage desc limit 3; //查询年龄最大的前3个学生的姓名和年龄
select Sname,Sage from student order by sage desc limit 3,2; //查询年龄最大的第4、5个学生的姓名和年龄
distinct(去掉重复的记录)
select distinct Sno from score; //查询选修了课程的学生学号(去掉重复的记录)
count(记数函数:统计元素总个数)
select count(Sno) from student; //按学号统计学生表的总人数
select count(distinct Sno) from score; //查询选修了课程的学生人数
sum(求和函数:对某一列的值求和,但属性必须是整型)
select sum(courseId) from course; //查询课程表id的总和
avg(计算平均值:对某一列的值计算平均值)
select avg(Sage) from student; //求学生表中所有学生年龄的平均值,varchar类型的也适用
max(求最大值:找出某一列的最大值)
select max(Sage) from student; //求学生表中所有学生年龄的最大值,varchar类型的也适用
min(求最小值:找出某一列的最小值)
select min(Sage) from student; //求学生表中所有学生年龄的最小值,varchar类型的也适用
连接查询(经常用左连接)
select * from a left join b on a.字段=b.字段;【左连接】
select * from student left join sc on student.sno = sc.sno;
select * from a right join b on a.字段=b.字段;【右连接】
select * from student right join sc on student.sno=sc.sno;
select * from a inner join b on a.字段=b.字段;【等值连接】
select * from student inner join sc on student.sno=sc.sno;
双表连接查询
//从学生表和成绩表中查询学生表的学号、姓名和成绩表的平均成绩,用学生表的学号和成绩表的学号连接再用学号来排序
select s.Sno,Sname,avg(grade) from student s,score sc where s.Sno=sc.Sno group by Sno;
//从学生表和成绩表中查询学生表的学号、姓名和成绩表的平均成绩
//用学生表的学号和成绩表的学号连接再用学号进行排序并去掉成绩小于70的学生
select s.Sno,Sname,avg(grade)
from student s,score sc
where s.Sno=sc.Sno
group by Sno
having avg(grade) > 70;
三表左连接查询
select * from (t1 left join t2 on t1.t1_id=t2.t1_id) left join t3 on t1.t1_id=t3.t1_id;
select *
from (student left join sc on student.sno=sc.sno)
left join course on student.courseid=course.courseid;
//或者
select *
from student s
left join score sc
on s.sno=sc.sno
left join course c
on c.courseid=sc.courseid;
或者
select * from t1,t2,t3 where t1.id = t2.id and t1.sno = t3.sno;
select * from student s,course c,sc where s.courseid = c.courseid and s.sno = sc.sno;
3.数据修改
update 要修改的表 set 表字段 = 某个值,表另一个字段 = 某个值 where 条件
update student set xzb="2017城乡规划2班" where xh="2017088821821";
4.数据删除
delete from 表的名称 where 删除条件; (delete用于删除表中的数据)
例如:
delete from student where id = 3; //删除id为3的行数据
delete from student where age < 20; //删除所有年龄小于20岁的数据
delete from student; //删除表中的所有数据
5.数据类型
5.1数字类型
类型 |
大小 |
范围(有符号) |
范围(无符号) |
用途 |
tinyint |
1 字节 |
(-128,127) |
(0,255) |
小整数值 |
smallint |
2 字节 |
(-32 768,32 767) |
(0,65 535) |
大整数值 |
mediumint |
3 字节 |
(-8 388 608,8 388 607) |
(0,16 777 215) |
大整数值 |
int或integer |
4 字节 |
(-2 147 483 648,2 147 483 647) |
(0,4 294 967 295) |
大整数值 |
float |
4 字节 |
(-3.402 823 466 E+38,-1.175 494 351 E-38),0,(1.175 494 351 E-38,3.402 823 466 351 E+38) |
0,(1.175 494 351 E-38,3.402 823 466 E+38) |
单精度 |
decimal |
对DECIMAL(M,D) ,如果M>D,为M+2否则为D+2 |
依赖于M和D的值 |
依赖于M和D的值 |
小数值 |
5.2日期和时间类型
类型 |
大小 |
范围 |
格式 |
用途 |
datetime |
8字节 |
1000-01-01 00:00:00/9999-12-31 23:59:59 |
YYYY-MM-DD HH:MM:SS |
混合日期和时间值 |
timestamp |
4字节 |
1970-01-01 00:00:00/2038 结束时间是第 2147483647 秒,北京时间 2038-1-19 11:14:07,格林尼治时间 2038年1月19日 凌晨 03:14:07 |
YYYYMMDD HHMMSS |
混合日期和时间值,时间戳 |
5.3字符串类型
类型 |
大小 |
用途 |
char |
0-255字节 |
定长字符串 |
varchar |
0-65535 字节 |
变长字符串 |
text |
0-65 535字节 |
长文本数据 |
6.常用数据类型
分类 |
备注和说明 |
数据类型 |
说明 |
二进制数据类型 |
存储非子符和文本的数据 |
BLOB |
可用来存储图像 |
文本数据类型 |
字符数据包括任意字母、符号或数字字符的组合 |
char |
固定长度的非 Unicode 字符数据 |
varchar |
可变长度非 Unicode 数据 | ||
text |
存储长文本信息 | ||
日期和时间 |
日期和时间在单引号内输入 |
time |
时间 |
date |
日期 | ||
datetime |
日期和时间 | ||
数值型数据 |
该数据仅包含数字,包括正数、负数以及浮点数 |
int smallint |
整数 |
float double |
浮点数 | ||
货币数据类型 |
用于财务数据 |
decimal |
定点数 |
Bit数据类型 |
表示是/否的数据 |
bit |
存储布尔数据类型 |