SQL views are virtual tables with rows and columns, just like actual tables in a database. To create a view, you have to select fields from tables that exist in the database.
Contents
SQL VIEW: Main Tips
- View in SQL means virtual tables made of a result-set.
- An SQL view has columns and rows.
- You can use WHERE and JOIN statements to show the information as if it was being received from a single table.
SQL VIEW Syntax and Examples
CREATE VIEW v_name AS
SELECT col1, col2, ...
FROM tbl_name
WHERE condition;
Note: VIEW only displays the present information.
Every one of the active cells that are inside Products table is listed in the Present List Products view:
CREATE VIEW [Present List Products] AS
SELECT ID, Name
FROM Products
WHERE Discontinued = No;
The example below shows how to query a view:
SELECT * FROM [Present List Products];
Updating the VIEW
The syntax example below shows how to update your view:
CREATE OR REPLACE VIEW v_name AS
SELECT col1, col2, ...
FROM tbl_name
WHERE condition;
The next step to finish updating the view is to insert one more column called Category:
CREATE OR REPLACE VIEW [Present List Products] AS
SELECT ID, Name, Category
FROM Products
WHERE Discontinued = No;
Dropping the VIEW
To remove a view use DROP VIEW:
DROP VIEW v_name;
SQL VIEW: Summary
- Virtual tables that are made of a result-set of an actual table(s) in a database are called SQL views.
- WHERE and JOIN statements used on views return the information as if it was stored in a single table.