Oracle认证 百分网手机站

Oracle数据库语句(2)

时间:2018-03-21 09:05:13 Oracle认证 我要投稿

Oracle数据库语句大全

  select * from user_table;--此时user_table可以不存在

  创建外联接视图:

  create view user_stu_view as

  select u.id,u.user_name,u.password,s.ddress

  from user_tbl u,stu_tbl s

  where u.s_id(+)=s.id;--哪一方带有(+),哪一方就是次要的

  删除视图:

  drop user_stu_view;

  索引

  用于提高SQL语句执行的性能

  索引类型:

  唯一索引,位图索引,组合索引,基于函数的索引,反向键索引

  创建标准索引:

  create index user_id_index on user_tbl(id) tablespace schooltbs;

  重建索引:

  alter index user_id_index rebuild;

  删除索引:

  drop index user_id_index;

  创建唯一索引:

  create unique index user_id_index on user_tbl(id);

  创建组合索引:

  create index name_pass_index on user_tbl(user_name,password);

  创建反向键索引:

  create index user_id_index on user_tbl(id) reverse;

  四.使用PL/SQL

  可用于创建存储过程,触发器,程序包,给SQL语句的执行添加程序逻辑。

  支持SQL,在PL/SQL中可以使用:

  数据操纵命令

  事务控制命令

  游标控制

  SQL函数和SQL运算符

  支持面向对象编程(OOP)

  可移植性

  更佳的性能,PL/SQL经过编译执行

  分为三个部分:声明部分,可执行部分和异常处理部分

  [declare

  declarations]

  begin

  executable statements

  [exception

  handlers]

  end;

  打开输出

  set serverout on;

  --根据输入编号获取某学员的成绩--if

  declare

  score user_tbl.score%type;

  begin

  select score into score from user_tbl where id='&id';

  if score>90 then

  dbms_output.put_line('优秀');

  elsif score>80 then

  dbms_output.put_line('良好');

  elsif score>60 then

  dbms_output.put_line('及格');

  else

  dbms_output.put_line('差');

  end if;

  end;

  --根据学员姓名获取某学员的成绩--if

  declare

  score user_tbl.score%type;

  begin

  select score into score from user_tbl where user_name='&name';

  if score>90 then

  dbms_output.put_line('优秀');

  elsif score>80 then

  dbms_output.put_line('良好');

  elsif score>60 then

  dbms_output.put_line('及格');

  else

  dbms_output.put_line('差');

  end if;

  end;

  --case的使用

  declare

  grade user_tbl.grade%type;

  begin

  select grade into grade from user_tbl where id='&id';

  case grade

  when 'A' then dbms_output.put_line('优异');

  when 'B' then dbms_output.put_line('优秀');

  when 'C' then dbms_output.put_line('良好');

  else dbms_output.put_line('一般');

  end case;

  end;

  --基本循环

  declare

  i number(4):=1;

  begin

  loop

  dbms_output.put_line('loop size:'||i);

  i:=i+1;

  exit when i>10;

  end loop;

  end;

  --while循环

  declare

  i number(4):=1;

  begin

  while i<=10 loop

  dbms_output.put_line('while loop size='||i);

  i:=i+1;

  end loop;

  end;

  --for循环

  declare

  i number(4):=1;

  begin

  for i in 1..10 loop

  dbms_output.put_line('for loop Size:'||i);

  end loop;

  end;

  declare

  i number(2):=1;

  j number(2):=1;

  begin

  for i in reverse 1..9 loop

  for j in 1..i loop

  dbms_output.put(j||'x'||i||'='||j*i||' ');

  end loop;

  dbms_output.put_line('');

  end loop;

  end;

  --动态SQL

  declare

  userId number(2);

  sql_str varchar2(100);

  userName user_tbl.user_name%type;

  begin

  execute immediate 'create table testExe(id number,test_name varchar2(20))';

  userId:='&userId';

  sql_str:='select user_name from user_tbl where id=:id';

  execute immediate sql_str into userName using userId;

  dbms_output.put_line(userName);

  end;

  (or

  declare

  id_param number:='&id_param';

  sql_str varchar2(100);

  name_param stu_tbl.stu_name%type;

  begin

  sql_str:='select stu_name from stu_tbl where id=:p';

  execute immediate sql_str into name_param using id_param;

  dbms_output.put_line(name_param);

  end;

  /

  )

  --异常处理

  declare

  grade number(4);

  begin

  grade:='&grade';

  case grade

  when 1 then dbms_output.put_line('好的');

  --else dbms_output.put_line('不好');

  end case;

  exception

  when case_not_found then

  dbms_output.put_line('输入类型不匹配!');

  end;

  --系统异常

  declare

  rowD user_tbl%rowtype;

  begin

  select * into rowD from user_tbl;

  dbms_output.put_line(rowD.id||''||rowD.user_name||' '||rowD.password);

  exception

  when too_many_rows then

  dbms_output.put_line('不能将多行赋予一个属性!');

  end;

  or

  declare

  rowD user_tbl%rowtype;

  begin

  select * into rowD from user_tbl where id=5;

  dbms_output.put_line(rowD.id||' '||rowD.user_name||' '||rowD.password);

  exception

  when too_many_rows then

  dbms_output.put_line('不能将多行赋予一个属性!');

  when no_data_found then

  dbms_output.put_line('没有您要查找的数据!');

  end;

  --自定义错误

  declare

  invalidError exception;

  category varchar2(20);

  begin

  category:='&category';

  if category not in('附件','顶盘','备件') then

  raise invalidError;

  else

  dbms_output.put_line('您输入的类别是:'||category);

  end if;

  exception

  when invalidError then

  dbms_output.put_line('无法识别的类别!');

  end;

  --引发应用程序异常

  declare

  app_exception exception;

  grade user_tbl.grade%type;

  begin

  select grade into grade from user_tbl where id=&id;

  if grade='A' then

  raise app_exception;

  else

  dbms_output.put_line('查询的等级为:'||grade);

  end if;

  exception

  when app_exception then

  raise_application_error(-20001,'未知的等级!');

  end;

  五、游标管理

  游标类型:隐式游标,显式游标,REF游标

  REF游标用于处理运行时才能确定的动态SQL查询的结果

  ==========隐式游标==========

  在PL/SQL中使用DML语句时自动创建隐式游标

  隐式游标自动声明、打开和关闭,其名为SQL

  隐式游标的属性:

  %found SQL语句影响实质后返回true

  %notfound SQL语句没有影响实质后返回true

  %rowcount SQL语句影响的行数

  %isopen 游标是否打开,始终为false

  示例:

  begin

  update user_tbl set score=score+5;

  if SQL%found then

  dbms_output.put_line('数据被更改: '||SQL%rowcount);

  elsif sql%notfound then

  dbms_output.put_line('没有找到数据!');

  end if;

  if SQL%isopen then

  dbms_output.put_line('Open');

  else

  dbms_output.put_line('Close');

  end if;

  end;

  ==========显式游标==========

  在PL/SQL的声明部分定义查询,该查询可以返回多行

  J 声明游标

  J 打开游标

  J 从游标中取回数据

  J 关闭游标

  声明游标完成两个任务:

  给游标命名

  将一个查询与游标关联

  cursor cursor_name is select statement;

  打开游标:

  open cursor_name;

  取数据:

  fetch cursor_name into record_list;

  关闭游标:

  close cursor_name;

  显式游标的属性:

  %found 执行最后一条fetch语句成功返回行时为true

  %notfound 执行最后一条fetch语句未能返回行时为true

  %rowcount 返回到目前为止游标提取的行数

  %isopen 游标是否打开

  示例:

  declare

  users user_tbl%rowtype;