Views and indexes
view
View of the role
- Access control data, simplify the query, to avoid repeated access to the same data
Advantage views
- Limit the user to retrieve data through the view, the user can not see the underlying base tables
Precautions
- View can be understood as a temporary table will automatically change as the data changes in real tables
- Name of the view that begin with V_, show a view;
- View does not improve the efficiency of any
- View does not take up space
- Not all views can be changed (if the energy corresponding to the original table changes, can, or not, such as the use of the aggregate function, or from the group by)
Simple view
grammar
create or replace view 视图名 as select 语句;
create or replace
: Keywords, create or replaced (if not original, create, if already there, then replacing the original)
example
SQL> create or replace view v_emp as
2 select empno, ename, job from emp;
视图已创建。
SQL> select * from v_emp;
EMPNO ENAME JOB
---------- -------------------- ------------------
7777 S_HH%GGH CLERK
7369 SMITH CLERK
7499 ALLEN SALESMAN
...
Read-only view
grammar
create or replace view 视图名
as select 语句
with read only;
example
SQL> create or replace view v_emp2 as
2 select empno,ename,job from emp
3 with read only;
视图已创建。
SQL> delete from v_emp2;
delete from v_emp2
*
第 1 行出现错误:
ORA-42399: 无法对只读视图执行 DML 操作
View Management
View
View all view under the specified user
SQL> select view_name from dba_views where owner='SCOTT';
VIEW_NAME
------------------------------------------------------------
V_DEPT
V_DEPT_EMP
V_EMP
V_EMP2
dba_views
All views view the database underowner='SCOTT'
Specifies the userview_name
Name Display view
delete
SQL> drop view v_emp;
视图已删除。
SQL> drop view v_emp2;
视图已删除。
SQL> select view_name from dba_views where owner='SCOTT';
VIEW_NAME
------------------------------------------------------------
V_DEPT
V_DEPT_EMP
index
The role of the index
- Improve query efficiency
Notes index
- Limit the number of indexes . As long as the index improve query speed, but will reduce the speed of DML operations
- The only index name, length not more than 32
- Column index can be more than one (or a combination of index composite index), but the only one in the first high (i.e., less duplication of data)
- When initialized to the table, the index must first drop or Unusable (to insert data, the indexing)
- Index names IND or the beginning of IDX ;
General index
grammar
create index 索引名 on 表名(列名);
example
SQL> create index IND_emp_deptno on emp(deptno);
索引已创建。
The only index
grammar
create unique index 索引名字 on 表名(列名);
example
SQL> create unique index ind_indtest_id on ind_test(id);
索引已创建。
Management Index
View
SQL> select index_name from user_ind_columns where table_name='EMP';
INDEX_NAME
------------------------------------------------------------
UNIQUE_EMP_ENAME
PK_EMP
SQL> select index_name from user_ind_columns where table_name='IND_TEST';
INDEX_NAME
------------------------------------------------------------
IND_INDTEST_ID
Delete Index
SQL> drop index IND_emp_deptno;
索引已删除。
SQL> drop index IND_INDTEST_ID;
索引已删除。