oracle增删改查操作
2016-05-12 10:43
597 查看
oracle查询:
case when col_name >0 then col_name else 0 end
多表级联更新:
update PBPARMTEMP_NEW t set (c4) = (select micid from meterinfo m where m.micode = t.c1)
where exists (select 1 from meterinfo m where m.micode = t.c1);
导入导出的命令:
imp username/password@oral file="xx.dmp",log="xx.log",ignore=y,fromuser=username(从哪个用户中导出),touser=username(导入到哪个用户),full=y
模糊查询:like
_:表示一个任意字符.
%:表示任意个字符包括零个.
select last_name from employees where last_name like '_o%';
在模糊查询中含有转义字符时,使用escape说明转义字符.
如果使用'/'做为检索字符,那么必须使用'/'转义,那么escape也必须使用'/'.
在查询时如果为空使用 is null,如果不为空使用is not null.
打印出 "2009年10月14日 9:25:40" 格式的当前系统的日期和时间.
select to_char(sysdate,'YYYY"年"MM"月"DD"日" HH:MI:SS') from dual
格式化数字: 1234567.89 为 1,234,567.89.
select to_char(1234567.89,'9,999,999.99') from dual
字符串转为数字时
1). 若字符串中没有特殊字符, 可以进行隐式转换:
select '1234567.89' + 100
from dual
2). 若字符串中有特殊字符, 例如 '1,234,567.89', 则无法进行隐式转换, 需要使用 to_number() 来完成
select to_number('1,234,567.89', '999,999,999.99') + 100
from dual
查询每个月倒数第 2 天入职的员工的信息.
select last_name, hire_date
from employees
where hire_date = last_day(hire_date) - 1
nvl函数:nvl(expr1,expr2)
如果第一个表达式的值为空,则返回第二个表达式的值;如果第一个表达式的值不为空,返回第一个表达式的值。
nvl2函数:nvl2(expr1,expr2,expr3)
如果第一个表达式的值为空,则返回第二个表达式的值;如果第一个表达式的值不为空,返回第三个表达式的值。
decode函数:
查询部门号为 10, 20, 30 的员工信息,
若部门号为 10, 则打印其工资的 1.1 倍, 20 号部门, 则打印其工资的 1.2 倍, 30 号部门打印其工资的 1.3 倍数
select last_name, department_id, salary, decode(department_id, 10, salary * 1.1,
20, salary * 1.2,
30, salary * 1.3
) new_sal
from employees
where department_id in (10, 20, 30)
多表连接查询时, 若两个表有同名的列, 必须使用表的别名对列名进行引用, 否则出错!
非等值连接:
查询每个员工的 last_name 和 GRADE_LEVEL(在 JOB_GRADES 表中). ---- 非等值连接
select last_name, salary, grade_level, lowest_sal, highest_sal
from employees e, job_grades j
where e.salary >= j.lowest_sal and e.salary <= j.highest_sal
多表连接
join on
连接 Employees 表和 Departments 表
1).
select *
from employees join departments
using(department_id)
缺点: 要求两个表中必须有一样的列名.
2).
select *
from employees e join departments d
on e.department_id = d.department_id
3).多表链接
select e.last_name, d.department_name, l.city
from employees e join departments d
on e.department_id = d.department_id
join locations l
on d.location_id = l.location_id
左外连接、右外连接和满外连接:
左外连接:左表需要返回更多的记录(员工没有部门的情况)
select last_name, department_name
from employees e left join departments d
on e.department_id = d.department_id
右外连接:右表需要返回更多的记录(部门没有员工的情况)
select last_name, department_name
from employees e right join departments d
on e.department_id = d.department_id
满外连接:
select last_name, department_name
from employees e full join departments d
on e.department_id = d.department_id
对表进行修改操作
添加一个新的列:
ALTER TABLE myemp ADD(age number(8,2))
修改现有列的类型
ALTER TABLE myemp MODIFY(age varchar2(10))
修改列名
ALTER TABLE myemp RENAME COLUMN salary to sal
删除列
ALTER TABLE myemp DROP COLUMN age
清空表中的数据并不删除表
TRUNCATE TABLE myemp
删除表
DROP TABLE myemp
创建具有相同表结构的表
CREATE TBALE emp2 AS SELECT * FROM EMPLOYEES WHERE 1=2
向表中插入数据
INSERT INTO emp2 SELECT * FROM EMPLOYEES WHERE EMPLOYEE_ID = 80
对表重命名
rename employees2 to emp5
将某一列设置为不可用
alter table emp5 set unused column test_column
删除不可用的列
alter table emp5 drop unused column
创建序列:
create sequence dept_id_seq
start with 200
increment by 10
maxvalue 10000
使用序列向表中插入值:
insert into dept values(dept_id_seq.nextval,'IT')
case when col_name >0 then col_name else 0 end
多表级联更新:
update PBPARMTEMP_NEW t set (c4) = (select micid from meterinfo m where m.micode = t.c1)
where exists (select 1 from meterinfo m where m.micode = t.c1);
导入导出的命令:
imp username/password@oral file="xx.dmp",log="xx.log",ignore=y,fromuser=username(从哪个用户中导出),touser=username(导入到哪个用户),full=y
模糊查询:like
_:表示一个任意字符.
%:表示任意个字符包括零个.
select last_name from employees where last_name like '_o%';
在模糊查询中含有转义字符时,使用escape说明转义字符.
如果使用'/'做为检索字符,那么必须使用'/'转义,那么escape也必须使用'/'.
在查询时如果为空使用 is null,如果不为空使用is not null.
打印出 "2009年10月14日 9:25:40" 格式的当前系统的日期和时间.
select to_char(sysdate,'YYYY"年"MM"月"DD"日" HH:MI:SS') from dual
格式化数字: 1234567.89 为 1,234,567.89.
select to_char(1234567.89,'9,999,999.99') from dual
字符串转为数字时
1). 若字符串中没有特殊字符, 可以进行隐式转换:
select '1234567.89' + 100
from dual
2). 若字符串中有特殊字符, 例如 '1,234,567.89', 则无法进行隐式转换, 需要使用 to_number() 来完成
select to_number('1,234,567.89', '999,999,999.99') + 100
from dual
查询每个月倒数第 2 天入职的员工的信息.
select last_name, hire_date
from employees
where hire_date = last_day(hire_date) - 1
nvl函数:nvl(expr1,expr2)
如果第一个表达式的值为空,则返回第二个表达式的值;如果第一个表达式的值不为空,返回第一个表达式的值。
nvl2函数:nvl2(expr1,expr2,expr3)
如果第一个表达式的值为空,则返回第二个表达式的值;如果第一个表达式的值不为空,返回第三个表达式的值。
decode函数:
查询部门号为 10, 20, 30 的员工信息,
若部门号为 10, 则打印其工资的 1.1 倍, 20 号部门, 则打印其工资的 1.2 倍, 30 号部门打印其工资的 1.3 倍数
select last_name, department_id, salary, decode(department_id, 10, salary * 1.1,
20, salary * 1.2,
30, salary * 1.3
) new_sal
from employees
where department_id in (10, 20, 30)
多表连接查询时, 若两个表有同名的列, 必须使用表的别名对列名进行引用, 否则出错!
非等值连接:
查询每个员工的 last_name 和 GRADE_LEVEL(在 JOB_GRADES 表中). ---- 非等值连接
select last_name, salary, grade_level, lowest_sal, highest_sal
from employees e, job_grades j
where e.salary >= j.lowest_sal and e.salary <= j.highest_sal
多表连接
join on
连接 Employees 表和 Departments 表
1).
select *
from employees join departments
using(department_id)
缺点: 要求两个表中必须有一样的列名.
2).
select *
from employees e join departments d
on e.department_id = d.department_id
3).多表链接
select e.last_name, d.department_name, l.city
from employees e join departments d
on e.department_id = d.department_id
join locations l
on d.location_id = l.location_id
左外连接、右外连接和满外连接:
左外连接:左表需要返回更多的记录(员工没有部门的情况)
select last_name, department_name
from employees e left join departments d
on e.department_id = d.department_id
右外连接:右表需要返回更多的记录(部门没有员工的情况)
select last_name, department_name
from employees e right join departments d
on e.department_id = d.department_id
满外连接:
select last_name, department_name
from employees e full join departments d
on e.department_id = d.department_id
对表进行修改操作
添加一个新的列:
ALTER TABLE myemp ADD(age number(8,2))
修改现有列的类型
ALTER TABLE myemp MODIFY(age varchar2(10))
修改列名
ALTER TABLE myemp RENAME COLUMN salary to sal
删除列
ALTER TABLE myemp DROP COLUMN age
清空表中的数据并不删除表
TRUNCATE TABLE myemp
删除表
DROP TABLE myemp
创建具有相同表结构的表
CREATE TBALE emp2 AS SELECT * FROM EMPLOYEES WHERE 1=2
向表中插入数据
INSERT INTO emp2 SELECT * FROM EMPLOYEES WHERE EMPLOYEE_ID = 80
对表重命名
rename employees2 to emp5
将某一列设置为不可用
alter table emp5 set unused column test_column
删除不可用的列
alter table emp5 drop unused column
创建序列:
create sequence dept_id_seq
start with 200
increment by 10
maxvalue 10000
使用序列向表中插入值:
insert into dept values(dept_id_seq.nextval,'IT')
相关文章推荐
- Oracle Sequence自动补全指定位数
- Mysql迁移到Oracle简单记录
- Oracle11gR2 sqlplus中可以执行上键查询backspace删除
- Oracle11gR2 RAC voting disk error
- linux下oracle11.2.0.1升级到11.2.0.4
- Oracle 11gR2 PSU安装手册
- Oracle用户密码过期问题解决
- ORACLE SQL TUNING ADVISOR 使用方法
- oracle的minus返回第一个表中有、第二个表中没有的数据
- 如何检查Oracle表空间使用情况
- MOS(Doc ID 1029252.6)最佳实践
- Oracle用户和权限
- Oracle 同义词(synonyms) 详解
- oracle时间戳-日期转换
- Oracle Logminer 与 rman 官方文档英文
- Oracle BRM处理逻辑
- Oracle 11gR2 PSU安装手册
- duplicate 数据库 from backupset [oracle 11.2.0.3 + asm] => [oracle 11.2.0.3 + fs ]
- Oracle,视图,序列,索引
- Oracle EBS OM Transaction