Create View in MySQL

A view in MySQL can be created based on a single table or multiple tables. The CREATE VIEW statement is used to create a view in MYSQL.

1. Create a View Based On Single Table

Syntax:

CREATE VIEW <view_name> AS

SELECT <column1>, <column2>……., <columnN>

FROM <table-name>

WHERE [conditions];

Example:

Let’s us create a view named “IITHyderabadStudentsView” from the StudentDetails table. This view selects the students from the StudentDetails table who study in “IIT Hyderabad” university and outputs their details like student id, name, and age.

CREATE VIEW IITHyderabadStudentsView AS
SELECT sid, sname, age
FROM StudentDetails
WHERE university = "IIT Hyderabad";

Output:

Create VIEW – Single table

Now to view the tuples in the IITHyderabadStudentsView, we will query the view the just like how we query a normal table.

SELECT * FROM IITHyderabadStudentsView;

Output:

Query Output – Create VIEW – Single table

2. Create a View Based On Multiple Tables With JOIN Clause

We can create a view by joining multiple tables using the JOIN clause and additionally using WHERE keyword or GROUP BY, HAVING clauses.

Syntax:

CREATE VIEW <view_name> AS

SELECT <column1>, <column2>……., <columnN>

FROM <table1>

[ INNER | LEFT | RIGHT | FULL ] JOIN <table2> ON <joining-column>

WHERE [condition1 | condition2 | ……];

Example:

Let’s us create a view named “PythonEnrolledView” using the StudentDetails, CourseDetails, and EnrolledIn table. This view outputs the students who are enrolled in “Python Fundamentals” course the details as student id, name, and age.

CREATE VIEW PythonEnrolledView AS
SELECT S.sid, S.sname, S.age
FROM StudentDetails S JOIN EnrolledIn E ON S.sid = E.sid
JOIN CourseDetails C ON C.cid = E.cid
WHERE C.cname = "Python Fundamentals";

Output:

Create VIEW – Multiple table using JOIN

Now to view the tuples in the PythonEnrolledView, we will again query the view the just like how we query a normal table.

SELECT * FROM PythonEnrolledView;

Output:

Query Output – Create VIEW – Multiple tables (JOIN)

MYSQL View

MySQL is an open-source RDBMS, i.e. Relational Database Management System which is maintained by Oracle. MySQL has support for major operating systems like Windows, MacOS, Linux, etc. MySQL makes it easy for users to interact with your relational databases, which store data in the form of tables. You can write SQL queries to get the required data from the databases using MySQL.

In this article, we will look at “Views in MySQL”, which act as virtual tables, and understand its advantages, as well as the syntax of Views for creation, updation, and deletion, with the help of awesome examples.

Similar Reads

What are Views in MySQL?

Views in MySQL are indeed “virtual tables” that are used to view data from one or more tables. Views do not have their data but rather store data virtually, consisting of rows and columns. Views are very helpful in restricting access to your application’s critical data to third-party users. Views in MySQL can be created by selecting some/all columns and some/all rows of a table by filtering out the rows based on some condition(s)....

What is the Benefit of Using Views in MySQL?

Views help particularly in the following ways:...

What is MySQL Command Line Client?

MySQL Command Line Client is a simple and elegant SQL shell with inline editing ability. It is basically a Non-GUI based approach to query and interact with our MySQL database. So in simpler terms, it is a tool that permits sending MySQL queries to MySQL database from the command line i.e. shell. This is typically useful when we cannot install GUI-based tools like MySQL Workbench for interaction with MySQL database like when we are having MySQL database on a remote server....

Create View in MySQL

A view in MySQL can be created based on a single table or multiple tables. The CREATE VIEW statement is used to create a view in MYSQL....

Update View in MySQL

There are certain conditions that need to be satisfied to update a view. If any one of these conditions is not met, then we are not allowed to update the view....

Drop View in MySQL

Suppose now there is no need of the created view anymore? So, we want to delete it now. MySQL allows to deletion an already existing view. We can drop a view using the DROP statement....

CREATE VIEW using MySQL Workbench

Step 1: Open the MySQL Workbench...

Temporary Table in MySQL

A temporary table in MySQL is a table that allows one to store temporary result set of a query, and which one can reuse multiple times during one session. A temporary table is useful in cases where a SELECT statement is expensive to query the data as it may involve complex and multiple joins on tables such that every table contain huge amount of data. So, one can use the temporary table to store the result and then use another query to process this data....

Conclusion

Views in MySQL provide a way to avoid writing lengthy SELECT complex joins and queries again and again. It also helps in limiting the access of table attributes from the end users. In this article, we looked at how to create, delete, and update views in MySQL using MySQL command line client and also using workbench and how it benefits in general, their syntax of views for creation, updation, and deletion, with the help of awesome examples....