update emp set name="lisi",age=10where empno=1001;
数据删除
deletefrom emp where empno=1001;
数据查询(重要)
-- 基本格式select*|结果列1,结果列2...from 表名
where 条件 -- 条件过滤groupby 字段1,字段2..-- 分组having 条件 -- 分组后条件过滤orderby 字段 -- 排序limit-- 限制查询条数-- 给表或列取别名selectround(pi()*2*2) area from dual;select empno from emp e;-- 关于null查询-- 背景:COMM列某些数据为nullselect sal+comm from emp;-- 错误,null不能直接参与运算select sal+ifnull(comm,0)from emp;select sal+comm from emp where comm isnotnull;-- 条件组合查询 and or not between...andselect*from emp where sal>=1000and sal<=2000;select*from emp where sal between1000and2000;select*from emp where sal<1000or sal>2000;select*from emp where sal notbetween1000and2000;-- 排序select*from emp orderby sal desc,comm asc;-- 消除重复记录selectdistinct deptno from emp;-- 限制查询条数,实现分页-- 起始索引=(页数-1)*每页显示记录条数select*from emp limit 起始索引,记录条数;-- 假设每页显示n条记录,显示m页select*from emp limit(m-1)*n,n;-- 分组查询-- 通常和集合函数一起使用-- 如果不分组,所有记录默认一组selectcount(*),sum(sal),max(sal),min(sal),round(avg(sal),2)from emp;selectcount(*),sum(sal),max(sal),min(sal),round(avg(sal),2)from emp groupby deptno;-- 组函数不能作为where的查询条件,必要时使用子查询select*from emp where sal>avg(sal);-- 错误select*from emp where sal>(selectavg(sal)from emp);-- 分组后select的查询列只能为分组列或者组函数,不能查询其他任意列select deptno,count(*)from emp groupby deptno;-- where在分组前过滤,having在分组后过滤select deptno,avg(sal) avgsal from emp groupby deptno having avgsal>2000;-- 可以多字段分组selectcount(*)from emp groupby deptno,job;-- 联合查询,多张表查询select ename,emp.deptno,dname
from emp,dept
where emp.deptno=dept.deptno;-- 内连接-- 两张表中都只有符合关联条件的记录才能被显示select ename,emp.deptno,dname
from emp innerjoin dept
on emp.deptno=dept.deptno;-- 外连接-- 某张表中的所有记录和另一张表中符合关联条件的记录-- 左连接,左边表所有记录全显示select empno,empname,emp,deptno,dname
from emp leftjoin dept
on emp.deptno=dept.deptno;-- 右连接,右边表所有记录显示select empno,empname,emp,deptno,dname
from emp rightjoin dept
on emp.deptno=dept.deptno;-- 自连接,物理上一张表,逻辑上两张表select e1.empno,e1.ename,e2.empno,e2.ename
from emp e1 leftjoin emp e2
on emp1.mgr=emp2.empno;-- 不等连接 关联条件为不等关系select empno,ename,sal,grade
from emp,salgrade
where sal>losal and sal<hisal;-- 子查询,将一条查询语句的结果作为另一条查询语句的条件-- any some 比其中某一个怎么样即可select*from emp where sal>any(select sal from emp where deptno=20);-- all 比所有select*from emp where sal>all(select sal from emp where deptno=20);-- exists 当子查询有结果时执行外层查询,否则不执行select*from emp whereexists(select ename from emp where deptno=10);-- in 类似逻辑判断orselect*from emp where deptno=10or deptno=20;select*from emp where deptno in(10,20);select*from emp where deptno in(select deptno from emp where ename in("SCOTT","BLAKE"));-- 合并查询-- union all 合并两条语句查询结果-- union 合并查询结果并去重select empno,ename,emp.deptno,dname from emp leftjoin dept on emp.deptno=dept.deptno
unionselect empno,ename,emp.deptno,dname from emp rightjoin dept on emp.deptno=dept.deptno;-- 正则表达式select ename from emp where ename regexp"^m";-- 不区分大小写