English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
A view is simply a SQLite statement stored in the database with a related name. It is actually a combination of tables that are the result of a predefined SQLite query form.
A view can include all rows of a table or selected rows from one or more tables. Views can be created from one or more tables, depending on the SQLite query written to create the view.
Views as virtual tables allow users-
Construct data in a natural or intuitive way for users or user categories.
Limit access to data so that users can only see limited data rather than the entire table.
Summarize data from various tables that can be used to generate reports.
SQLite views are read-only, so you may not be able to execute DELETE, INSERT, or UPDATE statements on the view. However, you can create triggers on the view that will be triggered when attempting to delete, insert, or update the view, and perform the required operations in the trigger body.
UsingCREATE VIEWThis statement creates a SQLite view. A SQLite view can be created from one table, multiple tables, or another view.
Here is the basic CREATE VIEW syntax.
CREATE [TEMP | TEMPORARY] VIEW view_name AS SELECT column1, column2.....FROM table_name WHERE [condition];
You can include multiple tables in a SELECT statement just like you would in a regular SQL SELECT query. If the optional TEMP or TEMPORARY keyword is present, the view will be created in the temp database.
The COMPANY table with the following records-
ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 1 Paul 32 California 20000.0 2 Allen 25 Texas 15000.0 3 Teddy 23 Norway 20000.0 4 Mark 25 Rich-Mond 65000.0 5 David 27 Texas 85000.0 6 Kim 22 South-Hall 45000.0 7 James 24 Houston 10000.0
Here is an example of creating a view from the COMPANY table. This view will only be used for a few columns in the COMPANY table.
sqlite> CREATE VIEW COMPANY_VIEW AS SELECT ID, NAME, AGE FROM COMPANY;
Now, you can query COMPANY_VIEW in a similar way as you would query an actual table. Here is an example-
sqlite> SELECT * FROM COMPANY_VIEW;
This will produce the following results.
ID NAME AGE ---------- ---------- ---------- 1 Paul 32 2 Allen 25 3 Teddy 23 4 Mark 25 5 David 27 6 Kim 22 7 James 24
To delete a view, simply use the DROP VIEW statement along withview_nameThe basic DROP VIEW syntax is as follows-
sqlite> DROP VIEW view_name;
The following commands will delete the COMPANY_VIEW view created in the previous section.
sqlite> DROP VIEW COMPANY_VIEW;