Lab9-Implementation of Views
Lab9-Implementation of Views
Database Systems
Laboratory Manual
0702325
Theory:
VIEW: In SQL, a view is a virtual table based on the result-set of an SQL statement. A view contains
rows and columns, just like a real table. The fields in a view are fields from one or more real tables in 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. A view is a virtual table, which consists of a set of columns
from one or more tables. It is similar to a table, but it does not store in the database. View is a query
stored as an object.
Syntax:
CREATE VIEW <view name> AS SELECT <set of fields>
FROM relation_name WHERE (Condition)
Example:
CREATE VIEW employee AS SELECT empno,ename,job
FROM EMP
WHERE job = ‘clerk’;
Example:
CREATE VIEW v_sailors_reserved_101 AS
SELECT *
FROM Sailors
WHERE sid IN ( SELECT sid FROM Reserves WHERE bid = 101);
Syntax :
CREATE OR REPLACE VIEW view_name AS SELECT column_name(s)
FROM table_name
WHERE condition
Dropping A View: A view can deleted with the DROP VIEW command.
1. Find all information of sailors who have reserved boat number 101.
2. Find the name of boat reserved by Bob.
3. Find the names of sailors who have reserved a red boat, and list in the order of age.
4. Find the names of sailors who have reserved at least one boat.
5. Find the ids and names of sailors who have reserved two different boats on the same day.
6. Find the ids of sailors who have reserved a red boat or a green boat.
7. Find the name and the age of the youngest sailor.
8. Count the number of different sailor names.
9. Find the average age of sailors for each rating level.
10. Find the average age of sailors for each rating level that has at least two sailors.