171208之Oracle总结

来源:互联网 发布:多功能双肩背包 知乎 编辑:程序博客网 时间:2024/06/07 15:59

1:子查询in/exists/比较运算符
(1)select empno,ename,deptno
from emp where deptno in (select deptno from dept where loc=’CHICAGO’);

执行顺序:先执行括号内的子查询,然后将查询到的deptno结果跟emp中的deptno进行比较,若列值存在于这些返回值中,则外层查询结果会在结果集中显示该行。

(2)select empno,ename from emp where exists (
select * from dept where emp.deptno = dept.deptno and loc=’CHICAGO’);

(3)查询emp表,将将薪资大于本职位平均薪资的雇员信息显示出来。
select empno,ename,sal,job from emp
where job=’SALESMAN’ and sal >(
select avg(sal) from emp where job=’SALESMAN’
);

2:数据操纵 INSERT/UPDATE/DELETE/TRUNCATE
update emp
set sal =
(select avg(sal) from emp where job=’SALESMAN’)
where empno=6666;
commit;

delete from emp where empno=5555;

DELETE/TRUNCATE区别:
如果确定要删除表中所有记录时,建议使用TRUNCATE,因为TRUNCATE删除数据时要比DELETE快的多。
但是TRUNCATE删除数据后,不能用rollback来恢复数据,但是delete可以用。

Reuse storage/drop storage

Truncate table emp reuse storage;

Reuse storage表示删除记录后保存记录占用的空间。

drop storage 表示删除记录后立刻回收记录占用的空间。

3:授权grant/回收revoke
grank select on emp to sup2db; 给sup2db授予查权限

revoke select on emp to sup2db; 给sup2db回收查权限

grant select on emp_sales to salesmanger; 授予salesmanger查看emp_sales视图的权限

Oracle和MySQL不一样,Oracle只有一个数据库,而MySQL可以有多个,Oracle是通过设置表空间来实现和Mysql一样的结构,
这时就需要给不同的表设置权限

4:创建视图:
(1)
create view prog_employees_1
as
select employee_id,first_name,last_name,email,
phone_number,salary,manager_id
from it_employees
where job_id=’IT_PROG’;

(2)
create view prog_employees_1
as
select employee_id,first_name,last_name,email,
phone_number,salary,manager_id
from it_employees
where job_id=’IT_PROG’;
with check option;

(3)删除视图
drop view prog_employees;

参考博客:http://www.cnblogs.com/200911/p/3202165.html