Description
The SQL keyword VIEW is used to perform operations on a view, as mentioned below.
Keyword | Description |
CREATE VIEW | Creates a new view. |
CREATE OR REPLACE VIEW | Creates a new view (if not exist), else replaces an existing view. |
DROP VIEW | Drops an existing view. |
CREATE VIEW
The SQL keyword CREATE VIEW is used to create a view, which is a virtual table based on the result set of an SQL statement.
The below SQL statement creates a view, containing all the customers from Spain.
CREATE VIEW [Spain Customers] AS
SELECT customer_name, address
FROM customers
WHERE country = "Spain";
Query View
The below SQL returns the data from a view.
SELECT * FROM SpainCustomers;
Use any of the below SQLs if the view name contains spaces, where we need to enclose the view name with double quotes or square brackets.
SELECT * FROM [Spain Customers];
SELECT * FROM "Spain Customers";
CREATE OR REPLACE VIEW
The SQL keyword CREATE OR REPLACE VIEW is used to create or update a view.
The below SQL statement creates (if not existing) or updates (if already exists) a view.
CREATE OR REPLACE VIEW [Spain Customers] AS
SELECT customer_name, address, city
FROM customers
WHERE country = "Spain";
The below SQL statement returns the data on the view.
SELECT * FROM [Spain Customers];
DROP VIEW
The SQL keyword DROP VIEW is used to delete an existing view, which deletes the view and its contents permanently from a database system.
- A view name with spaces must be enclosed within double quotes or square brackets.
The below SQL deletes an existing view, with the name "SpainCustomers".
DROP VIEW SpainCustomers;
The below SQLs delete an existing view, with the name "Spain Customers".
DROP VIEW [Spain Customers];
DROP VIEW "Spain Customers";