视图
视图实际上是一张或者多张表上的预定义查询,这些表称为基表。视图的优点:
1、可以限制用户只能通过视图检索数据。这样可以对最终用户屏蔽建表时底层的基表
2、可以将复杂的查询保存为视图。可以对最终用户屏蔽一定的复杂性。
3、限制某个视图只能访问基表中的部分列或者部分行的特定数据。这样可以实现一定的安全性。
4、从多张基表中按一定的业务逻辑抽出用户关心的部分,形成一张虚拟表。
//创建视图
create or replace view v_tbook_log as
select * from t_book_oplog where actionname='update'
with read only;
//查询视图
select * from v_tbook_log;
//删除视图 不会影响基表的数据
drop view v_tbook_log;
函数
declare num number:=1000;
v varchar2(32)=' oracle';
begin
dbms_output.put_line('hello '||numm||v);
end;
//hello 1000 oracle
declare emp_count number;
result_str varchar2(32);
salvalue number:=40000;
begin
select count(*) into emp_count from SCOTT.emp where sal >=salvalue;
if emp_count=1 then
dbms_output.put_line('有1名薪资大于等于'||salvalue);
else if emp_count>1 then
dbms_output.put_line('有'||emp_count||'名薪资大于等于'||salvalue);
else
dbms_output.put_line('没有');
end if;
end if;
end;
create or replace function myfunction return varchar2 as
begin
declare num number:=1000;
v varchar2(32):=' oracle';
begin
return 'hello '||num||v;
end;
end myfunction;
select myfunction() from dual;
//hello 1000 oracle
create or replace function myfunction_1(num number) return varchar2 as
begin
declare emp_count number;
begin
select count(*) into emp_count from SCOTT.emp where sal>= num;
if emp_count>0 then
return '有'||emp_count||'名员工薪资大于等于'||num;
else
return '没有员工薪资大于等于'||num;
end if;
end;
end;
select myfunction_1(3000) from dual;
循环
//loop end loop;
declare orinum number:=1;
sumvalue number:=0;
begin
loop
if(orinum>5) then
exit;
end if;
sumvalue:=sumvalue+orinum;
orinum:=orinum+1;
dbms_output.put_line(sumvalue);
end loop;
end;
//while
declare orinum number:=1;
sumvalue number:=0;
begin
while orinum<6 loop
sumvalue:=sumvalue+orinum;
dbms_output.put_line(sumvalue);
orinum:=orinum+1;
end loop;
end;
//for
declare sumvalue number:=0;
begin
for orinum in 1...5 loop
sumvalue:=sumvalue+orinum;
dbms_output.put_line(sumvalue);
end loop;
end;
//case when
declare emp_count number;
salvalue number:=3000;
begin
select count(*) into emp_count from SCOTT.emp where sal>=salvalue;
case emp_count
when 0 then
dbms_output.put_line('没有员工薪资大于等于'||salvalue||'元');
when 1 then
dbms_output.put_line('有1名员工薪资大于等于'||salvalue||'元');
when 2 then
dbms_output.put_line('有2名员工薪资大于等于'||salvalue||'元');
when 3 then
dbms_output.put_line('有3名员工薪资大于等于'||salvalue||'元');
else
dbms_output.put_line('有3名以上员工薪资大于等于'||salvalue||'元');
end case;
end;