DML (对数据操作) insert into (插入数据) update set(更新数据) delete from (删除数据)
where 查询条件
where 条件中使用数字值,字符值,日期值
between and 操作符, not between and 匹配,不匹配检索
= ,<> (!=),< , > ,<=, >= , 比较操作符
like操作符,in操作符, not in操作符
逻辑操作符号 and , or, not ,
查询 null
create table t_student (
stuid number (10) primary key,
stuname varchar2(10),
sex char (4),
claid number(2)
);
select * from t_student;
drop table t_student;
插入数据 insert into <表名> values (字段值) 字段要是全写出来顺序和字段要对应
insert into t_student values (1,'李大','男',6);
commit;
insert into t_student (stuid,stuname,sex ,claid) values (2,'李小','男',6);
commit;
insert into t_student (stuid) values (1);
commit;
删除数据 delete from <表名> where 条件
delete from t_student where stuid = 1;
commit;
更新数据 update <表名> set 条件/多个条件 逗号隔开
update t_student set sex ='女';
update t_student set claid = 6 where stuid = 3 ;
commit;
update t_student set sex ='男' where stuid =2;
commit;
update t_student set stuname = '李晓',sex ='女' ,claid = 5 where stuid =1;
commit;
查询数据!!!!!!!!!!!!!!!
语法 : select <*, column列名称,...> from <表名>
select * from emp;
select stuid,stuname from t_student;
empno 员工编号(工号) ename 员工姓名 job 职位 mgr 上司编号 hiredate 入职日期
sal 工资 comm 奖金 deptno 部门编号
不等于 <> , !=
select * from emp where mgr <> 7839;
select * from emp where mgr != 7839;
在什么什么之间的值 between
语法:select * from 字段名 between 值 and 值 ;
select * from emp where sal between 1000 and 5000;
select * from emp where sal not between 1000 and 5000;
select * from emp where sal >=1000 and sal <= 5000;
模糊查询 in(等于) , not in(不等于)
语法:select * from <表名> where 字段名 in (字段值);
select * from emp where sal in(3000,5000);
select * from emp where sal not in(3000,5000);
排序 默认是升序(asc),降序(desc)
语法:select * from <表名> 字段名 order by asc/desc;
select * from emp order by sal asc; -- 升序
select * from emp order by sal desc; -- 降序
select * from emp order by sal; -- 默认升序排序
select * from emp order by empno,sal asc; -- 多个字段排序
as 起别名
select ename as name,sal ,job from emp;
select sal,to_char(hiredate,'yyyy/mm/dd')as guyong from emp
to_char
select sal,to_char(hiredate,'yyyy-mm-dd') as ruzhi from emp ;
select sal,to_char(hiredate,'yyyy/mm/dd') as ruzhi from emp ;
to_date
select sal,to_date(hiredate) as ruzhi from emp ;
去重(distinct)
select distinct ename from emp;
算数表达式 select 字段名1,字段名2 +50 from <表名>; nvl
select ename ,sal+50 from emp;
--将工资为空的变成0 进行加运算
select ename ,nvl(sal,0)+50 from emp;
字符串连接符
select ename || '_' || sal as res01 from emp;
模糊查询 % _
select * from emp where ename like 'S%'; --以s开头
select * from emp where ename like '%S'; --以s结尾
select * from emp where ename like '%S%'; --只要有s
select * from emp where ename like '%M__'; --倒数第三位是o的名字
判断一个字段是不是空 is null 和 is not null
select * from emp where comm is null;
select * from emp where comm is not null;
分组 group by
-- 除了distinct 之外,又get到可以除重的一个方法
select deptno from emp group by deptno
分组函数(聚合函数)
count :用户取得总计行数
max : 取得该列的最大值,适用于任何类型
min :取得该列的最小值。适用于任何类型
avg :取得该列的平均值。 只适用于数字类型
sum :取得该列的合计值。只适用于数字类型
select count(empno) from emp
select max(sal) from emp
select min(sal) from emp
select avg(sal) from emp
select sum(sal) from emp
显示每个部门的最高工资
按职位
select job,max(sal),min(sal) from emp group by job
先按部门分组,然后再按职位
select deptno,job,max(sal),min(sal) from emp where deptno in(20,30)
group by deptno,job
order by deptno
显示每个部门的平均工资,并且平均工资小于2000的部门
使用 having子句,限制分组的结果
select deptno,avg(sal),max(sal) ,min(sal) from emp
group by deptno
having avg(sal) < 2000
1,分组函数只能出现在 group by,having,order by 子句
2,如何查询语句中,有groupby,having,order by 则order by 必须放到最后
3,如果出现 having,必须跟 group by 同时出现
4,如果查询语句出现 查询列,表达式,分组函数,则 查询列 和 表达式 必须出现在 group by 子句中
5, 当使用条件限制分组结果的显示时,必须使用 havaing子句,而不是在 where子句中使用分组函数
相等查询
显示所有员工的名称,工资,以及所在部门的名称
select e.ename,e.sal,d.dname from dept d,emp e
where d.deptno=e.deptno
and e.deptno=10
order by d.dname
不等连接
显示所有员工的名称,工资,以及工资级别
select e.ename,e.sal,s.grade from emp e,salgrade s
where e.sal between losal and hisal
select * from salgrade
自连接, 一个表起两个名字,当2个表用
显示 BLAKE 的 上司 是谁?
select e1.ename as myself,e2.* from emp e1,emp e2
where e1.ename in('SMITH','ALLEN','MARTIN')
and e2.empno=e1.mgr
内连接 inner join
显示部门10的部门名称,以及所属员工信息
select d.deptno,e.* from dept d inner join emp e
on d.deptno=e.deptno
where d.deptno=10
select d.deptno,d.dname,e.ename from dept d inner join emp e
on d.deptno=e.deptno
where e.deptno=10
左外连接 left join
不仅会返回满足连接条件的所有记录,还会返回不满足连接条件的左外表的其他行
显示部门10 部门名,员工名,以及其他部门名
select d.dname,e.ename from dept d left join emp e
on d.deptno = e.deptno
and e.deptno=10
右外连接 right join
不仅会返回满足连接条件的所有记录,还会返回不满足连接条件的 右表的其他记录
显示部门10 部门名,员工名,以及其他员工名
select d.dname,e.ename from dept d right join emp e
on d.deptno=e.deptno
and d.deptno=10
完全外连接 full join
不仅会返回满足连接条件的所有记录,还会返回不满足连接条件的其他记录
显示部门10 的部门名,员工名,以及其他的部门名和员工名
select d.dname,e.ename from dept d full join emp e
on d.deptno=e.deptno
and d.deptno=10
order by d.deptno
嵌套查询(也叫子查询)
将一个查询块嵌套到另一个查询块的 where子句中或having 短句中的查询语句成为嵌套查询,也叫子查询
显示 SCOTT同事的姓名,工资, 部门信息
select ename,sal,deptno from emp
where deptno=(select deptno from emp where ename='SCOTT')
order by sal
多行子查询
查询部门高于10 的信息
select * from emp
where deptno>all(select deptno from emp where deptno=10)
查询高于部门30的 所有员工工资的 员工的信息
方式1:
select * from emp
where sal >all(select sal from emp where deptno=30)
方式2:
select * from emp
where sal > (select max(sal) from emp where deptno=30)
查询高于部门30的 任一员工工资的 员工的信息
方式1:
select * from emp where sal>any(select sal from emp where deptno=30 )
方式2:
select * from emp where sal >(select min(sal)from emp where deptno =30)
多列子查询
查询跟 SMITH 部门和岗位都相同的所有员工
select * from emp
where deptno=(select deptno from emp where ename='SMITH')
and job=(select job from emp where ename='SMITH')
order by deptno
查询工资和奖金 与部门30 员工的都相同的员工信息
select * from emp
where (sal,comm)in(select sal,comm from emp where deptno =30)
查询 工资匹配 部门30 , 奖金也配置部门30的员工信息
select * from emp where
sal in(select sal from emp where deptno=30)
and comm in(select comm from emp where deptno=30)
合并查询 union ,union all, intersect, minus
合并操作时,必须满足每个项目的查询项目的个数、数据类型保持一致。跟查询的表没有关系
并集 union(除重) ,union all(不除重)
查询工资高于 2500的员工,和 职位是MANAGE的员工
select * from emp where sal>2500
union
select *from emp where job='MANAGER'
select *from emp where sal >2500
union all
select *from emp where job ='MANAGER'
交集 intersect
显示工资高于 2500 和岗位为经理的员工
select * from emp where sal>2500
intersect
select * from emp where job='MANAGER'
差集 minus(第一部分有,第二部分没有的)
查询工资高于2500的员工 和岗位不是经理
select * from emp where sal>2500
minus
select * from emp where job='MANAGER'
存在查询 exists(子查询有返回结果,为ture) ,not exists(子查询无返回结果,为false)
存在查询能替换大部分场景下的in,能用exists解决的无问题一定要用exists
1可以用'X'代替,也可以用*,但是用* 效率比较低
查询所有有员工的部门
select *from dept
where exists(select 1 from emp where dept.deptno=emp.deptno)
查询所有 没有有员工的部门
select * from dept where not exists
(select 1 from emp where dept.deptno=emp.deptno)
删除数据 truncate ,后面不能跟where条件
truncate table lxx