HA150 SQL Basics for SAP HANA . . INSTRUCTOR HANDBOOK INSTRUCTOR-LED TRAINING . Course Version: 12 Course Duration: 2 Day(s) Material Number: 50135954 SAP Copyrights and Trademarks © 2016 SAP SE or an SAP affiliate company. All rights reserved. No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP SE or an SAP affiliate company. SAP and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP SE (or an SAP affiliate company) in Germany and other countries. Please see http://global12.sap.com/corporate-en/legal/ copyright/index.epx for additional trademark information and notices. Some software products marketed by SAP SE and its distributors contain proprietary software components of other software vendors. National product specifications may vary. These materials are provided by SAP SE or an SAP affiliate company for informational purposes only, without representation or warranty of any kind, and SAP SE or its affiliated companies shall not be liable for errors or omissions with respect to the materials. The only warranties for SAP SE or SAP affiliate company products and services are those that are set forth in the express warranty statements accompanying such products and services, if any. Nothing herein should be construed as constituting an additional warranty. In particular, SAP SE or its affiliated companies have no obligation to pursue any course of business outlined in this document or any related presentation, or to develop or release any functionality mentioned therein. This document, or any related presentation, and SAP SE’s or its affiliated companies’ strategy and possible future developments, products, and/or platform directions and functionality are all subject to change and may be changed by SAP SE or its affiliated companies at any time for any reason without notice. The information in this document is not a commitment, promise, or legal obligation to deliver any material, code, or functionality. All forward-looking statements are subject to various risks and uncertainties that could cause actual results to differ materially from expectations. Readers are cautioned not to place undue reliance on these forward-looking statements, which speak only as of their dates, and they should not be relied upon in making purchasing decisions. Typographic Conventions American English is the standard used in this handbook. The following typographic conventions are also used. This information is displayed in the instructor’s presentation Demonstration Procedure Warning or Caution Hint Related or Additional Information Facilitated Discussion User interface control Example text Window title Example text © Copyright. All rights reserved. iii iv © Copyright. All rights reserved. Contents vii Course Overview 1 Unit 1: 2 15 21 Lesson: Understanding Motivation and Basic Concepts Lesson: Understanding the Sample Data Unit 2: 22 47 53 Unit 3: 70 91 95 96 107 111 112 117 121 122 129 133 134 143 149 150 157 30 Minutes 15 Minutes Data Storage Lesson: Defining How Data is Stored Exercise 7: Create, Change, and Delete a Database Table Unit 9: 30 Minutes 15 Minutes Data Stored in Tables Lesson: Changing Data Stored in Tables Exercise 6: Insert, Update, and Delete Rows in a Database Table Unit 8: 40 Minutes 10 Minutes NULL Values Lesson: Understanding NULL Values Exercise 5: Understand NULL Values Unit 7: 60 Minutes 20 Minutes Data From Multiple Tables Part II Lesson: Reading Data from Multiple Tables- Part 2 Exercise 4: Use Nested Queries Unit 6: 45 Minutes 20 Minutes Data From Multiple Tables Part I Lesson: Reading Data from Multiple Tables- Part 1 Exercise 3: Use JOIN to Combine Data from Several Tables Unit 5: 75 Minutes 25 Minutes Aggregation of Data Lesson: Aggregating Data Exercise 2: Use Aggregate Expressions Unit 4: 25 Minutes 10 Minutes Data From a Table Or View Lesson: Using Data from a Table or View Exercise 1: Using the SELECT Statement to Read from a Single Table 54 65 69 Motivation and Basic Concepts 45 Minutes 15 Minutes Views For Data Access Lesson: Using Views for Data Access Exercise 8: Work with Database Views © Copyright. All rights reserved. 30 Minutes 10 Minutes v 161 Unit 10: 162 167 173 Lesson: Creating User-Defined Functions Exercise 9: Create and Call Scalar User-Defined Functions Exercise 10: Create and Call Table User-Defined Functions 80 Minutes 15 Minutes 30 Minutes 178 185 Lesson: Creating Database Procedures Exercise 11: Create and Use a Database Procedure 60 Minutes 25 Minutes 191 Unit 11: 192 201 202 vi Functions and Procedures Data Access Lesson: Defining Data Access Unit 12: 20 Minutes Database Transactions Lesson: Explaining Database Transactions 25 Minutes © Copyright. All rights reserved. Course Overview TARGET AUDIENCE This course is intended for the following audiences: ● Application Consultant ● Development Consultant ● Technology Consultant © Copyright. All rights reserved. vii viii © Copyright. All rights reserved. UNIT 1 Motivation and Basic Concepts Lesson 1 Understanding Motivation and Basic Concepts 2 Lesson 2 Understanding the Sample Data 15 UNIT OBJECTIVES ● Understand the motivation for and foundation of the relational model ● Understand SQL and its relation to the relation model ● Understand database tables as the most important database objects ● Understand the sample database used throughout the course © Copyright. All rights reserved. 1 Unit 1 Lesson 1 2 Understanding Motivation and Basic Concepts LESSON OVERVIEW This lessons explains why learning SAP HANA SQL may be important and the relational database model it originates from. LESSON OBJECTIVES After completing this lesson, you will be able to: ● Understand the motivation for and foundation of the relational model ● Understand SQL and its relation to the relation model ● Understand database tables as the most important database objects Terms Database Management System, Database and Database System The terms Database, Database System and Database Management System are frequently used incorrectly and interchangeably: Figure 1: Terms DB, DBS, and DBMS The terms in fact refer to slightly different concepts: Database (DB) A database is a concrete structured collection of “records”. Database Management System (DBMS) A database management system is software that manages databases. Every access to a database (create, read, insert, update, delete) goes exclusively through the DBMS. The DBMS exercises complete control over the database(s) it manages. Database System 2 © Copyright. All rights reserved. Lesson: Understanding Motivation and Basic Concepts A database system is the combination of the implementation of a DBMS and the database(s) it manages. Figure 2: DBS = DB + DBMS Goals of the Relational Database Model Several database models, that are ways structure data records and thus implement DBMSs, have been developed during the last decades. The following list is a selection of such models: ● Hierarchical Database Model ● Network Database Model ● Relational Database Model ● Object-relational Database Model ● Object-oriented Database Model ● XML-based Database Model ● .... Design Goals of the Relational Database Model The Relational Database Model was invented in the late 1960s by Edgar F. Codd (1923–2003), then working at IBM Aldaden Research Lab in San José, California, with the following specific goals: ● Provide a simple yet mathematically profound and precise database model ● Make monitoring data integrity largely a responsibility of the DBMS ● Separate the conceptual schema from the internal schema ● Make data storage and retrieval the sole responsibility of the DBMS - A descriptive data access language removes need to traverse data links - Database optimizers choose the optimal execution strategy for a given query © Copyright. All rights reserved. 3 Unit 1: Motivation and Basic Concepts ● Provide a simple database access language with semantics described with mathematical precision - Easy to learn - Equivalence of different queries can be proved The goal behind separating the conceptual schema from the internal schema is to allow for a 3–level architecture such that changes on a lower level do not affect higher levels. Figure 3: Three Level Schema Architecture External Schema: How is data presented to the user? ● (Partial) views of the data as required by applications or users. ● In case of a relational database system implemented via views. Conceptual Schema: What is stored? ● ● Overall presentation of the data model at the logical, (if possible) DBMS and application independent level. For example, in relational representation, or even higher level of abstraction (e.g. E/R model). Internal Schema: How and where stored? ● 4 Describes (DBMS specific) the internal, physical representation of data: How and where exactly the data is stored, internal record format, access paths, etc. © Copyright. All rights reserved. Lesson: Understanding Motivation and Basic Concepts Figure 4: Three Level Schema Architecture Overview Why the Term “Relational” Database Model? The database model is called relational because it is based on the notion of relation in set theory. A relation in the mathematical sense is a subset of the Cartesian product of other sets. Figure 5: Relations Figure 6: Simple Base Set and Cartesian Example © Copyright. All rights reserved. 5 Unit 1: Motivation and Basic Concepts Figure 7: Comparative Relation <= Example Figure 8: Base Sets with Business Data Example Figure 9: Employee Records as a Relation Example 6 © Copyright. All rights reserved. Lesson: Understanding Motivation and Basic Concepts Figure 10: Relations and Tables The Role of SQL Languages for the Relational Database Model The following languages are available to interact with a relational DBMS: ● Relational Algebra - - ● 6 basic operations: Selection, Projection, Cartesian Product, Union, Difference and Rename Relational Calculus - ● Formal basis for DBMS internal query optimization Tuple variables and quantifiers SQL - Standardized and used in practice © Copyright. All rights reserved. 7 Unit 1: Motivation and Basic Concepts Figure 11: Why SQL? ANSI SQL SQL is a computer language that is used to define, manipulate and control relational databases. It is defined by the American Standards Institute (ANSI). SQL is designed as a descriptive rather than procedural language. Using SQL you express what data you want, not exactly how to retrieve it. This design allows the DBMS to first parse each statement and then optimize it. The optimizer determines the best execution plan (at least in theory). SQL Language Elements Table 1: SQL Language Elements SQL language elements can be divided into the following three categories: Data Manipulation Language (DML) statements DML statements insert, update and delete the data in a database. DML statements also select data that you want to read. Data Definition Language (DDL) statements DDL statements create, alter and drop databases, tables and other database objects in the server. Data Control Language (DCL) statements DCL statements grant and revoke permissions from database users. SQL is not Fully Relational SQL is the most important database language for the relational database model. But it deviates in important points from the “purely relational” model: Since SQL is designed to work with data stored in tables, in other words with sets, it is multiset oriented and not single record-based. Using a single SQL statement, you can read multiple table rows, modify or delete them in one go. 8 © Copyright. All rights reserved. Lesson: Understanding Motivation and Basic Concepts SQL is not Fully Relational ● Results of SQL queries can contain duplicates (identical rows). This makes SQL multi-set oriented and not set oriented. ● SQL allows for NULL values, leading to a three-valued logic. ● The SQL language is not closed. SAP HANA SQL and SQLScript SAP HANA SQL, including SQLScript, is the computer language created by SAP and used in the SAP HANA platform. It is built on a foundation of standard ANSI SQL, and it extends the standard considerably, in part to better support the column and in-memory store nature of SAP HANA. SAP HANA SQL and SQLScript Elements The following is a list of selected elements of SAP HANA SQL: ● Standard (ANSI) SQL statements ● SAP’s extensions to SQL statements for SAP HANA, e.g. - DML extentsions, e.g. SELECT INTO, UPSERT, - DDL extensions, e.g. to allow creating column and row store tables - Functions - Triggers ● Declarative SQLScript statements, for example variable assignments ● Imperative SQLScript statements (flow control statements) Database Objects ● Tables are the primary database object, but not the only one. Apart from tables a database usually contains the following: - Views to simplify and limit data access - Indexes to speed up (certain) read accesses - Constraints to ensure data consistency - Stored procedures for more complex tasks - Triggers to selectively respond to particular events © Copyright. All rights reserved. 9 Unit 1: Motivation and Basic Concepts Figure 12: Database Tables Figure 13: Table Rows as Points in Space Figure 14: Projection on the Plane 10 © Copyright. All rights reserved. Lesson: Understanding Motivation and Basic Concepts Figure 15: Projection on an Axis Components of Database Tables Figure 16: Components Of Database Tables Figure 17: Multi-Column Keys © Copyright. All rights reserved. 11 Unit 1: Motivation and Basic Concepts Figure 18: Multi-Column Keys Figure 19: Multiple Keys Figure 20: Foreign-Keys 12 © Copyright. All rights reserved. Lesson: Understanding Motivation and Basic Concepts Figure 21: Multi-Column Foreign-Keys Figure 22: Multiple Foreign-Keys Figure 23: Self-Referencing Foreign-Keys © Copyright. All rights reserved. 13 Unit 1: Motivation and Basic Concepts LESSON SUMMARY You should now be able to: 14 ● Understand the motivation for and foundation of the relational model ● Understand SQL and its relation to the relation model ● Understand database tables as the most important database objects © Copyright. All rights reserved. Unit 1 Lesson 2 15 Understanding the Sample Data LESSON OVERVIEW This lessons explains the sample data models and data used throughout lessons and exercises of this course. LESSON OBJECTIVES After completing this lesson, you will be able to: ● Understand the sample database used throughout the course The Sample Data Model The examples used in the lessons and on the figures of this course are based on a fictional car registration office. Example: Registration Office ● ● The database of a fictional registration office will serve as the basis for further explanations. The tables in this database have been specifically tailored to the SQL course and are not an example of good database design. ● The officials working in the fictional registration office have a manager. ● Each vehicle is registered for exactly one owner (or is unregistered). ● There is a list of vehicles that have been reported stolen. ● Owners, who have at least three vehicles registered, are assigned to one or multiple contacts. © Copyright. All rights reserved. 15 Unit 1: Motivation and Basic Concepts Figure 24: Registration Office Example Table 2: The Officials Table The following shows the content of the Official database table as an example. For the content of the other sample tables check the handbook. Official PNr Name Overtime Salary Manager P01 Mr A 10 A09 P04 P02 Mr B 10 A10 P04 P03 Ms C 20 A09 P04 P04 Ms D NULL A12 P09 P05 Mr E 10 A08 P08 P06 Mr F 18 A09 P09 P07 Ms G 22 A11 P08 P08 Ms H NULL A13 P09 P09 Mr I NULL A14 NULL Table 3: The Owner Table Database table Owner contains the information about the car owners. Note that real persons have a birthday, but companies don’t. Owner 16 OwnerID Name Birthday City H01 Ms T 20.06.1934 Wiesloch H02 Ms U 11.05.1966 Hockenheim H03 SAP AG NULL Walldorf H04 HDM AG NULL Heidelberg H05 Mr V 21.04.1952 Leimen © Copyright. All rights reserved. Lesson: Understanding the Sample Data Owner OwnerID Name Birthday City H06 Ms W 01.06.1957 Wiesloch H07 IKEA NULL Walldorf H08 Mr X 30.08.1986 Walldorf H09 Ms Y 10.02.1986 Sinsheim H10 Mr Z 03.02.1986 Ladenburg Table 4: The Contact Table Database table Contact relates a car owner owning more than two cars to the officials who are the respective contact persons. Contact PersNumber OwnerID P01 H03 P01 H04 P01 H07 P04 H03 P04 H04 P08 H04 P08 H07 P09 H03 Figure 25: Cars © Copyright. All rights reserved. 17 Unit 1: Motivation and Basic Concepts Figure 26: Stolen Cars Figure 27: Registration Office and EU Figure 28: Owner (EU-Wide) 18 © Copyright. All rights reserved. Lesson: Understanding the Sample Data Figure 29: Cars (EU-Wide) LESSON SUMMARY You should now be able to: ● Understand the sample database used throughout the course © Copyright. All rights reserved. 19 Unit 1: Motivation and Basic Concepts 20 © Copyright. All rights reserved. UNIT 2 Data From a Table Or View Lesson 1 Using Data from a Table or View Exercise 1: Using the SELECT Statement to Read from a Single Table 22 47 UNIT OBJECTIVES ● Write simple database queries using SQL‘s SELECT statement and project columns in and out of queries using the SELECT clause ● Calculate column values, use built-in functions and the CASE clause in column lists ● Avoid duplicates in SELECT statement result sets ● Limit results sets to a given number of rows and browse through result sets ● Ensure a specific order in result sets ● Restrict the result set using the WHERE clause © Copyright. All rights reserved. 21 Unit 2 Lesson 1 22 Using Data from a Table or View LESSON OVERVIEW The lesson covers the foundation of how to retrieve data from a database. LESSON OBJECTIVES After completing this lesson, you will be able to: ● Write simple database queries using SQL‘s SELECT statement and project columns in and out of queries using the SELECT clause ● Calculate column values, use built-in functions and the CASE clause in column lists ● Avoid duplicates in SELECT statement result sets ● Limit results sets to a given number of rows and browse through result sets ● Ensure a specific order in result sets ● Restrict the result set using the WHERE clause Overview of the SELECT Statement The SELECT statement is an SQL statement, which is used to read data from a database table or view. The SELECT statement is the central construct for read access to data, of the Data Manipulation Language. A SELECT statement must contain a SELECT and a FROM clause and can contain a WHERE, GROUP BY, HAVING and ORDER BY clause. Figure 30: The SELECT Statement: Mandatory and Optional Clauses The SELECT Clause in its Most Simple Form The SELECT clause specifies the Projection List, in other words which columns to read from the data or which columns to include in the result set of the SELECT statement. 22 © Copyright. All rights reserved. Lesson: Using Data from a Table or View Figure 31: Multiple Columns in Projection List Figure 32: Sequence of Columns in the Projection List Figure 33: All Columns Asterisk in Projection List The following options are also supported: ● You can include a single column in the projection list. ● You can include the same column in the projection list repeatedly. ● You can combine using the asterisk and explicitly named columns. © Copyright. All rights reserved. 23 Unit 2: Data From a Table Or View ● You can use the asterisk repeatedly in the projection list. We do not recommend using the last three options because each makes the result set difficult to understand and work with. Naming Columns of the Result Set The column names of the result set can differ from the column names in the table selected from. In other words you can “rename” result columns. Note: Renaming columns in the SELECT clause has no effect on the table selected from. It only affects the result set returned. Figure 34: Renaming Columns in the Projection Column names not enclosed in quotation marks are converted to upper case. A column name has to be enclosed in quotation marks if one of the following applies: ● ● The column name should not be converted to upper case but be kept exactly as spelled in your statement. The column name contains a space, a punctuation mark, a colon or any other special character. Note: The rule also applies to the names of the original database tables and their columns: The SQL parser converts names not enclosed in quotation marks to upper case. It then performs a case-sensitive search for the corresponding database objects. The keyword AS is optional. 24 © Copyright. All rights reserved. Lesson: Using Data from a Table or View Figure 35: Keyword AS is Optional You can use column names of the table selected from to rename other columns of the result set, e.g. to exchange column names. Figure 36: Reusing Existing Column Names for Other Columns The SELECT Clause: Calculated Columns The SELECT clause can also contain columns the value of which is computed dynamically, typically based on the values of other columns. In the most simple form, these calculated column can be literal values. Both string and numerical literals are supported. © Copyright. All rights reserved. 25 Unit 2: Data From a Table Or View Figure 37: Literal Values in the SELECT Clause Note: String values are enclosed in single quotes. This can lead to confusion if column names are enclosed in quotation marks as well. Numerical values are not enclosed in any quotes. Figure 38: Distinguish String and Numerical Literals for Column Names The projection list can even comprise of one or more literal values only. Note that the result set still contains as many records, all comprising of the same literal values only, as the table select from contains. If your intent is to select a single “literal record”, you can use the built-in table DUMMY. 26 © Copyright. All rights reserved. Lesson: Using Data from a Table or View Figure 39: Built-In Table DUMMY Figure 40: Calculated Columns © Copyright. All rights reserved. 27 Unit 2: Data From a Table Or View Figure 41: NULL Values and Calculated Columns Figure 42: Naming Calculated Columns SAP HANA supports using functions to calculate columns of the projection list. Different functions are available for different data types of the input columns and values. 28 © Copyright. All rights reserved. Lesson: Using Data from a Table or View Figure 43: Functions Figure 44: Function Calls can be Nested Table 5: Functions supported by SAP HANA (Selection) SAP HANA supports a rich list of built-in function. The following is only a selection. Function Explanation ADD_YEAR(<date>, <number>) Adds a <number> of years to the specified <date> CURRENT_DATE Determines the current date ABS(<number>) Determines the absolute value ROUND(<number>) Rounds the <number> SQRT(<number>) Determines the square root of <number> UPPER(<string>) Converts the <string> to upper case SUBSTR(<string>,<start>,<len>) Extracts a substring of <string> © Copyright. All rights reserved. 29 Unit 2: Data From a Table Or View Function Explanation CONCAT(<string>,<string>) Concatenates two strings. Equivalent to the binary operator ||. COALESCE(<expressions>) Returns the result of the first element of the list of <expressions> that does not evaluate to NULL. See the SAP HANA SQL Reference available on the SAP Help Portal for the full list of supported built-in functions. http://help.sap.com/hana_platform#section6 You can also create scalar User-Defined Functions and use the in the same was as built-in functions. The CASE Expression A special way of calculating column values is using a CASE expression. This expression allows you determine a column value based on conditions, in other words to include an IF-THENELSE type of logic in the projection list. Figure 45: Case Expression: Basic Form Figure 46: Case Expression: Result 30 © Copyright. All rights reserved. Lesson: Using Data from a Table or View Figure 47: Case Expression: Case not Handled Figure 48: Case Expression: Using Columns in the THEN and ELSE Branches © Copyright. All rights reserved. 31 Unit 2: Data From a Table Or View Figure 49: Case Expression: Result Eliminating Duplicates in Result Sets An important difference between SQL and the Relational Database Model is that SQL is multiset oriented, which allows for duplicate entries in result sets. However, you won’t notice this as long as the database table you select from have a primary key and once you select all of the columns. Figure 50: No Duplicates With Primary Key If you project a table and don’t include the full primary key, duplicate entries can result. 32 © Copyright. All rights reserved. Lesson: Using Data from a Table or View Figure 51: Duplicates By Projection In most cases, this is not intended and practical, as there is no way to distinguish the duplicate entries from each other in the result set. This is why duplicate entries can be removed explicitly using keyword DISTINCT. Note that duplicates are not removed by default to speed up data retrieval. Figure 52: Duplicate Elimination using DISTINCT Figure 53: Avoid Duplicate Elimination Using ALL © Copyright. All rights reserved. 33 Unit 2: Data From a Table Or View Figure 54: Duplicate Elimination: Treatment of NULL Values Figure 55: Duplicates With A Multi-Column Result Set Figure 56: DISTINCT Eliminates Duplicates In Multi-Column Result Set In principle, you could always include the DISTINCT keyword in SELECT statement to be sure to never get duplicates, since DISTINCT is compatible with using an asterisk in the projection list. Note, however, that using DISTINCT comes at a small performance penalty. 34 © Copyright. All rights reserved. Lesson: Using Data from a Table or View Figure 57: Duplicate Elimination The TOP N Clause You can use the TOP N clause as part of the SELECT clause to specify that the result set should contain at most N rows. Figure 58: Limiting the Result Set to a Maximum Number of Rows © Copyright. All rights reserved. 35 Unit 2: Data From a Table Or View Figure 59: TOP N Clause can be Combined with DISTINCT Figure 60: TOP N Clause with Fewer Rows than N Figure 61: TOP N Clause with N=0 The LIMIT and OFFSET Clauses The TOP N clause allows you to limit the result set to a specified maximum number of rows, but it does not allow you to retrieve the “next” N rows. To browse through a result in a pageby-page way, you can use the LIMIT and OFFSET clauses. 36 © Copyright. All rights reserved. Lesson: Using Data from a Table or View Figure 62: The LIMIT Clause as Alternative to TOP N Figure 63: Combine LIMIT and OFFSET to Browse through a Result Set Note: The LIMIT and OFFSET clauses are not part of the SELECT clause, but are the last clauses of a SELECT statement. The ORDER BY Clause By default, the result returned by a query in SAP HANA has no defined and no reliable sort order. Executing the same query repeatedly can result in different sort orders. Note: Query results in the SAP HANA database are not sorted by primary key by default. There is no defined and no reliable default sort order. © Copyright. All rights reserved. 37 Unit 2: Data From a Table Or View You can use the ORDER BY clause to explicitly specify the sort order of the result set and this way get a reliable sort order. Figure 64: The ORDER BY Clause Figure 65: Descending Sort Order 38 © Copyright. All rights reserved. Lesson: Using Data from a Table or View Figure 66: Ascending Sort Order Figure 67: Sorting by a Column not in the Result © Copyright. All rights reserved. 39 Unit 2: Data From a Table Or View Figure 68: Sorting by Multiple Columns Figure 69: Referencing Columns by Position 40 © Copyright. All rights reserved. Lesson: Using Data from a Table or View Figure 70: Sorting and Column Renaming Figure 71: Sorting by Calculated Columns You can also use functions in the expression in the ORDER BY clause. The WHERE Clause The WHERE clause is used to select a subset of rows from the data source based on a specified condition. Only the rows matching the condition are returned. © Copyright. All rights reserved. 41 Unit 2: Data From a Table Or View Figure 72: WHERE Clause Figure 73: WHERE Clause Condition expressions in SQL are very similar to condition expressions as known from programming languages such as ABAP, Java and so on. The columns of the data source take over the roles of variables used in these languages. ● ● ● A condition can refer to string-like or to numeric columns. The column used in the condition do not have to be included in the projection list. Atomic conditions can be combined to complex and nested conditions using the usual logical operators AND, OR and NOT and parentheses to override operator precedence rules. A condition can make use of functions, including nested functions. An atomic condition typically contains a comparison operator. The usual comparison operators =, <>, <, <=, > and >= are supported. But SQL supports a few additional comparison operators. 42 © Copyright. All rights reserved. Lesson: Using Data from a Table or View Figure 74: Comparison Operator IS NULL Figure 75: Comparison Operator IS NOT NULL Figure 76: Comparison Operator IN © Copyright. All rights reserved. 43 Unit 2: Data From a Table Or View Figure 77: Comparison Operator BETWEEN ... AND Figure 78: Comparison Operator LIKE Figure 79: Wildcards in the LIKE Predicate 44 © Copyright. All rights reserved. Lesson: Using Data from a Table or View Figure 80: Escaping Wildcards in the LIKE Predicate Figure 81: Escaping Escape Characters in the LIKE Predicate © Copyright. All rights reserved. 45 Unit 2: Data From a Table Or View Figure 82: A Complex WHERE Clause Figure 83: Operator Precedence 46 © Copyright. All rights reserved. Unit 2 Exercise 1 47 Using the SELECT Statement to Read from a Single Table Business Example Your company created a few database tables using the SAP HANA platform to manage rudimentary information about its employees and departments. The database tables have been created as follows: CREATE COLUMN TABLE EMPLOYEE -- data about company's emplyees ( DNumber CHAR(7), -- personnel number Name VARCHAR(20) NOT NULL, -- employee's name DateOfBirth DATE, -- employee's date of birth RemainderDays INTEGER, -- # of vacation days left DepID CHAR(3), -- department employee works for PRIMARY KEY(DNumber) ); CREATE COLUMN TABLE Department -- the company's departments ( DepID CHAR(3) PRIMARY KEY, -- department ID Name VARCHAR(20), -- department name Status VARCHAR(10) DEFAULT 'profitable' ); You are asked to answer a set of questions about employees and departments using SQL queries against the data. Note that this example used throughout the exercises represents a simplistic model for storing information about a company’s employees and organizational structure. Log on to SAP HANA and Open SQL Console Establish a connection to the SAP HANA Server to be prepared for writing and executing SQL queries. 1. Open SAP HANA Studio. 2. Add a new system. Use the server name, user ID and password provided by your instructor. 3. Open an SQL Console connected to the SAP HANA system. Using the SELECT Statement to Read from a Single Table Write and execute a SELECT statement for each of the following tasks to answer the question posed. 1. Create a list of employees, including all columns. 2. Write a query to determine which employees have fewer than 30 remaining vacation days. 3. Create a list of the department IDs the different employees work for. The list should not contain duplicates. © Copyright. All rights reserved. 47 Unit 2: Data From a Table Or View 4. Write a query to determine which employees have fewer than 30 remaining vacation days. Sort the output descending by RemainderDays and - for employees with the same number of RemainderDays - ascending by DNumber. 5. Write a query to determine the employees for which the remaining vacation days are unknown. 6. Write a query to determine the employees for which the remaining vacation days known. 7. Write a query to determine how many remaining vacation days can be found for the different employees in the company. Each quantity number should be displayed only once. 8. Create a list of employees. Only columns DNumber and Name are relevant. 9. Create a list of employees. Only columns DNumber and Name are relevant. The output column DNumber should be renamed to “PERSNUMBER”. 10. Create a list of employees. Only columns DNumber and Name are relevant. The output column DNumber should be renamed to “Personal Number” (with small letters). 11. Write a query to determine how many remaining vacation days “Ms D” has. 12. Write a query to determine which employees have exactly 10 remaining vacation days. 13. The value of a vacation day is estimated at EUR 300.- (all-in). Create a list of employees including the calculated value of the remaining vacation days. 14. The value of a vacation day is estimated at EUR 300.- (all-in). Write a query to determine which employees have remaining vacation days with a value of at least EUR 600.15. Write a query to determine which numbers of remaining vacation days can be found in the company. Each quantity number should be displayed only once. 16. Write a query to determine which employees work for department “A04” and have exactly 20 remaining vacation days. 17. Write a query to determine which employees work in department “A01” or “A04”. Use OR 18. Write a query to determine which employees work in department "A01" or "A04". Use IN 19. Write a query to determine which employees not working for department “A01” have exactly 10 remaining vacation days. 20. Write a query to determine which employee names include an “L”. 21. Write a query to determine which employees have a name beginning with “M” and in the fourth place a “K”. 48 © Copyright. All rights reserved. Unit 2 Solution 1 49 Using the SELECT Statement to Read from a Single Table Business Example Your company created a few database tables using the SAP HANA platform to manage rudimentary information about its employees and departments. The database tables have been created as follows: CREATE COLUMN TABLE EMPLOYEE -- data about company's emplyees ( DNumber CHAR(7), -- personnel number Name VARCHAR(20) NOT NULL, -- employee's name DateOfBirth DATE, -- employee's date of birth RemainderDays INTEGER, -- # of vacation days left DepID CHAR(3), -- department employee works for PRIMARY KEY(DNumber) ); CREATE COLUMN TABLE Department -- the company's departments ( DepID CHAR(3) PRIMARY KEY, -- department ID Name VARCHAR(20), -- department name Status VARCHAR(10) DEFAULT 'profitable' ); You are asked to answer a set of questions about employees and departments using SQL queries against the data. Note that this example used throughout the exercises represents a simplistic model for storing information about a company’s employees and organizational structure. Log on to SAP HANA and Open SQL Console Establish a connection to the SAP HANA Server to be prepared for writing and executing SQL queries. 1. Open SAP HANA Studio. a) Choose Start → All Programs → SAP HANA → SAP HANA Studio from the Windows Start Menu. b) In case the Welcome page is open, close the Welcome page or press the Workbench link. 2. Add a new system. Use the server name, user ID and password provided by your instructor. a) Make sure you are in the SAP HANA Modeler perspective. If not, change perspective using the buttons in the upper right-hand corner. b) Right-click anywhere in the Systems window on the left and choose Add System.... c) Enter host name and instance number provided by your instructor and a description, then press button Next >. d) Enter user ID and password provided by your instructor, then choose Finish. © Copyright. All rights reserved. 49 Unit 2: Data From a Table Or View 3. Open an SQL Console connected to the SAP HANA system. a) In the Systems frame, select the SAP HANA System or the Catalog folder underneath it. b) Choose context menu item Open SQL Console or press the corresponding button in the toolbar of the Systems frame. Using the SELECT Statement to Read from a Single Table Write and execute a SELECT statement for each of the following tasks to answer the question posed. 1. Create a list of employees, including all columns. a) SELECT * FROM Employee; 2. Write a query to determine which employees have fewer than 30 remaining vacation days. a) SELECT * FROM Employee WHERE RemainderDays < 30; 3. Create a list of the department IDs the different employees work for. The list should not contain duplicates. a) SELECT DISTINCT DEPID FROM Employee; 4. Write a query to determine which employees have fewer than 30 remaining vacation days. Sort the output descending by RemainderDays and - for employees with the same number of RemainderDays - ascending by DNumber. a) SELECT * FROM EmployeeWHERE RemainderDays < 30 ORDER BY RemainderDays DESC, DNumber ASC; 5. Write a query to determine the employees for which the remaining vacation days are unknown. a) SELECT * FROM Employee WHERE RemainderDays IS NULL; 6. Write a query to determine the employees for which the remaining vacation days known. a) SELECT * FROM Employee WHERE RemainderDays IS NOT NULL; 7. Write a query to determine how many remaining vacation days can be found for the different employees in the company. Each quantity number should be displayed only once. a) SELECT DISTINCT RemainderDays RemainderDays IS NOT NULL; FROM Employee WHERE 8. Create a list of employees. Only columns DNumber and Name are relevant. a) SELECT DNumber, Name FROM Employee; 9. Create a list of employees. Only columns DNumber and Name are relevant. The output column DNumber should be renamed to “PERSNUMBER”. a) SELECT DNumber AS PersNumber, Name FROM Employee; 10. Create a list of employees. Only columns DNumber and Name are relevant. The output column DNumber should be renamed to “Personal Number” (with small letters). a) SELECT DNumber AS "Personal Number", Name FROM Employee; 11. Write a query to determine how many remaining vacation days “Ms D” has. a) SELECT RemainderDays FROM Employee WHERE Name = 'Ms D'; 12. Write a query to determine which employees have exactly 10 remaining vacation days. 50 © Copyright. All rights reserved. Lesson: Using Data from a Table or View a) SELECT * FROM Employee WHERE RemainderDays = 10; 13. The value of a vacation day is estimated at EUR 300.- (all-in). Create a list of employees including the calculated value of the remaining vacation days. a) SELECT *, RemainderDays * 300 AS RemainderDaysValue Employee; FROM 14. The value of a vacation day is estimated at EUR 300.- (all-in). Write a query to determine which employees have remaining vacation days with a value of at least EUR 600.a) SELECT *, RemainderDays * 300 AS RemainderDayValue Employee WHERE RemainderDays * 300 >= 600; FROM 15. Write a query to determine which numbers of remaining vacation days can be found in the company. Each quantity number should be displayed only once. a) SELECT DISTINCT RemainderDays RemainderDays IS NOT NULL; FROM Employee WHERE 16. Write a query to determine which employees work for department “A04” and have exactly 20 remaining vacation days. a) SELECT * = 20; FROM Employee WHERE DepID = 'A04' AND RemainderDays 17. Write a query to determine which employees work in department “A01” or “A04”. Use OR a) SELECT * FROM Employee WHERE DepID = 'A01' OR DepID = 'A04'; 18. Write a query to determine which employees work in department "A01" or "A04". Use IN a) SELECT * FROM Employee WHERE DepID IN ('A01', 'A04'); 19. Write a query to determine which employees not working for department “A01” have exactly 10 remaining vacation days. a) SELECT * = 10; FROM Employee WHERE DepID <> 'A01' AND RemainderDays 20. Write a query to determine which employee names include an “L”. a) SELECT * FROM Employee WHERE Name LIKE '%L%'; 21. Write a query to determine which employees have a name beginning with “M” and in the fourth place a “K”. a) SELECT * © Copyright. All rights reserved. FROM Employee WHERE Name LIKE 'M__K%'; 51 Unit 2: Data From a Table Or View LESSON SUMMARY You should now be able to: ● 52 Write simple database queries using SQL‘s SELECT statement and project columns in and out of queries using the SELECT clause ● Calculate column values, use built-in functions and the CASE clause in column lists ● Avoid duplicates in SELECT statement result sets ● Limit results sets to a given number of rows and browse through result sets ● Ensure a specific order in result sets ● Restrict the result set using the WHERE clause © Copyright. All rights reserved. UNIT 3 Aggregation of Data Lesson 1 Aggregating Data Exercise 2: Use Aggregate Expressions 54 65 UNIT OBJECTIVES ● List the most important aggregate functions supported by HANA and use them to determine aggregated values on table columns using a single SELECT statement ● Determine aggregated values for groups of rows, using the GROUP BY clause ● Filter groups using the HAVING clause © Copyright. All rights reserved. 53 Unit 3 Lesson 1 54 Aggregating Data LESSON OVERVIEW This lesson covers how you can perform certain calculations on a database table column across multiple rows of the database table. LESSON OBJECTIVES After completing this lesson, you will be able to: ● List the most important aggregate functions supported by HANA and use them to determine aggregated values on table columns using a single SELECT statement ● Determine aggregated values for groups of rows, using the GROUP BY clause ● Filter groups using the HAVING clause Calculations Across Multiple Rows: Aggregation Function expressions allow calculating the values of columns based on the values of other columns on a row-by-row basis. But in many cases, you may want to calculate a single value across the values of multiple rows in one or more columns. You can use aggregate expressions for this purpose. Figure 84: COUNT(*) Expression 54 © Copyright. All rights reserved. Lesson: Aggregating Data Figure 85: Renaming the Result of an Aggregate Expression Figure 86: COUNT(<column>) Expressions © Copyright. All rights reserved. 55 Unit 3: Aggregation of Data Figure 87: COUNT(DISTINCT <column>) Expression Figure 88: Differences between the COUNT Expressions Figure 89: MIN and MAX Expressions 56 © Copyright. All rights reserved. Lesson: Aggregating Data Figure 90: Combining Aggregate and Function Expressions Note that, in general, the order in which functions and aggregate expressions are nested matters. As an example, the following two SELECT statements lead to different results: SELECT ABS( MAX(0-HP) ) FROM Car; -- Result: 75 SELECT MAX( ABS(0-HP) ) FROM Car; -- Result: 260 Figure 91: Aggregate Expressions Figure 92: SUM(<column>) and AVG(<column>) Expressions © Copyright. All rights reserved. 57 Unit 3: Aggregation of Data Figure 93: SUM(DISTINCT <column>) and AVG(DISTINCT <column>) Expressions Table 6: Aggregate Expressions Supported by SAP HANA The following table lists a selection of the aggregate expressions currently supported by SAP HANA Aggregate Name Description COUNT Returns the number of rows. MIN, MAX Returns the minimum, respectively maximum value of an input column with a numeric data type. SUM, AVG Returns the sum, respectively the arithmetic mean value of an input column with a numeric data type. MEDIAN Finds the statistical median of an input column with a numeric data type. VAR, STDDEV Returns the variance, respectively the standard deviation. STDDEV is the square root of VAR. CORR, CORR_SPEARMAN Computes the Pearson product momentum correlation coefficient, respectively the Spearman's rank correlation coefficient between two columns. STRING_AGG Returns the concatenated string. The GROUP BY Clause The examples so far all calculated a single value across all rows of a table that satisfied the WHERE condition of the SELECT statement. Sometimes you may need such aggregations not for all rows in total, but to produce different values for different subsets. This is here grouping and the GROUP BY clause come into play. 58 © Copyright. All rights reserved. Lesson: Aggregating Data Figure 94: COUNT(*) with GROUP BY Clause Figure 95: MAX with GROUP BY Clause Figure 96: Treatment of NULL Values © Copyright. All rights reserved. 59 Unit 3: Aggregation of Data Figure 97: Grouping and Sorting Figure 98: Combining Functions and Grouping Figure 99: Grouping and WHERE Clause 60 © Copyright. All rights reserved. Lesson: Aggregating Data Figure 100: Grouping by Several Columns Note: In SAP HANA SQL, you have to explicitly include each column in the group by clause that is not used in an aggregate expression. Figure 101: Grouping and Renaming Columns The HAVING Clause When using grouping, you can discard some of the resulting groups in a way similar to how you exclude rows from a non-aggregated result set. The corresponding keyword is HAVING and behaves similar to the WHERE clause, but affects the resulting groups instead of which rows are considered when calculating the aggregate values. © Copyright. All rights reserved. 61 Unit 3: Aggregation of Data Figure 102: HAVING Figure 103: HAVING 62 © Copyright. All rights reserved. Lesson: Aggregating Data Figure 104: SELECT statement © Copyright. All rights reserved. 63 Unit 3: Aggregation of Data 64 © Copyright. All rights reserved. Unit 3 Exercise 2 65 Use Aggregate Expressions 1. What is the number of employees? 2. How many employees have exactly 20 remaining vacation days? 3. In how many different departments do the employees work? 4. What is the maximum number of remaining vacation days? 5. What is the average number of remaining vacation days? Employees whose remaining vacation days are unknown should not be taken into account. 6. How many remaining vacation days have the employees accumulated in total? 7. The minimum, average, maximum, and total number of remaining vacation days should be displayed with a single query. 8. What is the difference between the maximum remaining vacation days and the average of remaining vacation days? 9. The total number of remaining vacation days should be displayed for each department. 10. The total number of remaining vacation days should be displayed for each department. Only departments with more than 30 days should be taken into account. The display of the total number of days should be sorted descending. © Copyright. All rights reserved. 65 Unit 3 Solution 2 66 Use Aggregate Expressions 1. What is the number of employees? a) SELECT COUNT(*) FROM Employee; 2. How many employees have exactly 20 remaining vacation days? a) SELECT COUNT(*) FROM Employee WHERE RemainderDays = 20; 3. In how many different departments do the employees work? a) SELECT COUNT(DISTINCT DepID) FROM Employee; 4. What is the maximum number of remaining vacation days? a) SELECT MAX(RemainderDays) FROM Employee; 5. What is the average number of remaining vacation days? Employees whose remaining vacation days are unknown should not be taken into account. a) SELECT AVG(RemainderDays) FROM Employee; 6. How many remaining vacation days have the employees accumulated in total? a) SELECT SUM(RemainderDays) FROM Employee; 7. The minimum, average, maximum, and total number of remaining vacation days should be displayed with a single query. a) SELECT MIN(RemainderDays) AS "min", AVG(RemainderDays) AS "avg", MAX(RemainderDays) AS "max", SUM(RemainderDays) AS "sum" FROM Employee; 8. What is the difference between the maximum remaining vacation days and the average of remaining vacation days? a) SELECT MAX(RemainderDays) - AVG(RemainderDays) FROM Employee; 9. The total number of remaining vacation days should be displayed for each department. a) SELECT DepID, SUM(RemainderDays) FROM Employee GROUP BY DepID; 10. The total number of remaining vacation days should be displayed for each department. Only departments with more than 30 days should be taken into account. The display of the total number of days should be sorted descending. a) SELECT DepID, SUM(RemainderDays) FROM Employee GROUP BY DepID HAVING SUM(RemainderDays) > 30 ORDER BY 2 DESC; 66 © Copyright. All rights reserved. Lesson: Aggregating Data LESSON SUMMARY You should now be able to: ● List the most important aggregate functions supported by HANA and use them to determine aggregated values on table columns using a single SELECT statement ● Determine aggregated values for groups of rows, using the GROUP BY clause ● Filter groups using the HAVING clause © Copyright. All rights reserved. 67 Unit 3: Aggregation of Data 68 © Copyright. All rights reserved. UNIT 4 Data From Multiple Tables Part I Lesson 1 Reading Data from Multiple Tables- Part 1 Exercise 3: Use JOIN to Combine Data from Several Tables 70 91 UNIT OBJECTIVES ● ● Combine the result of several select statements using the UNION, INTERSECT and EXCEPT statements List the various types of JOIN constructs and use the appropriate JOIN construct to combine data from several tables using a single query © Copyright. All rights reserved. 69 Unit 4 Lesson 1 70 Reading Data from Multiple Tables- Part 1 LESSON OVERVIEW This lesson covers how data can be retrieved and combined from several tables in a single statement, using UNIONs and JOINs. LESSON OBJECTIVES After completing this lesson, you will be able to: ● ● Combine the result of several select statements using the UNION, INTERSECT and EXCEPT statements List the various types of JOIN constructs and use the appropriate JOIN construct to combine data from several tables using a single query Reading from Multiple Tables: Overview The SELECT statement is not limited to reading from a single table. You can use it to read from several tables using a single statement. The following lists the options that are available, depending on the exact needs. ● You can combine the results of several partial queries to form the union of the results, determine their intersection or difference: - UNION ALL and UNION merge the results of partial queries to form their union. - INTERSECT is used to determine the elements contained in all partial query results. - ● You can use JOIN to combine data from several tables in common rows of a result set. - - ● CROSS JOIN, INNER JOIN and OUTER JOIN are the three types of JOINs. The are explained in the following. The two syntax options implicit JOIN and explicit JOIN are available. You can use nested queries (sub queries) to embed queries for information lookup in other queries. - 70 EXCEPT is used to determine the elements contained in one partial result, but not the second partial result. An uncorrelated sub query should be distinguished and favored over a correlated sub query. © Copyright. All rights reserved. Lesson: Reading Data from Multiple Tables- Part 1 UNION and UNION ALL Figure 105: UNION [ALL] Figure 106: Result Header is Determined by the First SELECT Clause Figure 107: Renaming Columns with UNION [ALL] © Copyright. All rights reserved. 71 Unit 4: Data From Multiple Tables Part I Figure 108: UNION ALL can Lead to Duplicates Figure 109: Avoiding Duplicates using UNION 72 © Copyright. All rights reserved. Lesson: Reading Data from Multiple Tables- Part 1 Figure 110: UNION [ALL] can Handle more than Two SELECTs INTERSECT and EXCEPT Figure 111: INTERSECT [DISTINCT] © Copyright. All rights reserved. 73 Unit 4: Data From Multiple Tables Part I Figure 112: EXCEPT [DISTINCT] Cross Join You can use a cross join, also called Cartesian product, to create a result set in which each row of each partial query involved is combined with each row of the other partial queries involved. Figure 113: CROSS JOIN: Explanation 74 © Copyright. All rights reserved. Lesson: Reading Data from Multiple Tables- Part 1 Figure 114: CROSS JOIN with Implicit Syntax Figure 115: CROSS JOIN with Explicit Syntax © Copyright. All rights reserved. 75 Unit 4: Data From Multiple Tables Part I Figure 116: CROSS JOIN and WHERE Clause Figure 117: CROSS JOIN You can also cross-join a table with itself. In general, note that a cross join can quickly lead to a very large result set, as its cardinality (number or rows) equals the product of the cardinalities of the queries it is comprised of. It can be easy to produce a cross join by mistake, because its syntax looks very similar to that of the inner join. Table Aliases Different tables can have columns with the same name. This can lead to ambiguities when joining such tables, for example when a column name that appears in two or more of the tables involved is used in the projection list, the WHERE condition or in a join condition. You have to resolve such ambiguities by qualifying the non-unique column name, that is by specifying the table the column belongs to. You can do this using the full table name as qualifier. 76 © Copyright. All rights reserved. Lesson: Reading Data from Multiple Tables- Part 1 Figure 118: Qualifying a Column Name using the Table Name Having to repeat the full table name as qualifier each time can be cumbersome. This is why you can use table aliases to introduce abbreviated table names and use the abbreviated form as qualifier in the same SELECT statement. Figure 119: Table Aliases Figure 120: Qualified Names can be Mixed with Unqualified Names © Copyright. All rights reserved. 77 Unit 4: Data From Multiple Tables Part I Figure 121: Supported Combinations of Qualified and Unqualified Column Names Inner Join An inner join is used to combine information from one table with corresponding information from another table, and to only include such rows in the result for which corresponding information is available. Figure 122: Inner Join: Explanation 78 © Copyright. All rights reserved. Lesson: Reading Data from Multiple Tables- Part 1 Figure 123: Inner Join — Implicit Syntax Figure 124: Inner Join — Explicit Syntax Note: The following examples will show the explicit syntax option only. Using the implicit syntax is more error-prone since the join condition cannot be distinguished from additional parts of a WHERE condition. This makes it easy to forget the join condition and produce a cross join by mistake. © Copyright. All rights reserved. 79 Unit 4: Data From Multiple Tables Part I Figure 125: Keyword INNER is Optional Figure 126: Joins can be Combined with Projections 80 © Copyright. All rights reserved. Lesson: Reading Data from Multiple Tables- Part 1 Figure 127: Table Aliases can be Handy for Inner Joins Figure 128: Projections can Reduce to One of the Tables Involved Figure 129: Renaming Columns of the Result Works as Usual © Copyright. All rights reserved. 81 Unit 4: Data From Multiple Tables Part I Figure 130: Aggregating, Grouping etc. also Work as Usual Figure 131: The JOIN Condition can be Complex Figure 132: Implicit INNER JOIN 82 © Copyright. All rights reserved. Lesson: Reading Data from Multiple Tables- Part 1 Figure 133: Combining Two Tables Figure 134: Use of Different Comparison Operators © Copyright. All rights reserved. 83 Unit 4: Data From Multiple Tables Part I Figure 135: Calculations and Functions in the JOIN Condition Left Outer Join The result set of an inner join does not contain the rows of any of the involved tables for which no corresponding row can be found in the other tables involved according to the join condition. Sometimes you may want to include such rows in the result set, to get the full set of rows of one table and augment the data with data from other tables as far as available. In addition to the inner join, three types of outer joins exist for this purpose: ● ● ● Left outer join: includes all entries of the first (left) table even if no corresponding entries are found in the second (right) table. Right outer join: includes all entries of the second table even if no corresponding entries are found in the first table. Full outer join: is the union of left and right outer join. In all three cases, the following applies: ● ● 84 A NULL value results for each column and row for which no corresponding data can be retrieved according to the join condition. SAP HANA only supports the explicit syntax option. © Copyright. All rights reserved. Lesson: Reading Data from Multiple Tables- Part 1 Figure 136: LEFT OUTER Join: Explanation Figure 137: LEFT OUTER Join: Example © Copyright. All rights reserved. 85 Unit 4: Data From Multiple Tables Part I Figure 138: LEFT OUTER Join can be Combined with WHERE Right Outer Join Figure 139: RIGHT OUTER Join: Explanation 86 © Copyright. All rights reserved. Lesson: Reading Data from Multiple Tables- Part 1 Figure 140: RIGHT OUTER Join: Example Figure 141: RIGHT OUTER Join can be Combined with WHERE © Copyright. All rights reserved. 87 Unit 4: Data From Multiple Tables Part I Full Outer Join Figure 142: FULL OUTER Join: Explanation Figure 143: FULL OUTER Join: Example 88 © Copyright. All rights reserved. Lesson: Reading Data from Multiple Tables- Part 1 Figure 144: FULL OUTER Join can be Combined with WHERE Each type of outer join supports joining a table with itself. The following shows the example of a left outer join. Figure 145: LEFT OUTER JOIN Replacing keyword LEFT with RIGHT or FULL in the example works and leads to different result sets. ● ● Using RIGHT OUTER JOIN would lead to a result including each official name in the MANAGER column and with only the names of the official who have a manager assigned in the EMPLOYEE column. USING FULL OUTER JOIN would lead to a result including each official name in the MANAGER column and with each official name in the EMPLOYEE column. Similar to what is supported for CROSS JOINs and INNER JOINs, you can also do the following with OUTER JOINs: ● Use any comparison operator in the join condition ● Join more than two tables, and join a table with itself. © Copyright. All rights reserved. 89 Unit 4: Data From Multiple Tables Part I ● ● Rename columns ● Eliminate duplicates using distinct ● Order the result set, use aggregate expressions, a GROUP BY clause, a HAVING clause ● 90 Project the result set to a subset of columns. You do not have to include a column of every table involved in the projection list. Use table aliases and reference them in the projection list and join condition. You have to qualify column names with the table names or use aliases if the column names are not unique. © Copyright. All rights reserved. Unit 4 Exercise 3 91 Use JOIN to Combine Data from Several Tables 1. Which employees work for department "A01" or A04"? Use UNION. 2. Which employees work for which department? Use an implicit JOIN without table alias. Employees without department and departments without employees should not be taken into account. 3. Which employees work for which department? Use an implicit JOIN with table aliases. Employees without department and departments without employees should not be taken into account. 4. Which employees work for which department? Use an explicit JOIN without table aliases. Employees without department and departments without employees should not be taken into account. 5. Which employees work for which department? Use an explicit JOIN with table aliases. Employees without department and departments without employees should not be taken into account. 6. Which employees work for which department? Employees without department should be included, whereas departments without employees should be ignored. Use a LEFT OUTER JOIN with table aliases. 7. Which employees work for which department? Employees without department should be ignored, whereas departments without employees should be included. Use a RIGHT OUTER JOIN with table aliases. 8. Which employees work for which department? Employees without department and departments without employees should be included. Use a FULL OUTER JOIN with table aliases. 9. Which employees work for the Sales department? Use an implicit Join with table aliases. 10. Which employees work for the Sales department? Use an explicit Join with table aliases. 11. Which department has accumulated how many remaining vacation days? Only colleagues born after 03.02.1972 should be taken into account. The output of the number of days should be sorted descending. Only departments that accumulated more than 30 remaining vacation days should be included. © Copyright. All rights reserved. 91 Unit 4 Solution 3 92 Use JOIN to Combine Data from Several Tables 1. Which employees work for department "A01" or A04"? Use UNION. a) SELECT * SELECT * FROM Employee FROM Employee WHERE DepID = 'A01' UNION WHERE DepID = 'A04'; 2. Which employees work for which department? Use an implicit JOIN without table alias. Employees without department and departments without employees should not be taken into account. a) SELECT Employee.Name, Department.Name FROM Employee, Department WHERE Employee.DepID = Department.DepID; 3. Which employees work for which department? Use an implicit JOIN with table aliases. Employees without department and departments without employees should not be taken into account. a) SELECT m.Name, a.Name m.DepID = a.DepID; FROM Employee m, Department a WHERE 4. Which employees work for which department? Use an explicit JOIN without table aliases. Employees without department and departments without employees should not be taken into account. a) SELECT Employee.Name, Department.Name FROM Employee JOIN Department ON Employee.DepID = Department.DepID; 5. Which employees work for which department? Use an explicit JOIN with table aliases. Employees without department and departments without employees should not be taken into account. a) SELECT m.Name, a.Name m.DepID = a.DepID; FROM Employee m JOIN Department a ON 6. Which employees work for which department? Employees without department should be included, whereas departments without employees should be ignored. Use a LEFT OUTER JOIN with table aliases. a) SELECT m.Name, a.Name FROM Employee m LEFT OUTER JOIN Department a ON m.DepID = a.DepID; 7. Which employees work for which department? Employees without department should be ignored, whereas departments without employees should be included. Use a RIGHT OUTER JOIN with table aliases. a) SELECT m.Name, a.Name FROM Employee m RIGHT OUTER JOIN Department a ON m.DepID = a.DepID; 8. Which employees work for which department? Employees without department and departments without employees should be included. Use a FULL OUTER JOIN with table aliases. 92 © Copyright. All rights reserved. Lesson: Reading Data from Multiple Tables- Part 1 a) SELECT m.Name, a.Name FROM Employee m FULL OUTER JOIN Department a ON m.DepID = a.DepID; 9. Which employees work for the Sales department? Use an implicit Join with table aliases. a) SELECT m.* FROM Employee m, Department a a.DepID AND a.Name = 'Sales'; WHERE m.DepID = 10. Which employees work for the Sales department? Use an explicit Join with table aliases. a) SELECT m.* FROM Employee m JOIN Department a ON m.DepID = a.DepID WHERE a.Name = 'Sales'; 11. Which department has accumulated how many remaining vacation days? Only colleagues born after 03.02.1972 should be taken into account. The output of the number of days should be sorted descending. Only departments that accumulated more than 30 remaining vacation days should be included. a) SELECT a.Name, SUM(m.RemainderDays) AS DepartmentsVacation FROM Employee m JOIN Department a ON m.DepID = a.DepID WHERE m.DateOfBirth > '1972-02-03' GROUP BY a.Name, a.DepID HAVING SUM(m.RemainderDays) > 30 ORDER BY DepartmentsVacation DESC; © Copyright. All rights reserved. 93 Unit 4: Data From Multiple Tables Part I LESSON SUMMARY You should now be able to: ● ● 94 Combine the result of several select statements using the UNION, INTERSECT and EXCEPT statements List the various types of JOIN constructs and use the appropriate JOIN construct to combine data from several tables using a single query © Copyright. All rights reserved. UNIT 5 Data From Multiple Tables Part II Lesson 1 Reading Data from Multiple Tables- Part 2 Exercise 4: Use Nested Queries 96 107 UNIT OBJECTIVES ● Explain the difference between uncorrelated and correlated sub-queries and use both to query data from multiple tables in a single select statement © Copyright. All rights reserved. 95 Unit 5 Lesson 1 96 Reading Data from Multiple Tables- Part 2 LESSON OVERVIEW This lesson covers how sub-queries can be used to retrieve data from several tables in a single statement. LESSON OBJECTIVES After completing this lesson, you will be able to: ● Explain the difference between uncorrelated and correlated sub-queries and use both to query data from multiple tables in a single select statement Sub Queries — Overview ● ● In addition to UNION and JOIN, sub queries or nested queries also provide the option of reading from multiple tables and views. Nested Queries contain a so called “Sub Query”. Figure 146: Sub Query: Explanation A sub query can be useful to achieve the following: ● ● ● 96 To make a SELECT statement more “readable”. To improve the performance of a SELECT statement – however, with a “perfect” optimizer, this should not be the case. To formulate a SELECT statement which cannot be formulated (or only extremely cumbersome) without a sub query - which can be particularly the case for aggregate expressions. © Copyright. All rights reserved. Lesson: Reading Data from Multiple Tables- Part 2 Figure 147: Uncorrelated Versus Correlated Sub Query Uncorrelated Sub Queries Figure 148: Frequent Case: Combining IN with a Sub Query © Copyright. All rights reserved. 97 Unit 5: Data From Multiple Tables Part II Figure 149: The ANY Expression Figure 150: Uses of the ANY Expression Figure 151: Example for > ANY 98 © Copyright. All rights reserved. Lesson: Reading Data from Multiple Tables- Part 2 Figure 152: Special Case: Single-Row Sub Query Figure 153: The ALL Expression © Copyright. All rights reserved. 99 Unit 5: Data From Multiple Tables Part II Figure 154: <= ALL Example Figure 155: Special Case: Single-Row Sub Query 100 © Copyright. All rights reserved. Lesson: Reading Data from Multiple Tables- Part 2 Figure 156: Sub Queries can use Functions Figure 157: Sub Query with a Multi-Column Result Set © Copyright. All rights reserved. 101 Unit 5: Data From Multiple Tables Part II Figure 158: Sub Queries can be Nested Figure 159: Sub Queries can be Combined with JOIN Figure 160: Sub Queries are not Limited to the WHERE Clause 102 © Copyright. All rights reserved. Lesson: Reading Data from Multiple Tables- Part 2 Figure 161: Sub Query in the SELECT Clause Correlated Sub Queries Figure 162: Correlated Sub Query with EXISTS © Copyright. All rights reserved. 103 Unit 5: Data From Multiple Tables Part II Figure 163: Table Aliases can be Optional Figure 164: Correlated Sub Query with NOT EXISTS Figure 165: Correlated Sub Query using Same Table 104 © Copyright. All rights reserved. Lesson: Reading Data from Multiple Tables- Part 2 Figure 166: Correlated Sub Query with >= ALL Figure 167: Correlated Sub Queries can be Nested Figure 168: Correlated Sub Query in the SELECT Clause Using nested queries you can also perform the following: © Copyright. All rights reserved. 105 Unit 5: Data From Multiple Tables Part II 106 ● Restrict the projection list to certain columns ● Explicitly rename the result columns ● Sort the query result ● Use grouping ● Include aggregate expressions ● Eliminate duplicates using DISTINCT ● Involve the same table multiple times ● Involve more than two tables © Copyright. All rights reserved. Unit 5 Exercise 4 107 Use Nested Queries 1. Which employees work for the sales department? Use an uncorrelated sub query. 2. Which employees work for the sales department? Use a correlated sub query. 3. Which employees accumulated the most remaining vacation days? Use an uncorrelated sub query with "MAX". 4. Which employees accumulated the most remaining vacation days? Use an uncorrelated sub query without "MAX". 5. Which employees have above average remaining vacation days? © Copyright. All rights reserved. 107 Unit 5 Solution 4 108 Use Nested Queries 1. Which employees work for the sales department? Use an uncorrelated sub query. a) SELECT * FROM Employee WHERE DepID = ANY (SELECT DepID Department WHERE Name = 'Sales'); FROM 2. Which employees work for the sales department? Use a correlated sub query. a) SELECT m.* FROM Employee m WHERE EXISTS (SELECT * FROM Department a WHERE a.DepID = m.DepID AND a.Name = 'Sales'); 3. Which employees accumulated the most remaining vacation days? Use an uncorrelated sub query with "MAX". a) SELECT * FROM Employee WHERE RemainderDays = (SELECT MAX(RemainderDays) FROM Employee); 4. Which employees accumulated the most remaining vacation days? Use an uncorrelated sub query without "MAX". a) SELECT * FROM Employee WHERE RemainderDays >= ALL (SELECT RemainderDays FROM Employee WHERE RemainderDays IS NOT NULL); 5. Which employees have above average remaining vacation days? a) SELECT * FROM Employee WHERE RemainderDays > (SELECT AVG(RemainderDays) FROM Employee); 108 © Copyright. All rights reserved. Lesson: Reading Data from Multiple Tables- Part 2 LESSON SUMMARY You should now be able to: ● Explain the difference between uncorrelated and correlated sub-queries and use both to query data from multiple tables in a single select statement © Copyright. All rights reserved. 109 Unit 5: Data From Multiple Tables Part II 110 © Copyright. All rights reserved. UNIT 6 NULL Values Lesson 1 Understanding NULL Values Exercise 5: Understand NULL Values 112 117 UNIT OBJECTIVES ● Interpret NULL values in databases and understand why their presence can lead to unexpected query results © Copyright. All rights reserved. 111 Unit 6 Lesson 1 112 Understanding NULL Values LESSON OVERVIEW This lesson explains what NULL values are and why dealing with them correctly is important. LESSON OBJECTIVES After completing this lesson, you will be able to: ● Interpret NULL values in databases and understand why their presence can lead to unexpected query results NULL Values: Interpretation and Origin Interpretation of NULL Values ● A NULL value signals that the corresponding value is unknown or does not exist. ● NULL is a special value reserved by the DBMS. ● - NULL does not equal 0 for numeric data types. - NULL does not equal an empty string or a space character for string based data types. A NULL value can express the following: - - The corresponding value exists in principle, but is unknown temporarily or permanently for the row at hand (such as a birthday of a person). The corresponding value does not exist, for example the owner of an unregistered car. NULL Values can Result in the Following Cases: ● ● When reading rows (SELECT statement) - If the table queried from already contains NULL values - When using outer joins When inserting or updating rows (INSERT and UPDATE statements) - - ● If a NULL value is explicitly specified for a column and NULL values are allowed When adding a column to a table (ALTER TABLE statement) - 112 If no value for a column is provided, NULL values are allowed and no default value is defined If the table has at least one row, NULL values are allowed for the added column and no default value is defined for the added column © Copyright. All rights reserved. Lesson: Understanding NULL Values Figure 169: NULL Values Lead to Trivalent Logic Figure 170: Trivalent Logic Extends Logical Operators. Example: AND © Copyright. All rights reserved. 113 Unit 6: NULL Values Figure 171: What Makes Trivalent Logic Difficult? The introduction of the logical value unknown and the way how expressions are evaluated by first evaluating their atomic sub expressions leads to an unexpected result: Logical expressions that always evaluate to true in usual programming languages and according to common sense no longer evaluate to true in all cases. The following list shows some examples: ● A = A ● (A<5) OR (A>=5) ● 0*A = 0 ● 2*A = A+A ● A OR (NOT A) ● MAX(...) >= ALL (<set of all values>) ● MIN(...) <= ALL (<set of all values>) Figure 172: Do these Queries have the Same Result? 114 © Copyright. All rights reserved. Lesson: Understanding NULL Values Figure 173: The Queries have Different Results! © Copyright. All rights reserved. 115 Unit 6: NULL Values 116 © Copyright. All rights reserved. Unit 6 Exercise 5 117 Understand NULL Values Business Example Your applications using SAP HANA SQL sometimes produce unexpected results. You are tasks with the analysis of the SELECT statements that have been identified as the potential cause of these issues. 1. Analyze the following two queries. Do they lead to the same result? Explain your conclusion. Query 1: SELECT * FROM Official WHERE PNr <> ALL( SELECT PNr FROM Official WHERE Overtime = Overtime) Query 2: SELECT * FROM Official WHERE Overtime IS NULL 2. Analyze the following two queries. Do they lead to the same result? Explain your conclusion. Query 1: SELECT Name, Overtime FROM Official WHERE Overtime >= ALL( SELECT Overtime FROM Official) Query 2: SELECT Name, Overtime FROM Official WHERE Overtime = (SELECT MAX(Overtime) FROM Official) 3. Analyze the following two queries. Do they lead to the same result? Explain your conclusion. Query 1: SELECT Name, Overtime FROM Official WHERE Overtime >= ALL( SELECT Overtime FROM Official WHERE Overtime IS NOT NULL) Query 2: SELECT Name, Overtime FROM Official WHERE Overtime = (SELECT MAX(Overtime) FROM Official) © Copyright. All rights reserved. 117 Unit 6 Solution 5 118 Understand NULL Values Business Example Your applications using SAP HANA SQL sometimes produce unexpected results. You are tasks with the analysis of the SELECT statements that have been identified as the potential cause of these issues. 1. Analyze the following two queries. Do they lead to the same result? Explain your conclusion. Query 1: SELECT * FROM Official WHERE PNr <> ALL( SELECT PNr FROM Official WHERE Overtime = Overtime) Query 2: SELECT * FROM Official WHERE Overtime IS NULL a) The two queries have the same result. b) In the first query, the sub query returns the list of PNrs of all Officials with known Overtime value. The outer query returns the list of Officials whose PNr is not in the sub query result, hence the list of Officials with unknown or NULL-valued Overtime. That’s exactly what the second statement does. 2. Analyze the following two queries. Do they lead to the same result? Explain your conclusion. Query 1: SELECT Name, Overtime FROM Official WHERE Overtime >= ALL( SELECT Overtime FROM Official) Query 2: SELECT Name, Overtime FROM Official WHERE Overtime = (SELECT MAX(Overtime) FROM Official) a) The two queries have different results. b) The first query has an empty result set. No Overtime value is greater or equal to all the values returned by the sub query since the sub query result also includes NULL values. The logical expression in the WHERE clause of the outer query thus never evaluates to true but always to unknown. c) The aggregate expression in the sub query in the second query ignores NULL values, hence the outer query determines the Official with the largest Overtime value. 3. Analyze the following two queries. Do they lead to the same result? Explain your conclusion. 118 © Copyright. All rights reserved. Lesson: Understanding NULL Values Query 1: SELECT Name, Overtime FROM Official WHERE Overtime >= ALL( SELECT Overtime FROM Official WHERE Overtime IS NOT NULL) Query 2: SELECT Name, Overtime FROM Official WHERE Overtime = (SELECT MAX(Overtime) FROM Official) a) The two queries have the same result. b) Since the inner query of the first query now explicitly excludes NULL-valued Overtime values, the outer query determines the Officials with an Overtime value greater or equal to all known values. This is equivalent to determine the officials with the maximum overtime value, hence the second query. Note: The case in which all Overtime values are unknown requires special consideration. In this case, the result of the first query could either be empty or contain all Officials, depending on how the DBMS handles the ALL expression with an empty set. SAP HANA returns an empty result. © Copyright. All rights reserved. 119 Unit 6: NULL Values LESSON SUMMARY You should now be able to: ● 120 Interpret NULL values in databases and understand why their presence can lead to unexpected query results © Copyright. All rights reserved. UNIT 7 Data Stored in Tables Lesson 1 Changing Data Stored in Tables Exercise 6: Insert, Update, and Delete Rows in a Database Table 122 129 UNIT OBJECTIVES ● Add rows to database tables using SQL ● Change existing rows of a database table ● Remove existing rows from a database table © Copyright. All rights reserved. 121 Unit 7 Lesson 1 122 Changing Data Stored in Tables LESSON OVERVIEW This lesson covers how you can add data to a database, change and delete data. LESSON OBJECTIVES After completing this lesson, you will be able to: ● Add rows to database tables using SQL ● Change existing rows of a database table ● Remove existing rows from a database table Modifying Data — Overview In the previous lessons, we have examined how you can read data from a database. Statements to Modify Data — Overview The following list provides an overview of the statements used to modify data: INSERT The INSERT statement adds rows to a database table. UPDATE The UPDATE statement changes values of rows existing in a table. DELETE The DELETE statement permanently removes existing rows from a table. UPSERT The UPSERT changes existing rows of table if found, else inserts new rows. It can be considered as a combination of INSERT and UPDATE. REPLACE is a synonym for UPSERT. Inserting Data The INSERT statement is used to add new rows to a database table. The DBMS checks If the table already contains rows with the same primary key values or if you try to add several rows with the same primary key values, and raises an exception in such a case. You can use three different syntax versions to insert data. The first two options allow inserting single rows only, the last option allows inserting several rows with a single statement. 122 © Copyright. All rights reserved. Lesson: Changing Data Stored in Tables Figure 174: Single Row Insert, Pass Values by Column Order Figure 175: Single Row Insert, Pass Values by Name © Copyright. All rights reserved. 123 Unit 7: Data Stored in Tables Figure 176: Omitting Values on Insert Figure 177: Inserting Multiple Rows Updating Data The fundamental statement to update existing rows of a table is the UPDATE statement. Its basic syntax is UPDATE <table> SET <column> = <value> [, <column> = <value>, ...] [WHERE <condition>] 124 © Copyright. All rights reserved. Lesson: Changing Data Stored in Tables Figure 178: Update Example Note: The WHERE clause is optional. But omitting it updates all rows of the table — which may not be what you intend. In practice, the WHERE clause often specifies the primary key values of the rows to be updated. Note that “missing rows” do not lead to an exception, but only to no row being updated. Figure 179: Updating Several Columns The WHERE clause supports the same features as for the SELECT statement, for example sub queries. © Copyright. All rights reserved. 125 Unit 7: Data Stored in Tables Figure 180: The WHERE Clause Works as Usual You can also use correlated sub-queries, functions etc. in the WHERE clause. Figure 181: Sub Queries also Work in the SET Clause The UPSERT / REPLACE Statement In addition to the INSERT and the UPDATE statements, SAP HANA also supports an UPSERT statement. This statement either updates rows in a table or inserts new rows. UPSERT Statement Syntax Due to its support for inserting data, the UPSERT statement comes in three syntax variants similar to the INSERT syntax variants: UPSERT Table(Column List) VALUES( Value List ) WHERE Condition If the condition evaluates to true, the matching rows are updated. Else inserts a new row with the values provided. UPSERT Table(Column List) VALUES( Value List ) WITH PRIMARY KEY 126 © Copyright. All rights reserved. Lesson: Changing Data Stored in Tables Updates the row with the primary key contained in the value list if present, else Inserts a new row with the values provided. UPSERT Table(Column List) SELECT ... FROM ... WHERE ... For each row returned by the sub query, checks if the table contains a row with the same primary key value. If yes updates the existing row with the row returned by the sub query else inserts a corresponding row. The first variant looks similar to the INSERT statement, with the key word changed to UPSERT and a WHERE clause added. The third variant looks like the INSERT statement in its set variant and behaves mainly the same. If you prefer, you can also use the REPLACE keyword alternatively. UPSERT and REPLACE are truly synonyms. Deleting Data The fundamental statement to delete existing rows of a table is the DELETE statement. Its basic syntax is DELETE FROM <table> [WHERE <condition>] Figure 182: Deleting Selected Rows Note: The WHERE clause is optional. But omitting it deletes all rows of the table — which may not be what you intend. In practice, the WHERE clause often specifies the primary key values of the rows to be updated. Note that “missing rows” do not lead to an exception, but only to no row being updated. The WHERE clause supports the same features as for the SELECT statement, for example sub queries. © Copyright. All rights reserved. 127 Unit 7: Data Stored in Tables Figure 183: The WHERE Clause Works as Usual You can also use correlated sub-queries, functions etc. in the WHERE clause. 128 © Copyright. All rights reserved. Unit 7 Exercise 6 129 Insert, Update, and Delete Rows in a Database Table Business Example You are asked to use fill a new database table STAFF, with the same set of columns as table EMPLOYEE, with data. 1. Copy the data from table "EMPLOYEE" into the (structural equal) table "STAFF". 2. Set the remaining vacation days of "Mr A" of table "STAFF" to 50 days. 3. Reduce the remaining vacation days to 18 for all employees (table "STAFF"), who have more than 18 days. 4. Triple the remaining vacation days of employees (table "STAFF") working for department "Development" or "Test". Use an uncorrelated sub query with "IN". 5. Create a new entry in table "STAFF". 6. "Mr A" is leaving the company. Delete his data from table "STAFF". 7. Delete employees from table "STAFF", where the department information is unknown. 8. The departments "Planning" and "Test" are closed. Delete all employees of those departments from table "STAFF". Use an uncorrelated sub query with "IN". © Copyright. All rights reserved. 129 Unit 7 Solution 6 130 Insert, Update, and Delete Rows in a Database Table Business Example You are asked to use fill a new database table STAFF, with the same set of columns as table EMPLOYEE, with data. 1. Copy the data from table "EMPLOYEE" into the (structural equal) table "STAFF". a) INSERT INTO STAFF SELECT * FROM Employee; 2. Set the remaining vacation days of "Mr A" of table "STAFF" to 50 days. a) UPDATE Staff SET RemainderDays = 50 WHERE Name = 'Mr A'; 3. Reduce the remaining vacation days to 18 for all employees (table "STAFF"), who have more than 18 days. a) UPDATE Staff SET RemainderDays = 18 WHERE RemainderDays > 18; 4. Triple the remaining vacation days of employees (table "STAFF") working for department "Development" or "Test". Use an uncorrelated sub query with "IN". a) UPDATE Staff SET RemainderDays = 3 * RemainderDays WHERE DepID IN (SELECT DepID FROM Department WHERE Name = 'Development' OR Name = 'Test'); 5. Create a new entry in table "STAFF". a) INSERT INTO Staff VALUES ('D200000', 'Mr X', '1977-07-07', 10, 'A01'); 6. "Mr A" is leaving the company. Delete his data from table "STAFF". a) DELETE FROM Staff WHERE Name = 'Mr A'; 7. Delete employees from table "STAFF", where the department information is unknown. a) DELETE FROM Staff WHERE DepID IS NULL; 8. The departments "Planning" and "Test" are closed. Delete all employees of those departments from table "STAFF". Use an uncorrelated sub query with "IN". a) DELETE FROM Staff WHERE DepID IN (SELECT DepID FROM Department WHERE Name = 'Planning' OR Name = 'Test'); 130 © Copyright. All rights reserved. Lesson: Changing Data Stored in Tables LESSON SUMMARY You should now be able to: ● Add rows to database tables using SQL ● Change existing rows of a database table ● Remove existing rows from a database table © Copyright. All rights reserved. 131 Unit 7: Data Stored in Tables 132 © Copyright. All rights reserved. UNIT 8 Data Storage Lesson 1 Defining How Data is Stored Exercise 7: Create, Change, and Delete a Database Table 134 143 UNIT OBJECTIVES ● List the most important data types SAP HANA supports ● Create new database tables in HANA ● Change tables by adding, removing or renaming columns © Copyright. All rights reserved. 133 Unit 8 Lesson 1 134 Defining How Data is Stored LESSON OVERVIEW This lesson covers how you can create and change database tables to define how data is stored. LESSON OBJECTIVES After completing this lesson, you will be able to: ● List the most important data types SAP HANA supports ● Create new database tables in HANA ● Change tables by adding, removing or renaming columns Managing Tables in SAP HANA — Overview Before data can be inserted into database tables, the tables themselves have to be defined and created. Some Options to Manage Database Tables in SAP HANA SAP HANA supports several means for this purpose, some of which are the following: ● Using SQL DDL Statements. This is the only option covered in this course. ● Using a form based editor in the SAP HANA Tools. ● Using Core Data Services and the HANA Development Perspective in the SAP HANA Tools. Note: SQL and the form-based editor provide no support for managing the lifecycle of database tables, for example for keeping track of how a table was defined or changed such that changes can be transported to another system without losing data in the target system. Nevertheless, this course only covers the SQL option. Understanding it can be helpful for prototyping or analyzing issues, for example. SAP HANA SQL Types When managing database tables on the SQL level, it is important to know the data type system provided by SAP HANA on the SQL level. This type system includes the following types. Table 7: Numerical Data Types The following table lists the numerical SQL data types supported by SAP HANA 134 © Copyright. All rights reserved. Lesson: Defining How Data is Stored Data Type Remark Inte- TINYINT gers SMALLINT Real num bers unsigned, 8–bit integer values signed, 16–bit integer values INTEGER signed, 32–bit integer values BIGINT signed, 64–bit integer values DECIMAL(p,s) Fixed-point decimal number with total number of digits p (precision) and s (scale) digits right of the decimal point (fractional digits). SMALLDECIMAL Floating point number with precision varying dynamically between 1 and 16 and scale between –369 and 368, respectively between 1 and 34 and scale between –6111 and 6167 DECIMAL REAL 32–bit floating point number DOUBLE 64–bit floating point number FLOAT(N) 32-bit or 64-bit real number with N significant bits. 1 <= N<= 53. Note: DECIMAL(p,s) is suitable for storing fixed-point decimal numbers with p digits in total and s fractional digits, whereas DECIMAL (without precision and scale) is used to store floating point numbers. Since floating point numbers often cannot be represented exactly in the binary system and are rounded, you should use them with care in database tables. Especially avoid using them in WHERE clauses. The number 1.0000259 has precision 8 and scale 7. The number 3.1415 requires at least precision 5 and scale 4 to be stored without loss of precision. Using DECIMAL(5,4) it is stored as 3.1415, using DECIMAL(8,7) as 3.1415000. Table 8: String-/Character Data Types The following table lists the string—like SQL data types supported by SAP HANA Data Type Remark VARCHAR(n) Variable-length ASCII character string with maximum length n (n ≤ 5,000) NVARCHAR(n) Variable-length unicode character string with maximum length n (n ≤ 5,000) ALPHANUM(n) Variable-length alphanumeric character string with maximum length n (n ≤ 127) SHORTTEXT(n) Variable-length unicode character string based on NVARCHAR(n) with support for text- and string-search features © Copyright. All rights reserved. 135 Unit 8: Data Storage Table 9: Data Types for Date and Time The following table lists the string—like SQL data types supported by SAP HANA Data Type Remark DATE Consists of year, month, day '2012-05-21' TIME Consists of hour, minute, second '18:00:57' SECONDDATE Combination of data and time '2012-05-21 18:00:57' TIMESTAMP Precision: ten millionth of a second '2012-05-21 18:00:57.1234567' Table 10: Data Types for Binary Data and Large Objects The following table lists the string—like SQL data types supported by SAP HANA Data Type Remark Binary Data VARBINARY(n) Binary data, maximum length n Bytes (n ≤ 5,000) Large Objects BLOB Large binary data (maximum 2 GB) CLOB Long ASCII character string (maximum 2 GB) NCLOB Long unicode character string (maximum 2 GB) TEXT Long unicode character string (maximum 2 GB) based on NCLOB with support for text- and string-search features Data type VARBINARY can be used to store short values in binary form, for example UUIDs: INSERT INTO MyTable VALUES( ... TO_BINARY('Walldorf') ...); -- Value stored in DB: 57616C6C646F7266 INSERT INTO MyTable VALUES( ... TO_BINARY(x'00075341500700FF') ...); -- x'..' is a hexadecimal literal value ● ● 136 The term LOB data type (LOB = large object) is used as a generic term for data types such as CLOB (character large object) or BLOB (binary large object). When using LOB data types it is important to note the following: - LOB columns can not be part of the primary key - LOB columns can not be used in the ORDER BY clause - LOB columns can not be used in the GROUP BY clause - LOB columns may not be part of the JOIN condition (explicit JOIN) - LOB columns can not be used as an argument for an aggregate function - LOB columns can not be used in the SELECT DISTINCT clause - LOB columns can not occur in an UNION statement - LOB columns can not be part of a database index © Copyright. All rights reserved. Lesson: Defining How Data is Stored Creating Database Tables In ANSI SQL, the statement to create new database tables is CREATE TABLE ... SAP HANA supports two types of table stores — row store and column store. As a result SAP HANA SQL extends the ANSI syntax to allow controlling whether a new table shall be created in the row store or in the column store. This leads to the following basic syntax: Basic CREATE TABLE syntax CREATE [ROW|COLUMN] TABLE <Table <Column Name> <SQL Data Type> <Column Name> <SQL Data Type> <Column Name> <SQL Data Type> [PRIMARY KEY (<column list>)] ) Name> ( [PRIMAY KEY], [NULL|NOT NULL], [DEFAULT <Default Value>, [UNIQUE (<column list>)] Note: You do not have to specify which table store to use by selecting the keywords ROW or COLUMN. The ROW store is the default for reasons of compatibility, but the COLUMN is the option recommended in most cases. This is why the following examples all include the keyword COLUMN. Figure 184: Defining a Table without a Primary Key Figure 185: Table and Column Names © Copyright. All rights reserved. 137 Unit 8: Data Storage Figure 186: Defining a Simple Primary Key Figure 187: Defining a Composite Primary Key Figure 188: Default Values and Prohibiting NULL Values 138 © Copyright. All rights reserved. Lesson: Defining How Data is Stored Figure 189: Simple Uniqueness Constraints Figure 190: Unique does not Exclude NULL Figure 191: Multiple Simple Uniqueness Constraints © Copyright. All rights reserved. 139 Unit 8: Data Storage Figure 192: Composite Uniqueness Constraints Changing Database Tables Once a table has been defined, you can modify it in certain ways using the ALTER TABLE statement. The statement is always followed by the name of the table to be changed, and an additional keyword to indicate the type of change: ● ALTER TABLE Table Name ● - ADD to add one or more columns - DROP to remove a column (including its data) - ALTER to change properties of columns such as their SQL data type, default value etc. Figure 193: Adding Columns 140 © Copyright. All rights reserved. Lesson: Defining How Data is Stored Figure 194: Removing Columns Figure 195: Changing Column Properties To remove a default value, use ALTER TABLE ... ALTER and set the default value to NULL. Figure 196: Removing the Primary Key Figure 197: Adding a Primary Key Renaming a Column You can also rename an existing table column. This is not achieved using an ALTER TABLE statement, but using a RENAME COLUMN statement. © Copyright. All rights reserved. 141 Unit 8: Data Storage Figure 198: Renaming a Column Renaming a Table You can also rename a database table, using a RENAME TABLE statement. Figure 199: Renaming a Table Removing a Table You can also remove an existing table, including its data. Figure 200: Removing a Table 142 © Copyright. All rights reserved. Unit 8 Exercise 7 143 Create, Change, and Delete a Database Table 1. Create a new column table "Locations" with the following columns: Column Name Data Type Token CHAR length 3 Region NVARCHAR length 20 Country NVARCHAR length 20 City NVARCHAR length 20 Each location is uniquely identified by "Token". When inserting data, "Country" and "Region" can be unknown, but not the "City". If no "Region" is specified during insert the default value "EMEA" should be used. 2. Create a new column table EmployeeSalary with columns. Column Name Data Type DNumber NVARCHAR length 7 FromDate DATE FixedSalary Choose a numerical type that can hold salaries between 1000 and 1 million €, and allows for cent precision (two fractional digits). VariableSalary Choose a numerical type that can hold percentage values with two fractional digits. The combination of DNumber and FromDate uniquely identify a row. Neither FixedSalary nor VariableSalary may be unknown. The VariableSalaray defaults to 10%. To check your definition, insert three rows into the table, each with a salary valid from the current date: ● Employee D100001 with a fixed salary of 2500€ and the default percentage of variable salary. ● Employee D100002 with a fixed salary of 3000€ and 15% variable salary, ● Employee D100003 with a fixed salary of 3200€ and 17% variable salary. 3. Extend the newly created table "Locations" with columns. • "Population" – type INTEGER • "Mayor" – type VARCHAR, length 30 © Copyright. All rights reserved. 143 Unit 8: Data Storage 4. Remove the columns "Country" and "Population" from table "Locations". 5. Change the table definition of "Locations" that column "City" can have NULL values. 6. Delete tables "Locations" and “EmployeeSalary”. 144 © Copyright. All rights reserved. Unit 8 Solution 7 145 Create, Change, and Delete a Database Table 1. Create a new column table "Locations" with the following columns: Column Name Data Type Token CHAR length 3 Region NVARCHAR length 20 Country NVARCHAR length 20 City NVARCHAR length 20 Each location is uniquely identified by "Token". When inserting data, "Country" and "Region" can be unknown, but not the "City". If no "Region" is specified during insert the default value "EMEA" should be used. CREATE a) COLUMN TABLE Locations ( Token CHAR(3) PRIMARY KEY, Region NVARCHAR(20) DEFAULT 'EMEA', Country NVARCHAR(20), City NVARCHAR(20) NOT NULL); 2. Create a new column table EmployeeSalary with columns. Column Name Data Type DNumber NVARCHAR length 7 FromDate DATE FixedSalary Choose a numerical type that can hold salaries between 1000 and 1 million €, and allows for cent precision (two fractional digits). VariableSalary Choose a numerical type that can hold percentage values with two fractional digits. The combination of DNumber and FromDate uniquely identify a row. Neither FixedSalary nor VariableSalary may be unknown. The VariableSalaray defaults to 10%. To check your definition, insert three rows into the table, each with a salary valid from the current date: ● Employee D100001 with a fixed salary of 2500€ and the default percentage of variable salary. ● Employee D100002 with a fixed salary of 3000€ and 15% variable salary, ● Employee D100003 with a fixed salary of 3200€ and 17% variable salary. © Copyright. All rights reserved. 145 Unit 8: Data Storage CREATE a) COLUMN TABLE EmployeeSalary( DNumber NVARCHAR(7), FromDate DATE, FixedSalary DECIMAL(9,2) NOT NULL, VariableSalary DECIMAL(5,2) NOT NULL DEFAULT 5.0, PRIMARY KEY(DNumber,FromDate) ); INSERT INTO VALUES INSERT INTO VALUES INSERT INTO VALUES EmployeeSalary ('D100001', current_date, 2500.0, 5.0 ); EmployeeSalary ('D100002', current_date, 3000.0, 15.0); EmployeeSalary ('D100003', current_date, 3200.0, 17.0); 3. Extend the newly created table "Locations" with columns. • "Population" – type INTEGER • "Mayor" – type VARCHAR, length 30 a) ALTER TABLE LOCATIONS ADD (Population INTEGER, Mayor VARCHAR(30)); 4. Remove the columns "Country" and "Population" from table "Locations". a) ALTER TABLE LOCATIONS DROP (Country, Population); 5. Change the table definition of "Locations" that column "City" can have NULL values. a) ALTER TABLE LOCATIONS ALTER (City VARCHAR(20) NULL); 6. Delete tables "Locations" and “EmployeeSalary”. a) DROP TABLE LOCATIONS; b) DROP TABLE EMPLOYEESALARY; 146 © Copyright. All rights reserved. Lesson: Defining How Data is Stored LESSON SUMMARY You should now be able to: ● List the most important data types SAP HANA supports ● Create new database tables in HANA ● Change tables by adding, removing or renaming columns © Copyright. All rights reserved. 147 Unit 8: Data Storage 148 © Copyright. All rights reserved. UNIT 9 Views For Data Access Lesson 1 Using Views for Data Access Exercise 8: Work with Database Views 150 157 UNIT OBJECTIVES ● Describe the use cases for and advantages of using database views, define database views and use them in queries © Copyright. All rights reserved. 149 Unit 9 Lesson 1 150 Using Views for Data Access LESSON OVERVIEW This lesson covers database views. LESSON OBJECTIVES After completing this lesson, you will be able to: ● Describe the use cases for and advantages of using database views, define database views and use them in queries Database Views — Overview 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. Note: Calculation Views and Core Data Servies 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 Provide the Following Advantages: ● They allow to de-couple applications from lower levels (relative to the three level schema architecture) - ● You can create views tailored to the users (applications) and their needs - ● 150 You can use them to change to the conceptual schema (e.g, add columns to a table) without causing side effects on applications. The application programmers do not have to know the full database structure but only the excerpt that is relevant to them. You can simplify queries © Copyright. All rights reserved. Lesson: Using Views for Data Access - ● Complex queries referencing views are easier to formulate, provided that the view provides an appropriate pre-selection of data. You can use them to limit access to data - You can us views to realize value-dependent access privileges or to make sure certain columns are never visible for . Figure 201: A Sample View: Red Cars Figure 202: Using Views in Queries Managing Database Views You can create database views using the CREATE VIEW statement. Its syntax basically looks as follows: CREATE VIEW <Name of the view> [(<list of column names>)] AS SELECT ... You can use nearly any valid SELECT statement as the SELECT clause for a CREATE VIEW statement. © Copyright. All rights reserved. 151 Unit 9: Views For Data Access Figure 203: CREATE VIEW 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 simplify ● A TOP N clause to limit the number of rows returned ● DISTINCT . Note that a view can contain a projection leading to potential duplicates in result sets ● Aggregate expressions, GROUP BY and HAVING clauses ● Sub queries, uncorrelated or correlated ● UNION ● 152 An ORDER BY clause to achieve a default sort order of results. Note that this order can be overridden when SELECTing from the view, hence the use of ORDER BY is questionable and not recommended © Copyright. All rights reserved. Lesson: Using Views for Data Access Figure 204: Renaming Columns with a View, First Option Figure 205: Renaming Columns with a View, Recommended Option Figure 206: Precedence Rule for Column Naming © Copyright. All rights reserved. 153 Unit 9: Views For Data Access Figure 207: Using Aggregation and Grouping is Possible You can also define views based on other views. Figure 208: View on View Figure 209: Removing a View Modifying Data with a View 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. 154 © Copyright. All rights reserved. Lesson: Using Views for Data Access 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. Figure 210: Inserting through a View Figure 211: Updating through a View © Copyright. All rights reserved. 155 Unit 9: Views For Data Access Figure 212: Deleting through a View Such data modifications through database views are not always possible. You cannot modify data through a view your database view definition contains at least one of the following: 156 ● A calculation or function in the projection list ● A sub query in the projection list ● DISTINCT ● The TOP N clause ● Aggregate expressions ● A GROUP BY clause ● UNION © Copyright. All rights reserved. Unit 9 Exercise 8 157 Work with Database Views 1. Create view "ManagerView". The manager of department "A04" should only view data of his own employees. To avoid discrimination/favoritism the birthday should be excluded. Rename column "RemainderDays" to "VacationDays". 2. Delete view "ManagerView". © Copyright. All rights reserved. 157 Unit 9 Solution 8 158 Work with Database Views 1. Create view "ManagerView". The manager of department "A04" should only view data of his own employees. To avoid discrimination/favoritism the birthday should be excluded. Rename column "RemainderDays" to "VacationDays". CREATE a) VIEW ManagerView AS SELECT DNumber, Name, RemainderDays AS VacationDays FROM Employee WHERE DepID = 'A04'; 2. Delete view "ManagerView". a) DROP VIEW ManagerView; 158 © Copyright. All rights reserved. Lesson: Using Views for Data Access LESSON SUMMARY You should now be able to: ● Describe the use cases for and advantages of using database views, define database views and use them in queries © Copyright. All rights reserved. 159 Unit 9: Views For Data Access 160 © Copyright. All rights reserved. UNIT 10 Functions and Procedures Lesson 1 Creating User-Defined Functions Exercise 9: Create and Call Scalar User-Defined Functions Exercise 10: Create and Call Table User-Defined Functions 162 167 173 Lesson 2 Creating Database Procedures Exercise 11: Create and Use a Database Procedure 178 185 UNIT OBJECTIVES ● Create and use scalar and table user-defined functions ● Create and use database procedures in SAP HANA and know how to debug them © Copyright. All rights reserved. 161 Unit 10 Lesson 1 162 Creating User-Defined Functions LESSON OVERVIEW This lesson introduces SQLScript and explains how to create and use user-defined functions. LESSON OBJECTIVES After completing this lesson, you will be able to: ● Create and use scalar and table user-defined functions SQLScript Basics Applications benefit most from the potential of SAP HANA if they perform as many dataintensive computations in the database as possible. This avoids loading large amounts of data into an application server separate from SAP HANA and to leverage fast column operations, query optimization and parallel execution. This can be achieved to a certain extent if the applications use advanced SQL statement, but sometimes you may want to push more logic into the database than possible using individual SQL statements, or make the logic more readable and maintainable. SQLScript has been introduced to assist with this task. SQL Script — What and Why? ● ● SQL Script is the following: - It is the language to write stored procedures and user-defined functions in SAP HANA - It is an extension of ANSI SQL The main goal is to allow the execution of data intensive calculations inside SAP HANA. This is helpful for the following reasons: - - It allows to eliminate data transfer between database & application tiers It allows to execute calculations in the database layer to benefit from fast column operations, query optimization, and parallel execution SQLScript Advantages Compared to plain SQL, SQLScript provides the following advantages: ● ● 162 Using SQLScript, complex logic can be broken down into smaller chunks of code. This enables modular programming, reuse and a better understandability by functional abstraction. SQL only allows the definition of SQL views to structure complex queries, and SQL views have no parameters. SQLScript supports local variables for intermediate results with implicitly defined types. With standard SQL, it would be required to define globally visible views even for intermediate steps. © Copyright. All rights reserved. Lesson: Creating User-Defined Functions ● ● SQL Script has flow control logic such as if—then—else clauses that are not available in SQL. Stored procedures can returns multiple results, while a SQL query returns only one result set. Scalar Used-Defined Functions The most simple database object for which you can make use of SQLScript are Scalar UserDefined Functions (Scalar UDFs) Scalar UDFs allow you to the define functions which take a number of input parameters and return scalar values. Only expressions are allowed in the body of the UDF, so no table operations, CE functions or array operations. Scalar User-Defined Functions Support the Following: ● ● ● ● ● ● They can have any number of scalar input parameters (primitive SQL types). Input parameters of table type are not supported. They can return multiple scalar values. They can contain expressions within their body. Table and array operations are not supported. They can be used in the field list or the WHERE clause of SELECT statements — like built-in functions. They are callable via direct assignment in other user-defined functions or stored procedures (x := my_scalar_func() ). They must be free of side-effects and do not support any type of SQL statement in their body. User-defined functions can be managed in several ways in SAP HANA. Some Options Used to Define User-Defined Functions in SAP HANA: ● ● Using SQL DDL Statements. This is the only option covered in this course. Using the HANA Development Perspective in the SAP HANA Tools and the native HANA application development model. Note: SQL statements alone provide no support for managing the lifecycle of functions, e.g. for adjusting the function code of an existing function. Nevertheless, this course only covers the SQL option. This is sufficient to get familiar with the SQLScript syntax and for prototyping or analyzing issues. The SQL statement to define user-defined functions is CREATE FUNCTION. The basic syntax to define a scalar user-defined function looks as follows: Basic Syntax to Define a Scalar User-Defined Function CREATE FUNCTION <function name> (<list of input parameters with type>) RETURNS <scalar result parameter name and type> © Copyright. All rights reserved. 163 Unit 10: Functions and Procedures AS BEGIN <function body> END; Figure 213: A Simple Scalar UDF User-defined functions — both scalar and table — can also be deleted using an SQL statement. The sytnax is as simple as follows: Deleting User-Defined Functions DROP FUNCTION <function name>; Note: The only way to change the body of an existing user-defined function using SQL statements is to delete the function and re-create it. You can also use imperative logic in scalar user-defined functions, to the extent this does not conflict with the statements above. Imperative language constructs allow the developer to control data and control flow, for example loops, scalar variables and IF-THEN-ELSE statements. Imperative Logic in SQLScript ● ● 164 Imperative logic allows you to control the flow of the logic - Scalar variable manipulation - Branching logic, for example using IF-THEN-ELSE - Loops — WHILE and FOR - DDL statements and INSERT, UPDATE and DELETE statements Imperative logic is executed exactly as scripted and procedural. It prevents parallel processing. © Copyright. All rights reserved. Lesson: Creating User-Defined Functions Note: DDL and DML are not supported in scalar user-defined functions anyway. They can be used in table user-defined functions and database procedures. Figure 214: Imperative Logic in a Scalar UDF You can find the code shown in the figure in file HA150/02_SCALAR_UDF_CONVERT_HOURS.sql in the instructor folder to be able to easily demo it. Vary the second parameter in the SELECT statement to show how an invalid value leads to unexpected results. You can also raise exceptions using SQLScript, by using DECLARE ... CONDITION at the beginning of the SQLScript code to declare a named exception condition and by using SIGNAL to raise the exception in the code. This looks as follows in a scalar user-defined function. © Copyright. All rights reserved. 165 Unit 10: Functions and Procedures Figure 215: Raising Exceptions in SQLScript You can find the code shown in the figure in file HA150/02_SCALAR_UDF_CONVERT_HOURS.sql in the instructor folder to be able to easily demo it. 166 © Copyright. All rights reserved. Unit 10 Exercise 9 167 Create and Call Scalar User-Defined Functions Business Example The monetary value of the remaining vacation days of the employees is expected to be needed in several to-be developed queries and views. To avoid having to repeat the conversion logic each time, you are asked to create a scalar user-defined function that can be used in all queries and views for the conversion. 1. Create a scalar user-defined function named Remainderdays_Value. The function should take an integer-valued number of remaining vacation days as input and convert it into a monetary value (type DEC(15,2)), assuming each vacation day is worth €300. Test the function by retrieving the full list of employees with their DNumber and the value of the remaining working days. 2. You are told that the conversion factor from vacation days into their monetary value in fact depends on the department an employee works for. Extend your UDF to also take the Department ID as input and to consider the following conversion factors, using a simple IF-THEN-ELSE clause: Department Conversion Factor A01 350 A02 310 A03 290 A04 325 NULL (unknown) NULL All others 300 © Copyright. All rights reserved. 167 Unit 10 Solution 9 168 Create and Call Scalar User-Defined Functions Business Example The monetary value of the remaining vacation days of the employees is expected to be needed in several to-be developed queries and views. To avoid having to repeat the conversion logic each time, you are asked to create a scalar user-defined function that can be used in all queries and views for the conversion. 1. Create a scalar user-defined function named Remainderdays_Value. The function should take an integer-valued number of remaining vacation days as input and convert it into a monetary value (type DEC(15,2)), assuming each vacation day is worth €300. Test the function by retrieving the full list of employees with their DNumber and the value of the remaining working days. a) A possible definition of the user-defined function is as follows: CREATE FUNCTION Remainderdays_Value(im_remainderdays INTEGER) RETURNS ex_result DEC(15,2) AS BEGIN ex_result := :im_remainderdays * 300; END; b) The following statement can be used to test the scalar UDF: SELECT DNumber, Remainderdays_Value(Remainderdays) FROM Employee; 2. You are told that the conversion factor from vacation days into their monetary value in fact depends on the department an employee works for. Extend your UDF to also take the Department ID as input and to consider the following conversion factors, using a simple IF-THEN-ELSE clause: Department Conversion Factor A01 350 A02 310 A03 290 A04 325 NULL (unknown) NULL All others 300 a) Since you cannot alter the existing function, delete it first using the following statement. DROP FUNCTION Remainderdays_Value; 168 © Copyright. All rights reserved. Lesson: Creating User-Defined Functions b) A possible definition of the user-defined function is as follows: CREATE FUNCTION Remainderdays_Value(im_remainderdays INTEGER, im_depid NVARCHAR(3)) RETURNS ex_result DEC(15,2) AS BEGIN IF :im_depid = 'A01' THEN -- Planning ex_result := :im_remainderdays * 350; ELSEIF :im_depid = 'A02' THEN -- Development ex_result := :im_remainderdays * 310; ELSEIF :im_depid = 'A03' THEN -- Test ex_result := :im_remainderdays * 290; ELSEIF :im_depid = 'A04' THEN -- Sales ex_result := :im_remainderdays * 325; ELSEIF :im_depid IS NULL THEN ex_result := NULL; ELSE ex_result := :im_remainderdays * 300; END IF; END; c) The following statement can be used to test the scalar UDF: SELECT DNumber, Remainderdays_Value(Remainderdays, DepID) FROM Employee; © Copyright. All rights reserved. 169 Unit 10: Functions and Procedures Table User-Defined Functions In addition to scalar user-defined functions, SAP HANA supports table user-defined functions. Table User-defined Functions Support the Following: ● They can have any number of input parameters ● They return exactly one table ● Table operations are allowed within the body ● They are used in the FROM clause of SELECT statements ● They must be free of side-effects, i.e. DDL statements or the DML statements INSERT, UPDATE and DELETE cannot be used in function bodies The SQL statement to define table user-defined functions also is CREATE FUNCTION. The difference to the definition of a scalar user-defined function is keyword TABLE for the return parameter and how the value of the sole return parameter is assigned using keyword RETURN: Basic Syntax to Define a Table User-Defined Function CREATE FUNCTION <function name> (<list of input parameters with type>) RETURNS TABLE [table type|(<list of table column definitions>)] AS BEGIN <function body> RETURN <expression to set return table> END; A feature that can be helpful in the context of defining table user-defined functions or stored procedures is dynamic filtering using built-in table function APPLY_FILTER. This function allows you to apply a dynamic WHERE clause to a database table or table variable and assign the result to a table variable. Figure 216: A Table User-Defined Functions Using Dynamic Filtering 170 © Copyright. All rights reserved. Lesson: Creating User-Defined Functions You can find the code shown in the figure in file HA150/03_TABLE_UDF_CONVERT_OFFICIAL.sql in the instructor folder to be able to easily demo it. Exceptions can be raised the same way as in scalar user-defined functions. Exceptions can also be handled, using DECLARE EXIT HANDLER to define a code block to be executed in case a certain exception is encountered. RESIGNAL allows to raise a handled exception again, using an error code in the range for user-defined error codes. You can also nest BEGIN-END blocks for more advanced exception handling, for example to make sure values are assigned to variables even in case of an exception, as shown in figure “Handling exceptions in SQLScript” Figure 217: Handling Exceptions in SQLScript You can find the code shown in the figure in file HA150/03_TABLE_UDF_CONVERT_OFFICIAL.sql in the instructor folder to be able to easily demo it. SQLScript supports the notion of array as an indexed collection of elements of a single data type. With Arrays in SQLScript, You Can Do the Following: ● Set and access elements by index using <array>[<index>] ● Add and remove elements from the ends of an array ● Concatenate two arrays using CONCAT or || ● Convert arrays into a table using UNNEST ● Convert a table column into an array using ARRAY_AGG © Copyright. All rights reserved. 171 Unit 10: Functions and Procedures ● Determine the length of the array using CARDINALITY An example showing how this can be used in a table user-defined function is as follows. Figure 218: Arrays in SQLScript You can find the code shown in the figure in file HA150/03_TABLE_UDF_CONVERT_OFFICIAL.sql in the instructor folder to be able to easily demo it. 172 © Copyright. All rights reserved. Unit 10 Exercise 10 173 Create and Call Table User-Defined Functions Business Example You are asked to also create a table user-defined function that returns the list of employees, filtered by caller-specified criteria, together with the monetary value of their remaining vacation days. 1. Create a table user-defined function named EmployeeRemainderDays_Value. The function shall take a VARCHAR(1000) parameter as input, pass it to function APPLY_FILTER and return a filtered list of employees with the monetary value of their remaining vacation days. Reuse your scalar UDF to calculate the monetary value. The return table should have the following columns: Column Type DNumber NVARCHAR(7) DepID NVARCHAR(3) RemainderDays_Value DEC(15,2) 2. Test your table UDF with the following two values for the input filter parameter: an empty string, and a string such that only employees of department A02 are returned. 3. Extend your function such that it handles the SQL exceptions 257 and 260, caused by invalid values passed for the filter parameter. 4. Test your UDF again using the test queries you used before. Also test that an incorrect filter parameter no longer leads to an exception, e.g. parameter value 'DepID=A02'. 5. Replace the call to your scalar UDF by introducing a local table with the departmentspecific conversion rates from vacation days to monetary value. 6. Delete your table and your scalar UDF again. © Copyright. All rights reserved. 173 Unit 10 Solution 10 174 Create and Call Table User-Defined Functions Business Example You are asked to also create a table user-defined function that returns the list of employees, filtered by caller-specified criteria, together with the monetary value of their remaining vacation days. 1. Create a table user-defined function named EmployeeRemainderDays_Value. The function shall take a VARCHAR(1000) parameter as input, pass it to function APPLY_FILTER and return a filtered list of employees with the monetary value of their remaining vacation days. Reuse your scalar UDF to calculate the monetary value. The return table should have the following columns: Column Type DNumber NVARCHAR(7) DepID NVARCHAR(3) RemainderDays_Value DEC(15,2) a) A possible definition of the user-defined function is as follows: CREATE FUNCTION EmployeeRemainderDays_Value(im_filter VARCHAR(1000) ) RETURNS TABLE ( DNumber nvarchar(7), DepID nvarchar(3), RemainderDays_Value dec(15,2)) AS BEGIN lt_employee = APPLY_FILTER("EMPLOYEE",:im_filter); RETURN SELECT DNumber, DepID, RemainderDays_Value(RemainderDays,DepID) AS RemainderDays_Value FROM :lt_employee; END; 2. Test your table UDF with the following two values for the input filter parameter: an empty string, and a string such that only employees of department A02 are returned. -- no a) filter - using empty string as filter value: SELECT * FROM EmployeeRemainderDays_Value(''); -- filter to employees of department A02 - note the single quotes SELECT * FROM EmployeeRemainderDays_Value('DepID=''A02'''); -- SELECT * FROM EmployeeRemainderDays_Value('DepID=A02'); -- results in error 3. Extend your function such that it handles the SQL exceptions 257 and 260, caused by invalid values passed for the filter parameter. 174 © Copyright. All rights reserved. Lesson: Creating User-Defined Functions a) Since the function code cannot be altered, delete the function first. DROP FUNCTION EmployeeRemainderDays_Value; b) A possible definition of the user-defined function is as follows: CREATE FUNCTION EmployeeRemainderDays_Value(im_filter VARCHAR(1000) ) RETURNS TABLE ( DNumber nvarchar(7), DepID nvarchar(3), remainderdays_value dec(15,2)) AS BEGIN DECLARE lv_filter NVARCHAR(1000) := :im_filter; -- use a block to make sure the RETURN statement is executed -- even in case of an exception BEGIN DECLARE EXIT HANDLER FOR SQL_ERROR_CODE 257, SQL_ERROR_CODE 260 -- or SQLEXCEPTION BEGIN lv_filter := ''; -- APPLY_FILTER needs a scalar variable as second argument, -- a literal does not work lt_employee = APPLY_FILTER(EMPLOYEE,:lv_filter); END; lt_employee = APPLY_FILTER(EMPLOYEE,:lv_filter); END; RETURN SELECT DNumber, DepID, RemainderDays_Value(RemainderDays,DepID) AS remainderdays_value FROM :lt_employee; END; 4. Test your UDF again using the test queries you used before. Also test that an incorrect filter parameter no longer leads to an exception, e.g. parameter value 'DepID=A02'. -- no a) filter - using empty string as filter value: SELECT * FROM EmployeeRemainderDays_Value(''); -- filter to employees of department A02 - note the single quotes SELECT * FROM EmployeeRemainderDays_Value('DepID=''A02'''); -- incorrect filter value leads to no error (and no filtering) SELECT * FROM EmployeeRemainderDays_Value('DepID=A02'); 5. Replace the call to your scalar UDF by introducing a local table with the departmentspecific conversion rates from vacation days to monetary value. a) A possible definition of the user-defined function is as follows: DROP FUNCTION EmployeeRemainderDays_Value; CREATE FUNCTION EmployeeRemainderDays_Value(im_filter VARCHAR(1000) ) RETURNS TABLE ( DNumber nvarchar(7), DepID nvarchar(3), remainderdays_value dec(15,2)) AS BEGIN DECLARE lv_filter NVARCHAR(1000) := :im_filter; -- construct table for "conversion rate" DECLARE depid NVARCHAR(3) ARRAY; DECLARE worth DEC(5,2) ARRAY; depid[1] := 'A01'; worth[1] := 350; depid[2] := 'A02'; worth[2] := 310; depid[3] := 'A03'; worth[3] := 290; depid[4] := 'A04'; worth[4] := 325; lt_conversion = UNNEST(:depid,:worth) as (DepID,Worth); © Copyright. All rights reserved. 175 Unit 10: Functions and Procedures -- use a block to make sure the RETURN statement is executed -- even in case of an exception BEGIN DECLARE EXIT HANDLER FOR SQL_ERROR_CODE 257, SQL_ERROR_CODE 260 -- or SQLEXCEPTION BEGIN lv_filter := ''; -- APPLY_FILTER needs a scalar variable as second argument, -- a literal does not work lt_employee = APPLY_FILTER(EMPLOYEE,:lv_filter); END; lt_employee = APPLY_FILTER(EMPLOYEE,:lv_filter); END; RETURN SELECT DNumber, e.DepID, RemainderDays*Worth AS remainderdays_value FROM :lt_employee AS e LEFT OUTER JOIN :lt_conversion AS c ON e.depid = c.depid; END; 6. Delete your table and your scalar UDF again. DROP a) FUNCTION EmployeeRemainderDays_Value; DROP FUNCTION Remainderdays_Value; 176 © Copyright. All rights reserved. Lesson: Creating User-Defined Functions LESSON SUMMARY You should now be able to: ● Create and use scalar and table user-defined functions © Copyright. All rights reserved. 177 Unit 10 Lesson 2 178 Creating Database Procedures LESSON OVERVIEW This lessons expands SQLScript and explains how to create and call stored procedures in SAP HANA. LESSON OBJECTIVES After completing this lesson, you will be able to: ● Create and use database procedures in SAP HANA and know how to debug them Database Procedures The most flexible mechanism to implement data-intensive calculations in SAP HANA and for which SQLScript can be used are database procedures. Database procedures can be managed in several ways in SAP HANA. Some Options to Define Database Procedures in SAP HANA ● ● Using SQL DDL Statements. This is the only option covered in this course. Using the HANA Development Perspective in the SAP HANA Tools and the native HANA application development model. Note: SQL statements alone provide no support for managing the lifecycle of procedures, e.g. for adjusting the code of an existing procedure. Nevertheless, this course only covers the SQL option. This is sufficient to get familiar with the SQLScript syntax and for prototyping or analyzing issues, The SQL statement to define database procedures is CREATE PROCEDURE. The following example illustrates the basic syntax: 178 © Copyright. All rights reserved. Lesson: Creating Database Procedures Figure 219: Input and Output Parameters In Procedures Caution: User-defined functions and database procedure share a common namespace. This means that you cannot have a user-defined function and a database procedure with the same name. Database procedures can have several output parameters, and a mix of parameters of scalar and table types is possible. They can also have input parameters and parameters which are both input and output. This is why the keywords IN, OUT and INOUT are necessary in the definition of the signature to indicate the kind of parameter. IN is the default. Several optional clauses can be used in the procedure header. The most important ones are the following: Elements of a Procedure Header ● ● ● ● LANGUAGE specifies which programming language the procedure is implemented in. SQLScript is the default. SQL SECURITY specifies which user’s privileges apply when the procedure is executed. Default is DEFINER. DEFAULT SCHEMA specifies the database schema to be used for unqualified database object accesses within the procedure body. READS SQL DATA specifies that the procedure is free of side effects, i.e. does not modify data in the database. READS SQL DATA prevents the use of constructs in the procedure body that could lead to side effects, for example INSERT statements or dynamic SQL using EXEC. © Copyright. All rights reserved. 179 Unit 10: Functions and Procedures Using declarative logic only — which implies side-effect free programming — has a very big advantage: Declarative Logic in SQLScript ● The procedure developer defines the what data to select using SELECT or CE functions. ● The database system determines the how and executes accordingly. ● Data selection using declarative statements can be parallelized massively. Figure 220: A Procedure Using Declarative Logic Only You can find the code shown in the figure in file HA150/04_DBPROC_DEMO.sql in the instructor folder to be able to easily demo it. The fact that the procedure uses declarative logic only allows executing the first three queries and assignments in parallel, because their results are mutually independent. Recap: Imperative Logic in SQLScript ● ● Imperative logic allows you to control the flow of the logic - Scalar variable manipulation - Branching logic, for example using IF-THEN-ELSE - Loops — WHILE and FOR - DDL statements and INSERT, UPDATE and DELETE statements Imperative logic is executed exactly as scripted, which is procedural, and prevents parallel processing. Database procedures can be deleted using an SQL statement. 180 © Copyright. All rights reserved. Lesson: Creating Database Procedures Deleting User-Defined Functions The sytnax is as simple as follows: DROP PROCEDURE <procedure name>; Note: The only way to change the body of an existing procedure using SQL statements is to delete the procedure and re-create it. You can find the code shown in the figure in file HA150/04_DBPROC_DEMO.sql in the instructor folder to be able to easily demo it. The fact that procedures can have table and scalar output parameters can be used to return more readable error messages in case of exceptions. The following illustrates an example and how the original SQL error code and text can be accessed using built-in variables :SQL_ERROR_CODE and :SQL_ERROR_MESSAGE (note the additional colon). Figure 221: Using SQL Errors In Exceptions You can find the code shown in the figure in file HA150/04_DBPROC_DEMO.sql in the instructor folder to be able to easily demo it. To call a procedure, use the CALL statement. There are different options depending on where you call the procedure and for passing parameters. © Copyright. All rights reserved. 181 Unit 10: Functions and Procedures Figure 222: Calling Procedures Arguments for IN parameters of table type can either be physical tables, views or table variables. The actual value passed for tabular OUT parameters must be ‘?’ when calling a procedure in the SQL Console. Calling a procedure WITH OVERVIEW will return one result set that holds the information of which table contains the result of a particular table output parameter. Scalar outputs are represented as temporary tables with only one cell. When you pass existing tables to the output parameters WITH OVERVIEW will insert the result set tuples of the procedure into the provided tables. When you pass '?' to the output parameters, temporary tables holding the result sets will be generated. These tables will be dropped automatically once the database session is closed. Debugging Database Procedures Database procedures can be debugged using the SAP HANA Tools. Since the recommended way for developing database procedures is to use the native SAP HANA application development model using among others repository packages and the SAP HANA development perspective in the SAP HANA tools, debugging procedures requires using this approach to a limited extent. 1. In the SAP HANA tools / SAP HANA Studio, open the SAP HANA development perspective. You can use menu item Window → Open Perspective ... → Other ... or press the Open Perspective button in the toolbar. Choose SAP HANA Development in either case. 2. In the Project Explorer in the navigation frame on the left, open the context menu and choose New ,,, → Other .... In the dialog window, open folder SAP HANA → Application Development, choose item XS Project and press Next. 3. Enter a Project Name, e.g. debug and press Next. 4. In step Share Project, press Add Workspace, choose your SAP HANA System (connection) and press Finish. 182 © Copyright. All rights reserved. Lesson: Creating Database Procedures 5. Back in step Share Project, unflag Add Project Folder as Subpackage. 6. Press Browse ..., choose a repository package, press OK and then Next. 7. In step Create Objects, unflag all checkboxes in the Acces Objects frame and leave the fields in frame Common Objects empty. This is important to avoid creating unwanted repository content. 8. Press Finish. You now have a project in the Project Explorer. 9. Open the project, sub-folder SAP HANA System Library → Catalog to locate the procedure you want to debug in your database schema. 10. Double-click the procedure to open it in an editor window. 11. In the left-most column of the editor window, double-click on the lines where you want to set break points. 12. Open the Debug perspective. You can use menu item Window → Open Perspective ... → Other ... or press the Open Perspective button in the toolbar. Choose Debug in either case. 13. Open the menu of the Debug button in the toolbar and choose Debug Configurations ... 14. Choose SAP HANA Stored Procedures, open the context menu and choose New. 15. Enter a name for the Debug Configuration. 16. Choose option Procedure to Debug: and Catalog Schemas in the corresponding dropdown list. 17. Press the Browse ... button, navigate to your database schema and the procedure you want to debug, choose it and press OK. 18. Press Debug. The system starts a debugging session and calls the procedure chose in the debug configuration. Execution stops at the first breakpoint. You can resume execution until the next breakpoint, terminate the debugging session, inspect the variable values, add break points, and more. Note: Make sure you always finish the execution of the procedure or explicitly terminate the debugging session when done. Dangling debugging sessions consume considerable resources, also on the server side. Debugging Database Procedures Demonstrate the procedure for debugging database procedures, using the Convert_OfficialsHours procedure. 1. In case you haven’t done so yet, create the procedure Convert_OfficialsHours as shown on figures “Input and Output Parameters In Procedures” and “Using SQL Errors In © Copyright. All rights reserved. 183 Unit 10: Functions and Procedures Exceptions”. You can find the code in file HA150/04_DBPROC_DEMO.sql in the instructor folder, or use the following code. CREATE PROCEDURE Convert_OfficialsHours( IN im_filter VARCHAR(1000), IN im_to VARCHAR(1), OUT ex_official TABLE (PNr nvarchar(3), Name nvarchar(20), Overtime dec(5,2)), OUT ex_message NVARCHAR(200)) LANGUAGE SQLSCRIPT SQL SECURITY INVOKER READS SQL DATA AS BEGIN DECLARE lv_filter NVARCHAR(1000) := :im_filter; -- use a block to make sure the RETURN statement is executed even in case of an exception BEGIN DECLARE EXIT HANDLER FOR SQL_ERROR_CODE 257, SQL_ERROR_CODE 260 -or SQLEXCEPTION BEGIN lv_filter := ''; -- APPLY_FILTER needs a scalar variable as second argument, a literal does not work lt_official = APPLY_FILTER(Official,:lv_filter); ex_message := 'Result not filtered because of SQL ERROR ' || ::SQL_ERROR_CODE || ' ' || ::SQL_ERROR_MESSAGE; END; lt_official = APPLY_FILTER(Official,:lv_filter); END; ex_official = SELECT PNr, Name, Convert_Hours(Overtime,:im_to) as Overtime FROM :lt_official; END; 2. To avoid having to enter input parameter values when the procedure is called, first create a very simple wrapper. The wrapper also illustrates calling a procedure within another procedure. CREATE PROCEDURE tester() AS BEGIN DECLARE lv_message NVARCHAR(200); CALL Convert_OfficialsHours( im_filter=>NULL, im_to=>'d', ex_official=>lt_official, ex_message=>lv_message ); END; 3. Follow the procedure as detailed above. You can use repository package workshop.exercises in the Share Project step. Make sure you unflag Add Project Folder as Subpackage in the same step. 184 © Copyright. All rights reserved. Unit 10 Exercise 11 185 Create and Use a Database Procedure Business Example You are asked to implement a conversion from the employees’ remaining vacation time to monetary value, this time as a database procedure to allow for flexible access to the logic. 1. Create a database procedure named EmployeeRemainderDays_Value. The procedure shall take a VARCHAR(1000) parameter as input and use function APPLY_FILTER to only work on a subset of employee records. The procedure shall return the filtered list of employees with the monetary value of their remaining vacation days. The return table should have the following columns: Column Type DNumber NVARCHAR(7) DepID NVARCHAR(3) RemainderDays_Value DEC(15,2) 2. Call your procedure with the following two values for the input filter parameter: an empty string, and a string such that only employees of department A02 are returned. Also test that an exception caused by an incorrect filter parameter value is caught. 3. Extend the procedure such that it returns a parameter of type NVARCHAR(200) in addition. The parameter shall contain an error message in case the procedure encounters an SQL error 257 or 260. The error message shall contain the SQL error code and error message. 4. Test your procedure again using the test queries you used before. © Copyright. All rights reserved. 185 Unit 10 Solution 11 186 Create and Use a Database Procedure Business Example You are asked to implement a conversion from the employees’ remaining vacation time to monetary value, this time as a database procedure to allow for flexible access to the logic. 1. Create a database procedure named EmployeeRemainderDays_Value. The procedure shall take a VARCHAR(1000) parameter as input and use function APPLY_FILTER to only work on a subset of employee records. The procedure shall return the filtered list of employees with the monetary value of their remaining vacation days. The return table should have the following columns: Column Type DNumber NVARCHAR(7) DepID NVARCHAR(3) RemainderDays_Value DEC(15,2) a) Note that the procedure looks quite similar to the table user-defined function from an earlier exercise. Since the procedure shall have the same name as the earlier function, first delete the function in case you have not done so yet. DROP FUNCTION EmployeeRemainderDays_Value; b) You can largely re-use the CREATE statement of the table function, replacing the keyword FUNCTION with PROCEDURE, adjusting the definition of the signature and how the output parameter value is determined. A possible definition of the procedure is as follows: CREATE PROCEDURE EmployeeRemainderDays_Value( in im_filter VARCHAR(1000), out ex_employee TABLE ( DNumber nvarchar(7), DepID nvarchar(3), remainderdays_value dec(15,2)) ) AS BEGIN DECLARE lv_filter NVARCHAR(1000) := :im_filter; -- construct table for "conversion rate" DECLARE depid NVARCHAR(3) ARRAY; DECLARE worth DEC(5,2) ARRAY; depid[1] := 'A01'; worth[1] := 350; depid[2] := 'A02'; worth[2] := 310; depid[3] := 'A03'; worth[3] := 290; depid[4] := 'A04'; worth[4] := 325; lt_conversion = UNNEST(:depid,:worth) as (DepID,Worth); -- use a block to make sure the RETURN statement is executed 186 © Copyright. All rights reserved. Lesson: Creating Database Procedures -- even in case of an exception BEGIN DECLARE EXIT HANDLER FOR SQL_ERROR_CODE 257, SQL_ERROR_CODE 260 BEGIN lv_filter := ''; -- APPLY_FILTER needs a scalar variable as second argument, -- a literal does not work lt_employee = APPLY_FILTER(EMPLOYEE,:lv_filter); END; lt_employee = APPLY_FILTER(EMPLOYEE,:lv_filter); END; ex_employee = SELECT DNumber, e.DepID, RemainderDays*Worth AS remainderdays_value FROM :lt_employee AS e LEFT OUTER JOIN :lt_conversion AS c ON e.depid = c.depid; END; 2. Call your procedure with the following two values for the input filter parameter: an empty string, and a string such that only employees of department A02 are returned. Also test that an exception caused by an incorrect filter parameter value is caught. -- no a) filter - using empty string as filter value: CALL EmployeeRemainderDays_Value('',?); -- filter to employees of department A02 - note the single quotes CALL EmployeeRemainderDays_Value('DepID=''A02''',?); -- incorrect filter value leads to no error (and no filtering) CALL EmployeeRemainderDays_Value('Nonsense',?); 3. Extend the procedure such that it returns a parameter of type NVARCHAR(200) in addition. The parameter shall contain an error message in case the procedure encounters an SQL error 257 or 260. The error message shall contain the SQL error code and error message. a) Note that you cannot simply extend the body of the procedure using SQL. You have to delete the procedure and re-create it. To delete it use the following statement. DROP PROCEDURE EmployeeRemainderDays_Value; b) A possible definition of the procedure is as follows: CREATE PROCEDURE EmployeeRemainderDays_Value( IN im_filter VARCHAR(1000), OUT ex_employee TABLE ( DNumber nvarchar(7), DepID nvarchar(3), Remainderdays_Value dec(16,2)), OUT ex_message NVARCHAR(200)) AS BEGIN DECLARE lv_filter NVARCHAR(1000) := :im_filter; -- construct table for "conversion rate" DECLARE depid NVARCHAR(3) ARRAY; DECLARE worth DEC(5,2) ARRAY; depid[1] := 'A01'; worth[1] := 350; depid[2] := 'A02'; worth[2] := 310; depid[3] := 'A03'; worth[3] := 290; depid[4] := 'A04'; worth[4] := 325; lt_conversion = UNNEST(:depid,:worth) as (DepID,Worth); -- use a block to make sure the RETURN statement is executed -- even in case of an exception © Copyright. All rights reserved. 187 Unit 10: Functions and Procedures BEGIN DECLARE EXIT HANDLER FOR SQL_ERROR_CODE 257, SQL_ERROR_CODE 260 BEGIN lv_filter := ''; -- APPLY_FILTER needs a scalar variable as second argument, -- a literal does not work lt_employee = APPLY_FILTER(EMPLOYEE,:lv_filter); ex_message := 'Result not filtered because of SQL ERROR ' || ::SQL_ERROR_CODE || ' ' || ::SQL_ERROR_MESSAGE; END; lt_employee = APPLY_FILTER(EMPLOYEE,:lv_filter); END; ex_employee = SELECT DNumber, e.DepID, RemainderDays*Worth AS remainderdays_value FROM :lt_employee AS e LEFT OUTER JOIN :lt_conversion AS c ON e.depid = c.depid; END; 4. Test your procedure again using the test queries you used before. -- no a) filter - using empty string as filter value: CALL EmployeeRemainderDays_Value('',?,?); -- filter to employees of department A02 - note the single quotes CALL EmployeeRemainderDays_Value('DepID=''A02''',?,?); -- incorrect filter value leads to no error (and no filtering) -- Should now return an error message in addition CALL EmployeeRemainderDays_Value('Nonsense',?,?); 188 © Copyright. All rights reserved. Lesson: Creating Database Procedures LESSON SUMMARY You should now be able to: ● Create and use database procedures in SAP HANA and know how to debug them © Copyright. All rights reserved. 189 Unit 10: Functions and Procedures 190 © Copyright. All rights reserved. UNIT 11 Data Access Lesson 1 Defining Data Access 192 UNIT OBJECTIVES ● ● Understand database schemas and access tables in other schemas Explain when database indexes make sense in SAP HANA and create and delete indexes using SQL © Copyright. All rights reserved. 191 Unit 11 Lesson 1 192 Defining Data Access LESSON OVERVIEW This lesson covers how you can manage data access permission on SQL level, and how data access can be sped up using database indexes. LESSON OBJECTIVES After completing this lesson, you will be able to: ● ● Understand database schemas and access tables in other schemas Explain when database indexes make sense in SAP HANA and create and delete indexes using SQL Database Schemas You created database tables or views in earlier exercises. You may wonder why this did not lead to naming collisions with other database users creating tables or views with the same names. You can consider the following situation: ● User 1 creates table “Car”. ● User 2 also creates table “Car”. Why are there no naming collision? The reason is that the name of the database object (table, view etc.) implicitly contains a schema name as prefix: <schema name>.<given Name>. Thus the two Car tables are not the same: USER1.Car ≠ USER2.Car Database Schemas ● 192 A database schema is the container that holds database objects such as tables or database views. ● Each database objects belongs to a schema. ● A database schema acts as namespace for the database objects it contains. © Copyright. All rights reserved. Lesson: Defining Data Access Figure 223: User Schemas Figure 224: Queries Across Schemas Controlling Data Access Note: Because the SAP HANA platform is more than a relational database management system, it also provides a richer authorization model extending beyond the SQL level. This model includes, for example, analytic privileges, application, development and administration permissions and roles. SAP HANA therefore also provides more advanced means to manage user permissions and roles than covered in the following, for example using the SAP HANA Tools user and role management. This course only covers the SQL level and how permissions can be managed using SQL statements. This can be helpful for prototyping, for example. Relational database management system, including SAP HANA, support two basic ways for controlling which database user can access which data using SQL: © Copyright. All rights reserved. 193 Unit 11: Data Access ● You can create database views that represent a portion of the data. - ● Example: You provide a view that contains only the owners of CityA to a user for whom the owners outside of CityA are not relevant. Grant specific access permissions to selected users using the Data Control Language of SQL - Example: You grant read only permissions to a user, who should be able to read the owner data but not to change it. The Following are the Two DCL Statements and their Basic Syntax: GRANT The GRANT statement is used to selectively grant access permissions to a user or role. Its basic syntax is: GRANT <list of privileges> ON <database object> TO <user or role> [WITH GRANT OPTION] REVOKE The REVOKE statement is used to selectively withdraw access permissions from a user or role. Its basic syntax is: REVOKE <list of privileges> ON <database object> FROM <user or role> Figure 225: Example: Granting Read Permission Figure 226: Granting Several Privileges in One Statement 194 © Copyright. All rights reserved. Lesson: Defining Data Access Note: There is no inherent dependency between privileges. For example, an INSERT privilege does not imply a SELECT or an UPDATE privilege. Figure 227: Granting Access to all Objects of a Schema Figure 228: Revoking a Privilege Figure 229: Revoking Multiple Privileges © Copyright. All rights reserved. 195 Unit 11: Data Access Figure 230: No Hierarchy of Privileges Database Indexes A Database Index has the Following Properties: ● It can be considered an access path that helps the DBMS locate rows faster. ● It can therefore speed up searching and sorting. - It reduces the need for “full table scans” or “full column scans”. - In SAP HANA, non-unique indexes are needed in exceptional cases only. ● It has no influence on the result of queries — only on how fast the result is returned. ● It can slow down INSERTs, UPDATEs and DELETEs (especially non-unique indexes). Figure 231: HANA: Why Indexes on Column Store? 196 © Copyright. All rights reserved. Lesson: Defining Data Access Figure 232: HANA: Table Scan With Value ID Figure 233: HANA: Inverted Index on Column Store Managing Database Indexes The following are the two statements to manage indexes using SQL and their basic syntax: CREATE INDEX This statement is used to create an index. Its basic syntax is: CREATE [UNIQUE] INDEX <index name> ON <table name> ( <list of columns> ) DROP INDEX This statement is used to delete an index. The underlying database table and its data are not deleted. The basic syntax is: DROP INDEX <index name> © Copyright. All rights reserved. 197 Unit 11: Data Access Figure 234: Example: Creating a Non-Unique Index Figure 235: Example: Creating a Unique Index Creating a unique index is equivalent to adding a UNIQUE constraint to the definition of the database table. Each such constraint automatically leads to a unique index. In addition, a unique index is created automatically for the primary key. You can also do the Following: ● You can create multiple indexes on the same table. ● You can create multi-column indexes. Note: This is not recommended for non-unique indexes on column store tables in SAP HANA because of the additional memory overhead involved. Typically, creating a non-unique index on one, most important column is sufficient to achieve good performance with SAP HANA. Figure 236: DROP INDEX 198 © Copyright. All rights reserved. Lesson: Defining Data Access LESSON SUMMARY You should now be able to: ● ● Understand database schemas and access tables in other schemas Explain when database indexes make sense in SAP HANA and create and delete indexes using SQL © Copyright. All rights reserved. 199 Unit 11: Data Access 200 © Copyright. All rights reserved. UNIT 12 Database Transactions Lesson 1 Explaining Database Transactions 202 UNIT OBJECTIVES ● Explain database transactions and the ACID requirements ● Finish database transactions in HANA using SQL statements ● Describe issues that arise if transactions are not mutually isolated ● Understand and control Isolation levels of transactions and how SAP HANA handles concurrency © Copyright. All rights reserved. 201 Unit 12 Lesson 1 202 Explaining Database Transactions LESSON OVERVIEW This lesson covers the necessity for database transactions and how SAP HANA treats them. LESSON OBJECTIVES After completing this lesson, you will be able to: ● Explain database transactions and the ACID requirements ● Finish database transactions in HANA using SQL statements ● Describe issues that arise if transactions are not mutually isolated ● Understand and control Isolation levels of transactions and how SAP HANA handles concurrency Database Transactions One of the strengths of relational DBMs is their support for database transactions. Transactions serve the following purpose: Figure 237: The Purpose of Database Transactions 202 © Copyright. All rights reserved. Lesson: Explaining Database Transactions Figure 238: The ACID Requirements Starting and Ending Transactions Starting a Transaction in SAP HANA ● ● SAP HANA does not provide a SQL statement to explicitly start a transaction. A transaction is started implicitly with the first DML statement, before execution of the statement begins. A Transaction in SAP HANA can be Ended with the Following Statements: COMMIT [WORK] Ends a transaction successfully, making modifications performed during the transaction durable. ROLLBACK [WORK] Ends a transaction by discarding all modifications performed during the transaction, this way cancels a transaction. Figure 239: Example Database Transactions © Copyright. All rights reserved. 203 Unit 12: Database Transactions DDL Statements automatically end the transaction they belong to successfully, that using an implicit COMMIT. Figure 240: Database Transactions Isolation Challenges The Isolation requirement becomes relevant when considering concurrent access to a database by several users or at least sessions. Support for such multi-user mode is absolutely necessary. Executing all transactions strictly in sequence would be a huge waste of resources, e.g. when waiting for user input or the applications using the DBMS. A strict serialization of all transactions also would not be accepted by the end users. Several transactions must thus run time-shared. This makes the isolation requirement nontrivial, because without certain control mechanisms, the following five types of problems can occur. Figure 241: Isolation Challenge 1: Inconsistent Reads 204 © Copyright. All rights reserved. Lesson: Explaining Database Transactions Figure 242: Isolation Challenge 2: Lost Updates Figure 243: Isolation Challenge 3: Phantom Reads Figure 244: Isolation Challenge 4: Non-Repeatable Reads © Copyright. All rights reserved. 205 Unit 12: Database Transactions Figure 245: Isolation Challenge 5: Dirty Reads Isolation Levels and MVCC DBMSs, including SAP HANA, allow you to control the extent to which problems are avoided for each transaction by supporting different Isolation Levels. Isolation levels define whether rows that are read, inserted, updated, or deleted by a transaction can be accessed by concurrent transactions and whether the current transaction should be able to access rows that were read, inserted, updated or deleted by other transactions. In summary, isolation levels control which types of problems that come from the multi-user mode are allowed and which ones are prevented. Figure 246: Isolation Levels Typical DBMSs support a single version of each persistent data record only and use both read and write locks (set automatically by the DBMS) to implement the isolation levels. The isolation level then impacts the granularity of read and write locks. In contrast, SAP HANA implements Multi-Version Concurrency Control (MVCC) and works with snapshot isolation. This means that consistency of write access is ensured using exclusive write locks “as usual, but consistency of read access works as follows:” 206 © Copyright. All rights reserved. Lesson: Explaining Database Transactions Consistency of Read Access in SAP HANA ● No shared (read) locks are necessary. ● A SELECT statement sees a consistent version of the database state — a snapshot. - By default, this is the version valid when the transaction the statement belongs to began — transaction level snapshot isolation. This roughly corresponds to Isolation Level REPEATABLE READ. - This can be changed to the version valid immediately before statement execution — statement level snapshot isolation. This roughly corresponds to Isolation Level READ COMMITTED. ● Write access creates a new version of the database state, which is not visible to other transactions that started already. Controlling the Isolation Level ● The higher the Isolation Level, the more types of problems are prevented. ● But the higher the burden on the system and the less concurrency. ● → Isolation level can be controlled ● SET TRANSACTION ISOLATION LEVEL IsolationLevel Figure 247: Isolation Levels Supported by SAP HANA © Copyright. All rights reserved. 207 Unit 12: Database Transactions Figure 248: Multi-Version Concurreny Control - An Example The figure, Multi-Version Concurreny Control - An Example, illustrates that “transaction level isolation” is not exactly the same as isolation-level “repeatable read” in single-version concurrency control: In classical DBMSs, T2 and T4 would see V2 if they read it first after T3 is committed. Figure 249: Write Locks & Snapshot Isolation - Additional Remarks LESSON SUMMARY You should now be able to: ● Explain database transactions and the ACID requirements ● Finish database transactions in HANA using SQL statements ● Describe issues that arise if transactions are not mutually isolated ● 208 Understand and control Isolation levels of transactions and how SAP HANA handles concurrency © Copyright. All rights reserved.