|
8.SQL函数 单行函数:从表中查询的每一行只返回一个值,可出现在select子句,where子句中 日期函数 数字函数 字符函数 转换函数:ToChar(),ToDate(),ToNumber() 其他函数: Nvl(exp1,exp2):表达式一为null时,返回表达式二 Nvl2(exp1,exp2,exp3):表达式一为null时返回表达式三,否则返回表达式二 Nullif(exp1,exp2):两表达式相等时,返回null,否则返回表达式一 分组函数:基于一组行来返回 Avg,Min,Max,Sum,Count Group by,having 分析函数 Row_number,rank,dense_rank 示例: select u.user_name,sum(oi.order_num*oi.order_price) as total,row_number() over (order by sum(oi.order_num*oi.order_price) desc) as sort from order_item_tbl oi,user_tbl u,order_tbl o where oi.order_id = o.id and o.user_id = u.id group by u.user_name;
三.锁和数据库对象 1.锁:数据库用来控制共享资源并发访问的机制。 锁的类型:行级锁,表级锁 行级锁:对正在被修改的行进行锁定。行级锁也被称之为排他锁。 在使用下列语句时,Oracle会自动应用行级锁: insert,select…… for update select……for update允许用户一次锁定多条记录进行更新。 使用commit or rollback释放锁。 表级锁: lock table user_tbl in mode mode; 表级锁类型: 行共享 row share 行排他 row exclusive 共享 share 共享行排他 share row exclusive 排他 exclusive 死锁:两个或两个以上的事务相互等待对方释放资源,从而形成死锁 2.数据库对象 oracle数据库对象又称模式对象 数据库对象是逻辑结构的集合,最基本的数据库对象是表 数据库对象: 表,序列,视图,索引
序列 用于生成唯一,连续序号的对象。 创建语法: create sequence user_id_seq start with 1000 increment by 1 maxvalue 2000 minvalue 1000 nocycle cache 1000;--指定内存中预先分配的序号 访问序列: select user_id_seq.currval from dual; select user_id-seq.nextval from dual; 更改删除序列: alter sequence user_id_seq maxvalue 10000;--不能修改其start with 值 drop sequence user_id_seq; 在Hibernate中访问序列:
user_id_seq
视图 以经过定制的方式显示来自一个或多个表的数据 创建视图: create or replace view user_tbl_view (vid,vname,vage) as select id,user_name,age from user_tbl [with check option]|[with read only]; 创建带有错误的视图: create force view user_tbl_force_view as 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; (编辑:PHP编程网 - 湛江站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|