/    /  MySQL – View

MySQL View

 

Views are database objects that do not contain any values. Its contents are derived from the base table. The table contains rows and columns similar to the real table. Views are virtual tables created by joining one or more tables in MySQL. Unlike the base table, it does not contain any data of its own. There is one main difference between a view and a table, namely that views are definitions that are built on top of other tables (or views). Changes made to the underlying table are reflected in the View as well.

Syntax:

CREATE [OR REPLACE] VIEW view_name AS    
SELECT columns    
FROM tables    
[WHERE conditions];   

Parameters:

There are the following parameters in the view syntax:

OR REPLACE: This is an optional step. When a VIEW already exists, it is used. The CREATE VIEW statement will return an error if this clause is not specified and the VIEW already exists.

View_name: In MySQL, it specifies the name of the VIEW that you would like to create.

WHERE conditions: This option is also available. In order for records to be included in the VIEW, certain conditions must be met.

Example

With the help of an example, we will be able to better understand it. Let’s say that our database contains a table called course, and we are going to create a view based on this table. Therefore, the following example will create a VIEW named “employee” that creates a virtual table from the data in the table courses.

CREATE VIEW details AS
SELECT cust_name, role
FROM employee e ;

v1

The created view can be viewed by using the following syntax:

SELECT * FROM details;

v2