6-mysql view view definition statement

View definition statement

What is a view

View can be seen as a table temporary storage of data consisting of (non-real table, virtual table, the contents of which are defined by a query), in fact, this

In essence, it is just a select statement . Just a "wrap" the select statement (usually more complicated), and set a name, then you can

To query the table by the name, and use the name as a table.

It can also be considered that the view is the query result of a select statement, which is only placed in the database in advance. You can use this query result (with rows and columns with field names) as a table.

If a select statement is more complicated and you need to use it on multiple pages, you can make it into a view for easy use.

If some fields in a data table do not want to be shown to others (when data services are exchanged between different companies), but another needs to be shown to others, the view can also be used at this time.

View creation

create  view   视图名 [(列名1,列名2,...)]   as  一条复杂select语句;
  • The view name cannot be the same as the table name-because they are all "objects" belonging to the database;
  • The default column name of the view is the column name set in the select statement when the view is defined;
  • You can also specify your own column names when defining a view, but the number of column names specified at this time should be equal to the number of column names in the select statement;
  • The select statement can fetch data from multiple tables, and can even fetch data from other views;
  • You can rename the column obtained by the select statement, or not rename it, and use the given column name in the select statement.

Use of views

In fact, it is used as a query table (usually only used for select)

select  *  from  视图名  where 条件  order by .....
Insert picture description here

Modify view

alter view 视图名 [(列名1,列名2,...)] as select语句;

Delete view

drop  view  [if exists] 视图名;