Careeroza
Changing & Dropping Views
Replace a View
-- MySQL, PostgreSQL, Oracle
CREATE OR REPLACE VIEW ActiveProducts AS
SELECT ProductID, ProductName, Price FROM Products WHERE Stock > 0;
-- SQL Server 2016 SP1+
CREATE OR ALTER VIEW ActiveProducts AS
SELECT ProductID, ProductName, Price FROM Products WHERE Stock > 0;Note: SQLite has no CREATE OR REPLACE VIEW: drop the view and create it again.
Drop a View
DROP VIEW ActiveProducts;
DROP VIEW IF EXISTS ActiveProducts;Dropping a view never deletes table data.
A View Always Shows Current Data
Example
Update the base table and the view reflects it immediately:
CREATE VIEW InStock AS
SELECT ProductName, Stock FROM Products WHERE Stock > 0;
UPDATE Products SET Stock = 25 WHERE ProductName = 'Dark Chocolate';
SELECT * FROM InStock;Result: 5 rows
| ProductName | Stock |
|---|---|
| Basmati Rice 5kg | 120 |
| Green Tea 100 bags | 300 |
| Olive Oil 1L | 80 |
| Dark Chocolate | 25 |
| Espresso Beans 1kg | 45 |