RianAcademy
Search
⌘
K
Premium

Views

Well-designed applications usually provide an open interface, hiding implementation details, which allows for design changes without affecting end users.

When developing your database, you can achieve a similar result by encapsulating tables and providing access to data only through a set of views.

In this article, we will discuss what views are, how they are created, and what they can be useful for.

What is a view

Views are sometimes called "virtual tables." This is because a view appears to the user as a table, but actually does not store data, but retrieves it from other tables at the time of access.

If the data in the underlying table changes, the user receives up-to-date data when accessing the view that uses this table. Views do not cache query results during operation.

Example of creating a view

As a simple example, suppose you want to partially hide email addresses in the user table (Users).

Users
id
INT
name
VARCHAR
email
VARCHAR
email_verified_at
DATETIME
password
VARCHAR
phone_number
VARCHAR
Reservations
id
INT
user_id
INT
room_id
INT
start_date
DATETIME
end_date
DATETIME
price
INT
total
INT
Rooms
id
INT
home_type
VARCHAR
address
VARCHAR
has_tv
BOOLEAN
has_internet
BOOLEAN
has_kitchen
BOOLEAN
has_air_con
BOOLEAN
price
INT
owner_id
INT
latitude
FLOAT
longitude
FLOAT
Reviews
id
INT
reservation_id
INT
rating
INT

This can be useful, for example, if your company's policy does not allow everyone to use confidential user information. Therefore, instead of allowing direct access to the user table (Users), you define a view named ViewUsers and require everyone to use it to access user data.

Here is an example of defining this view:

MySQL 8.4
CREATE VIEW ViewUsers AS
    SELECT id,
           name,
           CONCAT(SUBSTR(email, 1, 2), '****', SUBSTR(email, -4)) AS email
FROM Users;

The view in an SQL query appears and is used like a regular table.

MySQL 8.4
SELECT * FROM ViewUsers;
idnameemail
1Bruce Willisba****.com
2George Clooneyte****.com
3Kevin Costnerme****.com
4Donald Sutherlandra****.net
5Jennifer Lopezba****.com
6Ray Liottajd****.net
7Samuel L. Jacksonmo****.com
8Nikole Kidmanok****.com
9Alan Rickmanka****.net
10Kurt Russellga****.com
11Harrison Fordko****.com
12Russell Crowegl****.net
13Steve Martinne****.com
14Michael Cainedm****.com
15Angelina Joliech****.net
16Mel Gibsonro****o.ca
17Michael Douglasti****.com
18John Travoltawa****.com
19Sylvester Stalloneko****.com
20Tommy Lee Jonessz****.com
21Catherine Zeta-Jonesfl****.com
22Antonio Banderassa****.net
23Kim Basingerja****.net
24Sam Neillcl****.com
25Hideo Kojimaad****.org
26ClINT Eastwoodfr****.net
27Brad Pittke****.net
28Johnny Deppcg****o.ca
29Pierce Brosnantr****.com
30Sean Conneryjs****.com
31Bruce Williske****.com
32Mullah Omarjg****.com
33Vasanta Robertaro****.com

If you want to find out which columns are available in the view, you can use the DESCRIBE statement.

MySQL 8.4
DESCRIBE ViewUsers;
FieldTypeNullKeyDefaultExtra
idintNO<NULL>
namevarchar(32)NO<NULL>
emailvarchar(38)YES<NULL>

General syntax of a view

MySQL 8.4
CREATE [OR REPLACE]
VIEW view_name [(view_column [, ...])]
AS select_expression;
  • [ ]optional part
  • ...can be repeated
OR REPLACE

If a view with this name already exists, it is replaced with the new one. Without it, creating a view with a taken name fails.

KeywordOptional
view_name

The name of the new view. Queries refer to the view by this name, just like a table.

view_column

Column names of the view. If you omit them, the columns get their names from the SELECT query.

Optional
select_expression

The SELECT query whose result the view shows.

Subquery

Why are views needed

Simplifying complex queries

Views are used to simplify complex queries and create an abstraction between the user and the database. They can hide the complexity of data structures and provide a simplified interface for accessing data.

Improving performance

Creating views that encapsulate complex queries can help optimize the execution of these queries. This can lead to faster query execution and overall improvement in database performance.

Ensuring security

Views can be used to ensure the security of confidential data. Creating views that restrict access to specific columns or rows of data allows administrators to limit access to sensitive information. This helps ensure that only authorized users have access to confidential data.

Conclusion

Views are an important tool in SQL that allows for simplifying complex queries, standardizing data access, improving performance, and ensuring data security

Let's check how well you understood the topic: choose the correct statement for the question "What is a view in a database?"

Tasks for self-testing
0 / 1
1. Creating a view

Based on the Student table, create a view ViewStudent containing only the fields id, first_name and last_name.
Class
id
INT
name
VARCHAR
Schedule
id
INT
date
DATE
class
INT
number_pair
INT
teacher
INT
subject
INT
classroom
INT
Student
id
INT
first_name
VARCHAR
middle_name
VARCHAR
last_name
VARCHAR
birthday
DATE
address
VARCHAR
Student_in_class
id
INT
class
INT
student
INT
Subject
id
INT
name
VARCHAR
Teacher
id
INT
first_name
VARCHAR
middle_name
VARCHAR
last_name
VARCHAR
Timepair
id
INT
start_pair
TIME
end_pair
TIME
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Result
Class
Schedule
Student
Student_in_class
Subject
Teacher
Timepair

Run a query to see the result, or open a table above