How to create a view in an Oracle database [Sample Demo]

View: It is a logical table based on one table or multiple tables or views. It does not contain data. It can be used to query and modify the data in the table. The table on which the view is based is called the base table. Oracle's database objects are divided into five types: tables, views, sequences, indexes, and synonyms.

The view is a select statement stored in the data dictionary. The logical collection or combination of data can be extracted by creating a view.

Advantages of the view:

1. Access to the database, because the view can selectively select a part of the database.

2. Users can get results from complex queries through simple queries.

3. Maintain the independence of data, trying to retrieve data from multiple tables.

4. Different views can be generated for the same data.

The view is divided into simple view and complex view:

1. The simple view only obtains data from a single table, and the complex view obtains data from multiple tables;

2. The simple view does not contain functions and data groups, and the complex view does;

3. Simple views can implement DML operations, but complex views cannot.

Create the view syntax structure:

CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view_name
    [(alias[, alias]...)]

AS subquery
     [WITH CHECK OPTION [CONSTRAINT constraint]]
     [WITH READ ONLY]

Syntax analysis:

OR REPLACE: If the created attempt already exists, replace the old view;

FORCE: ORACLE will automatically create the view regardless of whether the base table exists (even if the base table does not exist, the view can be created, but the view cannot be used normally, the view can be used normally after the base table is created successfully);

NOFORCE: If the base table does not exist, the view cannot be created. This is the default option (the view will be created only if the base table exists in ORACLE).

alias: the alias defined for the column generated by the view;

subquery: a complete SELECT statement, aliases can be defined in the statement;

WITH CHECK OPTION: The inserted or modified data row must meet the constraints defined by the view;

WITH READ ONLY: By default, the base table can be added, deleted, and modified through the view, but there are many restrictions on the base table (for example: a column in the base table cannot be empty, but the column does not appear in the view, you can not pass the view Perform insert operation), WITH READ ONLY indicates that the view is a read-only view and cannot be added, deleted, or modified through this view. In the actual development, basically do not add, delete or modify the data in the table through the view.

E.g:

CREATE 
	OR REPLACE VIEW test_view ( PROJECT_ID, CONTRACT_ID,DEPT_NAME ) 
	AS SELECT p.PROJECT_ID,p.CONTRACT_ID,t.DEPT_NAME
FROM
	PINFO_CONTRACTS p
	LEFT JOIN T_SALE_CONTRACT_LIST_ALL t ON p.CONTRACT_ID = t.SALE_CONTRACT_ID
	WHERE t.DEPT_NAME = '卫生健康事业部'
    

Actual: The corresponding view is generated, and the data is also queried. As shown below:

to sum up:

1. Views are generally used to encapsulate complex query data such as related data from multiple tables. A single table is generally not very useful.

2. View data belongs to temporary data and generally cannot be directly modified. For views created by a single table, with read only can be added to prevent real data from being modified.

3. If the view is created in association with multiple tables, if you want to change the view data, you can use alternative triggers.


Reference: https://www.cnblogs.com/zl520/p/10245633.html


❤If the article is helpful to you, please click like at the top right corner of the article or at the end of the article! (づ ̄ 3 ̄)づ 

❤If you like the articles shared by the white rabbit, please pay attention to the white rabbit! (๑′ᴗ‵๑)づ╭❤~

❤If you have any questions about the article, please leave a message below or join the group to discuss [group number: 708072830]

❤In view of the limited personal experience, all opinions and technical research points, if you have any objections, please reply directly to the discussion (do not make offensive remarks)

Guess you like

Origin blog.csdn.net/weixin_43970743/article/details/111952662