Mysql基础知识点汇总

2019-01-04 20:53:14王旭

select subjectid fromSubjectwhereSubjectName='office'--2.查询出这一科目的考试日期select MAX(ExamDate)fromResultwhereSubjectId=(select subjectid fromSubjectwhereSubjectName='office')--3,写出查询的框架select MAX(StudentResult),MIN(StudentResult)fromResultwhereSubjectId=()andExamDate=()--4.使用子查询做为条件select MAX(StudentResult),MIN(StudentResult)fromResultwhereSubjectId=(select subjectid fromSubjectwhereSubjectName='office')andExamDate=(select MAX(ExamDate)fromResultwhereSubjectId=(select subjectid fromSubjectwhereSubjectName='office'))

16.表连接Join

--1.inner join :能够找到两个表中建立连接字段值相等的记录
--查询学员信息显示班级名称
select Student.StudentNo,Student.StudentName,grade.classname
from Student
inner join grade on Student.ClassId=grade.ClassId
--左连接: 关键字前面的表是左表,后面的表是右表
--左连接可以得到左表所有数据,如果建立关联的字段值在右表中不存在,那么右表的数据就以null值替换
select PhoneNum.*,PhoneType.*
from   PhoneNum 
left join  PhoneType on PhoneNum.pTypeId=PhoneType.ptId
--右连接: 关键字前面的表是左表,后面的表是右表
--右连接可以得到右表所有数据,如果建立关联的字段值在右左表中不存在,那么左表的数据就以null值替换
select PhoneNum.*,PhoneType.*
from   PhoneNum 
right join  PhoneType on PhoneNum.pTypeId=PhoneType.ptId
--full join :可以得到左右连接的综合结果--去重复
select PhoneNum.*,PhoneType.*
from   PhoneNum 
full join  PhoneType on PhoneNum.pTypeId=PhoneType.ptId

17.事务

一种处理机制。以事务处理的操作,要么都能成功执行,要么都不执行。

事务的四个特点 ACID:

A:原子性:事务必须是原子工作单元;对于其数据修改,要么全都执行,要么全都不执行。它是一个整体,不能再拆分
C:一致性:事务在完成时,必须使所有的数据都保持一致状态。。某种程度的一致
I:隔离性:事务中隔离,每一个事务是单独的请求将单独的处理,与其它事务没有关系,互不影响
D:持久性:如果事务一旦提交,就对数据的修改永久保留
使用事务:

将你需要操作的sql命令包含在事务中。

1.在事务的开启和事务的提交之间
2.在事务的开启和事务的回滚之间

三个关键语句:

开启事务:begin transaction
提交事务:commit transaction
回滚事务:rollback transaction
declare @num int =0 --记录操作过程中可能出现的错误号
begin transaction
  update bank set cmoney=cmoney-500 where name='aa'
  set @num=@num+@@ERROR
  --说明这一句的执行有错误  但是不能在语句执行的过程中进行提交或者回滚