Analysis of mysql view functions and usage examples

Analysis of mysql view functions and usage examples

This article uses examples to illustrate the functions and usage of MySQL views. Share with you for your reference, the details are as follows:

In layman's terms, a view is the result set returned after a SELECT statement is executed.

//Single table view CREATE [ALGORITHM]={UNDEFINED|MERGE|TEMPTABLE}]
    VIEW view name [(property list)]
    AS SELECT statement [WITH [CASCADED|LOCAL] CHECK OPTION];
  //Return the query result as a virtual table, which will change according to the database changes CREATE VIEW work_view(ID, Name, Addr) AS SELECT id,name,address FROM work;
  //Create a view on multiple tables//ALGORITHM=MERGE ALGORITHM has three parameters: merge, TEMPTABLE, UNDEFINED (merge merge table, temptable cannot update information, undefined)
   CREATE ALGORITHM=MERGE VIEW work_view2(ID,NAME,SALARY) AS SELECT work.id,name,salary FROM work,salary WHERE work.id=salary.id WITH LOCAL CHECK OPTION;

Facilitate operations, especially query operations, reduce complex SQL statements, and enhance readability;

The view and the table are in a one-to-one relationship : If there are no other constraints (such as fields that are not in the view but are required fields in the basic table), data addition, deletion and modification operations can be performed;

The view and table are in a one-to-many relationship : If you only modify the data of one table and there are no other constraints (such as fields that are not in the view but are required fields in the basic table), you can perform the data modification operation, such as the following statement, and the operation is successful;

Difference between views and temporary tables

  • A view is just a precompiled SQL statement and does not store actual data.
  • Temporary tables are actual tables stored in tempdb.
  • The allocation of physical space is different. Try not to allocate space. Temporary tables will allocate space.
  • A view is a snapshot, a virtual table
  • A temporary table is an objective table type object. Create TEMPORARY table
  • Their structures are a table and a snapshot. You can think of a view as a shortcut to a joint table.

Creating a Temporary Table

CREATE TEMPORARY TABLE tmp_table (
name VARCHAR(10) NOT NULL,
value INTEGER NOT NULL)

Import query results directly into a temporary table

CREATE TEMPORARY TABLE tmp_table SELECT * FROM table_name

The temporary table is only visible to the current connection and will be automatically dropped when the connection is closed. You can only search a temporary table once in the same query statement. The show tables statement does not list temporary tables, but it does list memory tables. You cannot use rename to rename a temporary table. However, you can alter table instead:

Memory table: The table structure is built on the disk and the data is in the memory. When the service is stopped, the data in the table is lost, but the table structure is not lost. Memory tables can also be considered as a type of temporary table.

Creation of memory table:

CREATE TEMPORARY TABLE tmp_table (
name VARCHAR(10) NOT NULL,
value INTEGER NOT NULL
) TYPE = HEAP

Note: TYPE = HEAP is required.

Memory tables must use the memory storage engine

Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL query skills", "MySQL transaction operation skills", "MySQL stored procedure skills", "MySQL database lock related skills summary" and "MySQL common function summary"

I hope this article will be helpful to everyone's MySQL database design.

You may also be interested in:
  • How to create a view on multiple tables in MySQL
  • Detailed explanation of the use of views in MySQL notes
  • Sharing tips on using views in MySQL and multi-table INNER JOIN
  • Detailed explanation of the usage and differences between indexes and views in MySQL
  • Summary of MySQL view principles and usage examples
  • Application analysis based on mysql transactions, views, stored procedures, and triggers
  • Mysql matters, views, functions, trigger commands (detailed explanation)
  • MySQL view principles and basic operation examples
  • MySQL view introduction and basic operation tutorial
  • Mysql database advanced usage of views, transactions, indexes, self-connections, user management example analysis
  • Detailed explanation of how to create an updateable view in MySQL

<<:  How to configure Hexo and GitHub to bind a custom domain name under Windows 10

>>:  Simple encapsulation of axios and example code for use

Recommend

Detailed explanation of CocosCreator message distribution mechanism

Overview This article begins to introduce content...

How to configure two-way certificate verification on nginx proxy server

Generate a certificate chain Use the script to ge...

CSS3 realizes the animation of shuttle starry sky

Result: html <canvas id="starfield"&...

Detailed installation and use of RocketMQ in Docker

To search for RocketMQ images, you can search on ...

Pure CSS to modify the browser scrollbar style example

Use CSS to modify the browser scroll bar style ::...

The difference between redundant and duplicate indexes in MySQL

MySQL allows you to create multiple indexes on a ...

20 JavaScript tips to help you improve development efficiency

Table of contents 1. Declare and initialize array...

Docker Compose installation and usage steps

Table of contents 1. What is Docker Compose? 2. D...

Simple implementation of vue drag and drop

This article mainly introduces the simple impleme...

Layui implements the login interface verification code

This article example shares the specific code of ...

Building a Redis cluster on Docker

Table of contents 1. Pull the image 2. Create a R...

Example of CSS3 to achieve div sliding in and out from bottom to top

1. First, you need to use the target selector of ...

What magical uses does CSS filter have

background Basic Concepts CSS filter property app...

WeChat applet implements simple chat room

This article shares the specific code of the WeCh...