[Database Principle] Relational Database Standard Language SQL and Relational Database Management System SQL Server (7)

View.

When I introduced some basic concepts of SQL in the standard language of relational database SQL and relational database management system SQL Server (1 ), views were mentioned. The view is a virtual data table whose content is defined by the query. From the user's point of view, the view uses different paths to see an actual table, just like a window. You can see the tall buildings outside through the window. Different parts of the high-rise building, and through the view you can see the content of interest to the user in the DB.
Like the basic table, the view contains a series of named column and row data, but the view does not exist in the database as a data value storage set, unless it is an indexed view. The row and column data in the view comes from the basic table referenced by the query that defines the view, and is dynamically generated when the view is referenced. Views are usually used to centralize, simplify, and customize each user's different understanding of the database. Views can be used as a security mechanism by allowing users to access data through the view without granting users direct access to the basic tables associated with the view.

Create a view.

Before we give a complete grammatical format for creating views, let's first have an intuitive understanding of this type of statement through a few examples.

[Example] Create a view Sub_T of the teachers in the computer department.

CREATE VIEW Sub_T
AS SELECT TNo,TN,Prof
   FROM T
   WHERE Dept='计算机'

In the above code, the name of the view is Sub_T, which is composed of the three columns TNo, TN, and Prof in the subquery. After the view is created, the user's access to it is restricted to the "Computer System", and can only see the contents of the three fields TNo, TN and Prof, thus achieving the purpose of data confidentiality.
It should be noted that after the view is created, only the definition of the view is stored in the data dictionary, and the subquery statement in the above code is not executed. When the user operates on the view, the corresponding data is taken out of the basic table according to the definition of the view.

[Example] Create a student status view S_SC_C (including student ID, name, course name and grades).

CREATE VIEW S_SC_C(SNo,SN,CN,Score)
AS SELECT S.SNo,SN,CN,Score
   FROM S,C,SC
   WHERE S.SNo=SC.SNo AND SC.CNo=C.CNo

This view is obtained by connecting three tables S, C and SC. SNo column exists in both S and SC tables, so you need to specify the view column name.

[Example] Create a student average score view S_Avg.

CREATE VIEW S_Avg(SNo,Avg)
AS SELECT SNo,Avg(Score)
   FROM SC
   GROUP BY SNo

The Avg of one of the columns of the view is the calculation result of the library function, so the column name needs to be specified when defining it.
Having seen the above three examples, we will give CREATE VIEWthe syntax format of the statement:

CREATE VIEW NAME (Col_1,Col_2,...,Col_n)
AS select_statement
  • NAME, The name of the view.
  • Col_i, The column name of the view, we need to specify the column name of the view when and only when the following situations occur: ①The existing attribute column is derived from an arithmetic expression, function or constant; ②Multiple columns may have the same name ( Because of the connection); ③The user wants the specified name of a column in the view to be different from the name of its source column (in short, the attribute column can be named custom).
  • select_statement, The SELECTstatement that defines the view , the statement can use multiple tables and other views.

Modify the view.

SQL provides ALTER VIEWstatements to modify the view.

[Example] Modify the created student status view S_SC_C, remove the student number attribute column, that is, only keep the name, course name and grade.

ALTER VIEW S_SC_C (SN,CN,Score)
AS SELECT SN,CN,Score
   FROM S,C,SC
   WHERE S.SNo=SC.SNo AND SC.CNo=C.CNo

Delete the view.

SQL provides statements DROP VIEWfor deleting views.

[Example] Delete Sub_T.

DROP VIEW Sub_T

Note that after the view is deleted, only the definition of the view in the DD is deleted (in fact, only the definition is saved), and the basic table from which the view is derived will not be affected. But the other views derived from this view have lost their meaning, although their definitions are still preserved.

Query view.

We said that a view is like a virtual table, and the query operations for the view are exactly the same as for the basic table.

[Example] Query the teacher's number and name of the professor in the sub_T of the computer department's teacher situation view.

SELECT TNo,TN
FROM Sub_T
WHERE Prof='教授'

The execution process of this query first is that the system finds the definition of Sub_T from DD (Data Dictionary), and then combines the definition with the user's query to convert it into an equivalent operation on the basic table. This conversion process is called View resolution View Resolution. The above code is equivalent to executing the following query:

SELECT TNo,TN
FROM T
WHERE Prof='教授' AND Dept=计算机''

Update the view.

Since the view is a virtual table, all our update operations on the view:

  • Add INSERT
  • Modify UPDATE
  • Delete DELETE

Will be converted to the operation of the basic table, and all the syntax and basic table update operations are exactly the same, you can refer to the relational database standard language SQL and relational database management system SQL Server (6) . Some updates are not feasible in theory, and some are difficult to implement. We only consider those updates that can be performed.

[Example] Add a record (Teacher ID: T6, Name: Li Dan, Title: Associate Professor) to the teacher situation view of the Department of Computer Science.

INSERT INTO Sub_T (TNo,TN,Prof)
VALUES ('T6','李丹','副教授')

Like the query operation above, the system will first find out the definition of Sub_T from DD, and then combine the definition with the operation to be performed by the user, and transform it into an operation on the basic table.

Guess you like

Origin blog.csdn.net/weixin_44246009/article/details/108065492