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
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 configure Hexo and GitHub to bind a custom domain name under Windows 10
>>: Simple encapsulation of axios and example code for use
Overview This article begins to introduce content...
Generate a certificate chain Use the script to ge...
Result: html <canvas id="starfield"&...
To search for RocketMQ images, you can search on ...
Use CSS to modify the browser scroll bar style ::...
MySQL allows you to create multiple indexes on a ...
Table of contents 1. Declare and initialize array...
Table of contents 1. What is Docker Compose? 2. D...
Table of contents 1. Deploy consul cluster 1. Pre...
This article mainly introduces the simple impleme...
This article example shares the specific code of ...
Table of contents 1. Pull the image 2. Create a R...
1. First, you need to use the target selector of ...
background Basic Concepts CSS filter property app...
This article shares the specific code of the WeCh...