This tutorial explains how to create, update, and drop VIEWS in MySQL with syntax and examples.
In MySQL, a VIEW is not a physical table, but rather, it is in essence a virtual table created by a query joining one or more tables.
The syntax for the CREATE VIEW statement in MySQL is:
Optional. If you do not specify this clause and the VIEW already exists, the CREATE VIEW statement will return an error.
The name of the VIEW that you wish to create in MySQL.
Optional. The conditions that must be met for the records to be included in the VIEW.
Here is an example of how to use the CREATE VIEW statement to create a view in MySQL:
This CREATE VIEW example would create a virtual table based on the result set of the SELECT statement. You can now query the MySQL VIEW as follows:
You can modify the definition of a VIEW in MySQL without dropping it by using the ALTER VIEW statement.
The syntax for the ALTER VIEW statement in MySQL is:
Here is an example of how you would use the ALTER VIEW statement in MySQL:
This ALTER VIEW example in MySQL would update the definition of the VIEW called hardware_suppliers without dropping it. In this example, we are adding the address and city columns to the VIEW.
Once a VIEW has been created in MySQL, you can drop it with the DROP VIEW statement.
The syntax for the DROP VIEW statement in MySQL is:
The name of the view that you wish to drop.
Optional. If you do not specify this clause and the VIEW does not exist, the DROP VIEW statement will return an error.
Here is an example of how to use the DROP VIEW statement in MySQL:
This DROP VIEW example would drop/delete the MySQL VIEW called hardware_suppliers.