一、 oracle基础知识
a、 CRUD操作 create read update delete
b、 数据库对象
c、 数据库设计
d、 数据库结构体系优化(DBA)
oracle中存在四张练习表 emp dept slmgad bonus 这四张表,还有一张dual表练习表表中只有一个字段一个数据。
二、 select字段中的一些常用字段
1、 distinct 删除重复字段
select distinct job from emp;
2、 between 在之间
select * from emp where sal between 1000 and 2000;
in 和 not in和between 有点类似
select * from emp where sal in(1000,2000);
select * from emp where sal not in(1000,2000);
3、 order by 排序asc升序(默认) desc降序
select * from emp order by ename desc,sal;
4、 like字段模糊找到 not like取否
select * from emp where ename like '%R%';
_表示一个字母
select * from emp where ename like '_A%'; 表示以A为第一个字母的姓名
5、 escape转义
select * from emp where ename like '/_A%' escape '/'; ‘对/后面的_’进行转义
6、 union 连接
SQL> select job from emp where job='CLERK' union select job from emp where job='SALESMAN';
7、 nulls first 空值排列放在前 nulls last
select * from emp order by comm nulls first;
select * from emp order by comm nulls last;
三、 函数
1、字符函数
Upper()字母小写变大写
select upper('addb') from dual;
lower()大字字母变小写
select lower('aAAddb') from dual;
initcap()首字母改为大写
select initcap('aAAddb') from dual;
concat 相加 相当于 ||
select concat('a','b') as combine from dual;
substr(string,begin,end)截取字符串,oracle中 0和1都开始坐标
select substr(ename,1,1) from emp; 和 substr(ename,0,1)相同
length 计算字符串长度计算
select length('abasd') from dual;
replace(string,string1,string2) 用string2中的字符代替string中的string1
select replace(ename,'S','*') from emp;
instr 有点类似java中的 index of
select instr('hello world','or') from dual;
trim去除空格
lpad 向左插空 rpad向右插空
select lpad('abc','5','*') from dual;
2、数学函数
Round 四舍五入函数 也可以round(num)
select round(3.1555,2) from dual; 3.16
select round(3878.123,-2) from dual;
trunk() 舍去小数点
sysdate 系统时间
提示:在oracle 5/2等于2.5
3、时间函数
Moths_between()月份之间
Add_months(sysdate,1) 给当前月加一个月
select add_months(sysdate,1) from dual;
next_day() 下一个星期是几号
last_day() 一个月的最后一天
4、字符转换
To_char(sysdate,’yyyy-mmdd hhmiss’) hh24小时制 fmmm = 1(01月转成1月)
to_number() 转换成number
to_date()例to_date(‘10100101,’yyyymmdd’)
5、通用函数
Nvl()替换为空的内容
select nvl(comm,0) from emp;
nvl2(comm.,0,1000) 0替换不为空的 1000替换为空
select nvl2(comm,0,1000) from emp;
nullif(‘abcd’,’abcd’) 字符相等返回空,不相等返回第一个字符串
case 表达式
select emp.*,case deptno
when 10 then '财务'
when 20 then '外联'
when 30 then '30部门'
else '其他部门'
end 部门
from emp;
decode升级case
decode(deptno,10,’财务’,20,’外联’,’其他部门’) abc;