oracle--知识点汇总1


  

同义词:
-- e是scott.emp表的临时别名
select e.*  from (select * from scott.emp) e;


--创建私有同义词
create synonym myemp for scott.emp;
select * from system.myemp;

--创建公有同义词
create public synonym pub_emp for scott.emp;

--使用公有同义词
select * from pub_emp;

  

PLSQL:

declare
--声明
name varchar2(20);
age number:=20;--声明的同时进行初始化
sex varchar2(20);
birthday date;--日期类型
begin
--变量的赋值语句
name:='老白';
age:='&age';
sex:='&sex';
--将字符串2013-09-08,按照yyyy-mm-dd格式转成date类型
birthday:=to_date('2013-09-08','yyyy-mm-dd');
dbms_output.put_line('姓名:'||name||';年龄:'||age||';性别:'||sex);
--将date类型的birthday变量,按yyyy-mm-dd 转成字符串输出
dbms_output.put_line('出生日期:'||to_char(birthday,'yyyy-mm-dd'));
end;

  

IF:

--多条件选择
declare
 age number;
begin
 age:='&a';  --输入年龄
  if (age<12) then
  dbms_output.put_line('小屁孩');
  elsif (age<18) then
  dbms_output.put_line('小骚年');
   else 
  dbms_output.put_line('中老年');
   end if;  
end;

-- 嵌套的if
declare
  isman varchar2(20);
  hobby varchar2(20);  
begin
 isman:='&isman';
 if(isman='男') then
   hobby:='&hobby';
   if(hobby='篮球') then
     dbms_output.put_line('男生都喜欢打篮球');
   else
      dbms_output.put_line('作为男生,你应该去打篮球');
   end if;
  else 
     dbms_output.put_line('我不了解女生!');
 end if;
 end;
 

  

CASE:

 --case 用法1--多条件判断
--相当于  elsif 或switch

declare
 season varchar2(20);
begin
 season := '&season';
 
 case season 
   when '春天' then 
     dbms_output.put_line('春暖花开');
   when '夏天'  then
     dbms_output.put_line('炎炎夏日');
   when '秋天'  then
     dbms_output.put_line('要穿秋裤');
   when '冬天'  then
     dbms_output.put_line('白雪皑皑');     
   else
     dbms_output.put_line('输入有误!');
   end case; 
end;

--case 用法2--值的转换输出
create table sex
(
  sexid number primary key,
  sexvalue varchar2(10) not null unique
)  

insert into sex values('1','10');
insert into sex values('2','20');
insert into sex values('3','30');
insert into sex values('4','40');


--对指定的字段进行case转义。必须等值,且类型一致。
select sexid, 
case  sexvalue
      when '10' then '男性'
      when '20' then '女性'
      when '30' then '未知' 
      else '未知性别'
  end as 性别
 from sex;

  

LOOP:

-- loop 循环
declare
  i number:=1;
 begin
    loop 
      dbms_output.put_line('第'||i||'行');
      i:=i+1;    
      exit when i=10; -- 相当于break
    end loop;  
 end;
 
 
 -- for循环.计算1+3+...+97的和。 
 declare
  i number;
  s number:=0;
 begin
   for i in  1..49  loop
    s:=s+2*i-1;
   end loop;
   dbms_output.put_line('sum='||s); 
 end;
 

 /*使用while循环画出    
   *
  * *
 * * *  
 
 * 
 * * 
 * * *
  */
  declare
    n number;
    i number:=1;
    j number:=1;
  begin
    n:='&请输入n';
    
    while(i<=n) loop
       --打空格
       while(j<=n-i) loop
       dbms_output.put(' ');            
       j:=j+1;       
       end loop;
       j:=1;
       --打*
       while(j<=i) loop
       dbms_output.put('* ');            
       j:=j+1;       
       end loop;
       dbms_output.put_line('');
       j:=1;--回到第1列
       i:=i+1;
    end loop;
  end;
  
  
 
 
 

  

原文地址:https://www.cnblogs.com/ipetergo/p/6237446.html