In SQL, a view is a virtual table based on the result set of an SQL statement.
The CREATE VIEW command is used to create a view.
The provided SQL creates a view that selects all customers from Brazil:
| CREATE VIEW [Brazil Customers] AS SELECT CustomerName, ContactName FROM Customers WHERE Country = “Brazil”; |
The view above can be queried as follows:
| SELECT * FROM [Brazil Customers]; |
The CREATE OR REPLACE VIEW command modifies a view.
The following SQL adds the “City” column to the “Brazil Customers” view:
| CREATE OR REPLACE VIEW [Brazil Customers] AS SELECT CustomerName, ContactName, City FROM Customers WHERE Country = “Brazil”; |
The DROP VIEW command removes a view.
The provided SQL drops the “Brazil Customers” view:
| DROP VIEW [Brazil Customers]; |