Reader small image

You're reading from  Learn SQL using MySQL in One Day and Learn It Well

Product typeBook
Published inApr 2024
PublisherPackt
ISBN-139781836205678
Edition1st Edition
Right arrow
Author (1)
Jamie Chan
Jamie Chan
author image
Jamie Chan

Jamie Chan is a tutor and freelance programmer with years of experience and a dedicated passion for sharing the joy of programming with as many people as possible. With seven bestselling programming books on Amazon, Jamie's publications stand out for their ability to break down complex concepts into simple terms. Additionally, each book includes complete projects at the end, enabling hands-on learning and a deep understanding of the concepts presented.
Read more about Jamie Chan

Right arrow

Chapter 8: Views

 

In the last three chapters, we learned to write some pretty complex SELECT statements to get information from our databases.

 

In this chapter, we’ll learn another concept that is closely related to SELECT statements - the concept of views.

 

What is a view?

 

Simply stated, an SQL view is a virtual table.

 

In contrast to actual tables, views do not contain data. Instead, they contain SELECT statements. The data to display is retrieved using those SELECT statements.

 

One advantage of using views is that it allows programmers to simplify their code. They can write relatively complex SELECT statements to join multiple tables into a single virtual table. Once that is done, they can access that virtual table like a normal table and perform simple searches on it.

 

In addition, views also allow us to restrict access to certain data in our tables. For instance, suppose we have a table with three columns - id, password and email. If we do not want other programmers to have access to all the data in the tables (such as the password column), we can create a view with only the id and email columns and let them have access to the view instead.

 

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...

Altering a View

 

After creating a view, if we want to make any changes to it, we use the ALTER VIEW keywords.

 

The syntax is

 

ALTER VIEW name_of_view AS

SELECT statement;

 

For instance, we can use the following statement to alter myView:

 

ALTER VIEW myView AS

SELECT employees.id, mentorships.mentor_id, employees.em_name AS 'Mentor', mentorships.project AS 'Project'

FROM

mentorships

JOIN

employees

ON

employees.id = mentorships.mentor_id;

 

This changes the alias for the mentorships.project column from Project Name to Project (refer to underlined code).

 

Deleting a View

 

Last but not least, to delete a view, we use the DROP VIEW keywords. The syntax is

 

DROP VIEW [IF EXISTS] name_of_view;

 

To delete myView, we write

 

DROP VIEW IF EXISTS myView;

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learn SQL using MySQL in One Day and Learn It Well
Published in: Apr 2024Publisher: PacktISBN-13: 9781836205678
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Jamie Chan

Jamie Chan is a tutor and freelance programmer with years of experience and a dedicated passion for sharing the joy of programming with as many people as possible. With seven bestselling programming books on Amazon, Jamie's publications stand out for their ability to break down complex concepts into simple terms. Additionally, each book includes complete projects at the end, enabling hands-on learning and a deep understanding of the concepts presented.
Read more about Jamie Chan