现在的位置: 首页 > 综合 > 正文

oracle 视图 序列 同义词

2014年02月14日 ⁄ 综合 ⁄ 共 2799字 ⁄ 字号 评论关闭

 

视图:它的功能,一个视图实际上就是封装了一条复杂的查询语句;
  创建视图的语法: create or replace view 视图名称 as 子查询;  
  删除视图的语法 drop view 视图名称 ;
       实际上此时的子查询就表示一条非常复杂的Sql语句.
      eg.建立视图,它包含了全部20部门的的雇员信息。
   create view emp_20 as select  * from emp where  deptno=20
查询视图跟查表一样 select * from emp_20;
 eg.求部门名称、部门人数、平均工资以及最低工资的雇员;

复杂sql语句
select  e.ename, d.dname, c.count_empno,c.min_sal,c.avg_sal  from emp e ,dept d,
 (select  deptno,avg(sal) avg_sal ,count(empno) count_empno,min(sal) min_sal from emp group by deptno ) c
where d.deptno = c.deptno and e.sal = c.min_sal

创建视图:
create or replace view emp_info as    select  e.ename, d.dname, c.count_empno,c.min_sal,c.avg_sal  from emp e ,dept d,
 (select  deptno,avg(sal) avg_sal ,count(empno) count_empno,min(sal) min_sal from emp group by deptno ) c
where d.deptno = c.deptno and e.sal = c.min_sal;
以后直接查询视图,就可以得到想要的结果.  select * from emp_info;(就不必每次都写复杂sql了)

更新视图<创建视图实际上存在创建条件的>
update emp_20 set deptno=30 where empno=7369<修改了视图的创建条件>,一旦修改后,创建视图的条件就破坏了
所以在创建视图的时候SQL提供了两个重要的参数:  with check option 检查不能更新视图的创建条件
create view emp_20 as select  * from emp where  deptno=20 with check option;
 update emp_20 set deptno=30 where empno=7369        error:子句违归              (不能再更新创建条件,其它字段可以)                                                                                         

视图本身是用来查询的,所以不应该允许修改,可以使用参数 with read only 创建的视图只读
 create view emp_20 as select  * from emp where  deptno=20 with read only;
       update emp_20 set deptno=30 where empno=7369   error:此处不允许虚拟列

序列:在很多数据库中都存在一个自动增长的列,若想在oracle中完成自动增长的功能,则只能依靠序列来完成,所有的自动增长操作,需要用户手工完成。序列中提供了以下两种操作: nextVal 取得序列的下一个内容;
                                                                 currVal 取得序列的当前内容.

create sequence 序列名称 [increment by n(长度)] [ start with n]
--创建序列
create or replace sequence myseq ;
--创建表
create table testseq1(
    nex number,curr number
                            );
--加入五次
insert into testseq1 (nex,curr) values(myseq.nextVal,myseq.currVal);
insert into testseq (nex,curr) values(myseq.nextVal,myseq.currVal);
insert into testseq (nex,curr) values(myseq.nextVal,myseq.currVal);
insert into testseq (nex,curr) values(myseq.nextVal,myseq.currVal);
insert into testseq (nex,curr) values(myseq.nextVal,myseq.currVal);

SQL> select * from testseq;
 
       NEX       CURR
---------- ----------
         1          1
         2          2
         3          3
         4          4
         5          5
 
SQL>  --可以看出nextVal的内容始终在进行自动增长的操作;currVal取出当前的操作.

 create sequence myseq increment by 2 start with 1; --步长为2,从1开始

SQL> select * from testseq1;
       NEX       CURR
---------- ----------
         1          1
         3          3
         5          5
         7          7
         9          9
        11         11
        13         13
7 rows selected
SQL>

同义词:之前一直存在这样的查询 select * from dual;
-- conn sys/change_on_install as sysdba;<直接连接超级管理员sys>
-- select  * from tab  (查当前用户下的所有表)
-- select * from tab where tname='DUAL';
在sys用户下存在此表,此表在sys下,但在scott用户下却可以通过表名称访问,那么正常情况下如果用户访问不同用户下的表需要使用 用户名.表名称 <此时相当于同义词的作用,同义词可以让其它用户通过一个名称方便的访问'用户名.表'>

创建同义词: create synonym 同义词名称 for 用户.表名称
删除同义词: drop synonym  同义词名称
此种特性只属于oracle;

抱歉!评论已关闭.