DML(Manipulation):数据操作语言 CRUD,Call —call a PL/SQL or Java subprogram,
Explain plan—explain access path to data,Explain可以用来迅速方便地查出对于给定SQL语句中的查询数据是如何得到的即搜索路径(我们通常称为Access Path)。从而使我们选择最优的查询方式达到最大的优化效果。
Lock table—control concurrency锁,用于控制并发
DDL(Definition): 数据定义语言,与表,索引,同义词有关 create,alter,drop,rename,truncate(清空)
DCL(Control): 数据控制语言,与权限有关 grant,revoke
TCL(Transaction Control): 事务控制语言,与事务有关 commit,rollback,savepoint set transaction 设置当前事务的特性
==========================
存储过程和存储函数,相当重要,在java需要调用存储过程和函数
存储过程是一个预编译的SQL语句,他的优点是允许模块化的设计,也就是说只需创建一次,在该程序中就可以调用多次。例如某次操作需要执行多次SQL,就可以把这个SQL做一个存储过程,因为存储过程是预编译的,所以使用存储过程比单纯SQL语句执行要快。可以用一个命令对象来调用存储过程。
写一个例子:写一个函数完成如下功能,输入两个department_id,dept_1,dept_2,返回他们平均工资较高的那个部门中 manager 的salary
函数版:
create or replace function get_mgr(
dept_id_1 employess.department_id%type,
dept_id_2 employees.department_id%type,
)
return number
is
max_sal_1 employees.salary%type;
max_sal_2 employees.salary%type;
dept_id employees.department_id%type;
mgr_id employees.manager_id%type;
sal employees.salary%type;
begin
select max(salary) into max_sal_1 from employees
where department_id = dept_id_1;
select max(salary) into max_sal_2 from employees
where department_id = dept_id_2;
if max_sal_1 > max_sal_2 then
dept_id := dept_id_1;
else
dept_id := dept_id_2;
end if;
select manager_id into mgr_id from departments where department_id = dept_id;
select salary into sal from employees where employee_id = mgr_id;
return sal;
end;
sql版:
第一步 查询给定的两个部门中高的平均工资:30,80
select max(avg_sal)
from (select avg(salary) avg_sal from employees where department_id in(30,80)
group by department_id)
第二步 查询平均工资高的部门号
select department_id from employees group by department_id
having avg(salary) =(...)
第三步 查询部门号对应的manager_id
select manager_id from departments where department_id = (...)
第四步 根据manager_id 查询 salary
select salary from employees where employee_id = (
select manager_id from departments where department_id=(
select department_id from employees group by department_id
having avg(salary) =(
select max(avg_sal) from ( select avg(salary) avg_sal from
employees where department_id in(30,80) group by department_id)
)
)
)
)
总结如下:
l 更快的执行速度:存储过程只在创造时进行编译,以后每次执行存储过程都不需再重新编译,而一般SQL语句每执行一次就编译一次,所以使用存储过程可提高数据库执行速度;
l 与事务的结合,提供更好的解决方案:当对数据库进行复杂操作时(如对多个表进行Update、Insert、Query和Delete时),可将此复杂操作用存储过程封装起来与数据库提供的事务处理结合一起使用;
l 支持代码重用:存储过程可以重复使用,可减少数据库开发人员的工作量;
l 安全性高:可设定只有某此用户才具有对指定存储过程的使用权。