oracle 学习笔记1

本文详细介绍了SQL查询的基础语法及高级技巧,包括条件筛选、排序、分组、子查询、多表联接等操作,同时提供了实用的示例帮助读者理解和掌握SQL查询的关键概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

select distinct --,-- from --; //查询指定行,非重复
set timing on; //打开时间显示
select count(*) from --; //计数
select -- '' -- from --; //显示的名字的别名
nvl(comm,0)  //如果comm未空,则用0来替代
select -- from -- where hiredate>'1982-01-01'; //筛选日期
select * from -- where -name like 'S%';   //S开头的名字
select * from -- where -name like '__O%';   //第三个字母为O的名字
select * from -- where -name in ('zhangsan','lisi','wangwu');   //
order by    //排序
order by -- desc;  //降序
select * from -- order by a1, a2 desc; //先按照a1升序排,再按照a2降序排列
select name sal*12 as '年薪' from -- order by '年薪';
子查询
select min(sal),max(sal) from --; //最大,最小,分组函数 (avg,count)
select max(sal) avg(sal) deptno from emp group by deptno; //分组查询
select max(sal) avg(sal) deptno from emp group by deptno having avg(sal)>2000; //分组查询平均工资大于2000的部门
顺序是group by, having, order by
select a1.ename,a1.sal,a2.deptname from emp,dept where a1.deptno=a2.deptno; //多表查询
多表查询的原则:查询结果至少小于表的个数-1
select a1.ename,a1.sal,a2.grade from emp a1, salgrade a2 where a1.sal between a2.losal and a2.hisal; //雇员工资级别
//自连接
select worker.ename,boss.ename from emp worker,emp boss where worker.mgr=boss.empno; //自连接,把一张表当成两张表来查询,查雇员的老板名称
筛选条件是从右到左,因此要把条件尽可能的写到_____
//多行子查询
select ename, job form emp where job in(select dinstinct job from emp); //同工种
select ename,sal,deptno from emp where sal>all(select sal from emp where deptno=20); //工资大于所有的
select ename,sal,deptno from emp where sal>any(select sal from emp where deptno=20); //工资大于任意一个
select deptno,job from emp where (deptno, job)=(select deptno, job from emp where ename='SMITH'); //多条件查询


//查询工资大于本部门平均工资的员工,内嵌视图,必须给子查询指定别名
select a1.ename a1.sal a2.mysal from emp a1, (select deptno avg(sal) mysal from emp group by deptno) a2 where a1.deptno=a2.deptno and a1.sal>a2.mysal;
列可以加别名,但是表不能家别名,as
//分页查询  oracle有三种分页方式:1、rowid   2、rownum 3、分析函数
select a1.*,rownum rn from (select * from emp) a1;
select a1.*,rownum rn from (select * from emp) a1 where rownum>=10; //查询前10行
select * from (select a1.*,rownum rn from (select * from emp) a1 where rownum>=10) where rownum>=6; //查询6-10行,必须子查询
select * from (select a1.*,rownum rn from (select * from emp order by sal) a1 where rownum>=10) where rownum>=6;
//所有的改动只需要改最里面的,按工资排序

create table myemp (id, ename,sal) as select empno,ename,sal from emp; //利用旧表迅速创建新表,可做拷贝和实验数据用


//合并查询  union , union all,  intersect, minus
union //合并两张表,并自动去掉重复的记录
union all//合并两张表,不会去掉重复的记录
intersect   //取两个集合的交集
minus   //取两个集合的差集,非包含关系则结果为空


创建数据库工具:database configuration assistant




insert into -- values('','');
alter session set nls_date_format='yyyy-mm-dd'; //更改默认的日期格式
select * from -- where -- is null;
select * from -- where -- is null; //判断空的值
update -- set --='' where --=''; //更新数据
delete from --; // 删除数据,记录日志
savepoint --; rollback to --; //保存+回滚  日志
drop table --; //删除数据和表结构,记录日志
truncate table --; //删除数据和表结构,不记录日志,比较快




desc table //查看表
alter table -- add (-- number(2)); //修改表格
alter talbe -- modify(-- number(3));
alter talbe -- drop colomn --; //删除一个字段




图片型: blob
日期型:date / timestamp
数字型:number
字符型:char (2000)(效率高)  /  vchar2(4000)  / clob (4G)


disc/passw/show user/start *.sql/edit/spool/conn
用户: system/sys/scott
系统权限
--对数据库的相关权限(140多种)
对象权限
--数据对象的权限 (25种)
角色  connect/dba/resource











create profile  -- limit password_life_time 10 password_grace_time 2 password_reuse_time 10
旧密码更换10天后可重新使用
drop profile -- cascade //删除profile文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值