Decode函数用来实现IF~ELSE 的逻辑功能 case--when
select
ename,job,sal,
Decode(job --if要判断的字段
'ANALYST',sal*1.2, --条件
'MANAGER',sal*3,
'PRESIDENT',sal*1.5,
'SALESMAN',sal*2,
sal --默认
) yearcomm
from emp;
--
select
ename,job,sal,
case job --if要判断的字段
when' ANALYST' then sal*1.2 --条件
when 'MANAGER' then sal*3
when 'PRISEDENT' then sal*1.5
when 'SALESMAN' then sal*2
else sal --默认
end yearcomm
from emp;
组合排序函数
ROW_NUMBER语法如下:ROW_NUMBER() OVER(PARTITION BY col1 ORDER BY col2) 例子:先按班级排序,再按学号排序
Rank() 1 1 3 4
Dense_Rank 1 1 2 3
集合操作
select *
from emp
where deptno=10
union --all
select *
from emp
where mgr=7839; union并集,unionall 两个集合相加(有重复)
--
视图,一张临时表,不存在,查询结果
create view emp_10
as
select * from emp where deptno=10;
with read only; 加只读选项
drop view emp_10; 删除视图
update emp_10 set comm=10; 修改视图,也会修改原表数据
----oracle user_tables user_views user_objects 数据字典
非空约束:not null 必须要填的内容
主键约束: Primary key -非空+唯一
代理主键:业务中不存在 非空+唯一 特点的 字段 添加上的 id
自然主键:业务中就存在 非空+唯一特点的 字段 qq
开发环境中:所有的表必须都有主键
唯一性约束:unique 字段内容不能重复
外键约束 foreign key --主表的主键-从表的外键
检查约束: check
alter table user
add constraint pk_user primary key (id); --修改表结构是添加主键
create table user(
id number(6) primary key
name varchar2(20) not null unique
gender char(2) check (gender in ('f','m'))); --建表时添加约束
–create sequence seq_user; 创建序列,添加到主键
insert into user values(
seq_user.nextVal,‘lucy’,‘f’);
select seq_user.currVal from dual; 获得序列的当前值
create table order (
id number(3) primary key
name varchar2(20)
userid number(6));
alter table order
add constraint user_order_fk
foreign key (userid)
references user(id); 主表中被从表引用的数据,不能改也不能删,要删除,必先确保从表中无引用
–索引 提高查询效率
create idnex emp_no on emp(empno); 使用索引时,当where句子中出现添加许哦因的字段,就会自动调用索引
drop index emp_no; 删除索引