The select statement Source: View

view

Table view there are many similarities, the view is composed of several fields and a number of records, it can be used as a data source view select statement. Even under certain conditions, can be updated through operations on the table view.
Here Insert Picture Description

Create a view

Saved views only a select statement, the source data from the view are database tables, database table called a base table, a view called virtual table. Data base table is changed, the data will change the virtual table. Create a view of syntax is as follows:

CREATE 或者REPLACE  VIEW  视图名(视图列)
	AS SELECT 表列别名
	FROM 表名

A view is an object database, so when you create a view, you need to specify which database is part of the view; number of fields in the view field list must equal the number of fields in the field list select statement.

In order to distinguish the base view table, when naming the view, it is often added to "view" or the prefix "_view" suffix.

See the definition of a view

1. After choose the database successfully created a view course_view, define the default view is stored in the database directory (for example, choose a directory), the file name course_view.frm. Use Notepad to open the file, you can see the view definition.

2. A view is a virtual table, use can also view the table structure definition of a view of view, DESC course_view;

3. MySQL command "show tables;" command not only displays the current database of all base tables, all views will also set out.

4. Information_schema MySQL database system table stores the definition of the views of all views, use the following select statement to query all the records of the table, you can also view details of all views.

select * from information_schema.views\G

Modify View

When some of the fields of the base table changes, by modifying the view exists in the database, to maintain consistency with the base table. The syntax is as follows:

CREATE OR REPLACE  VIEW 视图名(视图列名)AS SELECT  表列名

Consistent with the view to create the statement, when there is a view, edit modification statements on views; when the view does not exist, create.
Here Insert Picture Description

MySQL ALTER statement is another view of the modified method, the following syntax:

ALTER VIEW 视图名(视图列名)AS  SELECT 表列名

Update View

Update view refers to a view through the insert, update, delete data in the table, because the view is a virtual table, where there is no data. Updating through views updated all the time to the base table of the view if add or delete records, delete records or actually increase its basic form.

Not all views can be updated, the following cases can not update views:
(1) view contains COUNT (), SUM (), MAX () and MIN () function and the like. E.g:

CREATE VIEW book_view1(a_sort,a_book)
AS SELECT sort,books, COUNT(name) FROM tb_book;

(2) contained in the view UNION, UNION ALL, DISTINCT, GROUP BY, and the like HAVIG keywords. E.g:

CREATE VIEW book_view1(a_sort,a_book)
AS SELECT sort,books, FROM tb_book GROUP BY id;

(3) Constant view. E.g:

CREATE VIEW book_view1
AS SELECT 'Aric' as a_book;

(4) view contains a subquery SELECT. E.g:

CREATE VIEW book_view1(a_sort)
AS SELECT (SELECT name FROM tb_book);

(5) derived from the non-updated view of the view. E.g:

CREATE VIEW book_view1 AS SELECT * FROM book_view2;

(6) When you create a view, ALGORITHM is TEMPTABLE type. E.g:

CREATE ALGORITHM=TEMPTABLE
VIEW book_view1 AS SELECT * FROM tb_book;

No default value is present on the column (7) corresponding to the view of the table, and it is not contained in the view. For example, name field of the table contains no default value, but not included in the view field. Well, this view is not updated. Because, when updating the view, there is no record that the default value is no value is inserted, there is no NULL values ​​inserted. The database system will not allow such a situation, which prevents this view updates.

note:

Although you can view updated data, but there are a lot of restrictions. In general, it is best to view as a virtual table query data, and not through the view to update the data. Because, when using a view to update the data, if not fully consider updating data limitations in the view, it may cause data update failed.

Delete View

When a view is no longer needed, you can delete, delete one or more views can use the DROP VIEW statement, delete the view must have DROP permission. Delete to delete the view refers to a view that already exists in the database. When you delete a view, the view can only delete the definition, does not delete data. The syntax is as follows:

DROP VIEW  IF EXISTS 
     view_name [, view_name] ...
Published 141 original articles · won praise 28 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_42554191/article/details/103979170