Table of Contents
Common database objects
The concept of view
Create a view
View the view
Update the view data
Modify the view
Delete View
Advantages and Disadvantages of View
Home Database Mysql Tutorial What are the concepts and operating functions of MySQL views?

What are the concepts and operating functions of MySQL views?

May 27, 2023 pm 10:17 PM
mysql

Common database objects

Object Description
Table( TABLE) Table is a logical unit for storing data, which exists in the form of rows and columns. Columns are fields and rows are records
Data Dictionary is the system table, a table that stores database-related information. The data of system tables is usually maintained by the database system. Programmers usually should not modify it. They can only view the
Constraints (CONSTRAINT) rules for performing data verification. Use Rules for ensuring data integrity
View (VIEW) The logical display of data in one or more data tables, the view does not store the data
Index (INDEX) is used to improve query performance, equivalent to the directory of the book
Stored procedure (PROCEDURE) Used to complete a complete business process, there is no return value, but multiple values ​​can be passed to the calling environment through outgoing parameters
Storage function (FUNCTION) Used to complete a specific calculation, with a return value
Trigger (TRIGGER) is equivalent to an event listener, when a specific event occurs in the database After that, the trigger is triggered and the corresponding processing is completed

The concept of view

A view is a kind of virtual table, which itself does not have data and occupies very little memory space. It is an important concept in SQL.

Views are built on existing tables, and these tables on which views are built are called base tables.

The creation and deletion of views only affects the view itself and does not affect the corresponding base table. When add, delete, modify (DML) operations are performed on the view, the data in the view will be updated accordingly, and vice versa, the data in the data table will also change.

The statement that the view provides the data content is the SELECT statement. The view can be understood as the stored SELECT statement

In the database, the view does not save the data, the data is actually saved in the data table. If you add, delete, or modify data in the data table, the data in the view will change accordingly. vice versa.

View is another form of expression that provides users with base table data. Under normal circumstances, the database of small projects does not need to use views, but in large projects and when the data tables are relatively complex, the value of views becomes prominent. It can help us put frequently queried result sets into virtual tables. , improve usage efficiency. It is very easy to understand and use.

Create a view

The alias of the field in the query statement will appear as the alias of the view

CREATE VIEW vu_emps
AS 
SELECT employee_id,last_name,salary
FROM emps;
Copy after login
CREATE VIEW vu_emps2(emp_id,name,monthly_sal)
AS 
SELECT employee_id,last_name,salary
FROM emps;
Copy after login

Create a view for multiple tables

CREATE VIEW vu_emp_dept
AS
SELECT employee_id,e.department_id,department_name
FROM emps e JOIN depts d
ON e.department_id = d.department_id;
SELECT * FROM vu_emp_dept;
Copy after login

Use the view to manipulate the data Formatting

CREATE VIEW vu_emp_dept1
AS
SELECT CONCAT(e.last_name,'(',d.department_name,')') emp_info
FROM emps e JOIN depts d
ON e.department_id = d.department_id;
Copy after login

Create a view based on a view

CREATE VIEW vu_emp4
AS 
SELECT department_id,department_name FROM vu_emp_dept;
SELECT * FROM vu_emp4;
Copy after login

View the view

View the table object of the database, view object

SHOW TABLES;
Copy after login

View the database structure

DESC vu_emp4;
Copy after login

View the attribute information of the data

mysql> SHOW TABLE STATUS LIKE 'vu_emp4'\G;
*************************** 1. row ***************************
           Name: vu_emp4
         Engine: NULL
        Version: NULL
     Row_format: NULL
           Rows: NULL
 Avg_row_length: NULL
    Data_length: NULL
Max_data_length: NULL
   Index_length: NULL
      Data_free: NULL
 Auto_increment: NULL
    Create_time: NULL
    Update_time: NULL
     Check_time: NULL
      Collation: NULL
       Checksum: NULL
 Create_options: NULL
        Comment: VIEW
1 row in set (0.00 sec)
ERROR: 
No query specified
Copy after login

View the detailed definition information of the view

mysql> SHOW CREATE VIEW vu_emp4\G;
*************************** 1. row ***************************
                View: vu_emp4
         Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `vu_emp4` AS select `vu_emp_dept`.`department_id` AS `department_id`,`vu_emp_dept`.`department_name` AS `department_name` from `vu_emp_dept`
character_set_client: utf8
collation_connection: utf8_general_ci
1 row in set (0.00 sec)
ERROR: 
No query specified
Copy after login

Update the view data

Updating the data in the view will result in the data in the base table Modification

Updating the data in the table will also result in modification of the data in the view

In order for the view to be updatable, each row in the view must correspond to a row in the base table And there is a one-to-one relationship between the two. In addition, when the following conditions occur in the view definition, the view does not support update operations:

  • If "ALGORITHM = TEMPTABLE" is specified when defining the view, the view will not support INSERT and DELETE operations;

  • The view does not contain all columns in the base table that are defined as non-empty and have no default value specified, and the view will not support the INSERT operation;

  • If a JOIN joint query is used in the SELECT statement that defines the view, the view will not support INSERT and DELETE operations;

  • Mathematical expressions are used in the field list after the SELECT statement that defines the view formula or subquery, the view will not support INSERT, nor does it support UPDATE field values ​​that use mathematical expressions or subqueries;

  • is in the field list after the SELECT statement that defines the view Using DISTINCT, aggregate functions, GROUP BY, HAVING, UNION, etc., the view will not support INSERT, UPDATE, DELETE;

  • The SELECT statement that defines the view contains a subquery, and the subquery The table after FROM is referenced in the query, and the view will not support INSERT, UPDATE, and DELETE;

  • The view definition is based on a non-updatable view; Constant view.

Although views can update data, in general, views as virtual tables are mainly used to facilitate queries, and it is not recommended to update view data. Changes to the view data are completed by operating the data in the actual data table.

Modify the view

Method 1: Use the CREATE OR REPLACE VIEW clause to modify the view

CREATE OR REPLACE VIEW empvu80
(id_number, name, sal, department_id)
AS
SELECT employee_id, first_name || ' ' || last_name, salary, department_id
FROM employees
WHERE department_id = 80;
Copy after login
CREATE OR REPLACE VIEW vu_emp4
AS 
SELECT CONCAT(e.last_name,'(',d.department_name,')') emp_info
FROM emps e JOIN depts d
ON e.department_id = d.department_id;
Copy after login

Note: The aliases of each column in the CREATE VIEW clause should be the same as each column in the subquery Corresponding.

Method 2: ALTER VIEW

The syntax to modify the view is:

ALTER VIEW view name
AS
Query statement

ALTER VIEW vu_emp4
AS 
SELECT CONCAT(e.last_name,'(',d.department_name,')') emp_info
FROM emps e JOIN depts d
ON e.department_id = d.department_id;
Copy after login

Delete View

DROP VIEW vu_emp4;
DROP VIEW IF EXISTS vu_emp1;
Copy after login

Advantages and Disadvantages of View

Advantages:

  • Simple operation

  • Reduce data redundancy

  • Data security

  • Adapt to flexible and changing needs

  • Able to decompose complex query logic

Disadvantages:

  • High maintenance cost

  • Readability not good

The above is the detailed content of What are the concepts and operating functions of MySQL views?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

Explain the purpose of foreign keys in MySQL. Explain the purpose of foreign keys in MySQL. Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

Compare and contrast MySQL and MariaDB. Compare and contrast MySQL and MariaDB. Apr 26, 2025 am 12:08 AM

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

What software is better for yi framework? Recommended software for yi framework What software is better for yi framework? Recommended software for yi framework Apr 18, 2025 pm 11:03 PM

Abstract of the first paragraph of the article: When choosing software to develop Yi framework applications, multiple factors need to be considered. While native mobile application development tools such as XCode and Android Studio can provide strong control and flexibility, cross-platform frameworks such as React Native and Flutter are becoming increasingly popular with the benefits of being able to deploy to multiple platforms at once. For developers new to mobile development, low-code or no-code platforms such as AppSheet and Glide can quickly and easily build applications. Additionally, cloud service providers such as AWS Amplify and Firebase provide comprehensive tools

SQL vs. MySQL: Clarifying the Relationship Between the Two SQL vs. MySQL: Clarifying the Relationship Between the Two Apr 24, 2025 am 12:02 AM

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

See all articles