LESSON 48 / 62 · VIEW StatementsVIEW Statements
Syntax for creating views in MySQL and PostgreSQL, description of how views work and why they are needed.
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
A view is a database object that is the result of executing a query on the database, defined using the SELECT statement, at the time of accessing the 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).
idINTidINTuser_idINTroom_idINTidINTowner_idINTidINTreservation_idINT
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.
SELECT * FROM ViewUsers;| id | name | |
|---|---|---|
| 1 | Bruce Willis | ba****.com |
| 2 | George Clooney | te****.com |
| 3 | Kevin Costner | me****.com |
| 4 | Donald Sutherland | ra****.net |
| 5 | Jennifer Lopez | ba****.com |
| 6 | Ray Liotta | jd****.net |
| 7 | Samuel L. Jackson | mo****.com |
| 8 | Nikole Kidman | ok****.com |
| 9 | Alan Rickman | ka****.net |
| 10 | Kurt Russell | ga****.com |
| 11 | Harrison Ford | ko****.com |
| 12 | Russell Crowe | gl****.net |
| 13 | Steve Martin | ne****.com |
| 14 | Michael Caine | dm****.com |
| 15 | Angelina Jolie | ch****.net |
| 16 | Mel Gibson | ro****o.ca |
| 17 | Michael Douglas | ti****.com |
| 18 | John Travolta | wa****.com |
| 19 | Sylvester Stallone | ko****.com |
| 20 | Tommy Lee Jones | sz****.com |
| 21 | Catherine Zeta-Jones | fl****.com |
| 22 | Antonio Banderas | sa****.net |
| 23 | Kim Basinger | ja****.net |
| 24 | Sam Neill | cl****.com |
| 25 | Hideo Kojima | ad****.org |
| 26 | ClINT Eastwood | fr****.net |
| 27 | Brad Pitt | ke****.net |
| 28 | Johnny Depp | cg****o.ca |
| 29 | Pierce Brosnan | tr****.com |
| 30 | Sean Connery | js****.com |
| 31 | Bruce Willis | ke****.com |
| 32 | Mullah Omar | jg****.com |
| 33 | Vasanta Roberta | ro****.com |
If you want to find out which columns are available in the view, you can use the DESCRIBE statement.
DESCRIBE ViewUsers;| Field | Type | Null | Key | Default | Extra |
|---|---|---|---|---|---|
| id | int | NO | |||
| name | varchar(32) | NO | |||
| varchar(38) | YES |
General syntax of a view
CREATE [OR REPLACE]
VIEW view_name [(view_column [, ...])]
AS select_expression;- [ ]optional part
- ...can be repeated
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.
The name of the new view. Queries refer to the view by this name, just like a table.
Column names of the view. If you omit them, the columns get their names from the SELECT query.
The SELECT query whose result the view shows.
- OR REPLACE
- KeywordOptional
- view_column
- Optional
- select_expression
- 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?"
A virtual table that does not store data but retrieves it from other tables when accessed.A physical table that permanently stores data.An interface for direct user access to the database.A temporary table used for data manipulation.CheckArticle contentWhat is a viewExample of creating a viewGeneral syntax of a viewWhy are views neededSimplifying complex queriesImproving performanceEnsuring securityConclusionEdit on GitHub
Python Academy
Practice
Solve it in the editor. The query runs against a sample database in your browser.
Write a query and run it to see the results.