M358 - Views
fellstrider.com - the logo!
Home| OU Study Rooms | M358 Index | Block 1 - Information Systems | Block 2 - Relational Theory | Block 3 - SQL
 
Views

A view is a table that appears to contain rows of data. A view exists as a definition of a query that produces rows of data. These rows of data exist elsewhere in the database.

To create a view requires an SQL statement:

CREATE VIEW counselling AS
   SELECT student_id, counsellor_no, region 
   FROM student

Joining tables to form a view, specifying view column names, ordering the columns:

CREATE VIEW counselling2 (s_name, s_no, region, c_name, c_no) AS                         //names view columns
   SELECT s.name, student_id, counsellor_no, s.region, c.name, counsellor_no             //orders view columns
   FROM student s, staff c                                                                                         //joins tables
   WHERE counsellor_no = staff_no

CREATE VIEW statement format:

CREATE VIEW <view name> (<column list>) AS
   <query specification>

Creating column names is optional. By default they will inherit the name from the base tables. Exceptions to this are:

In the above situations column names must be specified in the CREATE VIEW statement.

Views can be used to access a database. Logical processing dictates that the query specification from the view definition is processed first. A query statement can then be used on the resulting view as input data.

Remember a view contains no data!

Why views exist

Removing views

DROP VIEW <view name> 

Care must be taken when dropping views. Other views may be dependent on them. SQL Anywhere provides no safeguard through RESTRICT or CASCADE. Therefore dependent views would become meaningless.

Updating views

A view contains no data. Therefore it cannot be updated. However it is possible to UPDATE a base table with an INSERT statement through a view. This in effect will update the view. Think about it!

INSERT INTO mod_counselling
   VALUES ('s24', '6451', '1')

Points to note:

For a view to be updatable there are several conditions that apply to the query specification. This is in order that a mapping to a base table can be established:

Home| OU Study Rooms | M358 Index | Block 1 - Information Systems | Block 2 - Relational Theory | Block 3 - SQL
Move on to Sharing Data

Valid CSS! Valid XHTML 1.0!

Comments, suggestions, ideas to
Stuart Banner