Oracle 序列 和 视图

      
 -----------------------------------------序列------------------------------------
--序列(sequence)的作用
      --1.主外键的应用需求
      --2.流水号应用需求
      --3.序列的生成与定义的内容

--1.创建序列 (要有 create sequence 或者 create Any sequence 权限)
     create sequence sequence_name   --新建的sequence 的名字
     [start with n1]            --指定要生成的第一个序列号  
     [increment by n2]          --序列之间的间隔 默认为 “1” 如果 n2 >0 则是升序  n2<0  则是降序 
     [{maxvalue n3 | nomaxvalue}]  --maxvalue:指定可以生成的最大值 (n3>=n1 && n3> n4); nomaxvalue:没有最大上线  最大可达 10……27(……表示幂)
     [{minvalue n4 | nominvalue}]  --minvalue:指定序列生成的最小值 (n4>=n1 && n4<n3);nominvalue:没有序列最下线
     [{cache n5| nocache}]      --cache:指定在高速缓冲区预先分配的序列号个数,默认为 “20” nocache:指定在高速缓冲区中不预先分配序列号 (即,序列生成器每次生成一个序列号,该好就是默认值)
     [{cycle| nocycle}]  --cycle:用于指定序列号达到最大上线或者最小下线的时候 再循环 ,nocycle 表示不再循环;
     [order];     --用于指定按顺序生成序列号
     
     --示例
     conn sys/admin@scce;
     grant create sequence to scott;
     grant create any sequence to scott;    --获取权限 
    
     ---表示创建一个序列seq_id 从300开始  每次增长1  每次生成10序列号, 最大值为99999999 达到最大上线后不再循环
     conn scott/tiger@scce;
     crate sequence seq_id
     increment by 1
     start with 300
     maxvalue 99999999
     cache 10
     nocycle;
     
--2.使用序列
     --1. nextVal :返回序列生成的下一个值
     --2. currVal : 返回序列生成的但前值  (在第一次使用时候,必须要使用nextVal对序列进行初始化 否则报错)
     --3.示例
     insert into tb_shop values(seq_id.nextVal,''....);
     
--3.更改序列  (更改自己的 要有 alter sequence 权限 要修改别人的 要有 alter any sequence)
     --1:不能修改起始值  要修改就必须 删除旧的在新建一个
     --2:可以修改 maxvalue | minvalue  值
     --3: 可以修改 increment 值
     --4:可以修改高速缓冲值  cache
     
    --示例
     conn sys/admin@scce;
     grant alter sequence to scott;
     grant alter any sequence to scott;
     
--4.删除序列  (要有 Drop sequence 或drop any sequence 权限)
    --语法:Drop SEQUENCE sequence_name;
    --示例:Drop sequence seq_id;








--____________________________________________________视图_________________________________________
--1创建视图 (要有create View 或者 Create any View 的权限) 
   /*语法:  Create [Or replace] [force] View view_name         -- Or replace :表示视图如果存在就覆盖   force: 无论视图是否存在都将要创建视图  view_name:视图名称
              [(alias1,alias2....]                              --alias:指定由视图查询所选择的表达式的别名,数目必须与视图所选择的表达式的数目相同
		as select_sataement                             --创建视图时使用的select 语句
	      [with vheck option [constraint contraint]]        --在使用视图时,检查涉及的数据是否通过select子查询的where条件 ,否则不允许操作返回错误
     	       [with read only];                                --该视图只能用于查询,不能更改数据  不能与Order By子句同时纯在
  */
   --示例1
     conn sys/admin@orcl;
     grant create view to scott;
     
     conn scott/tiger@orcl;
     create view v_scott_1 as select empNo,ename,job from soctt.emp wehre scott.emp.deptno=30;

   --示例2
     create view v_scott_2 (员工,姓名,职位) as  select empno,ename,job from scott.emp where scott.emp.deptno=20;
     
   --示例3  (创建视图后只能查询  不能 insert update delete)
     create view v_scott_3 as select empno, ename,job from emp where deptno =20 with read only;  
   
   --示例4 (视图创建后 能 insert update delete  但是 条件是 sal>2000)
     create view v_scott_4 as select empno,ename,job,sal from emp where sal>2000 with check option;
     insert itno v_soctt_4 values(7960,'jack','Clerk',1200); -- 报错    如果把1200  改为  sal>2000  就可以了


--2.强制创建视图 
     /*(正常情况下 如果基本表不存在 视图会创建失败。如果视图的语句没有错误,则使用Force 就可以创建视图 这种视图被称为:"带有编译错误的视图" 
     此时是处于失效状态  不能执行, 如果 之后基本表被创建了 该视图就可以正常使用) */
   --示例1
     create force view v_force_test as select c1,c2 from tb_force_test;
     select * from v_force_test;  -- 会报错  (表或试图不存在)
     create table tb_force_test
     (
      id number(20) primary key,
      c1 varchar2(20) not null,
      c2 varchar2(20) not null
     );
     insert into tb_force_test values(1,'aaa','aaa');
     select * from v_force_test;  --能成功执行!
     
--3.可更新的连接视图
   /*创建连接视图的select子句要满足一下条件
     1. 不要集中运算符 union ,union all ,intersect ,minus
     2.不包含 distinct 关键字
     3.不包含 group by,order by,connect by start with
     4.不包含子查询
     5.不包含分组函数
     6.需要定义的列不是列 表达式定义的
     7.表中所有 not null 均属于该视图
     8.只能对键值保存表 进行更改 (一般有父子关系组成的连接视图中,子表就是键值保存)
   */
   --示例 1 (deptno 是emp 的父表 ,所以  emp 是键值保存表,只能更新这个表 而且 这个表中的主键 唯一键 都在这个视图中)
   create or replace view v_dept_emp_1
   as
   select b.empno,b.ename,a.deptno,a.loc from dept a ,emp b,where a.deptno=b.deptno and a.deptno in (10,30);
   update v_dept_emp_1 set ename='JACK' where empno=7521;  --可以成功执行
   update v_dept_emp_1 set loc='BEIJIN' where deptno=30;   --错误  不能修改非键值保存表 对应的列
   
  
--4.查询视图的可更新列 (使用User_UpdateTable_Columns 数据库字典:查询当前用户方案中所有的表和视图可修改的列)
   select a.column_name,a.updatable,a.insertable,a.insertable,a.deletable from user_updatable_columns a where a.table_name=upper('v_dept_emp_1');

--5.查询视图的定义信息 (使用User_views 数据库字典 :查询当前方案中视图的定义信息。)
  column view_name format a16
  column text format a63
  select c.view_name,c.text from user_views c  where c.view_name=upper('v_dept_emp_1');
 
--6.删除视图  (要权限 drop any view)
   conn sys/admin@orcl;
   grant drop any view to scott;
   
   drop view v_dept_emp_1;
   

猜你喜欢

转载自takeme.iteye.com/blog/2149900