[learning] SQL Basics-015-View
Nature: Logical data sets, no real data
2. Type
Simple view: no functions, no aggregates; DML is generally acceptable
Complex View: uses functions and aggregates; cannot accept DML
3. Principle
oracle accesses the user_views data dictionary, finds the subquery of the view, executes it, and returns the data;
Access to the view is actually access to the base table;
A view is a subquery stored in a data dictionary.
4. Creation
Premise: create view permission
Grammar:
Parameters:
force: Creates a view even if a schedule does not exist in the subquery.
noforce: Default, raises an error if the schedule does not exist.
check with check option and constraint, check where condition when creating view when dml operation is performed. Ensure that DML operates within a specific scope
With read only queries can be made, base tables cannot be modified through views. Disable DML operations
5. Application examples
Query Tablespace Usage
create view tablesp_usage as
select a.tablespace_name as tablespace_name,
to_char(a.total/1024/1024,99999999) as total_mb,
to_char((a.total-b.free)/1024/1024,99999999) use_mb,
to_char(b.free/1024/1024,99999999) as free_mb,
to_char(((total-free)/total)*100,999.99) as "Used %"
from
(select tablespace_name,sum(bytes) as total from dba_data_files
group by tablespace_name) a,
(select tablespace_name,sum(bytes) as free from dba_free_space
group by tablespace_name) b
where a.tablespace_name=b.tablespace_name order by 5 desc;
6, deleted
Drop view does not delete base table data