View in MySQL

Source: Internet
Author: User

First, create a view

Create [algorithm = view algorithm] View name as SELECT statement; --can be any query statement (normal query, connection query, union query, subquery)

There are three view algorithm options:

1. Undefined: The default setting, which indicates that no algorithm is specified, typically this option is automatically specified as merge.

2. temptable: Temporal table algorithm, the system first executes the view encapsulated SELECT statement, and then executes the SELECT statement outside the view, because to execute two query statements, so the efficiency is relatively low.

3. Merge: Merge algorithm, the system merges the view-encapsulated SELECT statement with the SELECT statement outside the view before executing the merged statement, which is relatively efficient because it only needs to be executed once.

  Note: When you create a multi-table view, fields with the same name in multiple tables cause the creation to fail , either by using aliases or by querying only the different name segments.

  CREATE VIEW my_v1 as SELECT * from My_student; --Create a single table view

  CREATE VIEW my_v2 as Select c.name,c.room,s.* from My_class as C left join my  _student as s on c.id = S.class; --Create multiple table views without querying the same name field ID

  CREATE VIEW my_v3 as select c.id as c_id,c.name,c.room,s.* From My_class as C left joins My_student as s on c.id = S.class; --Query the same name field ID with an alias

Second, view view

All methods used to view the table are available for viewing views:

Show tables;

Show tables like ' pattern ';

Show create table view name; --You can also use the show CREATE view name

DESC view name;

Third, use the view

The view is used primarily for querying, and the view is queried as if it were a table.

Four, modify the view

ALTER view name as a new SELECT statement; --Changes to the view can only modify the statements he encapsulates

V. Delete a view

Drop view name;

View in MySQL

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.