mysql< View >

Source: Internet
Author: User

View View Overview

A view is a table that is exported from one or several basic tables and can operate as a basic table. Only view definitions are stored in the view, not the data corresponding to the view.

If the data for the base table changes, the data that is queried from the view also changes. So you can think of a view as a virtual table.

Advantages

Simplifying query statements

In daily development we can define frequently used queries as views, allowing users to avoid a large number of repetitive operations.

Security

With view users can only query and modify the data they see, other data in the database is neither visible nor accessible.

Logical Data Independence

Views can help users mask the impact of changes in the real-world table structure.

View Management Create a syntax format for a view

Create view< View name >[(< list; ...)] as subquery [with CHECK OPTION]

A subquery can appear with any SELECT statement, but the order by and distinct are usually not allowed in the query statement

Create a view on a single table Create a view on multiple tables View View

Use the Describe statement to view field information for a view

DESCRIBE view name;

or desc view name;

Use the show TABLE status statement to view basic information about a view

SHOW TABLE STATUS like ' view name '

You can use the show CREATE VIEW statement to view not only the definition statement when creating the view, but also the character encoding of the view

SHOW CREATE view name;

Modify a View

Because views are virtual tables that do not actually store data, updating the operation of the view ultimately translates to an update operation on the base table. To prevent data updates on a view, you can add a with CHECK option clause when you define the view when you are manipulating basic table data that is not part of the views scope.

We found that the data in the table changed. This is not safe because views are mostly browsed to the user and do not need to be modified. So we usually add the WITH CHECK option clause at the end of the SQL statement so that the user cannot modify it at will.

1. Modify the view using the Create OR REPLACE view statement

CREATE [OR REPLACE][algorithm = {UNDEFINED | MERGE | TempTable}]

VIEW view_name [(column_list)]

As Select_statement

[with[cascaded | LOCAL] CHECK OPTION]

2. Modify the view with the ALTER statement

ALTER [algorithm = {UNDEFINED | MERGE | TempTable}]

VIEW view_name [(column_list)]

As Select_statement

[with[cascaded | LOCAL] CHECK OPTION]

Update View

When the view contains the following content, the update operation for the view cannot be performed:

The view does not contain columns that are defined as non-empty in the base table.

A mathematical expression is used in the field list after the SELECT statement that defines the view.

The aggregate function is used in the field list after the SELECT statement that defines the view.

The Distinct,union,top,group by or HAVING clause is used in the SELECT statement that defines the view.

Update a view with the UPDATE statement

Updating views with DELETE statements

Updating views with INSERT statements

Delete a view

Only the definition of the view can be deleted and the data will not be deleted.

Drop View < view name >

Application case-The application of the view

mysql< View >

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.