Data Application Master's SQL Basic Tutorial Sharing 12-View

Chapter 4 Other Features of SQL

(This chapter will briefly introduce views, transaction management, cursors, etc. in SQL to you. As for their practical application, you still need to further understand and learn according to the specific DBMS)


Eagle-eyed Perspective - View

1. View

1. What is view

【Introduction to knowledge points】

In SQL, we define the view VIEW as a visual table based on the results of the SQL statement. It does not contain data itself, and the data is dynamically generated.
A view contains virtual rows and columns, similar to a virtual table, also called a virtual table. We can add functions to the view, perform SQL operations, etc., and it will not affect the original table.
So what is the use of views?
Before we study this chapter, the robot has expanded a table StUnion (student union table) for us, which contains student ID, student union ID UID, name SName, and job Post (the data type is VARCHAR(20)):



 

If we now want to query the ID, name, and credit information of students who have the position of Officer, we need to connect the student table and the StUnion table to query.

SELECT student.ID,student.SName,stunion.Post,student.Credit FROM student,stunion
WHERE student.ID = stunion.ID
AND stunion.Post = 'Officer';

 

 

Then if we wrap the entire query into a view, the query becomes very simple.
What kind of view to create? How to create it?
Click to go to the next lesson.

(Access does not support views, SQLite only supports read-only views)

2. Create and use views

【Introduction to knowledge points】

The syntax for creating a view and creating a table is similar, both are created using CREATE.
The syntax is as follows:

CREATE VIEW view name AS
SELECT statement;

 

 

According to the example in the previous section, the robot helps us create the original SQL into a view:

CREATE VIEW myview AS
SELECT student.ID,student.SName,stunion.Post,student.Credit FROM student,stunion
WHERE student.ID = stunion.ID;

 

 

At this time, it is much more convenient to use myview to query the previously requested content.
In fact, myview is a virtual table at this time, and its content is as follows (myview has no data when not in use):



 

【Task】

Try to use myview to complete the query.

【hint】

SELECT * FROM myview
WHERE Post = 'Officer';

 

【check】

A query using a view is successful.

【Code Template】

SELECT
WHERE Post = 'Officer';

 

3. Modify delete view

【Introduction to knowledge points】

Modify the delete view, a lot of content is actually consistent with the operation table.

CREATE OR REPLACE VIEW 视图 AS
SELECT[clause]
FROM 表
WHERE[clause];
-- update view

DROP VIEW view;
-- delete view

 

In fact, creating, modifying, and deleting views can be done intuitively on most DBMSs.

4. Summary

【Introduction to knowledge points】

A view is a virtual table, which is actually a query statement. In general, using a view has the following advantages:
the use of a view can simplify complex table joins;
the retrieved data can be formatted and output;
the unwanted ones can be filtered out data.

If you want to know more about views, you might as well find the view data of the corresponding database software according to your needs. Compare views are used differently in different DBMSs.

 

To be continued below. . . . . .

 

Welcome to visit our official website:

http://www.datanew.com/datanew/homepage

http://www.lechuangzhe.com/homepage

Guess you like

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