字符函数
concat(’’,’’) 连接字符串
substr(’’,0,9) 切割字符串 从0开始取9个
length(’’) 返回字符串个数
instr(’’,‘s’) 字符串中 s 第一次出现的索引
lpad(‘1’,4,‘0’) 字符串不足4位 左自动补零 输出为 0001
rpad (‘1’,4,‘0’) 字符串不足4位 右自动补零 输出为 1000
trim(‘h’ from ‘hhheloohworh’) 去除首尾的h 输出为eloohwor
replace(‘hello’,‘l’,‘m’) 把字符串中l替换为m 输出为hemmo
distinct xxx 去重
数字函数
round(435.453,2) 保留两位小数 输出 435.45 四舍五入
round(435.453,-2) 输出 400 四舍五入
round(435.453) 输出 435 四舍五入
trunc(‘435.453’ , 1) 截断 不四舍五入
mod(1100,300) 同java% 取模
日期
months_between(sysdate,hire_date) 比较两个日期相差月数
add_months(sysdate,2) 向指定的日期加入几个月
next_day(sysdate,‘星期五’) 指定下一个星期指定的日期
last_day(sysdate) 本月的最后一条
round(sysdate,‘mm’) 按月四舍五入
trunc(sysdate,‘dd’) 按日 截断
通用函数
nvl(‘null’,1) 参数一为空时用1代替
nvl(‘1’,‘2’,‘3’) 1为空 取2 不为空取3
to_char(’’,’’)转字符函数
SELECT TO_CHAR(T.SALARY,‘999,999.99’) FROM EMPLOYEES T
nullif(‘1’,‘2’) 相等返回null 不相等返回’1’
case when then 语句
select case t.department_id when 10 then t.salary * 1.1
when 20 then t.salary * 1.2
else t.salary * 1.3 end as newSalary
from employees t
where t.department_id in(‘10’,‘20’,‘30’)
DECODE 语句
select DECODE( t.department_id,10,t.salary * 1.1,
20 ,t.salary * 1.2
,t.salary * 1.3) as newalary
from employees t
where t.department_id in(‘10’,‘20’,‘30’)
多行函数
AVG(e.salary) 求平均值 只能用number类型 为空不计算
COUNT() 求总数 任何数据类型都能使用 为空不计算
MAX(e.salary) 求最大值 任何数据类型都能使用
MIN(e.salary) 求最小值 任何数据类型都能使用
STDDEV() 求均值平方差
SUM(e.salary) 求和 只能用number类型 为空不计算
where 后不能跟多行函数 可以换为 having
create table table_name as
select employee_id id,lats_name name,hire_date,salary from employees where 1=2
根据某表创建一个表
truncate table table_name 删除表中数据 不可回滚
drop tbale table_name 删除表
alter table tbale_name rename column old_name to new_name
alter table table_name modifty (column_name varhca2()) 修改列的数据类型或长度
add(column_name type ) 新增一列
drop column column_name 删除一列
rename old_name to new_name 改表名
create table back_table as select * from employees 备份某表
on delete set null 级联置空
on delete cascade 级联删除
constraint 约束
distinct 去重
create view empview as select * from empoyees ( with read only)创建视图 (仅查询)
create or replace view empview as select * from empoyees 覆盖视图
create sequence seqname 创建序列
increment by n – 每次增加的数值
start with n --从那个值开始
maxvalue n --最大到那个值
minvalue n --最小到那个值
cycle(nocycle) – 是否循环
cache(nocache) --是否缓存登入
select seqname.nextval from dual; 查序列下一个数
select seqname.currval from dual;查当前数
create index indexname 创建索引 更新数据时有索引反而会慢
on table_name(column,…,)
select * from employees1 union select * from employees2 取两表并集 (去重)
select * from employees1 union all select * from employees2 取两表并集 (不去重)
select * from employees1 intersect select * from employees2 交集
select * from employees1 minus select * from employees2 差集 前表减后表的重复数据
pl/sql
declare
v_sal employees.salary%type;
begin
select salary into v_sal from employees
where employee_id = 100;
dbms_output.put_line(v_sal);
end;