Views are an important tool for providing users with a simple, customized environment and for hiding data. As explained in Section 5.2, a relational view does not correspond exactly to the general external view but can be either a window into a base table or a virtual table derived from one or more underlying base tables. It usually does not exist in storage in the same way as base tables but is created by selecting specified rows and columns from the base tables and possibly performing operations on them.
A view is dynamically produced as the user works with it. To make up a view, the DBA decides which data the user needs to access, determines what base tables contain the data, and creates one or more views to display the attributes and rows the user should see. Views allow a dynamic external model to be created for the user easily. The reasons for providing views rather than allowing all users to work with base tables are as follows:
Views allow different users to see the data in different forms, permitting an external model that differs from the logical model.
The view mechanism provides a simple authorization control device, easily created and automatically enforced by the system. View users are unaware of, and cannot access, certain data items.
Views can free users from complicated DML operations, especially in the case where the views involve joins. The user writes a simple SELECT statement using the view name in place of a base table name in the FROM clause, and the system takes care of executing the operations on the base tables to support the view.
If the database is restructured on the logical level, the view can be used to keep the user’s model constant. For example, if a table is split by projection and the primary key appears in each resulting new table, the original table can always be reconstructed when needed by defining a view that is the join of the new tables.
Standard SQL supports relational views. Oracle, PostgreSQL, and SQL Server also support materialized views. Materialized views function as snapshots of data and cannot be updated directly; however, they can be refreshed periodically as needed. They are used primarily in data warehouse environments.
The following is the most common form of the command used to create a view
The view name is chosen using the same rules as the table name, and it should be unique within the database. Columns chosen from the base tables may be rearranged in any desired manner in the view. Column names in the view can be different from the corresponding column names in the base tables, but they must obey the same rules of construction. If we choose to make them the same, we need not specify them twice, so we leave out the viewcolname specifications. In the AS SELECT line, also called the subquery, we list the names of the columns from the underlying base tables that we wish to include in the view. The order of these names should correspond exactly to the viewcolnames if those are specified. As in the usual SELECT . . . FROM . . . WHERE, the condition is a logical predicate that expresses restrictions on the records to be included. A more general form of CREATE VIEW uses any valid subquery in place of the SELECT we have described. For materialized views, the Oracle syntax is
Example 1. Choosing a Vertical and Horizontal Subset of a Table
Operation: Assume a user needs to see IDs and names of all history majors. Create a view for this user.
SQL Command:
Here we renamed the columns from our base table. The user of this view need not know the actual column names.
Example 2. Choosing a Vertical Subset of a Table
Operation: Create a view of all classes with their schedules and rooms.
SQL Command:
Notice we did not need a condition here because we wanted these parts of all Class records. This time we kept the names of the columns as they appear in the base table.
Example 3. A View Using Two Tables
Operation: Assume a user needs a view containing the IDs and names of all students in class CSC101.
The virtual table can be created by choosing records in Enroll that have a classNumber of CSC101, matching the stuId of those records with the stuId of the Student records, and taking the corresponding lastName and firstName from Student. We could express this as a join or as a subquery.
SQL Command:
Example 4. A View of a View
Operation: Define a view derived from a view. We can ask for a subset of the ClassLoc (virtual) table consisting of just classNumber and room.
SQL Command:
Example 5. A View Using a Function
In the SELECT statement in the AS line, we can include built-in functions and GROUP BY options.
Operation: Create a view of Enroll that gives classNumber and the number of students enrolled in each class:
SQL Command:
Notice we had to supply a name for the second column of the view because there was none available from the base table.
This could also have been done using a name for the second column.
SQL Command:
Example 6. Operations on Views
Once a view is created, the user can write SELECT statements to retrieve data through the view. The system takes care of mapping the user names to the underlying base table names and column names, and performing whatever functions are required to produce the result in the form the user expects. Users can write SQL queries that refer to joins, ordering, grouping, built-in functions, and so on, of views—just as if they were operating on base tables. Because the SELECT operation does not change the underlying base tables, there is no restriction on allowing authorized users to perform SELECT with views.
Operation: Using the ClassLoc view, find all room values that start with the letter H.
SQL Command:
INSERT, DELETE, and UPDATE can present problems with views.
Example 7. A View That Is Not Updatable
Suppose we create a view of Student records, using
and we attempt to insert a new record, using
This insert will be rejected. If we were permitted to insert records, any records created through this view would actually be Student records, but would not contain stuId, which is the key of the Student table. Because stuId has the NOT NULL constraint, any records without this column will be rejected.
Example 8. An Updatable View
If we create the following view
we should have no problem inserting records because we would be inserting Student records with a null major column, which is allowable.
Operation: Insert a new StudentV2 record, providing data for all columns.
SQL Command:
The system will actually insert the record into the Student table.
Example 9. Attempting to Insert a Value into a Virtual Column
Now let us consider inserting records into the view ClassCount, as described earlier in Example 5. This view used the COUNT function on groups of records in the Enroll table. Obviously, this table was meant to be a dynamic summary of the Enroll table, rather than being a row and column subset of that table. It would not make sense for us to permit new ClassCount records to be inserted because these do not correspond to individual rows or columns of a base table. If we attempt to add a record using
the record will be rejected.
The problems we have identified for INSERT into views apply with minor changes to UPDATE and DELETE as well. As a rule, these three operations can be performed on views that consist of actual rows and columns of underlying base tables, provided the primary key is included in the view and no other constraints are violated.
Example 10. Modifying a View
To modify an existing view definition, the following command, which is nearly identical to the original CREATE VIEW, is used
Operation: Change StudentV2 to drop the credits column.
SQL Command:
18.216.118.40