Views
Database: The database stores the sample data of an e-commerce aplication. Here, the database consists of
Refer the tables in the code playground for a better understanding of the database.
View
A view can simply be considered as a name to a SQL Query
Create View
To create a view in the database, use the
Example
Create
In general, views are read only.
We cannot perform write operations like updating, deleting & inserting rows in the base tables through views.
Try it Yourself!
Create
Querying Using View
We can use its name instead of writing the original query to get the data.
Output
id | name | age | gender | pincode |
---|---|---|---|---|
1 | Sai | 40 | Male | 400068 |
2 | Boult | 20 | Male | 30154 |
3 | Sri | 20 | Female | 700009 |
... | ... | ... | ... | ... |
We can use same operations which are used on tables like WHERE clause, Ordering results, etc.
If we try to retrive data which is not defined in the view it raises an
Example
Output
Try it Yourself!
From the
Expected Output
name | no_of_units |
---|---|
Oneplus 8 Pro | 1 |
Gorilla Glass | 1 |
List All Available Views
In SQLite, to list all the available views, we use the folowing query.
Output
name |
---|
order_with_products |
user_base_details |
Delete View
To remove a view from a database, use the
Syntax
Example
Delete
Advantages
- Views are used to write complex queries that involves multiple joins, group by, etc., and can be used whenever needed.
- Restrict access to the data such that a user can only see limited data instead of a complete table.