MySql数据库操作
1、事务?
事务可以保证数据恢复到初始状态
事务的四大特征:
A:原子性:不成功便成仁
B:隔离性:事务之间相互隔离,互不干扰
C:一致性:事务的前后状态保持一致
D:持久性:事务一旦提交或回滚,将永久保存
使用事务的语法:
Begin:开启事务
Commit:提交事务
Rollback:回滚事务
2、插入数据
注:所有符号都是英文状态,字符串使用单引号,自动增长的字段不能插入(如设置主键为自动增长)
Insert into 表名(字段列表)values(值列表)
(1):标准插入 insert into student(stu_name,stu_age) values(‘小华’,12);
(2):如果全插入 insert into student values(‘小华’,12);
(3):一次插入多条 insert into student values(‘小华’,12),(‘小华’,12)(‘小华’,12);
3、更新数据
Update 表名 set 字段1 = 值1 ,字段2 = 值2,字段3 = 值3 where 条件
A:如果没有where条件会更新表里所有字段
B:set表示指定要更新的字段
C:where指定更新的范围
D:执行顺序,先执行where再执行set
E:多条件逻辑
a:and->(java)&&
b:or->(java)||
c:not->(java)!
(1)更新所有学生信息
Update student set stu_age=20;
(2)更新ID=1的学生
Update student set stu_name=’小小’,stu_age=13 where id=1;
(3)更新ID=1和stu_name=小小的同学的年龄
Update student set stu_age=23 where id=1 and stu_name=’小小’;
(4)更新ID!=1的同学的信息
Update student set stu_age=24 where not id=1;
(5)更新ID=1或stu_name=小小的同学的信息
Update student set stu_age=25 where id=1 or stu_name=’小小’;
4、删除数据
Delete from 表名 where 条件
A:where条件表明要删除的数据
B:没有where条件默认删除全部数据
(1)删除所有数据
Delete from student;
(2)删除ID=1的学生
Delete from student where id=1;
5、查询数据
Select 字段列表 from 表名 (where 条件) where子句可选
A:where子句决定能查询出多少行
B:select子句决定我要现实多少字段
a:条件查询
常用运算符:
比较: >、>=、<、<=、=、!=、<>
逻辑:and、or、not
范围:in、between...and
模糊匹配:like
(1)查询所有学生信息(*)表示所有
Select * from student;
(2)查询id=1的所有学生
Select * from student where id=1;
(3)查询所有学生的stu_name,stu_age
Select stu_name,stu_age form student;
(4)查询stu_age>20且id=1的所有学生
Select * from student where stu_age>20 and id=1;
(5)IN关键字的使用(查询id包含在in里的所有学生)
Select * from student where id in(1,2,3);
(6)Between...and关键字的使用(查询年龄在20到25之间的所有学生)
Select * from student where stu_age between 20 and 25;
(7)Like关键字的使用(查询学生姓名以王开头的所有学生)
Select * from student where stu_name like’王%’;
(8)Like关键字的使用(查询学生姓名以王结尾的所有学生)
Select * from student where stu_name like’%王’;
(9)Like关键字的使用(查询学生姓名中有王的所有学生)
Select * from student where stu_name like’%王%’;
b:分页查询
在mysql当中,可以同limit限制查询条数。limit是mysql特有关键字。再其他关系型数据库当中不适用。
limit n,m or limit m
表示:从n条记录开始取出m条记录,如果是后者只有一个m的情况,则n从0开始。n = (p - 1)*m ; p表示页数(从1开始),
(1)分页查询学生信息,每页显示3条记录
Select * from student where limit 0,3;//第一页
Select * from student where limit 3,3;//第二页
Select * from student where limit 6,3;//第三页
(2)分页查询学生信息,每页显示3条记录
Select * from student where limit 3;
c:分组查询
统计函数:
sum():求和
max():最大值
min():最小值
avg():平均数
count():记录总数
分组子句:
group by 字段名,表示在当前查询表中,需要用哪个字段进行分组。
分组统计语法:
select 字段,统计函数 from 表名 [where 条件] group by 字段 [having 分组过滤条件]
分组之前是用where子句进行数据过滤,分组之后是用having子句进行过滤
(1)查询所有学生年龄的和
Select sum(stu_age) from student;
(2)查询所有学生年龄的最大值
Select max(stu_age) from student;
(3)查询所有学生年龄的最小值
Select min(stu_age) from student;
(4)查询所有学生年龄的平均值
Select avg(stu_age) from student;
(5)查询所有学生个数
Select count(*) from student;
(6)对性别分组求对应的人数
SELECT stu_gender,COUNT(*) from student GROUP BY stu_gender;
结果为:男:2,女:3
(7)对年龄分组,并且18>stu_age>15
SELECT stu_age,COUNT(*) from student GROUP BY stu_age HAVING stu_age >15 and stu_age < 18;
结果为:16:2,17:3
(8)子查询,查询年龄大于平均年龄的所有学生
SELECT * from student where stu_age >(SELECT avg(stu_age) from student);
(9)在结果集里查询,在年龄大于平均年龄的所有学生中查询性别为男的学生
SELECT * from (SELECT * from student where stu_age >(SELECT avg(stu_age) from student)) as stu where stu.stu_gender='男';
d:关联查询
将多个表的数据连接再进行查询
(1)内联查询(inner join):默认查询方式
语法:
select 表名.字段名 from 表1 inner join 表2 on 表1.外键字段 = 表2.主键字段
SELECT * from teacher t INNER JOIN student s on s.stu_id = t.id;(这里s,t分别为两个表的别名,stu_id为student表的外键与teacher主键id关联)
(2)左关联查询(left outer join):左关联会显示左表的所有数据
SELECT * FROM teacher t LEFT OUTER JOIN student s on t.id=s.stu_id ;
(3)右关联查询(right out join):右关联会显示右表的所有数据
SELECT * FROM teacher t RIGHT OUTER JOIN student s on t.id=s.stu_id ;
e:排序
排序是通过order by 排序字段 排序类型
排序的类型:升序(asc),降序(desc),默认情况:升序
(1)按学生ID升序
Select * from student order by id asc;
(2)按学生ID降序
Select * from student order by id desc;