Oracle -- view, index

1.what (what is a view?)
1. A view is a database object, which is a virtual table derived from one or more data tables or views. The data corresponding to the view is not really stored in the view.
   
  Instead, it is stored in the referenced data table, and the structure and data of the view is the result of a query on the data table.

2. According to the conditions given when creating the view, the view can be part of a data table or a union of multiple base tables,
It stores the definition of the query statement to perform the retrieval to use when referencing the view.
 
2. Why (Why use a view? The advantages of a view)
1. Simplify data manipulation: Views can simplify the way users work with data.

2. Focus on specific data: Unnecessary or sensitive data can be excluded from the view.

3. Views provide a simple and effective security mechanism to customize the access rights of different users to data.

4. Provides backward compatibility: Views enable users to create a backward compatible interface for a table when its schema changes.

5. Customize data: Views allow users to view data in different ways.

6. Export and import data: Views can be used to export data to other applications.

Three: HOW (how to build a view, use a view)

-- Note: Creating a view requires the create view permission

Basic syntax for creating a view:
    CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view_name
[(alias[, alias]...)]

AS subquery
[WITH CHECK OPTION [CONSTRAINT constraint]]
[WITH READ ONLY]
in:
OR REPLACE: If the created attempt already exists, ORACLE automatically rebuilds the view;
FORCE : ORACLE will automatically create the view regardless of whether the base table exists or not;
NOFORCE: ORACLE will create the view only if the base tables exist:
alias : the alias defined for the column generated by the view;
subquery : a complete SELECT statement in which aliases can be defined;
WITH CHECK OPTION :
                            Inserted or modified data rows must satisfy the constraints defined by the view;
WITH READ ONLY :
                           No DML operations can be performed on this view.

--example:
create or replace view dept_sum_vw
              (name,minsal,maxsal,avgsal) -- define aliases for columns
       as select d.dname,min(e.sal),max(e.sal),avg(e.sal)
       from    emp e,dept d
       where e.deptno=d.deptno
       group by d.dname;
-- View query (similar to table query)
select * from dept_sum_vw where name='ACCOUNTING';
-- View DML operations
The case of --update must be the following
--1. The field of view only involves one table.
--2. If multiple tables are involved, the table column (or combination of columns) mapped by the (involved) view column must have primary or unique constraints
-- prepare test data
create table t_emp as select * from emp;
create table t_dept as select * from dept;
alter table t_emp add constraint pk_empno primary key(empno); -- add primary key
alter table t_dept add constraint pk_deptno primary key(deptno);
-- create a view
create or replace view ed_vw
as
select e.empno,e.ename,e.sal,d.dname,d.deptno from t_emp e inner join
t_dept d on e.deptno = d.deptno  --with read only;

--update operation (note: when the view definition contains
-- Grouping function, GROUP BY clause, DISTINCT keyword, ROWNUM pseudo-column
-- The update operation cannot be performed in the case of
--)
update  ed_vw set ename='SMITH_TEST' where empno=7369;

--delete operation (note: when the view definition contains the grouping function, GROUP BY clause, DISTINCT keyword, ROWNUM pseudo-column, the column is defined as an expression
-- The delete operation cannot be performed in the case of the DUAL pseudo-table
--)
delete from ed_vw where empno = 7369;

--insert operation (note: when the view definition contains
-- grouping function, GROUP BY clause, DISTINCT keyword,
-- ROWNUM pseudo-column, the definition of the column as an expression cannot perform the insert operation                  
)
insert into ed_vw(empno,ename,sal) values(2345, 'test', 99.9);


-- delete view
  The DROP VIEW VIEW_NAME statement drops a view.
            Deleting the definition of a view does not affect the data in the base tables.
            Only view owners and users with DROP VIEW privileges can drop views.
            After a view is deleted, other views or applications based on the deleted view will be invalid.

drop view ed_vw --(delete the specified view);
1.what (what is an index?)
1. Book-like directory structure
2. Oracle's "index" object, an optional object associated with the table (can be built or not)
3. The index points directly to the location of the row containing the queried value, reducing disk I/O
4. It is a separate physical structure from the indexed table
5. Oracle automatically uses and maintains indexes, and automatically updates indexes after inserting, deleting, and updating tables

2. why (why use an index?)
    Improve the speed of SQL query statements

Three: HOW (how to use)
type of index
1: Unique index
2: Combined index
3: reverse index
4: Bitmap index
5: Index based on function or condition


-- create index

create index index name ON table name (column name)

tablespace tablespace name;

 --Create a product table (tb_product) and create an index for the product_id column of the table to improve query efficiency when this column is used.
create table tb_product
(
  product_id number, --Number
  product_name varchar2(100),--name
  product_type varchar2(20), -- type
  product_price number(10,4) -- price
);

insert into tb_product values(1,'Java programming ideas','learning materials',99.9);
insert into tb_product values(2,'C++ programming ideas','study materials',98.9);
insert into tb_product values(3,'C programming ideas','learning materials',97.9);
insert into tb_product values(4, 'Javascript entry to proficient', 'learning materials', 96.9);
insert into tb_product values(5,'Java Design Pattern','Learning Materials',95.9);
insert into tb_product values(6,'C# basic to actual combat','learning materials',94.9);
insert into tb_product values(7,'Design Pattern','Learning Materials',93.9);

-- unique index

 When to create: when a column has different values

 When creating a primary Key (primary key) or unique constraint (unique constraint), a unique index will be created automatically

 Syntax: create unique index index name on table name (column name) TABLESPACE table space name;


  create unique index product_id_u1 on tb_product(product_id);


--combined index
When to create: When two or more columns frequently appear together in the where condition, then create a composite index on these columns at the same time

The order of columns in a composite index is arbitrary and need not be adjacent. But it is recommended to put the most frequently accessed columns at the top of the list

Syntax: CREATE INDEX index name ON table name (column name 1, column name 2)

     TABLESPACE tablespace name;

create unique index name_type_index on tb_product(product_name,product_type);
explain plan from select *  from  tb_product where product_name='Java编程思想'
and product_type='Learning materials';

--reverse key index
Purpose: For example, the index value is an auto-incrementing column:
When multiple users modify index rows concentrated on a few blocks, it is easy to cause resource contention, such as waiting for data blocks. At this point, an inverted index is created.
Syntax: CREATE INDEX index name ON table name (column name) reverse

     TABLESPACE tablespace name;

create unique index product_id_rev on tb_product(product_id) reverse;

--bitmap index
When to create: When there are too many duplicate values ​​in the column. For example, a column holds "gender" information.
Where conditions contain many OR operators. Fewer update operations, because all bitmaps need to be updated accordingly

Structure: A bitmap index uses a bitmap as a key value, and the bitmap contains a TRUE(1), FALSE(0), or NULL value for each data row in the table.
Pros: Bitmaps are stored in a compressed format, so they take up much less disk space than standard indexes

语法:CREATE BITMAP INDEX index ON table (column[, column]...);

CREATE BITMAP  INDEX pt_index ON tb_product (product_type);

--based on function index
When to create: When a function or expression is included in the WHERE conditional statement

Functions include: arithmetic expressions, PL/SQL functions, package functions, SQL functions, and user-defined functions.

语法:CREATE INDEX index ON table (FUNCTION(column));

CREATE INDEX fn_index ON tb_product (UPPER(product_name));


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324666627&siteId=291194637