


Introduction to mysql views and why they are used and their rules and limitations
View introduction
Requires MySQL 5 MySQL 5 adds support for views. Therefore, the contents of this chapter apply to MySQL 5 and later versions.
Views are virtual tables. Unlike tables that contain data, views only contain queries that dynamically retrieve the data when used.
The best way to understand the view is to look at an example:
Input:
select cust_name ,cust_contact from customers,orders,orderitems where customers.cust_id = orders.cust_id and orderutems.order_num = order.orders.order_num and prod_id = 'TNT2';
This query is used to retrieve customers who have ordered a specific product. Anyone who needs this data must understand the structure of the related tables and know how to create queries and join the tables. In order to retrieve the same data for other products (or products), the final WHERE clause must be modified. Now, if you can wrap the entire query into a virtual table called productcustomers, you can easily retrieve the same data as follows:
Enter:
select cust_name ,cust_contact from productcustomers where prod_id = 'TNT2';
This That's what the view is for. productcustomers is a view, and as a view, it does not contain any columns or data that should be in the table, it contains a SQL query (the same query used above to join the tables correctly).
Why use views
We have already seen an example of the application of views. Below are some common applications of views.
1. Reuse SQL statements.
2. Simplify complex SQL operations. After you write a query, you can easily reuse it without knowing its underlying query details.
3. Use parts of the table instead of the entire table.
4. Protect data. A user can be granted access to a specific portion of a table rather than the entire table.
5. Change data format and representation. Views can return data that is represented and formatted differently than the underlying table.
After views are created, they can be utilized in essentially the same way as tables. You can perform SELECT operations on views, filter and sort data, join views to other views or tables, and even add and update data (there are certain restrictions on adding and updating data. More on this later).
It is important to know that a view is simply a facility for viewing data stored elsewhere. Views themselves do not contain data, so the data they return is retrieved from other tables. When you add or change data in these tables, the view returns the changed data.
Performance Issues Because the view does not contain data, every time the view is used, any retrieval required for query execution must be processed. If you create complex views with multiple joins and filters or nested views, you may find that performance degrades significantly. Therefore, before deploying an application that uses a large number of views, you should test it.
Using Views
After understanding what views are (and the rules and constraints that govern them) , let’s take a look at the creation of views.
1. Views are created using the CREATE VIEW statement.
2. Use SHOW CREATE VIEW viewname; to view the statement that creates the view.
3. Use DROP to delete the view, the syntax is DROP VIEW viewname;.
4. When updating a view, you can use DROP first and then CREATE, or you can use CREATE ORREPLACE VIEW directly. If the view to be updated does not exist, the second update statement will create a view; if the view to be updated exists, the second update statement will replace the original view.
Rules and restrictions for views
The following are some of the most common rules and restrictions regarding the creation and use of views.
1. Like tables, views must be uniquely named (views cannot be given the same name as other views or tables).
2. There is no limit to the number of views that can be created.
3. In order to create a view, you must have sufficient access rights. These limits are usually granted by the database administrator.
4. Views can be nested, that is, you can construct a view using queries that retrieve data from other views.
5.ORDER BY can be used in a view, but if the SELECT to retrieve data from the view also contains ORDER BY, the ORDER BY in the view will be overwritten.
6. Views cannot be indexed and cannot have associated triggers or default values.
7. Views can be used together with tables. For example, write a SELECT statement that joins a table and a view.
The above is the detailed content of Introduction to mysql views and why they are used and their rules and limitations. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

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.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

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.

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.
