Creating a View
The syntax for creating a view is:
CREATE VIEW name_of_view AS
SELECT statement;
Quite straightforward, right?
Let’s look at an example.
In the previous chapter, we wrote a SELECT statement to join the employees table with the mentorships table.
The code below shows how we can create a view for that SELECT statement:
CREATE VIEW myView AS
SELECT employees.id, mentorships.mentor_id, employees.em_name AS 'Mentor', mentorships.project AS 'Project Name'
FROM
mentorships
JOIN
employees
ON
employees.id = mentorships.mentor_id;
The only additional code is the first line (CREATE VIEW myView AS).
To use this view, we need to first execute the CREATE VIEW statement above to create the view.
Next, we can select data from it like how we select data from a table.
For instance, we can write
SELECT * FROM myView;
to view all the data...