Views
A view is the mechanism for examining the data in your database. A view can combine data from multiple tables or can include only certain columns from a single table. Although a view looks like a table, it consists of a selected set of columns or calculations based on those columns from tables in your database. Thus, a view may contain data from columns in more than one table or data that is not actually in any table at all (for example, SELECT COUNT (*) FROM Person).
Features of Views
Following are some of the features of views:
Temporary and Stored Views
You can use SELECT statements to create temporary views or stored views. You use a temporary view only once and then release it. Pervasive PSQL places the definition of a stored view in the data dictionary (X$Proc) so you can recall the view later. You use CREATE VIEW statements to create and name stored views.
Each view name must be unique within a database and cannot exceed 20 characters. For more information about rules for naming views, refer to Chapter 14, Inserting and Deleting Data.
Pervasive PSQL is case-sensitive when defining database element names. If you create a stored view named PhoNE, Pervasive PSQL stores the view name in the data dictionary as PhoNE. Pervasive PSQL is case-insensitive after you define the view name. After defining the stored view PhoNE, you can refer to it as phone.
Using stored views provides the following features:
CREATE VIEW Phones (PName, PPhone)
AS SELECT Name, Phone_Number
FROM Department#
UPDATE Phones
SET PPhone = '5125552426'
WHERE PName = 'History'#
You can specify headings. A heading specifies a column name that is different from the name you defined for the column in the dictionary. The following example specifies the headings Department and Telephone for the stored view Phones.
CREATE VIEW Dept_Phones (Department, Telephone)
AS SELECT Name, Phone_Number
FROM Department#
You can use the headings in subsequent queries on the view, as in the following example.
SELECT Telephone
FROM Dept_Phones#
If the selection list contains simple column names and you do not provide headings, Pervasive PSQL uses the column name as the column heading.
You must use headings to name constants and computed columns that you include in the view. The following example creates the headings Student and Total.
CREATE VIEW Accounts (Student, Total)
AS SELECT Student_ID, SUM (Amount_Paid)
FROM Billing
GROUP BY Student_ID#
You must also use headings if you specify SELECT * from multiple tables that have any duplicate column names.
Read-Only Tables in Views
You cannot insert, update, or delete rows from views that contain read-only tables. (Here the term update refers to insert, update, and delete; if a table is read-only, you cannot update it.) Some tables are read-only whether or not they are in views that are specified as such; such tables are intrinsically read-only, and you cannot update them. A table is read-only if it meets one of the following criteria:
ORDER BY
SCROLL
Mergeable Views
A view is mergeable if you can rewrite the SELECT statement using only base tables and columns.
For example, if you want to know how many students are in a class, you can define a view to calculate that. The view NumberPerClass is defined as follows:
CREATE VIEW NumberPerClass (Class_Name, Number_of_Students)
AS SELECT Name, COUNT(Last_Name)
FROM Person, Class, Enrolls
WHERE Person.ID = Enrolls.Student_ID
AND Class.ID = Enrolls.Class_ID
GROUP BY Name#
The view NumberPerClass is defined as follows:
SELECT *
FROM NumberPerClass#
The view NumberPerClass is then mergeable because we can rewrite the SELECT statement as follows:
SELECT Name, COUNT(Last_Name)
FROM Person, Class, Enrolls
WHERE Person.ID = Enrolls.Student_ID
AND Class.ID = Enrolls.Class_ID
GROUP BY NAME#
The view NumberPerClass is non-mergeable if you want to write a SELECT statement such as the following:
SELECT COUNT(Name)
FROM NumberPerClass
WHERE Number_of_Students > 50#
This statement is invalid for the view NumberPerClass. You cannot rewrite it using only base tables and base columns.
A view is mergeable if it does not contain any of the following characteristics: