oracle学习笔记(第五章:数据字典)

怎样修改oracel数据库的默认日期?
alter session set nls_date_format='yyyymmddhh24miss'
查看表的注释
select comments from user_tab_comments where table_name = 'empm';
查看列注释
select comments from user_col_comments where table_name= 'empm' and column_name='emp_nm';
为表或列加注释
comment on table table_nm is '基本信息表';
comment on column table_nm.col_nm is '工号';
伪列﹕rowid,rownum
select * from empm where ROWNUM <= 10 --查询一个表的前10行记录
删除重复记录,只保留第一条
delete from dumpy_part a
where a.rowid <>(select min(rowid) from dumpy_part b where a.id = b.id )
在select 语句中 distinct 和 Unique的用法相同
使用了distinct 或unique 后,order by 后面的列需要出现在select列表中
例如:tab_1表中有a,b,c三列;select distinct a,b from tab_1 order by c;这样写会报错,原因

update/insert 与 select 的配合
UPDATE empm SET emp_age =(SELECT age + 10 FROM empm WHERE emp_no = '0001')
INSERT INTO empm_copy SELECT * FROM empm

外连接 替代 not in ,可以提高查询效率
SELECT emp_no FROM empm WHERE emp_no NOT IN (SELECT emp_no FROM empimg)
--等价于(但是效率较高)
SELECT empm.emp_no,empimg.emp_no from empm,empimg where empm.emp_no=empimg.emp_no(+) AND empimg.emp_no IS NULL;

单行子查询包括﹕where单行子查询,having单行子查询﹐from单行子查询
---------where单行子查询
select * from empm
where emp_no =(select emp_no from empimg where emp_no='123');
---------having单行子查询
select avg(age),min(age),max(age)
from empm
having avg(age)> min(age)

---------from 子句中的单行查询子查询
select emp_nm from empm a,(select emp_no, emp_nm from emp_d) x
where a.emp_no=x.emp_no

---------多行子查询﹐包括﹕in 多行子查询﹐all 多行子查询﹐any 多行子查询
---------in 多行子查询
select * from empm where emp_no in (select emp_no from empimg)
---------any 多行子查询
select * from empm where age < any (select avg(age) from empm group by emp_no);

select * from empm where eage = any (select avg(age) from empm group by emp_no);
等价于
select * from empm where age in (select avg(age) from empm group by emp_no)
--------any 多行子查询
select * from empm
where age > all (select avg(age) from empm)

-------多列子查询﹕与单列子查询不一杨﹐多列子查询要返回多列﹐多列子查询
-------又分为成对比较子查询和非成对比较子查询
-------成对子比较多列查询
-------哪些员工的年龄在本部门最高﹖
select emp_no,dept_no,age from empm
where (dept_no,age) in (select dept_no,max(age) from empm group by dept_no)
------非成对比较多列子查询
------哪些员工的年龄与某一职位的最高工资相同
select emp_no,dept_no,age from empm
where age in (select max(age) from empm group by dept_no)
and dept_no in (select distinct dept_no from empm)


-- WITH AS 的用法:将查询结果放在临时表中
WITH
T1 AS
(SELECT EMP_NO,EMP_NM,SEX,AGE FROM empm WHERE sex='1'),
T2 AS
(SELECT EMP_NO,EMP_NM,SEX,AGE FROM empm WHERE AGE<=35)
SELECT * FROM T1,T2 WHERE T1.EMP_NO=T2.EMP_NO;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值