Oracle入门基础(十)一一数据库其他对象-视图/序列/索引/同义词

SQL> --视图
SQL> create view empinfoview as select e.empno,e.ename,e.sal,e.sal*12  annsal,d.dname from emp e,dept d  where e.deptno=d.deptno;
create view empinfoview

视图已创建。

SQL> desc empinfoview
 名称                                      是否为空? 类型
 ----------------------------------------- -------- ----------------------------
 EMPNO                                     NOT NULL NUMBER(4)
 ENAME                                              VARCHAR2(10)
 SAL                                                NUMBER(7,2)
 ANNSAL                                             NUMBER
 DNAME                                              VARCHAR2(14)



SQL> create or replace view empinfoview  as  select e.empno,e.ename,e.sal,e.sal*12 annsal,d.dname from emp e,dept d where e.deptno=d.deptno  with read only;
视图已创建。

SQL> --序列
助解:序列是一数据库对象,利用它可生成唯一的整数。一般使用序列自动地生成主码值。一个序列的值是由特殊的Oracle程序自动生成,因此序列避免了在应用层实现序列而引起的性能瓶颈。
SQL> create sequence myseq;
序列已创建。

SQL> create table testseq
  2  (tid number,tname varchar2(20));

表已创建。

SQL> select myseq.currval from dual;
第 1 行出现错误: 
ORA-08002: 序列 MYSEQ.CURRVAL 尚未在此会话中定义 

SQL> select myseq.nextval from dual;

   NEXTVAL                                                                      
----------                                                                      
         1                                                                      

已选择 1 行。

SQL> select myseq.currval from dual;

   CURRVAL                                                                      
----------                                                                      
         1                                                                      

已选择 1 行。

SQL> insert into  testseq values(myseq.nextval,'aaa');
SQL> insert into  testseq values(myseq.nextval,'aaa');
SQL> insert into  testseq values(myseq.nextval,'aaa');
SQL> insert into  testseq values(myseq.nextval,'aaa');
SQL> commit;
提交完成。

SQL> select * from testseq;

       TID TNAME                                                                
---------- --------------------                                                 
         2 aaa                                                                  
         3 aaa                                                                  
         4 aaa                                                                  
         5 aaa                                                                  

已选择 4 行。


SQL> --索引
SQL> --SQL的执行计划
SQL> explain plan for select * from emp where deptno=10;
助解: explain plan
           1. 工作实质
			将SQL语句预估的执行计划加载到表plan_table,是对表plan_table 执行了DML操作,故不会执行隐式提交。(可以对select,insert,update,merge,delete,create table, create index,alter index等加载执行计划到plan_table。)
			2. 前提条件
			 需要先创建plan_table,创建方法(可参考Oracle的AUTOTRACE功能):@J:/oracle/product/10.2.0/db_1/RDBMS/ADMIN/utlxplan.sql
			 对当前的SQL语句有执行权限以及对依赖的对象有相应操作的权限
			
			3. 使用方法:
			  explain plan for select * from dept where deptno=20;    --未设置标记位
			  explain plan set statement_id='t1' for select * from dept where deptno=20;  --设置标记位为T1
			
SQL> select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT                                                               
--------------------------------------------------------------------------------
Plan hash value: 3956160932                                                     
                                                                                
--------------------------------------------------------------------------      
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |      
--------------------------------------------------------------------------      
|   0 | SELECT STATEMENT  |      |     3 |   261 |     3   (0)| 00:00:01 |      
|*  1 |  TABLE ACCESS FULL| EMP  |     3 |   261 |     3   (0)| 00:00:01 |      
--------------------------------------------------------------------------      
                                                                                
Predicate Information (identified by operation id):                             
---------------------------------------------------                             

PLAN_TABLE_OUTPUT                                                               
--------------------------------------------------------------------------------
                                                                                
   1 - filter("DEPTNO"=10)                                                      
                                                                                
Note                                                                            
-----                                                                           
   - dynamic sampling used for this statement                                   

已选择 17 行。



SQL> --创建目录(索引)
SQL> create index myindex on emp(deptno);

索引已创建。

SQL> --同义词(别名)
SQL> show user
USER 为 "SCOTT"
SQL> select count(*) from hr.employees;

  COUNT(*)                                                                                                                                                                                              
----------                                                                                                                                                                                              
       107                                                                                                                                                                                              

已选择 1 行。

SQL> --为hr.employees起别名  ---> 同义词
SQL> create synonym hremp for hr.employees;
create synonym hremp for hr.employees
同义词已创建。

SQL> select count(*) from hremp;

  COUNT(*)                                                                                                                                                                                              
----------                                                                                                                                                                                              
       107                                                                                                                                                                                              

已选择 1 行。
原文地址:https://www.cnblogs.com/Aaron-007/p/12814621.html