0% found this document useful (0 votes)
25 views2 pages

SQL - Views: SQL Create View Code

SQL views are virtual tables that are dynamically generated from queries on other SQL tables. Views can be queried like tables but do not actually store data - instead they reference the underlying tables. Creating a view involves using the CREATE VIEW statement to define a SELECT query, and any changes made to the referenced tables will be automatically reflected in the view. Views can also be removed using the DROP VIEW command.

Uploaded by

zooferic
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views2 pages

SQL - Views: SQL Create View Code

SQL views are virtual tables that are dynamically generated from queries on other SQL tables. Views can be queried like tables but do not actually store data - instead they reference the underlying tables. Creating a view involves using the CREATE VIEW statement to define a SELECT query, and any changes made to the referenced tables will be automatically reflected in the view. Views can also be removed using the DROP VIEW command.

Uploaded by

zooferic
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

sql - views

SQL VIEWS are data objects, and like SQL Tables, they can be queried, updated, and dropped. A SQL VIEW is a virtual table containing columns and rows except that the data contained inside a view is generated dynamically from SQL tables and does not physically exist inside the view itself.

SQL Create View Code:


CREATE VIEW virtualInventory AS SELECT * FROM inventory;

With a successful execution of this query, we have now created a view data object of the inventory table. The virtualInventory view is considered a data object (like a table) and is now accessible to us the developer. Views can be queried exactly like any other SQL table.

SQL View Code:


USE mydatabase;

SELECT * FROM virtualInventory;

SQL Results:
id product 2 HP Printer 3 Pen 4 Stapler 5 Hanging Files 6 Laptop quantity price 179.99 89.99 0.99 7.99 14.99 499.99 9 78 3 33 16 1 19" LCD Screen 25

Even though a SQL VIEW is treated like a data object in SQL, no data is actually stored inside of the view itself. The view is essentially a dynamic SELECTquery, and if any changes are made to the originating table(s), these changes will be reflected in the SQL VIEW automatically.

SQL Code:
USE mydatabase;

UPDATE inventory

SET price = '1.29' WHERE product = 'Pen';

Execute the following query to verify the results:

SQL Verification Query Code:


USE mydatabase;

SELECT * FROM virtualInventory WHERE product = 'Pen';

SQL Results:
id product quantity price 3 Pen 78 1.29

sql - drop view


Views can also be removed by using the DROP VIEW command.

SQL Drop View:


USE mydatabase;

DROP VIEW virtualInventory;

You might also like