Querying Database Views

Objective

After completing this lesson, you will be able to query database views.

Advantages of Database Views

The following views are supported by SAP HANA:

  • You can define database views using SQL DDL Statements. This is the only option covered in this course.

  • You can define Calculation Views or, more generally, information models using SAP HANA Tools. You can define views using Core Data Services and the HANA Development Perspective in the SAP HANA Tools. Information models and Core Data Services are used in HANA-specific data modelling and outside the scope of this course.

Note

Calculation Views and Core Data Services may be more powerful than SQL views, or may simplify the development process. Nevertheless, this course only covers SQL views. Understanding them is the foundation for understanding the other types of views and can be helpful for prototyping or analyzing issues.

Database Views: Advantages

Database views provide the following advantages:

  • De-couple applications from lower levels (relative to the three level schema architecture).

    You can use database views to change to the conceptual schema (for example, add columns to a table) without causing side effects on applications.

  • Create views tailored to the users (applications) and their needs.

    The application programmers do not have to know the full database structure but only the excerpt that is relevant to them.

  • Simplify queries.

    Complex queries referencing views are easier to formulate, provided that the view provides an appropriate pre-selection of data.

  • Limit access to data.

    You can use views to realize value-dependent access privileges or to make sure certain columns are never visible for certain users.

A Sample View: Red Cars

Example of a view containing only red cars:

Code Snippet
1234567
CREATE VIEW redCars AS SELECT CarID, Color, PlateNumber, HP AS Power FROM Car WHERE Color = 'red';
CARIDCOLORPLATENUMBERPOWER
F01redHD-V 10675
F09redHD-UP 13105
F12redHD-XY 4711105
F13redHD-IK 1001136
F18redHD-MQ 200690

Using Views in Queries

You can select data from a view in the same way you select from a table.

Which red cars have more than 100 HP?

Code Snippet
1234
SELECT CarID, PlateNumber, Power FROM redCars WHERE Power > 100 ORDER BY Power DESC, CarID ASC;

In this example, the redCars view would have listed all red cars, but the addition of the WHERE clause further reduced the result set to red card with more than 100 HP.

CARIDPLATENUMBERPOWER
F13HD-IK 1001136
F09HD-UP 13105
F12HD-XY 4711105

Defining Views

You can create database views using the CREATE VIEW statement:

Code Snippet
12
CREATE VIEW <view_name> [(<column list>)] AS SELECT ...

You can use nearly any valid SELECT statement as the SELECT clause for a CREATE VIEW statement.

CREATE VIEW

You can restrict a view to specific columns and rows of the underlying base table.

Code Snippet
1234567
CREATE VIEW Audis AS SELECT Brand, Color, HP FROM Car WHERE Brand = 'Audi';
BRANDCOLORHP
Audiyellow260
Audiblue116
Audiorange184
Audigreen184

You can also use the following in the SELECT part of the CREATE VIEW statement:

  • Calculations and functions

  • Any type of join; the need to formulate the same complex join condition frequently in application development is a typical case where views can be used to simplify

  • A TOP N clause to limit the number of rows returned

  • DISTINCT

    Note

    A view can contain a projection leading to potential duplicates in result sets.
  • Aggregate expressions, GROUP BY and HAVING clauses

  • Subqueries, uncorrelated or correlated

  • UNION [ALL]

  • An ORDER BY clause to achieve a default sort order of results

    Note

    This order can be overridden when SELECT is chosen from the view. For this reason, the use of ORDER BY is questionable and not recommended.

Precedence Rule for Column Naming

If you rename columns in both the CREATE VIEW statement and in the SELECT clause, the name of the CREATE VIEW clause "wins". As this can cause confusion, we recommend renaming columns only in the SELECT clause.

Code Snippet
1234567
CREATE VIEW Audis (Manufacturer, Color, Power) AS SELECT Brand AS Description, Color, HP AS HorsePower FROM Car WHERE Brand = 'Audi';
MANUFACTURERCOLORPOWER
Audiyellow260
Audiblue116
Audiorange184
Audigreen184

View on View

You can also define a view based on another view.

Code Snippet
1234567
CREATE VIEW Volkswagen AS SELECT CarID, PlateNumber, Color, HP FROM Car WHERE Brand = 'VW';
Code Snippet
1234567
CREATE VIEW blackVolkswagen AS SELECT CarID, PlateNumber, HP FROM Volkswagen WHERE Color = 'black';
CARIDPLATENUMBERHP
F02HD-VW 4711120
F08HD-IK 1002160
F19HD-VW 2012125

Define View to Simplify Join Expressions

Views are commonly created with a join expression to hide the complexity of the underlying query from the user or application that queries the view.

Code Snippet
1234567
CREATE VIEW OwnerInfo AS SELECT Name, Birthday, PlateNumber, Brand, Color FROM Owner JOIN Car ON Owner.OwnerID = Car.Owner WHERE Birthday IS NOT NULL;
NAMEBIRTHDAYPLATENUMERBRANDCOLOR
Ms TJun 20, 1934HD-VW 2012VWblack
Ms UMay 11, 1966HD-UP 13Skodared
Ms UMay 11, 1966HD-MM 208BMWgreen
Mr VApr 21, 1952HD-VW 1999Audiyellow
Ms WJun 1, 1957HD-V 106Fiatred
Ms YFeb 10, 1986HD-Y 333Audiorange

Removing a View

You can discard an existing view, the data in the underlying table(s) are not affected.

Code Snippet
1
DROP VIEW redCars;

Using Views

In principle, an application developer should not be able to tell if a database table or a view is accessed when looking at an SQL statement. This largely holds true as far as SELECT statements / querying data is concerned.

To a certain extent, this statement even holds true when considering data modifications. Depending on how a database view is defined, you can insert, update, or delete database records using the view.

Inserting Through a View

You can INSERT new rows into a view. The new rows are not inserted into the view, but into the underlying base table!

Code Snippet
12345
INSERT INTO redCars VALUES ('F77', 'red', 'HD-MT 2509', 170); SELECT * FROM Car;
 CARIDPLATENUMBERBRANDCOLORHPOWNER
 F01HD-V 106Fiatred75H06
 F02HD-VW 4711VWblack120H03
 F03HD-JA 1972BMWblue184H03
 
 F18HD-MQ 2006Renaultred90H03
 F19HD-VW 2012VWblack125H01
 F20?Audigreen184?
F77HD-MT 2509?red170?

Updating Through a View

You can UPDATE rows in a view as well. The rows are not changed in the view, but in the underlying base table!

Code Snippet
123456
UPDATE redCars SET Power = 120 WHERE Power < 120; SELECT * FROM Car;
 CARIDPLATENUMBERBRANDCOLORHPOWNER
F01HD-V 106Fiatred120H06
 
 F07HD-ML 3206Audiblue116H03
 F08HD-IK 1002VWblack160H07
F09HD-UP 13Skodared120H02
 F10HD-MT 507BMWblack140H04
 F11HD-MM 208BMWgreen184H02
F12HD-XY 4711Skodared120H04
 F13HD-IK 1001Renaultred136H07
 
F18HD-MQ 2006Renaultred120H03
 

Deleting Through a View

You can DELETE rows from a view. The rows are not deleted from the view, but from the underlying base table!

Code Snippet
123456
DELETE FROM redCars WHERE Power < 125; SELECT * FROM Car;
 CARIDPLATENUMBERBRANDCOLORHPOWNER
F01HD-V 106Fiatred120H06
 
       
Table where deleted rows are highlighted and shown as strike through.

Restrictions

Such data modifications through database views are not always possible. You cannot modify data through a view if the view definition contains any of the following:

  • A calculation or function in the projection list

  • A subquery in the projection list

  • DISTINCT

  • The TOP N clause

  • Aggregate expressions

  • A GROUP BY clause

  • UNION

Only one table may be affected by a change statement. If the view is defined on a join of tables, INSERT and DELETE are not allowed, and UPDATE only permitted if the column(s) changed come from a single underlying table.

Additionally, the change can only affect columns visible through the view. If a view is defined on five columns of a table that contains nine columns, only those five columns can be specified in a WHERE, SET, or VALUES clause. In such cases, an INSERT would only be allowed if the four columns not visible through the view allowed NULL or default values.

Finally, Only whole rows are modified for DELETE. Even if some columns in the underlying table were not visible from the view, a DELETE on the view would remove entire rows in the table, not just the columns visible from the view!

Because the underlying table(s) of a view are not always visible, it is always safer to restrict view usage to queries and disallow data changes.