Oracle basic part three (views, materialized views, sequences, synonyms, indexes)

Oracle basic part three (views, materialized views, sequences, indexes)

1 view

1.1 Overview

View definition:
Advantages of views:
1. Simplify data operations: Views can simplify the way users process data
2. Focus on specific data: unnecessary data or sensitive data can not appear in the view
3. Views provide a simple and effective security mechanism that can Customize different users' access to data
4. Provide backward compatibility: Views enable users to create backward-compatible interfaces for tables when their schema changes.

1.2 Create a normal view

CREATE [OR PEPLACE] [FORCE] VIEW view_name
AS subquery
[WITH CHECK OPTION]
[WITH READ ONLY]

Explanation:
OR REPLACE: If the created view already exists, ORACLE will automatically rebuild the view.
FORCE: ORACLE will automatically create the view regardless of whether the base table exists.
subquery: a complete SELECT statement in which the alias
WITH CHECK OPTION can be defined: Inserted or modified data must satisfy the constraints defined by the view
WITH READ ONLY: the view cannot perform any DML operations

1.2.1 Create a normal view

create view view_owners1 as
select * from t_owners where ownertypeid = 1

1.2.2 Create a view with check constraints

create view view_address2 as
select * from t_address where areaid = 2
with check option

1.2.3 Creation and use of read-only views

create or replace view view_owners1 as
select * from t_owners where ownertypeid = 1
with read only

1.2.4 Forced view creation

create force view view_test as  -- view_test不存在
select * from t_test

1.2.5 Creating complex views

1.2.5.1 Multi-table association

create or replace view view_owners as 
select ow.id 业主编号, ow.name 业主名称,ot.name 业主类型  from t_owners ow, t_ownertype ot
where ow.ownertypeid = ot.id

1.2.5.2 Complex views of grouped aggregate statistics

create view view_accountsum as
select year,month,sum(money) money from t_account
group by year,month
order by year,month

1.3 Create materialized views

1.3.1 Syntax

CREATE METERIALIZED VIEW view_name 
[BUILD IMMEDIATE | BUILD DEFERRED]
REFRESH [FAST|COMPLETE|FORCE]
[
ON  [COMMIT | DEMAND] | START WITH [start_time] NEXT(next_time) 
]
as

Explanation:
BUILD IMMEDIATE generates data when creating a materialized view (default)
BUILD DEFERRED does not generate data when creating it, and will generate data as needed later.

COMPLETE completely
FAST incremental update
FORCE automatic (default)

ON COMMIT refreshes the materialized view when the base table performs a commit operation
ON DEMAND refreshes manually (default)

1.3.2 Example

1.3.2.1 Create a manually refreshed materialized view

create materialized view mv_address1 
as
select ad.id, ad.name, ar.name arname  from t_address ad, t_area ar
where ad.areaid = ar.id

1.3.2.2 Create an automatically refreshed materialized view

create materialized view mv_address2
refresh COMPLETE
on commit  -- 自动刷新
as
select ad.id, ad.name, ar.name arname  from t_address ad, t_area ar
where ad.areaid = ar.id

1.3.2.3 Create a materialized view that does not generate data

create materialized view mv_address3
build deferred  -- 创建不产生数据
refresh
on commit
as
select ad.id, ad.name, ar.name arname  from t_address ad, t_area ar
where ad.areaid = ar.id

Note: You must refresh manually for the first time to view the data.

1.3.2.4 Create a materialized view with incremental refresh

Note: The prerequisite is that a materialized view log must be created: what changes have occurred in the base table, use these records to update the materialized view

Step 1: Create materialized view log

create materialized view log on t_address with rowid
create materialized view log on t_area with rowid

Step 2: Create an incrementally refreshed materialized view.
Note: The statement in creating an incrementally refreshed materialized view must have the ROWID of the base table, and query the association through the ROWID.

create materialized view mv_address4
refresh fast
as
select ad.rowid adrowid,ar.rowid arrowid,ad.id, ad.name, ar.name arname  from t_address ad, t_area ar
where ad.areaid = ar.id

1.3.2.5 Performing a manual refresh of a materialized view

grammar:

begin
  DBMS_MVIEW.refresh('视图','C');
end;

Example:

begin
  DBMS_MVIEW.refresh('mv_address1','c');
end;

1.4 Query view

1.4.1 Syntax

select * from 视图名称 where 条件

1.4.2 Example

select * from view_owners1  where addressid = 1

1.5 Modify view data

1.5.1 Syntax

update 视图 set 字段 =where 条件

1.5.2 Example

update view_owners1 set name = '范小冰' where id = 1

Note : 1. Read-only views cannot be modified; 2. Modify views with check constraints, and check constraints cannot be modified; 3. For complex views, you can only modify the key to retain the data in the table, and complex views with grouped aggregate statistics cannot be modified. No key retention table

-- 无法修改成功的语句,因为该视图的条件时 areaid = 2
update view_address2 set areaid = 3 where id =4

2 sequence

2.1 Grammar

CREATE SEQUENCE sequence //创建序列名称
[INCREMENT BY n] //递增的序列值是n 如果n是正数就递增如果是负数就递减 默
认是1
[START WITH n] 1/开始的值,递增默认是 minvalue 递减是 maxvalue
[MAXVALUE n|NOMAXVALUE)] //最大值
[(MINVALUE n|NOMINVALUE)//最小值
KCYCLE|NOCYCLE]] //循环/不循环(默认不循环)
[(CACHE n|NOCACHE)];//分配并存入到内存中(默认缓存20个)

2.2 Create a sequence

2.2.1 Create a normal sequence

create sequence seq_test

2.2.2 Creating complex sequences

2.2.2.1 Create a non-cyclic sequence with maximum value

create sequence seq_test1
maxvalue 20;

2.2.2.2 Create a non-cyclic sequence with start value and maximum value

create sequence seq_test2
increment by 10
start with 10
maxvalue 100

2.2.2.3 Create a loop sequence with a start value and a maximum value

create sequence seq_test3
increment by 10
start with 10
minvalue 10
maxvalue 210
cycle 

2.3 Modify sequence

2.3.1 Syntax

ALTER SEQUENCE 序列名称 修改的需求参数

2.3.2 Example

ALTER SEQUENCE seq_test MAXVALUE 5000 CYCLE

2.4 Delete sequence

2.4.1 Syntax

drop SEQUENCE 序列名称

2.4.1 Example

drop sequence seq_test5

3 synonyms

3.1 Create synonyms

Synonyms are equivalent to aliases

3.1.1 Syntax

create [public] SYNONYM synooym for objec

Explanation: synonym represents the name of the synonym to be created, and object represents a table, view, or sequence.

3.1.1 Creating examples

3.1.1.1 Creating private synonyms

Private synonyms are only available to this user

create synonym owners for t_owners

3.1.1.1 Create shared synonyms

Shared synonyms are available to all users

create public synonym owners2 for t_owners

4 index

4.1 Overview

1) What is an index?
(1) A 'database structure' for the server to quickly find a row in a table
(2) Can be understood as: a 'table of contents' in a book
2) Advantages of indexes
(1) Speed ​​up the 'retrieval speed' of data
(2 ) can guarantee the 'uniqueness' of column values ​​(unique, primary key)
(3) achieve 'referential integrity' (foreign key) between tables
(4) when using order by, group by clauses, can reduce Sorting and grouping time
3) Disadvantages of indexes
(1) When DML operations are performed on table data, the indexes are automatically maintained, which 'reduces the speed of DML operations'
(2) Indexes need to occupy 'physical space', which is the same as that of data tables. Tablespaces are the same as tablespaces
(3) Creating and maintaining indexes takes time, and this time increases as the amount of data increases.
4) Principles of using indexes
(1) 'Primary key' and 'foreign key' will automatically create indexes, No manual operation required
(2) Select the correct table: often retrieve less than 15% of the rows in a table containing a large amount of data
(3) Select the correct column: the relationship between multiple tables
(4) Arrange the index columns reasonably: (A , B, C) A is the most commonly used, B is the second most commonly used, and C is the last

4.2 Create index syntax

4.2.1 Create a normal index

create index 索引名 on 表名(列明)

4.2.2 Create a unique index

Unique index: cannot be repeated, the unique index automatically created by the primary key index

create unique index 索引名称 on 表明(列名)

4.2.3 Create composite index

Composite index: The order is also required. Build a composite index according to query habits.

create  index 索引名称 on 表明(列名1, 列明2......)

4.2.4 Creating a reverse key index

Reverse key index: (sequential) decimal → binary → reverse key → decimal (random number)), the level is more balanced.
Advantages of reverse key index:
1. Using traditional B-tree index, when the index column is When generated sequentially, the corresponding index key values ​​will be basically distributed in the same leaf block. When users operate on this column, contention for index blocks will inevitably occur.
2. Use an inverted index to reverse the key values ​​of the index column, so that the sequential key values ​​are dispersed into different leaf blocks, thereby reducing the index Contention for blocks.

create  index 索引名称 on 表明(列名) reverse;

4.2.4 Create bitmap index

Usage scenarios: Bitmap indexes are suitable for creating on low-cardinality columns.
Bitmap indexes do not store ROWID directly, but store the mapping of byte bits to ROWID.
Advantages: Reduce response time and save space.
Bitmap indexes only take effect when using equals. Bitmap index does not take effect during range query

create bitmap index 索引名称 on 表明(列名)

4.3 Example of creating an index

4.3.1 Create a normal index

create index index_owners_name on t_owners(name);

4.3.2 Create a unique index

create unique index index_owners_meter on t_owners(watermeter);

4.3.3 Create composite index

create index index_owners_ah on t_owners(addressid,housenumber);

4.3.4 Create bitmap index

create bitmap index index_owners_typeid on t_owners(ownertypeid)

Guess you like

Origin blog.csdn.net/slb190623/article/details/130117200