10/11/2019
Contents
Introduction
View Creating a view
A view from two tables
NGUYEN HongPhuong Dropping a view
Email: phuongnh@soict.hust.edu.vn
Updating a table from a view
Site: http://is.hust.edu.vn/~phuongnh
Face: https://www.facebook.com/phuongnhbk
Hanoi University of Science and Technology
1 2
Introduction Creating a view
In SQL, a view is a virtual table based Syntax
on the result-set of an SQL statement.
CREATE VIEW view_name AS
A view contains rows and columns, just
SELECT column1, column2, ...
like a real table. The fields in a view are
FROM table_name
fields from one or more real tables in WHERE condition;
the database.
You can add SQL functions, WHERE,
and JOIN statements to a view and
present the data as if the data were
coming from one single table.
3 4
1
10/11/2019
Creating a view Querying on a view
Examples
CREATE VIEW vCompany1 AS
SELECT Name, Address, Telephone
FROM Company SELECT * FROM vCompany1
WHERE Address LIKE'%Germany%';
CREATE VIEW vCompany2 AS
SELECT Name, Address, Telephone
SELECT * FROM vCompany2;
FROM Company
WHERE Address LIKE '%Japan%'
CREATE VIEW vComSupPro1(ComName, ProdName, Qty) AS
SELECT Company.Name, Product.Name, Quantity
FROM Company INNER JOIN Supply ON Company.CompanyID
= Supply.CompanyID
INNER JOIN Product ON Supply.ProductID = Product.ProductID
5 6
Deleting a view
Syntax
DROP VIEW view_name;
Example
DROP VIEW vCompany1;
DROP VIEW vCompany2;
DROP VIEW vComSupPro1;
7 8