HA150 SQL Basics for HANA
Material Number: 50119271
SQL Basics For HANA – Agenda Motivation And Basic Concepts Reading Data From A Table Or View
Exercises 1 - 19
Aggregating Data
Exercises 20 - 29
Reading Data From Multiple Tables Part I
Exercises 30 - 40
Reading Data From Multiple Tables Part II
Exercises 41 - 45
Understanding NULL Values Changing Data Stored in Tables
Exercises 46 - 53
Defining How Data Is Stored
Exercises 54 - 58
Using Views For Data Access
Exercises 59 - 60
Defining Data Access Database Transactions ©
2013 SAP AG. All rights reserved.
2
SQL Basics For HANA Course
The SQL Basics course is designed to ...
Explain basic concepts in the database world and the Relational Database Model
Refresh and deepen SQL knowledge, especially for developing on HANA database systems
The SQL Basics course is not designed to ...
Explain implementation details of SAP HANA
Supersede official SAP HANA documentation
Solve specific development problems
Serve as a basis for decision-making for development projects
©
2013 SAP AG. All rights reserved.
3
Unit 1 Motivation & Basic Concepts
4
Unit 1: Motivation And Basic Concepts Learning Objectives After completing this unit, you will be able to: • List some database models • Understand the motivation for and foundation of the relational model • Understand why learning SQL is important when dealing with HANA • Understand how the relation model and SQL are related • List the three sub-languages of SQL • Understand the sample database used throughout the course
© 2013 SAP AG. All rights reserved.
5
Basic Concepts
What are the terms •
Database
•
Database System
•
Database Management System ?
©
2013 SAP AG. All rights reserved.
6
DBS = DB + DBMS
The Database Management System (DBMS) manages the database
Every access to the database (create, read, insert, update, delete) goes exclusively through the DBMS The DBMS exercises complete control over the database
Database (DB) = structured collection of "records"
Database System (DBS) = specific database + DBMS
©
2013 SAP AG. All rights reserved.
7
DBS = DB + DBMS
User / Application
DBMS DBS
DB ©
2013 SAP AG. All rights reserved.
8
DBS = DB + DBMS
„What Database do you use?“
„Which Database Management System do you use?“
„We are using HANA as Database.“
„We are using SAP HANA as Database Management System.“
In everyday life, these terms are usually used incorrectly …
©
2013 SAP AG. All rights reserved.
9
Basics Concepts
3 Level Schema Architecture
©
2013 SAP AG. All rights reserved.
10
3 Level Schema Architecture
Objective: Changes at a lower level should not affect a higher level (if possible)
Application/User
External Schema 1
Application/User …
External Schema n
Conceptual Schema
Internal Schema
©
2013 SAP AG. All rights reserved.
External Level
Conceptual Level
Internal Level
11
3 Level Schema Architecture
The address book should not display salary data External Level (VIEW) An Employee has a D-Number, name, and salary and is assigned to a department Conceptual Level (TABLE) The board mostly accesses employee data according to descending order of salary (which has to be very fast) Internal Level (INDEX)
©
2013 SAP AG. All rights reserved.
12
Basic Concepts
Which Database Models are available?
©
2013 SAP AG. All rights reserved.
13
Database Models
Hierarchical Database Model
Network Database Model
Relational Database Model
Object-relational Database Model
Object-oriented Database Model
XML-based Database Model
…
©
2013 SAP AG. All rights reserved.
14
Basic Concepts
Relational Database Model
©
2013 SAP AG. All rights reserved.
15
Relational Database Model
Invented in the late 1960s by Edgar F. Codd (1923-2003) IBM Almaden Research Lab in San Jose (California)
First prototype in the mid-1970s • "System R", IBM San Jose
Relational DBMS products (selection) • • • •
©
Oracle (since 1979) IBM DB2 (since 1983) SQL Server (since 1989) SAP HANA (since 2011)
2013 SAP AG. All rights reserved.
16
Relational Database Model
Initial Development Goals (1960s): Simple but mathematically profound database model – Easy to understand but still mathematically precise
Simple but mathematically-based database language – Easy to learn, but semantics still described with mathematical precision – Provable equivalence of two queries
Integrity monitoring largely by the DBMS Storage & retrieval of data is the responsibility of the DBMS (and not of the user) – Descriptive language rather than navigation (optimizer selects optimal execution strategy)
Clean separation between conceptual and internal schema
©
2013 SAP AG. All rights reserved.
17
Basic Concepts
Why is the Relational Database Model actually called Relational Database Model?
©
2013 SAP AG. All rights reserved.
18
Relations
A relation (in the sense of mathematics) is the subset of the Cartesian product of sets
R⊆AxBxC
©
2013 SAP AG. All rights reserved.
19
Comparative Relations (=,≠,<,≤,>,≥)
N = {1, 2, 3, 4, 5} N × N = {(1,1), (1,2), (1,3), (1,4), (1,5), (2,1), (2,2), (2,3), (2,4), (2,5), (3,1), (3,2), (3,3), (3,4), (3,5), (4,1), (4,2), (4,3), (4,4), (4,5), (5,1), (5,2), (5,3), (5,4), (5,5)}
©
2013 SAP AG. All rights reserved.
20
Comparative Relations (=,≠,<,≤,>,≥) ≤ ⊆ N × N ≤ ⊆ {(1,1), (2,1), (3,1), (4,1), (5,1),
(1,2), (2,2), (3,2), (4,2), (5,2),
(1,3), (2,3), (3,3), (4,3), (5,3),
(1,4), (2,4), (3,4), (4,4), (5,4),
(1,5), (2,5), (3,5), (4,5), (5,5)}
≤ = {(1,1), (1,2), (1,3), (1,4), (1,5), (2,2), (2,3), (2,4), (2,5), (3,3), (3,4), (3,5), (4,4), (4,5), (5,5)} (2,3) ϵ ≤ 2 ≤ 3
©
2013 SAP AG. All rights reserved.
21
Relations PersNumber = {P036407, P040824, P052867, …} Name = {Ben, Paul, Raja, …} HiringYear = {2001, 2008, 2010, …} PersNumber × Name × HiringYear= {(P036407,Ben,2001), (P036407,Ben,2008), (P036407,Paul,2001), (P036407,Paul,2008), (P035607,Raja,2001), (P036407,Raja,2008), (P040824,Ben,2001), (P040824,Ben,2008), (P040824,Paul,2001), (P040824,Paul,2008), (P040824,Raja,2001), (P040824,Raja,2008), (P052867,Ben,2001), (P052867,Jim,2008), (P052867,Paul,2001), (P052867,Vishal,2008), (P052867,Raja,2001), (P052867,Werner,2008),
©
2013 SAP AG. All rights reserved.
(P036407,Ben,2010), (P036407,Paul,2010), (P036407,Raja,2010), (P040824,Ben,2010), (P040824,Paul,2010), (P040824,Raja,2010), (P052867,Ben,2010), (P052867,Paul,2010), (P052867,Raja,2010), …}
22
Relations Employee ⊆ PersNumber × Name × HiringYear Employee ⊆ {(P036407,Ben,2001), (P036407,Paul,2001), (P035607,Raja,2001), (P040824,Ben,2001), (P040824,Paul,2001), (P040824,Raja,2001), (P052867,Ben,2001), (P052867,Paul,2001), (P052867,Raja,2001),
(P036407,Ben,2008), (P036407,Paul,2008), (P036407,Raja,2008), (P040824,Ben,2008), (P040824,Paul,2008), (P040824,Raja,2008), (P052867,Jim,2008), (P052867,Vishal,2008), (P052867,Werner,2008),
(P036407,Ben,2010), (P036407,Paul,2010), (P036407,Raja,2010), (P040824,Ben,2010), (P040824,Paul,2010), (P040824,Raja,2010), (P052867,Ben,2010), (P052867,Paul,2010), (P052867,Raja,2010), …}
Employee= {(P036407,Paul,2001), (P040824,Ben,2008), (P052867,Raja,2010)}
©
2013 SAP AG. All rights reserved.
23
Relations & Tables
A relation can be represented as a table Employee= {(P036407,Paul,2001), (P040824,Ben,2008), (P052867,Raja,2010)}
Employee
©
2013 SAP AG. All rights reserved.
PersNumber
Name
HiringYear
P036407 P040824 P052867
Paul Ben Raja
2001 2008 2010
24
Basic Concepts
Which languages are available for the Relational Database Model?
©
2013 SAP AG. All rights reserved.
25
Relational Languages
Relational Algebra
Formal basis for DBMS internal query optimization 6 basic operations (selection, projection, cross join, union, difference, rename)
Relational Calculus
Tuple variables and quantifiers
SQL
©
Standardized and used in practice
2013 SAP AG. All rights reserved.
26
Why SQL?
Why SQL is important?
Why it is worth working with SQL
©
2013 SAP AG. All rights reserved.
27
Why SQL? → Reasons #1 and #2 In almost every business application scenario, the data is managed using database systems. The most significant are database systems based on the relational data model and using SQL (Structured Query Language) as a database language.
SQL is a widely-established, powerful, standardized database language many application programmers have experience in. There is (so far) no other database language that has all the advantages mentioned.
SAP HANA is a relational data base management system and SAP HANA supports SQL ©
2013 SAP AG. All rights reserved.
28
Why SQL?
Different application architectures and development models are possible with SAP HANA Data Marts with SAP HANA
Standalone HANA Apps
BI Tools, MS Excel, Web Browser …
Standalone Application SAP HANA clients
Traditional DBMS
©
2013 SAP AG. All rights reserved.
HANA Content / Database Tables
HANA Content / Database Tables
SAP HANA
Schema replication
SAP HANA
BI 4.0 (optional) Semantic Layer
29
Why SQL?
Different application architectures and development models are possible with SAP HANA HANA as primary Database HANA as Accelerator (secondary DB) (for AS ABAP) SAP GUI, Browser-based GUI
SAP GUI, Browser-based GUI
SAP Application Server
SAP Application Server
replication
Data
SAP HANA
ABAP Schema
ABAP Schema / Database Tables
SAP HANA
E.g. CRM on HANA – NW 7.40
Aggregation Levels
Traditional DBMS
©
2013 SAP AG. All rights reserved.
30
Why SQL? → Reason #3
HANA supports an extended version of SQL
No matter which HANA-based architecture or development model you work with, it is likely that using HANA SQL will be beneficial.
©
2013 SAP AG. All rights reserved.
31
SQL
SQL Features
©
2013 SAP AG. All rights reserved.
32
SQL Features
SQL is standardized – No uncontrolled growth with respect to syntax and semantics
SQL is descriptive (rather than procedural) – The "what" and not "how" is described
SQL execution is optimized – SQL statement is first parsed and optimized, and then executed – Optimizer determines optimal execution plan (at least in theory)
SQL is multi-set oriented (and not single record-based) – Using a single SQL statement multiple table rows can be read, modified or deleted in one go
©
2013 SAP AG. All rights reserved.
33
SQL Language Elements
SQL language elements can be divided into three categories:
DML = Data Manipulation Language SELECT, INSERT, UPDATE, DELETE
DDL = Data Definition Language CREATE, ALTER, DROP, RENAME
DCL = Data Control Language GRANT, REVOKE
©
2013 SAP AG. All rights reserved.
34
SQL
SQL is standardized!
©
2013 SAP AG. All rights reserved.
35
SQL Standard: History
1970s
SEQUEL (Structured English Query Language) as a language for "System R" • "System R" was the first relational prototype developed by IBM in San Jose
End of 1970
Renaming of SEQUEL into SQL (Structured Query Language)
1982
American National Standards Institute (ANSI) begins with standardization of SQL
1986
First version of the SQL standard is adopted (as ANSI) SQL-86 ("SQL0“)
1987
International Organization for Standardization (ISO) does SQL standard (ISO standard)
1989
SQL-89 ("SQL1")
1992
SQL-92 ("SQL2"), 3 attenuation levels: Entry Level, Intermediate Level, Full Level
1999
SQL:1999 ("SQL3")
2003
SQL:2003 ("SQL4"), "SQL/XML:2006" 2006 official standard part of SQL:2003
2008
SQL:2008 ("SQL5"), officially: "ISO/IEC 9075:2008" and "DIN 9075" (> 3,000 pages)
©
2013 SAP AG. All rights reserved.
36
SQL
SQL ≠ relational
©
2013 SAP AG. All rights reserved.
37
SQL
SQL is the most important database language for relational database model. Although SQL is based on the relational database model, it deviates in important points from the "purely relational" model:
No (primary) key required
NULL values are allowed
3-valued logic required ("A = A" need not be TRUE)
The language is not closed
©
Duplicates (identical rows) allowed (multi-sets instead of sets)
Order of the result rows may be relevant (ORDER BY) Anonymous result columns allowed Duplicate result column names allowed
2013 SAP AG. All rights reserved.
38
Database Objects
What can be found in the database?
©
2013 SAP AG. All rights reserved.
39
Database Objects
The table is the primary database object - but not the only one. Apart from tables a database usually contains:
©
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
2013 SAP AG. All rights reserved.
40
Basic Concepts
What is a table?
©
2013 SAP AG. All rights reserved.
41
Tables
A (database) table represents a relation (but: multi-set of rows instead of set of tuples)
Employee PersNumber
Name
HiringYear
P036407
Paul
2001
P040824
Ben
2008
P052867
Raja
2010
Employee = {(P036407,Paul,2001), (P040824,Ben,2008), (P052867,Raja,2010)}
©
2013 SAP AG. All rights reserved.
42
Tables
A table consists of rows and columns
A table can therefore be represented two-dimensionally
A table represents a relation (with multi-sets property)
©
A table represents a (unordered) multi-set of points in n-dimensional space: Each point of the multi-set corresponds to a table row Each of the n dimensions is equivalent to a table column
2013 SAP AG. All rights reserved.
43
Table rows as points in space
Name
Paul HiringYear Raja
(P040824, Ben, 2008) 2010 2008
Ben
2001 PersNumber
P030407
©
2013 SAP AG. All rights reserved.
P040824
P052867
44
Projection on the plane
Projection list
Name
SELECT PersNumber, Name FROM Employee WHERE PersNumber = 'P040824';
Paul
HiringYear Raja
(P030824, Ben, 2008) 2010 2008
Ben
(P040824, Ben)
2001 PersNumber
P036407
©
2013 SAP AG. All rights reserved.
P040824
P052867
45
Projection on an axis
Projection list
Name
SELECT Name FROM Employee WHERE PersNumber = 'P040824';
Paul
HiringYear Raja
(P040824, Ben, 2008) 2010 2008
Ben
2001 PersNumber
P036407
©
2013 SAP AG. All rights reserved.
P040824
P052867
46
Basic Concepts
What are the components of a (database) table?
©
2013 SAP AG. All rights reserved.
47
Tables
Table-Name
Primary Key
Column Name
Employee PersNumber Name P036407 Paul Table Row P040824 Ben P052867 Raja
HiringYear 2001 2008 2010 (Table Column) value
(Table) Column
©
2013 SAP AG. All rights reserved.
48
Basic Concepts
What is a key?
©
2013 SAP AG. All rights reserved.
49
Key
Key = is a set of columns which serves to uniquely identify any row in the table. The ability to uniquely identify rows must apply in principle (and not only for the rows existing at a certain point in time).
Employee PersNumber Name P036407 Paul P040824 Ben P052867 Raja
©
2013 SAP AG. All rights reserved.
HiringYear 2001 2008 2010
50
Multi-column Keys
A key can consist of multiple columns
Employee Name Country City BuildingNr Block Floor Room SeatNr Mr A
DE
WDF
03
A
5
29
1
Ms B
DE
WDF
03
A
5
29
2
Ms C
DE
WDF
03
A
5
29
3
…
…
…
…
…
…
…
…
In this example the key consists of 7 columns 1 Key (not 7 keys)!
©
2013 SAP AG. All rights reserved.
51
Multiple Keys
There may be more than one key
Employee PNumber Name TaxID
Passport ID
Plate Number
ChasisNr
CarLicense ID
SWIFT
IBAN
P000815
…
…
…
HD-MM 815
…
…
…
…
P004711
…
…
…
HD-ML 4711
…
…
…
…
P012345
…
…
…
HD-OI 2345
…
…
…
…
…
…
…
…
…
…
…
…
…
Here are 7 keys (provided there are no joint accounts) Just 1 key is selected as Primary Key
©
2013 SAP AG. All rights reserved.
52
Basic Concepts
What is a Foreign-Key?
©
2013 SAP AG. All rights reserved.
53
Foreign-Keys
Foreign-Key = set of columns, which is a (primary) key in an(other) table •
The foreign-key can refer to its own table
•
It is not necessary to share the same name(s)
•
The foreign-key can contain only those values that occur as a (primary) key value in another table (in addition and if applicable, NULL values are allowed)
•
The foreign-key is usually not a key!
Employee
©
DNumber D010000 D010001 D010002 D010003 D010004 D010005
2013 SAP AG. All rights reserved.
Name Mr A Ms B Mr C Ms D Mr E Ms F
DepartmentNR A01 A01 A02 A02 A02 A03
Foreign-key Relationship
Department DNr Function A01 HANA-Development A02 HANA-Sales A03 HANA-Training A04 ABAP-Development
54
Foreign-Keys
A Foreign-Key can consist of multiple columns
Publication PaperID Topic P001 In-Memory P002 Column Store P003 Row Store P004 SQL & XML P005 XML & SQL P006 DB Recovery
Uni HPI HPI
StudentID 12345 12345
HPI FSU FSU FSU
77777 12345 12345 77777
Student
Uni StudentID Name FirstName HPI 12345 A B HPI 77777 C D FSU 12345 E F FSU 77777 G H
This example shows 1 Foreign-Key built on 2 columns
©
2013 SAP AG. All rights reserved.
55
Foreign-Keys
There can be multiple Foreign-Keys
Vendor
VID L1 L2 L3 …
©
Name Heidelberg Paper Wiesloch Paper Walldorf Paper …
2013 SAP AG. All rights reserved.
Delivery VID PNr Year Quantity
L1 L1 L1 L1 L2 L2 L3 …
A1 A1 A1 A2 A1 A2 A1 …
2010 2011 2012 2012 2011 2011 2012 …
320 570 925 102 577 100 999 …
Product PNr Description A1 Sticky Notes A2 Printing Paper A3 Envelopes … …
56
Foreign-Keys
The foreign-key can refer to its own table
Employee
©
2013 SAP AG. All rights reserved.
DNumber D010000 D010001 D010002 D010003 D010004 D010005 D010006 D010007
Name Mr A Ms B Mr C Ms D Mr E Ms F Mr G Ms H
DNumberManager D010007 D010007 D010004 D010002 D010002 D010005 D010005
57
Example Scenario
Fictional vehicle registration office as an example scenario
©
2013 SAP AG. All rights reserved.
58
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.
2013 SAP AG. All rights reserved.
59
Example: Registration Office
Official(PNr, Name, Overtime, Salary, Manager)
Contact(PersNumber, OwnerID) Owner(OwnerID, Name, Birthday, City)
Car(CarID, PlateNumber, Brand, Color, HP, Owner) Stolen(PlateNumber, reported_at)
©
2013 SAP AG. All rights reserved.
60
Officials
Official
©
PNr
Name
P01
Mr A
P02
Salary
Manager
10
A09
P04
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
P08
P07
Ms G
22
A11
P08
P08
Ms H
NULL
A13
P09
P09
Mr I
NULL
A14
NULL
2013 SAP AG. All rights reserved.
Overtime
61
Owner
Owner
©
2013 SAP AG. All rights reserved.
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
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
62
Contact
Contact
©
2013 SAP AG. All rights reserved.
PersNumber
OwnerID
P01
H03
P01
H04
P01
H07
P04
H03
P04
H04
P08
H04
P08
H07
P09
H03
63
Cars Car
©
CarID
PlateNumber
Brand
Color
F01 F02 F03 F04 F05 F06 F07 F08 F09 F10
HD-V 106 HD-VW 4711 HD-JA 1972 HD-AL 1002 HD-MM 3206 HD-VW 1999 HD-ML 3206 HD-IK 1002 HD-UP 13 HD-MT 507
Fiat VW BMW Mercedes Mercedes Audi Audi VW Skoda BMW
red black blue white black yellow blue black red black
75 120 184 136 170 260 116 160 105 140
H06 H03 H03 H07 H03 H05 H03 H07 H02 H04
F11 F12 F13 F14 F15 F16 F17 F18 F19 F20
HD-MM 208 HD-XY 4711 HD-IK 1001 HD-MM 1977 HD-MB 3030 NULL HD-Y 333 HD-MQ 2006 HD-VW 2012 NULL
BMW Skoda Renault Mercedes Skoda Opel Audi Renault VW Audi
green red red white black green orange red black green
184 105 136 170 136 120 184 90 125 184
H02 H04 H07 H03 H03 NULL H09 H03 H01 NULL
2013 SAP AG. All rights reserved.
HP
Owner
64
Stolen Cars Stolen
©
PlateNumber
reported_at
HD-VW 1999
20.06.2012
HD-V 106
01.06.2012
HD-Y 333
21.05.2012
2013 SAP AG. All rights reserved.
65
Registration Office & EU
A new (fictional) European Union directive requires that the information about which vehicle is registered to which owner, has to be stored in a central transnational database. The vehicle identification number (CarID) is unique across the EU, but not the Owner ID. Owner_EU(Country, OwnerID, Name, Birthday, City) Car_EU(CarID, PlateNumber, Brand, Color, HP, Country, Owner)
©
2013 SAP AG. All rights reserved.
66
Owner (EU-wide) Owner_EU
©
Country
OwnerID
Name
Birthday
City
D
H01
Ms T
20.06.1934
Wiesloch
D
H02
Ms U
11.05.1966
Hockenheim
D
H03
SAP AG
NULL
Walldorf
D
H04
HDM AG
NULL
Heidelberg
D
H05
Mr V
21.04.1952
Leimen
D
H06
Ms W
01.06.1957
Wiesloch
D
H07
IKEA
NULL
Walldorf
D
H08
Mr X
30.08.1986
Walldorf
D
H09
Ms Y
10.02.1986
Sinsheim
D
H10
Mr Z
03.02.1986
Ladenburg
A
H01
Ms O
21.05.1977
Wien
A
H02
Mr P
02.08.1977
Salzburg
E
H01
Señor Q
18.02.1925
Madrid
E
H02
Señora R
27.02.1927
Barcelona
2013 SAP AG. All rights reserved.
67
Cars (EU-wide) Car_EU
©
CarID F01 F02 F03 F04 F05 F06 F07 F08 F09 F10 F11 F12 F13 F14 F15 F16 F17 F18 F19 F20 F21 F22 F23 F24
2013 SAP AG. All rights reserved.
PlateNumber HD-V 106 HD-VW 4711 HD-JA 1972 HD-AL 1002 HD-MM 3206 HD-VW 1999 HD-ML 3206 HD-IK 1002 HD-UP 13 HD-MT 507 HD-MM 208 HD-XY 4711 HD-IK 1001 HD-MM 1977 HD-MB 3030 NULL HD-Y 333 HD-MQ 2006 HD-VW 2012 NULL W-302 ML S-215 MM 1409 EMM 3206 MLM
Brand Fiat VW BMW Mercedes Mercedes Audi Audi VW Skoda BMW BMW Skoda Renault Mercedes Skoda Opel Audi Renault VW Audi Mercedes VW Audi VW
Color red black blue white black yellow blue black red black green red red white black green orange red black green black silvern blue black
HP 75 120 184 136 170 260 116 160 105 140 184 105 136 170 136 120 184 90 125 184 170 184 116 170
Country D D D D D D D D D D D D D D D NULL D D D NULL A A E E
Owner H06 H03 H03 H07 H03 H05 H03 H07 H02 H04 H02 H04 H07 H03 H03 NULL H09 H03 H01 NULL H01 H02 H01 H02
68
Unit 1: Motivation And Basic Concepts Summary
You should now be able to: • List some database models
• • • • •
Understand the motivation for and foundation of the relational model Understand why learning SQL is important when dealing with HANA Understand how the relation model and SQL are related List the three sub-languages of SQL Understand the sample database used throughout the course
© 2012 SAP AG. All rights reserved.
Internal
6969
Unit 2 Reading data from a table or view
70
Unit 2: Reading Data From A Table Or View
After completing this unit, you will be able to: • Write simple database queries using SQL‘s SELECT statement • Project columns in and out of queries using the SELECT clause • Avoid duplicates in SELECT statement result sets • Include columns based on conditions • Use built-in functions in column lists and WHERE clauses • Limit results sets to the first N rows • Ensure a specific order in SELECT statement result sets • Restrict the result set using the WHERE clause
© 2013 SAP AG. All rights reserved.
Internal
7171
Reading Database Access
Which SQL statement can be used for reading data from a table or view?
©
2013 SAP AG. All rights reserved.
72
Reading Database Access
SELECT … FROM … WHERE … It
is used to read from a table or view
The SELECT statement is the central construct for read access to data (database) with SQL
The SELECT statement can include the following (optional) clauses:
WHERE, GROUP BY, HAVING, ORDER BY
©
2013 SAP AG. All rights reserved.
73
Reading Database Access
The SELECT statement
©
2013 SAP AG. All rights reserved.
74
SELECT Statement
SELECT FROM WHERE GROUP BY HAVING ORDER BY
©
2013 SAP AG. All rights reserved.
Column, Column, COUNT(*) Table Condition Column, Column Group_Condition Column ASC, Column DESC;
75
SELECT
SELECT clause
©
2013 SAP AG. All rights reserved.
76
SELECT Clause
You can specify a single column in the projection list:
SELECT Name FROM Official;
©
2013 SAP AG. All rights reserved.
NAME ---Mr A Mr B Ms C Ms D Mr E Mr F Ms G Ms H Mr I
77
SELECT Clause
You can specify multiple columns in the projection list:
SELECT PNr, Name, Salary FROM Official;
©
2013 SAP AG. All rights reserved.
PNR --P01 P02 P03 P04 P05 P06 P07 P08 P09
NAME ---Mr A Mr B Ms C Ms D Mr E Mr F Ms G Ms H Mr I
Salary -----A09 A10 A09 A12 A08 A09 A11 A13 A14
78
SELECT Clause
The sequence of the columns in the projection list is relevant:
SELECT PNr, Name FROM Official; PNR --P01 P02 P03 P04 …
©
NAME ---Mr A Mr B Ms C Ms D …
2013 SAP AG. All rights reserved.
SELECT Name, PNr FROM Official; NAME ---Mr A Mr B Ms C Ms D …
PNR --P01 P02 P03 P04 …
79
SELECT Clause
The same column can appear several times in the projection list: We do not recommend using this option
SELECT PNr, PNr, PNr FROM Official;
©
2013 SAP AG. All rights reserved.
PNR --P01 P02 P03 P04 P05 P06 P07 P08 P09
PNR --P01 P02 P03 P04 P05 P06 P07 P08 P09
PNR --P01 P02 P03 P04 P05 P06 P07 P08 P09
80
SELECT Clause
An asterisk (*) in the projection list represents "all columns“:
SELECT * FROM Official; PNR --P01 P02 P03 P04 …
©
2013 SAP AG. All rights reserved.
NAME ---Mr A Mr B Ms C Ms D …
OVERTIME -------10 10 20 ? …
SALARY -----A09 A10 A09 A12 …
MANAGER ------P04 P04 P04 P09 …
81
SELECT Clause Columns can be specified in the projection list in addition to the asterisk (*): We do not recommend using this option
SELECT PNr, *, Name FROM Official; PNR --P01 P02 P03 P04 …
©
PNR --P01 P02 P03 P04 …
NAME ---Mr A Mr B Ms C Ms D …
2013 SAP AG. All rights reserved.
OVERTIME -------10 10 20 ? …
SALARY -----A09 A10 A09 A12 …
MANAGER ------P04 P04 P04 P09 …
NAME ---Mr A Mr B Ms C Ms D …
82
SELECT Clause
The asterisk (*) can be used multiple times in the projection list: We do not recommend using this option
SELECT *, *, * FROM Official; PNR
NAME
OVERTIME
SALARY
MANAGER
PNR
NAME
OVERTIME
SALARY
MANAGER
PNR
NAME
OVERTIME
SALARY
MANAGER
---
----
--------
------
-------
---
----
--------
------
-------
---
----
--------
------
-------
P01
Mr A
10
A09
P04
P01
Mr A
10
A09
P04
P01
Mr A
10
A09
P04
P02
Mr B
10
A10
P04
P02
Mr B
10
A10
P04
P02
Mr B
10
A10
P04
P03
Ms C
20
A09
P04
P03
Ms C
20
A09
P04
P03
Ms C
20
A09
P04
P04
Ms D
?
A12
P09
P04
Ms D
?
A12
P09
P04
Ms D
?
A12
P09
P05
Mr E
10
A08
P08
P05
Mr E
10
A08
P08
P05
Mr E
10
A08
P08
P06
Mr F
18
A09
P08
P06
Mr F
18
A09
P08
P06
Mr F
18
A09
P08
P07
Ms G
22
A11
P08
P07
Ms G
22
A11
P08
P07
Ms G
22
A11
P08
P08
Ms H
?
A13
P09
P08
Ms H
?
A13
P09
P08
Ms H
?
A13
P09
P09
Mr I
?
A14
?
P09
Mr I
?
A14
?
P09
Mr I
?
A14
?
©
2013 SAP AG. All rights reserved.
83
SELECT Clause
You can generate additional “artificial” result columns:
SELECT 'Today', Name, 'has been assigned to salary group', Salary FROM Official; 'Today' ------Today Today Today Today Today …
©
NAME ---Mr A Mr B Ms C Ms D Mr E …
2013 SAP AG. All rights reserved.
'has been assigned to salary group' ----------------------------------has been assigned to salary group has been assigned to salary group has been assigned to salary group has been assigned to salary group has been assigned to salary group …
SALARY -----A09 A10 A09 A12 A08 …
84
SELECT Clause
The additional artificial result column can have a numeric type:
SELECT 'The working week of', Name, 'amounts to', 40, 'hours.' FROM Official; 'The working week of' --------------------The working week of The working week of The working week of The working week of The working week of The working week of The working week of The working week of The working week of
©
2013 SAP AG. All rights reserved.
NAME ---Mr A Mr B Ms C Ms D Mr E Mr F Ms G Ms H Mr I
'amounts to' -----------amounts to amounts to amounts to amounts to amounts to amounts to amounts to amounts to amounts to
40 -40 40 40 40 40 40 40 40 40
'hours'. -------hours. hours. hours. hours. hours. hours. hours. hours. hours.
85
SELECT Clause
The projection list may only contain artificial result columns:
SELECT 'Good Morning!' FROM Official;
©
2013 SAP AG. All rights reserved.
'Good Morning!' --------------Good Morning! Good Morning! Good Morning! Good Morning! Good Morning! Good Morning! Good Morning! Good Morning! Good Morning!
86
SELECT Clause
If the projection list only contains artificial result columns, you can use the special table “Dummy” as reference:
SELECT 'Good Morning!' FROM Dummy;
'Good Morning!' --------------Good Morning!
The “DUMMY“ table contains one column and one row:
SELECT * FROM Dummy;
©
2013 SAP AG. All rights reserved.
DUMMY ----X
87
SELECT Clause
SELECT a, 'b', "c", 1, '2', "3" FROM "4";
a
©
A … … …
'b' --b b b
c … … …
1 1 1 1
'2' --2 2 2
3 … … …
Existing column named “A“ (“A“ as capital letter)
'b'
Artificial result column with string “b“ as value in each row
"c"
Existing column named “c“ (“c“ as lower case letter)
1
Artificial result column with 1 as numeric value in each row
'2'
Artificial result column with string “2“ as value in each row
"3"
Existing column named “3“
"4"
Existing table named “4“
2013 SAP AG. All rights reserved.
88
SELECT Clause
The projection list can have computed columns. If a NULL value is used in an arithmetic operation, the result is also a NULL value:
SELECT Name, Overtime * 60 FROM Official;
©
2013 SAP AG. All rights reserved.
NAME ---Mr A Mr B Ms C Ms D Mr E Mr F Ms G Ms H Mr I
OVERTIME*60 ----------600 600 1200 ? 600 1080 1320 ? ? 89
SELECT Clause
0 * NULL results in NULL (and not 0):
SELECT Name, Overtime, Overtime * 0 FROM Official; NAME ---Mr A Mr B Ms C Ms D Mr E Mr F Ms G Ms H Mr I ©
2013 SAP AG. All rights reserved.
OVERTIME -------10 10 20 ? 10 18 22 ? ?
OVERTIME*0 ---------0 0 0 ? 0 0 0 ? ? 90
SELECT Clause
You can explicitly name computed result columns:
SELECT Name, Overtime * 60 AS Overminutes FROM Official; NAME ---Mr A Mr B Ms C Ms D …
©
2013 SAP AG. All rights reserved.
OVERMINUTES ----------600 600 1200 ? …
91
SELECT Clause
Quotation marks are required, if the result column name should include lowercase letters:
SELECT Name, Overtime * 60 AS "Overminutes" FROM Official; NAME ---Mr A Mr B Ms C Ms D …
©
2013 SAP AG. All rights reserved.
Overminutes ----------600 600 1200 ? …
92
SELECT Clause
Quotation marks are also required, if the result column name should contain blanks:
SELECT Name, Overtime * 60 AS "overtime minutes" FROM Official; NAME ---Mr A Mr B Ms C Ms D …
©
2013 SAP AG. All rights reserved.
overtime minutes ---------------600 600 1200 ? …
93
SELECT Clause
You can also rename non-calculated result columns:
SELECT PNr AS PersNumber, Salary AS "Salary Group" FROM Official; PERSNUMBER ---------P01 P02 P03 P04 …
©
2013 SAP AG. All rights reserved.
Salary Group -----------A09 A10 A09 A12 …
94
SELECT Clause
In the (re)naming of result columns the keyword "AS“ can be omitted:
SELECT PNr PersNumber, Salary "Salary Group" FROM Official; PERSNUMBER ---------P01 P02 P03 P04 …
©
2013 SAP AG. All rights reserved.
Salary Group -----------A09 A10 A09 A12 …
95
SELECT Clause
You can use existing column names for (re)naming of result columns:
SELECT PNr Salary, Salary PNr FROM Official;
©
2013 SAP AG. All rights reserved.
SALARY -----P01 P02 P03 P04 P05 P06 P07 P08 P09
PNR --A09 A10 A09 A12 A08 A09 A11 A13 A14
96
SELECT Clause
Multiple result columns can have the same name: We do not recommend using this option
SELECT PNr AS xyz, Name AS xyz, Salary AS xyz FROM Official; XYZ --P01 P02 P03 P04 …
©
2013 SAP AG. All rights reserved.
XYZ ---Mr A Mr B Ms C Ms D …
XYZ --A09 A10 A09 A12 …
97
SELECT Clause
You can use functions in the projection list. The corresponding result columns can be named explicitly.
Which owner is born in which year?
SELECT Name, YEAR(Birthday) AS "Year of Birth" FROM Owner;
©
2013 SAP AG. All rights reserved.
NAME -----Ms T Ms U SAP AG HDM AG Mr V Ms W IKEA Mr X Ms Y Mr Z
Year of Birth ------------1934 1966 ? ? 1952 1957 ? 1986 1986 1986
98
SELECT Clause
When were vehicle owners first allowed to drive a car in Germany?
SELECT Name, ADD_YEARS(Birthday, 18) AS "18th Birthday" FROM Owner; NAME -----Ms T Ms U SAP AG HDM AG Mr V Ms W IKEA Mr X Ms Y Mr Z
©
2013 SAP AG. All rights reserved.
18th Birthday ------------1952-06-20 1984-05-11 ? ? 1970-04-21 1975-06-01 ? 2004-08-30 2004-02-10 2004-02-03
99
SELECT Clause
Function calls can be nested On which day is the owner’s 18th birthday?
SELECT Name, DAYNAME ( ADD_YEARS (Birthday, ROUND(ABS(-18.2)) ) ) AS Weekday FROM Owner;
©
2013 SAP AG. All rights reserved.
NAME -----Ms T Ms U SAP AG HDM AG Mr V Ms W IKEA Mr X Ms Y Mr Z
WEEKDAY ------FRIDAY FRIDAY ? ? TUESDAY SUNDAY ? MONDAY TUESDAY TUESDAY 100
Functions
Which functions are provided by SAP HANA?
©
2013 SAP AG. All rights reserved.
101
Functions
SAP HANA provides following functions (selection):
©
Function
Explanation
YEAR(Date)
year
ADD_YEARS(Date, n)
n years later
DAYNAME(Date)
weekday (English)
CURRENT_DATE
current date
ABS(Number)
absolute value
ROUND(Number)
rounding
SQRT(Number)
square root
UPPER(String)
convert to upper case
SUBSTR(String, Start, Length)
cut out of a string (substring)
LENGTH(String)
length of a string
2013 SAP AG. All rights reserved.
102
SELECT Clause
You can qualify the column name by adding the table name:
SELECT Official.Name FROM Official;
©
2013 SAP AG. All rights reserved.
NAME ---Mr A Mr B Ms C Ms D Mr E Mr F Ms G Ms H Mr I
103
SELECT Clause
The qualification with the table name is also allowed, if the output table has to contain all columns:
SELECT Official.* FROM Official; PNR --P01 P02 P03 P04 …
©
2013 SAP AG. All rights reserved.
NAME ---Mr A Mr B Ms C Ms D …
OVERTIME -------10 10 20 ? …
SALARY -----A09 A10 A09 A12 …
MANAGER ------P04 P04 P04 P09 …
104
SELECT Clause
The projection list can include a mix of qualified and unqualified column names:
SELECT Name, Official.PNr FROM Official;
©
2013 SAP AG. All rights reserved.
NAME ---Mr A Mr B Ms C Ms D Mr E Mr F Ms G Ms H Mr I
PNR --P01 P02 P03 P04 P05 P06 P07 P08 P09
105
Tuple Variables Or Table Aliases
Using Tuple Variables, also known as Table Aliases
©
2013 SAP AG. All rights reserved.
106
Tuple Variables aka Table Aliases
You can use tuple variables in the projection list. Tuple variables are defined in the FROM clause and also referred to as table aliases in SAP HANA (and most other DBMSs).
SELECT o.Name, o.PNr FROM Official o; You can use the (optional) key word AS in the definition of a tuple variable:
SELECT o.Name, o.PNr FROM Official AS o;
©
2013 SAP AG. All rights reserved.
NAME ---Mr A Mr B Ms C Ms D Mr E Mr F Ms G Ms H Mr I
PNR --P01 P02 P03 P04 P05 P06 P07 P08 P09
107
Tuple Variables aka Table Aliases
Despite having defined table aliases the projection list may contain unqualified column names:
SELECT Name, PNr FROM Official o; A mix of qualified and unqualified column names is possible:
SELECT o.Name, PNr FROM Official o;
©
2013 SAP AG. All rights reserved.
NAME ---Mr A Mr B Ms C Ms D Mr E Mr F Ms G Ms H Mr I
PNR --P01 P02 P03 P04 P05 P06 P07 P08 P09
108
Tuple Variables aka Table Aliases
If a table alias is defined in the FROM clause, you are not allowed to use the corresponding table name for qualification of a column name:
SELECT Official.Name FROM Official o;
SELECT Official.Name FROM Official;
SELECT o.Name FROM Official o;
SELECT o.Name FROM Official;
SELECT Name FROM Official o;
SELECT Name FROM Official;
©
2013 SAP AG. All rights reserved.
109
Case Differentiation
Can the projection list contain columns that are based on a case differentiation?
©
2013 SAP AG. All rights reserved.
110
Case Differentiation
The projection list can contain columns that are based on a case differentiation These columns can be named explicitly:
SELECT *, CASE WHEN WHEN ELSE END AS FROM Car;
©
2013 SAP AG. All rights reserved.
HP < 120 THEN 'low' HP >= 120 AND HP < 180 THEN 'medium' 'high' Rating
111
Case Differentiation CARID ----F01 F02 F03 F04 F05 F06 F07 F08 F09 F10 F11 F12 F13 F14 F15 F16 F17 F18 F19 F20
©
PLATENUMBER ----------HD-V 106 HD-VW 4711 HD-JA 1972 HD-AL 1002 HD-MM 3206 HD-VW 1999 HD-ML 3206 HD-IK 1002 HD-UP 13 HD-MT 507 HD-MM 208 HD-XY 4711 HD-IK 1001 HD-MM 1977 HD-MB 3030 ? HD-Y 333 HD-MQ 2006 HD-VW 2012 ?
2013 SAP AG. All rights reserved.
BRAND -------Fiat VW BMW Mercedes Mercedes Audi Audi VW Skoda BMW BMW Skoda Renault Mercedes Skoda Opel Audi Renault VW Audi
COLOR -----red black blue white black yellow blue black red black green red red white black green orange red black green
HP --75 120 184 136 170 260 116 160 105 140 184 105 136 170 136 120 184 90 125 184
OWNER ----H06 H03 H03 H07 H03 H05 H03 H07 H02 H04 H02 H04 H07 H03 H03 ? H09 H03 H01 ?
RATING -----low medium high medium medium high low medium low medium high low medium medium medium medium high low medium high
112
Case Differentiation
If a case differentiation does not correspond to any of the listed cases, a NULL value is returned:
SELECT CarID, CASE Color WHEN 'red' WHEN 'green' WHEN 'yellow' WHEN 'blue' WHEN 'white' WHEN 'black' END AS Color FROM Car;
©
2013 SAP AG. All rights reserved.
THEN THEN THEN THEN THEN THEN
'FF0000' '00FF00' 'FFFF00' '0000FF' 'FFFFFF' '000000'
CARID ----F01 F02 F03 F04 F05 F06 F07 F08 F09 F10 F11 F12 F13 F14 F15 F16 F17 F18 F19 F20
COLOR -----FF0000 000000 0000FF FFFFFF 000000 FFFF00 0000FF 000000 FF0000 000000 00FF00 FF0000 FF0000 FFFFFF 000000 00FF00 ? FF0000 000000 00FF00
113
Case Differentiation
Both the THEN and the ELSE branch can contain references to table columns:
SELECT CarID, CASE WHEN PlateNumber IS NOT NULL THEN PlateNumber ELSE 'The Car is not registered!' END AS PlateNumber, CASE Brand WHEN 'Mercedes' THEN 'Mercedes-Benz' WHEN 'VW' THEN 'Volkswagen' ELSE Brand END AS Brand, Color, HP FROM Car;
©
2013 SAP AG. All rights reserved.
114
Case Differentiation CARID ----F01 F02 F03 F04 F05 F06 F07 F08 F09 F10 F11 F12 F13 F14 F15 F16 F17 F18 F19 F20
©
PLATENUMBER -------------------------HD-V 106 HD-VW 4711 HD-JA 1972 HD-AL 1002 HD-MM 3206 HD-VW 1999 HD-ML 3206 HD-IK 1002 HD-UP 13 HD-MT 507 HD-MM 208 HD-XY 4711 HD-IK 1001 HD-MM 1977 HD-MB 3030 The Car is not registered! HD-Y 333 HD-MQ 2006 HD-VW 2012 The Car is not registered!
2013 SAP AG. All rights reserved.
BRAND ------------Fiat Volkswagen BMW Mercedes-Benz Mercedes-Benz Audi Audi Volkswagen Skoda BMW BMW Skoda Renault Mercedes-Benz Skoda Opel Audi Renault Volkswagen Audi
COLOR -----red black blue white black yellow blue black red black green red red white black green orange red black green
HP --75 120 184 136 170 260 116 160 105 140 184 105 136 170 136 120 184 90 125 184
115
Duplicate Elimination
SELECT DISTINCT
©
2013 SAP AG. All rights reserved.
116
Duplicate Elimination
A table with a primary key does not contain duplicates
SELECT * FROM Car; CARID ----… F04 F05 … F09 … F14 F15 …
©
2013 SAP AG. All rights reserved.
PLATENUMBER ----------… HD-AL 1002 HD-MM 3206 … HD-UP 13 … HD-MM 1977 HD-MB 3030 …
BRAND -------… Mercedes Mercedes … Skoda … Mercedes Skoda …
COLOR ----… white black … red … white black …
HP --… 136 170 … 105 … 170 136 …
OWNER ----… H07 H03 … H02 … H03 H03 …
117
Duplicate Elimination
Duplicates can occur when a key column is not included in the projection list:
SELECT Brand FROM Car;
©
2013 SAP AG. All rights reserved.
BRAND -------… Mercedes Mercedes … Skoda … Mercedes Skoda …
118
Duplicate Elimination
If duplicate elimination is not intended, you can specify the keyword ALL:
SELECT ALL Brand FROM Car;
©
2013 SAP AG. All rights reserved.
BRAND -------… Mercedes Mercedes … Skoda … Mercedes Skoda …
119
Duplicate Elimination
The keyword
DISTINCT ensures that the result table contains no duplicates:
SELECT DISTINCT Brand FROM Car;
©
2013 SAP AG. All rights reserved.
BRAND -------Fiat VW BMW Mercedes Audi Skoda Renault Opel
120
Duplicate Elimination
NULL values are treated in duplicate elimination as "normal" values The result table contains (at most) one row that consists entirely of NULL values:
SELECT DISTINCT Overtime FROM Official;
©
2013 SAP AG. All rights reserved.
OVERTIME -------10 20 ? 18 22
121
Duplicate Elimination The "duplicate" term always refers to full result rows:
SELECT Brand, Color FROM Car;
Duplicates
©
2013 SAP AG. All rights reserved.
BRAND -------… Mercedes Mercedes Skoda Skoda Mercedes Skoda …
COLOR ----… white black red red white black …
122
Duplicate Elimination
If a projection list contains multiple columns, DISTINCT always refers to the combination of all these columns:
SELECT DISTINCT Brand, Color FROM Car;
©
2013 SAP AG. All rights reserved.
BRAND -------… Mercedes Mercedes Skoda Skoda …
COLOR ----… white black red black …
123
Duplicate Elimination
DISTINCT can also be used if the result table should contain all the columns If the (source) table has a (primary) key DISTINCT is not required: SELECT DISTINCT * FROM Car; CARID ----… F04 F05 … F09 … F14 F15 …
©
2013 SAP AG. All rights reserved.
PLATENUMBER ----------… HD-AL 1002 HD-MM 3206 … HD-UP 13 … HD-MM 1977 HD-MB 3030 …
BRAND -------… Mercedes Mercedes … Skoda … Mercedes Skoda …
COLOR ----… white black … red … white black …
HP --… 136 170 … 105 … 170 136 …
OWNER ----… H07 H03 … H02 … H03 H03 …
124
Sorting
ORDER BY
©
2013 SAP AG. All rights reserved.
125
Sorting
The result table can be sorted by a specific column:
SELECT Brand, Color FROM Car ORDER BY Brand;
©
2013 SAP AG. All rights reserved.
BRAND -------Audi Audi Audi Audi BMW BMW BMW Fiat Mercedes Mercedes Mercedes Opel Renault Renault Skoda Skoda Skoda VW VW VW
COLOR -----yellow blue orange green blue black green red white black white green red red red red black black black black 126
Sorting
A descending sorting is possible:
SELECT Brand, Color FROM Car ORDER BY Brand DESC;
©
2013 SAP AG. All rights reserved.
BRAND -------VW VW VW Skoda Skoda Skoda Renault Renault Opel Mercedes Mercedes Mercedes Fiat BMW BMW BMW Audi Audi Audi Audi
COLOR -----black black black black red red red red green white black white red green black blue green orange blue yellow 127
Sorting
To sort ascending apply the optional keyword
SELECT Brand, Color FROM Car ORDER BY Brand ASC;
©
2013 SAP AG. All rights reserved.
ASC:
BRAND -------Audi Audi Audi Audi BMW BMW BMW Fiat Mercedes Mercedes Mercedes Opel Renault Renault Skoda Skoda Skoda VW VW VW
COLOR -----yellow blue orange green blue black green red white black white green red red red red black black black black
128
Sorting
The sorting can be applied using a column that does not appear in the projection list:
SELECT Brand, Color FROM Car ORDER BY PlateNumber;
©
2013 SAP AG. All rights reserved.
BRAND -------Opel Audi Renault Mercedes VW BMW Skoda Audi Mercedes BMW Mercedes Renault BMW Skoda Fiat Audi VW VW Skoda Audi
COLOR -----green green red white black blue black blue white green black red black red red yellow black black red orange 129
Sorting
You can sort using a combination of columns:
SELECT Brand, Color FROM Car ORDER BY Brand ASC, Color DESC;
©
2013 SAP AG. All rights reserved.
BRAND -------Audi Audi Audi Audi BMW BMW BMW Fiat Mercedes Mercedes Mercedes Opel Renault Renault Skoda Skoda Skoda VW VW VW
COLOR -----yellow orange green blue green blue black red white white black green red red red red black black black black 130
Sorting
Instead of the column names in the ORDER BY clause, the column numbers (based on the projection list) can be used:
SELECT Brand, Color FROM Car ORDER BY 1 ASC, 2 DESC;
©
2013 SAP AG. All rights reserved.
BRAND -------Audi Audi Audi Audi BMW BMW BMW Fiat Mercedes Mercedes Mercedes Opel Renault Renault Skoda Skoda Skoda VW VW VW
COLOR -----yellow orange green blue green blue black red white white black green red red red red black black black black 131
Sorting
If result columns are named explicitly, you can refer to the new name for sorting:
SELECT Brand AS Manufacturer, Color FROM Car ORDER BY Manufacturer ASC, Color DESC;
©
2013 SAP AG. All rights reserved.
MANUFACTURER -----------Audi Audi Audi Audi BMW BMW BMW Fiat Mercedes Mercedes Mercedes Opel Renault Renault Skoda Skoda Skoda VW VW VW
COLOR -----yellow orange green blue green blue black red white white black green red red red red black black black black 132
Sorting
If result columns are explicitly renamed, you can still reference the original name in the ORDER BY clause:
SELECT Brand AS Manufacturer, Color FROM Car ORDER BY Brand ASC, Color DESC;
©
2013 SAP AG. All rights reserved.
MANUFACTURER -----------Audi Audi Audi Audi BMW BMW BMW Fiat Mercedes Mercedes Mercedes Opel Renault Renault Skoda Skoda Skoda VW VW VW
COLOR -----yellow orange green blue green blue black red white white black green red red red red black black black black 133
Sorting
You can sort based on calculated values. Sorting criteria: How much is the power below 200 kW?
SELECT CarID, Brand, HP FROM Car ORDER BY 200 - HP / 1.36 ASC;
©
2013 SAP AG. All rights reserved.
CARID ----F06 F03 F11 F17 F20 F05 F14 F08 F10 F04 F13 F15 F19 F02 F16 F07 F09 F12 F18 F01
BRAND -------Audi BMW BMW Audi Audi Mercedes Mercedes VW BMW Mercedes Renault Skoda VW VW Opel Audi Skoda Skoda Renault Fiat
HP --260 184 184 184 184 170 170 160 140 136 136 136 125 120 120 116 105 105 90 75 134
Sorting
You can reference functions in the ORDER BY clause:
SELECT Name, Birthday FROM Owner ORDER BY YEAR(Birthday) DESC, Name ASC; NAME -----Mr X Mr Z Ms Y Ms U Ms W Mr V Ms T HDM AG IKEA SAP AG ©
2013 SAP AG. All rights reserved.
BIRTHDAY ---------1986-08-30 1986-02-03 1986-02-10 1966-05-11 1957-06-01 1952-04-21 1934-06-20 ? ? ? 135
Top-n Clause
How many rows are in the result set?
©
2013 SAP AG. All rights reserved.
136
Top-n Clause
You can determine how many rows should be included in the query result (maximum). What are the ten most powerful vehicles (based on horse power)?
SELECT TOP 10 CarID, Brand, Color, HP FROM Car CARID BRAND ORDER BY 4 DESC; ----- -------F06 F20 F17 F11 F03 F14 F05 F08 F10 F15 ©
2013 SAP AG. All rights reserved.
Audi Audi Audi BMW BMW Mercedes Mercedes VW BMW Skoda
COLOR -----yellow green orange green blue white black black black black
HP --260 184 184 184 184 170 170 160 140 136 137
Top-n Clause
You can also use the Top-n Clause if the result table should include all columns. What are the 5 most powerful vehicles (based on horse power)?
SELECT TOP 5 * FROM Car ORDER BY HP DESC; CARID ----F06 F20 F17 F11 F03
©
2013 SAP AG. All rights reserved.
PLATENUMBER ----------HD-VW 1999 ? HD-Y 333 HD-MM 208 HD-JA 1972
BRAND ----Audi Audi Audi BMW BMW
COLOR -----yellow green orange green blue
HP --260 184 184 184 184
OWNER ----H05 ? H09 H02 H03
138
Top-n Clause
You can combine the Top-n Clause with the keyword DISTINCT. What are the 7 highest HP values?
SELECT TOP 7 DISTINCT HP FROM Car ORDER BY 1 DESC;
©
2013 SAP AG. All rights reserved.
HP --260 184 170 160 140 136 125
139
Top-n Clause
No error is thrown if you request more rows than available. The result set will not be filled with additional, “artificial” rows. 570 different colors are requested:
SELECT TOP 570 DISTINCT Color FROM Car;
©
2013 SAP AG. All rights reserved.
COLOR -----red black blue white yellow green orange
140
Top-n Clause
It is possible to request 0 result rows. In this case the result set does not contain any row. Zero colors are requested:
SELECT TOP 0 Color FROM Car;
©
2013 SAP AG. All rights reserved.
COLOR -----
141
Limit And Offset Clause
You can use the Limit clause as alternative to the Top-n clause. The Limit clause comes at the very end of the SELECT statement. What are the 5 most powerful vehicles (based on horse power)?
SELECT * FROM Car ORDER BY HP DESC LIMIT 5; CARID ----F06 F20 F17 F11 F03
©
2013 SAP AG. All rights reserved.
PLATENUMBER ----------HD-VW 1999 ? HD-Y 333 HD-MM 208 HD-JA 1972
BRAND ----Audi Audi Audi BMW BMW
COLOR -----yellow green orange green blue
HP --260 184 184 184 184
OWNER ----H05 ? H09 H02 H03
142
Limit And Offset Clause
The Limit clause can be combined with the Offset clause to skip records. This allows you to read result sets “page by page”. What are the next 5 most powerful vehicles (based on horse power)?
SELECT * FROM Car ORDER BY HP DESC LIMIT 5 CARID OFFSET 5; ----F78 F77 F14 F05 F07
©
2013 SAP AG. All rights reserved.
PLATENUMBER ----------HD-MT 2510 HD-MT 2509 HD-MM 1977 HD-MM 3206 HD-IK 1002
BRAND -------? ? Mercedes Mercedes VW
COLOR -----green red white black black
HP --260 184 184 184 184
OWNER ----? ? H03 H03 H07
143
WHERE Clause
Which rows are included in the result set?
©
2013 SAP AG. All rights reserved.
144
WHERE Clause
The WHERE clause is used to filter rows. It is used to extract only those rows that fulfill a specified criterion.
SELECT PlateNumber, Brand, Color FROM Car WHERE Brand = 'BMW'; PLATENUMBER ----------HD-JA 1972 HD-MT 507 HD-MM 208
©
2013 SAP AG. All rights reserved.
BRAND ----BMW BMW BMW
COLOR ----blue black green
145
WHERE Clause
You can reference numeric columns in the WHERE clause.
SELECT HP, Brand, Color FROM Car WHERE HP >= 170;
©
2013 SAP AG. All rights reserved.
HP --184 170 260 184 170 184 184
BRAND -------BMW Mercedes Audi BMW Mercedes Audi Audi
COLOR -----blue black yellow green white orange green
146
WHERE Clause
You can reference a column in the WHERE clause that is not included in the projection list.
SELECT Brand, Color FROM Car WHERE HP >= 170;
©
2013 SAP AG. All rights reserved.
BRAND -------BMW Mercedes Audi BMW Mercedes Audi Audi
COLOR -----blue black yellow green white orange green
147
WHERE Clause
You can check for NULL in the WHERE clause:
SELECT CarID, Brand, Color FROM Car WHERE PlateNumber IS NULL; CARID ----F16 F20
©
2013 SAP AG. All rights reserved.
BRAND ----Opel Audi
COLOR ----green green
148
WHERE Clause
You can check for IS in the WHERE clause:
NOT NULL
SELECT CarID, Brand, Color FROM Car WHERE PlateNumber IS NOT NULL;
©
2013 SAP AG. All rights reserved.
CARID ----F01 F02 F03 F04 F05 F06 F07 F08 F09 F10 F11 F12 F13 F14 F15 F17 F18 F19
BRAND -------Fiat VW BMW Mercedes Mercedes Audi Audi VW Skoda BMW BMW Skoda Renault Mercedes Skoda Audi Renault VW
COLOR -----red black blue white black yellow blue black red black green red red white black orange red black
149
WHERE Clause
You can check if values are included IN a value-list.
SELECT Brand, Color FROM Car WHERE Color IN ('red', 'blue', 'orange'); BRAND ------Fiat BMW Audi Skoda Skoda Renault Audi Renault ©
2013 SAP AG. All rights reserved.
COLOR -----red blue blue red red red orange red 150
WHERE Clause
You can check if values are included in an interval.
SELECT PlateNumber, Brand, Color, HP FROM Car WHERE HP BETWEEN 140 AND 170; PLATENUMBER ----------HD-MM 3206 HD-IK 1002 HD-MT 507 HD-MM 1977
©
2013 SAP AG. All rights reserved.
BRAND -------Mercedes VW BMW Mercedes
COLOR ----black black black white
HP --170 160 140 170
151
WHERE Clause
You can reference calculated values in the WHERE clause. We do not recommend this option for performance reasons. Which cars have a power of at least 125 kW?
SELECT CarID, Brand, HP FROM Car WHERE HP / 1.36 >= 125;
©
2013 SAP AG. All rights reserved.
CARID ----F03 F05 F06 F11 F14 F17 F20
BRAND -------BMW Mercedes Audi BMW Mercedes Audi Audi
HP --184 170 260 184 170 184 184
152
WHERE Clause
You can use functions in the WHERE clause.
SELECT * FROM Owner WHERE YEAR(Birthday) = 1986; OWNERID ------H08 H09 H10
©
2013 SAP AG. All rights reserved.
NAME ---Mr X Ms Y Mr Z
BIRTHDAY ---------1986-08-30 1986-02-10 1986-02-03
CITY --------Walldorf Sinsheim Ladenburg
153
WHERE Clause
You can use search patterns in the WHERE clause.
SELECT PlateNumber, Brand, Color, HP FROM Car WHERE PlateNumber LIKE '%MM%'; PLATENUMBER ----------HD-MM 3206 HD-MM 208 HD-MM 1977
©
2013 SAP AG. All rights reserved.
BRAND -------Mercedes BMW Mercedes
COLOR ----black green white
HP --170 184 170
154
LIKE Predicate The wildcard character % (percentage sign) represents any string containing no, one, or multiple characters. The wildcard character _ (underscore) represents any single character. WHERE MyColumn LIKE 'M%'
String starting with “M“
WHERE MyColumn LIKE 'M _'
String of two characters starting with “M“
WHERE MyColumn LIKE '%M'
String ending with “M“
WHERE MyColumn LIKE '%M%'
String containing “M“
WHERE MyColumn LIKE '_ _ _'
String with length 3
WHERE MyColumn LIKE '_ _ _ _T_M%'
“T“ on fifth and “M“ on seventh position
©
2013 SAP AG. All rights reserved.
155
LIKE Predicate If you want to search for the percentage sign (%) or underscore (_) itself, you have to place an ESCAPE character in front. You can choose the ESCAPE character (with some restrictions). LIKE '$%%' ESCAPE '$'
String starting with a percentage sign
LIKE '$_ _' ESCAPE '$'
String of two characters starting with an underscore
LIKE '%$%' ESCAPE '$'
String ending with a percentage sign
LIKE '%$%%' ESCAPE '$'
String containing a percentage sign
LIKE '%$_$%%' ESCAPE '$'
String containing an underscore followed by a percentage sign
LIKE '_ _ _ _ $%_ $ _%' ESCAPE '$'
“%“ on fifth and “_“ on seventh position.
©
2013 SAP AG. All rights reserved.
156
LIKE Predicate If you need to search for the escape character within a string, you need to put the escape character in front of itself to mask it. LIKE '$%%$$' ESCAPE '$'
String starting with a percentage sign and ending with $
LIKE '$_%$$%$_' ESCAPE '$'
String starting and ending with an underscore and containing the escape character $
LIKE '%$%$$%' ESCAPE '$'
String containing a percentage sign followed by $
LIKE '%$_%$$' ESCAPE '$'
String containing an underscore and ending with $
LIKE '_ _ _ _ $%_ $$%' ESCAPE '$'
“%“ on fifth and “$“ on seventh position
©
2013 SAP AG. All rights reserved.
157
WHERE Clause
You can use compound WHERE clauses.
SELECT PlateNumber, Brand, Color FROM Car WHERE Brand = 'Skoda' AND Color = 'red'; PLATENUMBER ----------HD-UP 13 HD-XY 4711
©
2013 SAP AG. All rights reserved.
BRAND ----Skoda Skoda
COLOR ----red red
158
WHERE Clause
You can reference the same column multiple times.
SELECT PlateNumber, Brand, Color FROM Car WHERE Brand = 'Skoda' OR Brand = 'BMW'; PLATENUMBER ----------HD-JA 1972 HD-UP 13 HD-MT 507 HD-MM 208 HD-XY 4711 HD-MB 3030
©
2013 SAP AG. All rights reserved.
BRAND ----BMW Skoda BMW BMW Skoda Skoda
COLOR ----blue red black green red black
159
WHERE Clause
You can use brackets.
SELECT PlateNumber, Brand, Color FROM Car WHERE (Brand = 'Skoda' OR Brand = 'BMW') AND Color = 'black'; PLATENUMBER ----------HD-MT 507 HD-MB 3030
©
2013 SAP AG. All rights reserved.
BRAND ----BMW Skoda
COLOR ----black black
160
WHERE Clause The license plate started with "HD" for Heidelberg I'm sure I saw an "M" I clearly remember the digits "2" and "6" The "2" was definitely left of the "6" Between the "2" and the "6" was exactly one digit It was neither Skoda, nor a VW The car was blue or green CARID ----F07
PLATENUMBER ----------HD-ML 3206
BRAND ----Audi
COLOR ----blue
HP --116
SELECT * FROM Car WHERE PlateNumber LIKE 'HD-%M% %2_6%' AND Brand <> 'Skoda' AND Brand <> 'VW' AND (Color = 'blue' OR Color = 'green');
©
2013 SAP AG. All rights reserved.
OWNER ----H03
161
Operator Precedence
What is the precedence of the different operators?
©
2013 SAP AG. All rights reserved.
162
Operator Precedence
The operators have the precedence indicated by the table below: Precedence
Operator
Explanation
Highest
()
parentheses
‒
unary minus
*, /
multiplication, division
+, ‒
addition, subtraction
=, <, <=, >, >=, <>, IS NULL, LIKE, BETWEEN
Lowest
©
2013 SAP AG. All rights reserved.
comparison
NOT
logical negation
AND
conjunction
OR
disjunction
163
Unit 2: Reading Data From A Table And View Summary
You should now be able to: • Write simple database queries using SQL‘s SELECT statement
• • • • • • •
©
Project columns in and out of queries using the SELECT clause Avoid duplicates in SELECT statement result sets Include columns based on conditions Use built-in functions in column lists and WHERE clauses Limit results sets to the first N rows Ensure a specific order in SELECT statement result sets Restrict the result set using the WHERE clause
2013 SAP AG. All rights reserved.
164
Unit 3 Aggregating Data
165
Unit 3: Aggregating Data Learning Objectives After completing this unit, you will be able to: • Determine aggregated values on table columns using a single SELECT statement • List the aggregate functions supported by HANA • Determine such aggregated values for groups of rows, using the GROUP BY clause • Filter such groups using the HAVING clause
©
2013 SAP AG. All rights reserved.
166
Aggregate Expressions
Calculations across multiple rows
©
2013 SAP AG. All rights reserved.
167
Aggregate Expressions
Count(*)
You can calculate the number of rows in the result set using COUNT .
Rows containing only NULL values are included.
What is the quantity of cars with the brand “Audi“?
SELECT COUNT(*) FROM Car WHERE Brand = 'Audi';
©
2013 SAP AG. All rights reserved.
COUNT(*) -------4
168
Count(*)
Aggregate Expressions
You can explicitly name columns created by aggregate expressions.
What is the quantity of cars with the brand “Audi“?
SELECT COUNT(*) AS "Quantity of Audi" FROM Car WHERE Brand = 'Audi'; Quantity of Audi ---------------4
©
2013 SAP AG. All rights reserved.
169
Aggregate Expressions
Count()
You can calculate the number of values within a single column. NULL values are not included.
You can only use a single column as parameter of COUNT.
How many cars are registered to an owner? This does not calculate the number of different owners!
SELECT COUNT(Owner) FROM Car;
©
2013 SAP AG. All rights reserved.
COUNT(OWNER) -----------18
170
Aggregate Expressions
Count(DISTINCT )
You can calculate the number of different NOT-NULL-values of a certain column. You can use only a single column as parameter for COUNT DISTINCT. NULL values are not included.
How many different owners have a registered car?
SELECT COUNT(DISTINCT Owner) FROM Car; COUNT(DISTINCT OWNER) --------------------8
©
2013 SAP AG. All rights reserved.
171
Aggregate Expressions
SELECT * FROM T; SELECT S FROM T;
S ? ? ? X X
SELECT COUNT(*) FROM T;
COUNT(*) -------5
SELECT COUNT(S) FROM T;
COUNT(S) -------2
SELECT COUNT(DISTINCT *) FROM T; SELECT COUNT(DISTINCT S) FROM T;
©
2013 SAP AG. All rights reserved.
COUNT(DISTINCT S) ----------------1
172
Aggregate Expressions
MIN/MAX()
You can calculate the minimum or maximum value in a column.
What is the horsepower range of the registered cars?
SELECT MIN(HP), MAX(HP) FROM Car;
©
2013 SAP AG. All rights reserved.
MIN(HP) ------75
MAX(HP) ------260
173
Aggregate Expressions
You can combine aggregate expressions and “normal” functions.
In which year was the youngest owner born?
SELECT MAX(YEAR(Birthday)) AS Year FROM Owner;
YEAR ---1986
SELECT YEAR(MAX(Birthday)) AS Year FROM Owner;
©
2013 SAP AG. All rights reserved.
174
Aggregate Expressions
The sequence of nested functions can be relevant.
What is the lowest or highest HP value?
SELECT ABS(MAX(0 - HP)) FROM Car;
SELECT MAX(ABS(0 - HP)) FROM Car;
©
2013 SAP AG. All rights reserved.
ABS(MAX(0-HP)) -------------75
MAX(ABS(0-HP)) -------------260
175
Aggregate Expressions
The WHERE clause is included in the minimum and maximum calculation.
What is the horsepower range of BMWs?
SELECT MIN(HP), MAX(HP) FROM Car WHERE Brand = 'BMW';
©
2013 SAP AG. All rights reserved.
MIN(HP) ------140
MAX(HP) ------184
176
Aggregate Expressions
SUM()
You can sum up the values in a column. The WHERE clause is included in the summation. NULL values in the column are ignored. Individual NULL values contained in the column do not result in NULL for the sum. (as long as there is at least one numeric value)
What is the sum of horsepower for all Audis?
SELECT SUM(HP) FROM Car WHERE Brand = 'Audi';
©
2013 SAP AG. All rights reserved.
SUM(HP) ------744
177
Aggregate Expressions
You can sum up the distinct values of a column. The WHERE clause is included in the summation. NULL values of the column are ignored. Individual NULL values contained in the column do not result in NULL for the sum. (as long as there is at least one numeric value) If a value occurs multiple times, it is still counted only once when summing
SELECT SUM(DISTINCT HP) FROM Car WHERE Brand = 'Audi';
©
SUM(DISTINCT )
2013 SAP AG. All rights reserved.
SUM(DISTINCT HP) ---------------560
178
Aggregate Expressions
AVG()
You can calculate the average of all values in a column. The WHERE clause is included in the calculation. NULL values in the column are completely ignored (in the numerator and denominator) Individual NULL values contained in the column do not result in NULL for the average.
What is the average horsepower of Audi?
SELECT AVG(HP) FROM Car WHERE Brand = 'Audi';
©
2013 SAP AG. All rights reserved.
AVG(HP) ------186
179
Aggregate Expressions
AVG(DISTINCT )
You can calculate the average of all distinct values in a column. The WHERE clause is included in the calculation. NULL values in the column are completely ignored (in the numerator and denominator) Individual NULL values contained in the column do not result in NULL for the average. If a value occurs multiple times, it is still counted only once for the average calculation.
SELECT AVG(DISTINCT HP) FROM Car WHERE Brand = 'Audi'; AVG(DISTINCT HP) ----------------------------------186.6666666666666666666666666666667
©
2013 SAP AG. All rights reserved.
180
Aggregate Expressions
Which aggregate expressions are available in SAP HANA?
©
2013 SAP AG. All rights reserved.
181
Aggregate Expressions
SAP HANA provides the following aggregate expressions:
©
2013 SAP AG. All rights reserved.
Aggregate Name
Description
COUNT
Count
MIN
Minimum
MAX
Maximum
SUM
Sum
AVG
Average
STDDEV
Standard Deviation
VAR
Variance
182
Group By
GROUP BY
©
2013 SAP AG. All rights reserved.
183
Group By
A table can be divided into (disjoint) groups of rows A group is represented in the query result by a single row Aggregate expressions will be evaluated separately for each group
What is the number of cars per brand?
SELECT Brand, COUNT(*) FROM Car GROUP BY Brand;
©
2013 SAP AG. All rights reserved.
BRAND -------Fiat VW BMW Mercedes Audi Skoda Renault Opel
COUNT(*) -------1 3 3 3 4 3 2 1
184
Group By
What is the highest horse power value per brand?
SELECT Brand, MAX(HP) FROM Car GROUP BY Brand;
©
2013 SAP AG. All rights reserved.
BRAND -------Fiat VW BMW Mercedes Audi Skoda Renault Opel
MAX(HP) ------75 160 184 170 260 136 136 120
185
Group By
NULL values of the GROUP BY column are treated as “normal” values creating a single group. How often a certain overtime value occurs?
SELECT Overtime, COUNT(*) AS Frequency FROM Official GROUP BY Overtime; OVERTIME FREQUENCY -------10 20 ? 18 22
©
2013 SAP AG. All rights reserved.
--------3 1 3 1 1
186
Group By
You can combine GROUP BY with ORDER BY for sorting.
SELECT FROM GROUP BY ORDER BY
©
2013 SAP AG. All rights reserved.
Brand, MAX(HP) Car Brand 2 DESC, 1 ASC;
BRAND -------Audi BMW Mercedes VW Renault Skoda Opel Fiat
MAX(HP) ------260 184 170 160 136 136 120 75
187
Group By
You can use functions in the GROUP BY clause.
What is the number of owners per year of birth?
SELECT FROM GROUP BY ORDER BY
YEAR(Birthday), COUNT(*) Owner YEAR(Birthday) 2 DESC, 1 ASC; YEAR(BIRTHDAY) -------------? 1986 1934 1952 1957 1966
©
2013 SAP AG. All rights reserved.
COUNT(*) -------3 3 1 1 1 1
188
Group By
The WHERE clause is processed before the grouping. What is the number of black cars per brand? Only brands with black cars are included into the result set.
SELECT FROM WHERE GROUP BY
©
Brand, COUNT(*) Car Color = 'black' Brand;
2013 SAP AG. All rights reserved.
BRAND -------VW Mercedes BMW Skoda
COUNT(*) -------3 1 1 1
189
Group By
You can use a combination of columns in the GROUP BY clause.
SELECT Brand, Color, COUNT(*) FROM Car GROUP BY Brand, Color;
©
2013 SAP AG. All rights reserved.
BRAND -------Fiat VW BMW Mercedes Mercedes Audi Audi Skoda BMW BMW Renault Skoda Opel Audi Audi
COLOR -----red black blue white black yellow blue red black green red black green orange green
COUNT(*) -------1 3 1 2 1 1 1 2 1 1 2 1 1 1 1
190
Group By
You can explicitly rename result columns in combination with GROUP BY.
SELECT Brand AS Manufacturer, Color, COUNT(*) AS "# of cars" FROM Car GROUP BY Brand, Color; MANUFACTURER -----------Fiat VW BMW Mercedes Mercedes Audi Audi Skoda BMW BMW Renault …
©
2013 SAP AG. All rights reserved.
COLOR -----red black blue white black yellow blue red black green red …
# of cars --------1 3 1 2 1 1 1 2 1 1 2 …
191
HAVING Clause
What groups are included in the query result?
©
2013 SAP AG. All rights reserved.
192
HAVING
Using the HAVING clause you can specify which conditions a group must meet to be included in the result set. Which combinations of brand and color occur at least twice?
SELECT FROM GROUP BY HAVING
Brand, Color, COUNT(*) Car Brand, Color COUNT(*) >= 2; BRAND -------VW Mercedes Skoda Renault
©
2013 SAP AG. All rights reserved.
COLOR ----black white red red
COUNT(*) -------3 2 2 2
193
HAVING
The HAVING condition can reference columns not included in the projection list. What is the number of brand-color combinations, where at least one car has less than 120 HP? We want to analyze based on the quantity of all cars, not only the cars with less than 120 HP.
SELECT FROM GROUP BY HAVING
Brand, Color, COUNT(*) Car Brand, Color MIN(HP) < 120; BRAND ------Fiat Audi Skoda Renault
©
2013 SAP AG. All rights reserved.
COLOR ----red blue red red
COUNT(*) -------1 1 2 2
194
SELECT statement
What is the number of cars per brand, which are black or red? Rename the Brand column to "Manufacturer" Display only those manufacturers with at least 2 cars Sort the result set first descending by number of cars Sort the result set second ascending by manufacturer.
SELECT FROM WHERE GROUP BY HAVING ORDER BY
©
c.Brand AS "Manufacturer", COUNT(*) Car c Color IN ('black', 'red') Brand COUNT(*) >= 2 Manufacturer 2 DESC, Brand ASC;
2013 SAP AG. All rights reserved.
-----------Skoda VW Renault
COUNT(*) -------3 3 2
195
Unit 3: Aggregating Data Summary
You should now be able to: • Determine aggregated values on table columns using a single SELECT statement • List the aggregate functions supported by HANA • Determine such aggregated values for groups of rows, using the GROUP BY clause • Filter such groups using the HAVING clause
©
2013 SAP AG. All rights reserved.
196
Unit 4 Reading Data From Multiple Tables Part I
197
Unit 4: Reading Data From Multiple Tables Part I Learning Objectives After completing this unit, you will be able to: • Merge the result of several select statements using the UNION statement • • •
©
Combine data from several tables when querying data using JOIN constructs List the various types of Joins Explain the differences between the various types of Joins, and apply the right type of Join depending on the problem
2013 SAP AG. All rights reserved.
198
Access to multiple tables
How can I read data from multiple tables and views?
©
2013 SAP AG. All rights reserved.
199
Access to multiple tables
For read access to multiple database tables / views the following options are available: Combination of results from several partial queries (UNION) UNION ALL vs. UNION
Combination of several tables (JOIN) CROSS JOIN vs. INNER JOIN vs. OUTER JOIN Implicit JOIN vs. Explicit JOIN
Nested queries (Sub Query) Uncorrelated Sub Query vs. Correlated Sub Query
©
2013 SAP AG. All rights reserved.
200
UNION [ALL]
Combination of results from several partial queries
©
2013 SAP AG. All rights reserved.
201
UNION [ALL]
SELECT Column, Column, Column FROM Table WHERE Condition UNION ALL SELECT Column, Column, Column FROM Table WHERE Condition;
©
2013 SAP AG. All rights reserved.
202
UNION [ALL]
You can combine the result tables of multiple queries using UNION [ALL].
The individual result tables must have the same number of columns. The corresponding result columns must have compatible data types. The column names of the resulting output table are based on the first SELECT statement.
SELECT PNr, Name FROM Official WHERE Salary = 'A09' UNION ALL SELECT OwnerID, Name FROM Owner WHERE Birthday >= '1977-05-21';
©
2013 SAP AG. All rights reserved.
PNR --P01 P03 P06 H08 H09 H10
NAME ---Mr A Ms C Mr F Mr X Ms Y Mr Z
203
UNION [ALL]
Almost the same statement as on the previous slide – but with a changed sequence of partial queries. The column names of the resulting output table are based on the first SELECT statement.
SELECT OwnerID, Name FROM Owner WHERE Birthday >= '1977-05-21' UNION ALL SELECT PNr, Name FROM Official WHERE Salary = 'A09';
©
2013 SAP AG. All rights reserved.
OWNERID ------H08 H09 H10 P01 P03 P06
NAME ---Mr X Ms Y Mr Z Mr A Ms C Mr F
204
UNION [ALL]
With UNION [ALL] you can also explicitly rename the result columns. SELECT OwnerID AS "Person ID", Name FROM Owner WHERE Birthday >= '1977-05-21' UNION ALL SELECT PNr, Name FROM Official WHERE Salary = 'A09';
©
2013 SAP AG. All rights reserved.
Person ID --------H08 H09 H10 P01 P03 P06
NAME ---Mr X Ms Y Mr Z Mr A Ms C Mr F
205
UNION ALL
If the results of multiple partial queries overlap, the overall result includes duplicates with UNION ALL. SELECT PlateNumber, Brand, Color FROM Car WHERE Brand = 'Mercedes' UNION ALL SELECT PlateNumber, Brand, Color FROM Car WHERE Color = 'white';
Duplicates
©
2013 SAP AG. All rights reserved.
PLATENUMBER ----------HD-AL 1002 HD-MM 3206 HD-MM 1977 HD-AL 1002 HD-MM 1977
BRAND -------Mercedes Mercedes Mercedes Mercedes Mercedes
COLOR ----white black white white white
206
UNION
You use UNION instead of UNION ALL to eliminate duplicates.
SELECT FROM WHERE UNION SELECT FROM WHERE
PlateNumber, Brand, Color Car Brand = 'Mercedes' PlateNumber, Brand, Color Car Color = 'white'; PLATENUMBER ----------HD-AL 1002 HD-MM 3206 HD-MM 1977
©
2013 SAP AG. All rights reserved.
BRAND -------Mercedes Mercedes Mercedes
COLOR ----white black white 207
UNION [ALL]
You can use UNION [ALL] to combine result tables of multiple partial queries. SELECT FROM WHERE UNION SELECT FROM WHERE UNION SELECT FROM WHERE
©
PlateNumber, Brand, Color Car Brand = 'BMW' PlateNumber, Brand, Color Car Color = 'yellow'
PlateNumber, Brand, Color Car Color = 'orange';
2013 SAP AG. All rights reserved.
PLATENUMBER ----------HD-JA 1972 HD-MT 507 HD-MM 208 HD-VW 1999 HD-Y 333
BRAND ----BMW BMW BMW Audi Audi
COLOR -----blue black green yellow orange 208
Joining Tables
Joining tables
©
2013 SAP AG. All rights reserved.
209
Joining Tables
With JOIN semantics you can distinguish between: CROSS JOIN INNER JOIN OUTER JOIN –
LEFT OUTER JOIN
–
RIGHT OUTER JOIN
–
FULL OUTER JOIN
With JOIN syntax you can distinguish between: Implicit JOIN Explicit JOIN
©
2013 SAP AG. All rights reserved.
210
Joining Tables
CROSS JOIN (Cartesian Product)
©
2013 SAP AG. All rights reserved.
211
CROSS JOIN
Each row of the left table is connected to each row of the right table. There is no CROSS JOIN join condition.
L
©
… … … … … …
X 1 2 3 4 5
2013 SAP AG. All rights reserved.
… … … … … …
… … … … … …
R
… … … … … …
Y 3 4 5 6 7
… … … … … …
… … … … … …
212
CROSS JOIN
SELECT Column, Column, Column FROM Table, Table WHERE Condition; SELECT Column, Column, Column FROM Table CROSS JOIN Table WHERE Condition;
©
2013 SAP AG. All rights reserved.
213
Implicit CROSS JOIN
Each row of a table is connected to each row of the other table. The result set contains 10 * 20 = 200 rows
SELECT * FROM Owner, Car; OWNERID ------H01 H02 H03 … H01 H02 H03 … H01 H02 H03 … H10
©
2013 SAP AG. All rights reserved.
NAME -----Ms T Ms U SAP AG … Ms T Ms U SAP AG … Ms T Ms U SAP AG … Mr Z
BIRTHDAY ---------1934-06-20 1966-05-11 ? … 1934-06-20 1966-05-11 ? … 1934-06-20 1966-05-11 ? … 1986-02-03
CITY ---------Wiesloch Hockenheim Walldorf … Wiesloch Hockenheim Walldorf … Wiesloch Hockenheim Walldorf … Ladenburg
CARID ----F01 F01 F01 … F02 F02 F02 … F03 F03 F03 … F20
PLATENUMBER ----------HD-V 106 HD-V 106 HD-V 106 … HD-VW 4711 HD-VW 4711 HD-VW 4711 … HD-JA 1972 HD-JA 1972 HD-JA 1972 … ?
BRAND ----Fiat Fiat Fiat … VW VW VW … BMW BMW BMW … Audi
COLOR ----red red red … black black black … blue blue blue … green
HP --75 75 75 … 120 120 120 … 184 184 184 … 184
OWNER ----H06 H06 H06 … H03 H03 H03 … H03 H03 H03 … ?
214
Explicit CROSS JOIN
Each row of a table is connected to each row of the other table. The result set contains 10 * 20 = 200 rows
SELECT * FROM Owner CROSS JOIN Car; OWNERID ------H01 H02 H03 … H01 H02 H03 … H01 H02 H03 … H10
©
2013 SAP AG. All rights reserved.
NAME -----Ms T Ms U SAP AG … Ms T Ms U SAP AG … Ms T Ms U SAP AG … Mr Z
BIRTHDAY ---------1934-06-20 1966-05-11 ? … 1934-06-20 1966-05-11 ? … 1934-06-20 1966-05-11 ? … 1986-02-03
CITY ---------Wiesloch Hockenheim Walldorf … Wiesloch Hockenheim Walldorf … Wiesloch Hockenheim Walldorf … Ladenburg
CARID ----F01 F01 F01 … F02 F02 F02 … F03 F03 F03 … F20
PLATENUMBER ----------HD-V 106 HD-V 106 HD-V 106 … HD-VW 4711 HD-VW 4711 HD-VW 4711 … HD-JA 1972 HD-JA 1972 HD-JA 1972 … ?
BRAND ----Fiat Fiat Fiat … VW VW VW … BMW BMW BMW … Audi
COLOR ----red red red … black black black … blue blue blue … green
HP --75 75 75 … 120 120 120 … 184 184 184 … 184
OWNER ----H06 H06 H06 … H03 H03 H03 … H03 H03 H03 … ?
215
CROSS JOIN
You can specify a WHERE clause for the implicit and explicit CROSS JOIN.
SELECT * FROM Owner, Car WHERE HP > 250; OWNERID ------H01 H02 H03 H04 H05 H06 H07 H08 H09 H10
©
NAME -----Ms T Ms U SAP AG HDM AG Mr V Ms W IKEA Mr X Ms Y Mr Z
2013 SAP AG. All rights reserved.
BIRTHDAY ---------1934-06-20 1966-05-11 ? ? 1952-04-21 1957-06-01 ? 1986-08-30 1986-02-10 1986-02-03
SELECT * FROM Owner CROSS JOIN Car WHERE HP > 250; CITY ---------Wiesloch Hockenheim Walldorf Heidelberg Leimen Wiesloch Walldorf Walldorf Sinsheim Ladenburg
CARID ----F06 F06 F06 F06 F06 F06 F06 F06 F06 F06
PLATENUMBER ----------HD-VW 1999 HD-VW 1999 HD-VW 1999 HD-VW 1999 HD-VW 1999 HD-VW 1999 HD-VW 1999 HD-VW 1999 HD-VW 1999 HD-VW 1999
BRAND ----Audi Audi Audi Audi Audi Audi Audi Audi Audi Audi
COLOR -----yellow yellow yellow yellow yellow yellow yellow yellow yellow yellow
HP --260 260 260 260 260 260 260 260 260 260
OWNER ----H05 H05 H05 H05 H05 H05 H05 H05 H05 H05
216
CROSS JOIN
You can join a table to itself.
The result set contains 20 * 20 = 400 rows
SELECT * FROM Owner, Car WHERE HP > 250; CARID ----F01 F01 F01 … F02 F02 F02 … F03 F03 F03 … F20 ©
PLATENUMBER ----------HD-V 106 HD-V 106 HD-V 106 … HD-VW 4711 HD-VW 4711 HD-VW 4711 … HD-JA 1972 HD-JA 1972 HD-JA 1972 … ?
2013 SAP AG. All rights reserved.
BRAND ----Fiat Fiat Fiat … VW VW VW … BMW BMW BMW … Audi
COLOR ----red red red … black black black … blue blue blue … green
SELECT * FROM Owner CROSS JOIN Car WHERE HP > 250; HP --75 75 75 … 120 120 120 … 184 184 184 … 184
OWNER ----H06 H06 H06 … H03 H03 H03 … H03 H03 H03 … ?
CARID ----F01 F02 F03 … F01 F02 F03 … F01 F02 F03 … F20
PLATENUMBER ----------HD-V 106 HD-VW 4711 HD-JA 1972 … HD-V 106 HD-VW 4711 HD-JA 1972 … HD-V 106 HD-VW 4711 HD-JA 1972 … ?
BRAND ----Fiat VW BMW … Fiat VW BMW … Fiat VW BMW … Audi
COLOR ----red black blue … red black blue … red black blue … green
HP --75 120 184 … 75 120 184 … 75 120 184 … 184
OWNER ----H06 H03 H03 … H06 H03 H03 … H06 H03 H03 … ? 217
CROSS JOIN
You can combine more than two tables. The result set contains 10 * 20 * 3 = 600 rows
SELECT * FROM Owner CROSS JOIN Car CROSS JOIN Stolen;
SELECT * FROM Owner, Car, Stolen; OWNERID ------H01 H01 H01 … H01 H01 H01 … H02 H02 H02 … H10
©
NAME ---Ms T Ms T Ms T … Ms T Ms T Ms T … Ms U Ms U Ms U … Mr Z
BIRTHDAY ---------1934-06-20 1934-06-20 1934-06-20 … 1934-06-20 1934-06-20 1934-06-20 … 1966-05-11 1966-05-11 1966-05-11 … 1986-02-03
2013 SAP AG. All rights reserved.
CITY ---------Wiesloch Wiesloch Wiesloch … Wiesloch Wiesloch Wiesloch … Hockenheim Hockenheim Hockenheim … Ladenburg
CARID ----F01 F01 F01 … F02 F02 F02 … F01 F01 F01 … F20
PLATENUMBER ----------HD-V 106 HD-V 106 HD-V 106 … HD-VW 4711 HD-VW 4711 HD-VW 4711 … HD-V 106 HD-V 106 HD-V 106 … ?
BRAND ----Fiat Fiat Fiat … VW VW VW … Fiat Fiat Fiat … Audi
COLOR ----red red red … black black black … red red red … green
HP --75 75 75 … 120 120 120 … 75 75 75 … 184
OWNER ----H06 H06 H06 … H03 H03 H03 … H06 H06 H06 … ?
PLATENUMBER ----------HD-VW 1999 HD-V 106 HD-Y 333 … HD-VW 1999 HD-V 106 HD-Y 333 … HD-VW 1999 HD-V 106 HD-Y 333 … HD-Y 333
REGISTERED_AT ------------2012-06-20 2012-06-01 2012-05-21 … 2012-06-20 2012-06-01 2012-05-21 … 2012-06-20 2012-06-01 2012-05-21 … 2012-05-21
218
Joining Tables
INNER JOIN
©
2013 SAP AG. All rights reserved.
219
INNER JOIN
One row of the left table and one row of the right table are always joined to a common result row - provided that the JOIN condition is fulfilled. JOIN Condition: L.X
L
©
… … … … … …
X 1 2 3 4 5
2013 SAP AG. All rights reserved.
… … … … … …
= R.Y
… … … … … …
R
… … … … … …
Y 3 4 5 6 7
… … … … … …
… … … … … …
220
INNER JOIN
SELECT Column, Column, Column FROM Table, Table WHERE JOIN_Condition AND Supplementary_Condition; SELECT Column, Column, Column FROM Table JOIN Table ON JOIN_Condition WHERE Supplementary_Condition;
©
2013 SAP AG. All rights reserved.
221
Implicit INNER JOIN One row of a table and one row of another table are always connected to a common result row - provided that the JOIN condition is fulfilled. The JOIN condition is part of the WHERE clause To whom which car is registered?
SELECT * FROM Owner, Car WHERE OwnerID = Owner; OWNERID ------H06 H03 H03 H07 H03 …
©
NAME -----Ms W SAP AG SAP AG IKEA SAP AG …
BIRTHDAY ---------1957-06-01 ? ? ? ? …
2013 SAP AG. All rights reserved.
CITY -------Wiesloch Walldorf Walldorf Walldorf Walldorf …
CARID ----F01 F02 F03 F04 F05 …
PLATENUMBER ----------HD-V 106 HD-VW 4711 HD-JA 1972 HD-AL 1002 HD-MM 3206 …
BRAND -------Fiat VW BMW Mercedes Mercedes …
COLOR ----red black blue white black …
HP --75 120 184 136 170 …
OWNER ----H06 H03 H03 H07 H03 …
222
Explicit INNER JOIN
One row of a table and one row of another table are always connected to a common result row - provided that the JOIN condition is fulfilled. The JOIN condition is part of the JOIN operation (and not of the WHERE clause) To whom is which car registered?
SELECT * FROM Owner INNER JOIN Car ON OwnerID = Owner; OWNERID ------H06 H03 H03 H07 H03 …
©
NAME -----Ms W SAP AG SAP AG IKEA SAP AG …
BIRTHDAY ---------1957-06-01 ? ? ? ? …
2013 SAP AG. All rights reserved.
CITY -------Wiesloch Walldorf Walldorf Walldorf Walldorf …
CARID ----F01 F02 F03 F04 F05 …
PLATENUMBER ----------HD-V 106 HD-VW 4711 HD-JA 1972 HD-AL 1002 HD-MM 3206 …
BRAND -------Fiat VW BMW Mercedes Mercedes …
COLOR ----red black blue white black …
HP --75 120 184 136 170 …
OWNER ----H06 H03 H03 H07 H03 …
223
Explicit INNER JOIN
You can simply write "JOIN“ instead of "INNER JOIN“. The INNER JOIN is the most important JOIN variant (and therefore the default)
To whom is which car registered?
SELECT * FROM Owner JOIN Car ON OwnerID = Owner; OWNERID ------H06 H03 H03 H07 H03 …
©
NAME -----Ms W SAP AG SAP AG IKEA SAP AG …
BIRTHDAY ---------1957-06-01 ? ? ? ? …
2013 SAP AG. All rights reserved.
CITY -------Wiesloch Walldorf Walldorf Walldorf Walldorf …
CARID ----F01 F02 F03 F04 F05 …
PLATENUMBER ----------HD-V 106 HD-VW 4711 HD-JA 1972 HD-AL 1002 HD-MM 3206 …
BRAND -------Fiat VW BMW Mercedes Mercedes …
COLOR ----red black blue white black …
HP --75 120 184 136 170 …
OWNER ----H06 H03 H03 H07 H03 …
224
Implicit INNER JOIN
You can specify certain columns in the projection list also for the implicit JOIN. To whom is which car registered?
SELECT Name, Brand, Color FROM Owner, Car WHERE OwnerID = Owner;
©
2013 SAP AG. All rights reserved.
NAME -----Ms W SAP AG SAP AG IKEA SAP AG Mr V SAP AG IKEA Ms U HDM AG Ms U HDM AG IKEA SAP AG SAP AG Ms Y SAP AG Ms T
BRAND -------Fiat VW BMW Mercedes Mercedes Audi Audi VW Skoda BMW BMW Skoda Renault Mercedes Skoda Audi Renault VW
COLOR -----red black blue white black yellow blue black red black green red red white black orange red black
225
Explicit INNER JOIN
You can specify certain columns in the projection list also for the explicit JOIN. To whom is which car registered?
SELECT Name, Brand, Color FROM Owner JOIN Car ON OwnerID = Owner;
©
2013 SAP AG. All rights reserved.
NAME -----Ms W SAP AG SAP AG IKEA SAP AG Mr V SAP AG IKEA Ms U HDM AG Ms U HDM AG IKEA SAP AG SAP AG Ms Y SAP AG Ms T
BRAND -------Fiat VW BMW Mercedes Mercedes Audi Audi VW Skoda BMW BMW Skoda Renault Mercedes Skoda Audi Renault VW
COLOR -----red black blue white black yellow blue black red black green red red white black orange red black
226
Implicit INNER JOIN
You can also use table aliases or tuple variables in the projection list for the implicit JOIN.
The JOIN condition can also refer to table aliases or tuple variables. If column names are not unique, you must qualify them NAME with the table aliases or tuple variables. -----Ms W To whom is which car registered? SAP AG
SELECT o.Name, c.Brand, c.Color FROM Owner o, Car c WHERE o.OwnerID = c.Owner;
©
2013 SAP AG. All rights reserved.
SAP AG IKEA SAP AG Mr V SAP AG IKEA Ms U HDM AG Ms U HDM AG IKEA SAP AG SAP AG Ms Y SAP AG Ms T
BRAND -------Fiat VW BMW Mercedes Mercedes Audi Audi VW Skoda BMW BMW Skoda Renault Mercedes Skoda Audi Renault VW
COLOR -----red black blue white black yellow blue black red black green red red white black orange red black
227
Explicit INNER JOIN
You can use table aliases or tuple variables in the projection list for the explicit JOIN.
The JOIN condition can also refer to table aliases or tuple variables. If column names are not unique, you must qualify them NAME with the table aliases or tuple variables. -----Ms W To whom is which car registered? SAP AG
SELECT o.Name, c.Brand, c.Color FROM Owner o JOIN Car c ON o.OwnerID = c.Owner;
©
2013 SAP AG. All rights reserved.
SAP AG IKEA SAP AG Mr V SAP AG IKEA Ms U HDM AG Ms U HDM AG IKEA SAP AG SAP AG Ms Y SAP AG Ms T
BRAND -------Fiat VW BMW Mercedes Mercedes Audi Audi VW Skoda BMW BMW Skoda Renault Mercedes Skoda Audi Renault VW
COLOR -----red black blue white black yellow blue black red black green red red white black orange red black
228
Implicit INNER JOIN
You do not have to include a column from every involved table in the projection list.
Besides the JOIN condition the WHERE clause can contain additional conditions. To whom is a black car registered (at least one)?
SELECT DISTINCT o.Name FROM Owner o, Car c WHERE o.OwnerID = c.Owner AND c.Color = 'black';
©
2013 SAP AG. All rights reserved.
NAME -----Ms T SAP AG HDM AG IKEA
229
Explicit INNER JOIN
You do not need to include a column from every involved table in the
projection list. In addition to the JOIN condition you can specify a WHERE clause. To whom is a black car registered (at least one)?
SELECT DISTINCT o.Name FROM Owner o JOIN Car c ON o.OwnerID = c.Owner WHERE c.Color = 'black';
©
2013 SAP AG. All rights reserved.
NAME -----Ms T SAP AG HDM AG IKEA
230
Implicit INNER JOIN
You can explicitly rename result columns for the implicit JOIN. Sorting the result table is also possible To whom is a black car registered (at least one)?
SELECT DISTINCT o.Name AS "Owners' name" FROM Owner o, Car c WHERE o.OwnerID = c.Owner AND c.Color = 'black' ORDER BY o.Name; Owners'
name -----------HDM AG IKEA Ms T SAP AG
©
2013 SAP AG. All rights reserved.
231
Explicit INNER JOIN
You can explicitly rename result columns for the explicit JOIN. Sorting the result table is also possible To whom is a black car registered (at least one)?
SELECT DISTINCT o.Name AS "Owners' name" FROM Owner o JOIN Car c ON o.OwnerID = c.Owner WHERE c.Color = 'black' ORDER BY o.Name; Owners'
name -----------HDM AG IKEA Ms T SAP AG
©
2013 SAP AG. All rights reserved.
232
Implicit INNER JOIN
You can add a GROUP BY clause for the implicit Join. How many black cars are registered to which owner?
SELECT FROM WHERE GROUP BY ORDER BY
o.Name, COUNT(*) AS "# black cars" Owner o, Car c o.OwnerID = c.Owner AND c.Color = 'black' o.OwnerID, o.Name 2 DESC, 1 ASC; NAME -----SAP AG HDM AG IKEA Ms T
©
2013 SAP AG. All rights reserved.
# black cars -----------3 1 1 1
233
Explicit INNER JOIN
You can add a GROUP BY clause for the explicit Join. How many black cars are registered to which owner?
SELECT o.Name, COUNT(*) AS "# black cars" FROM Owner o JOIN Car c ON o.OwnerID = c.Owner WHERE c.Color = 'black' GROUP BY o.OwnerID, o.Name ORDER BY 2 DESC, 1 ASC; NAME # black -----SAP AG HDM AG IKEA Ms T
©
2013 SAP AG. All rights reserved.
cars -----------3 1 1 1
234
Implicit INNER JOIN
You can build the JOIN condition on multiple columns for the implicit JOIN. To whom within the EU is a black car registered (at least one)?
SELECT DISTINCT o.Name AS "Owners' name" FROM Owner_EU o, Car_EU c WHERE o.Country = c.Country AND o.OwnerID = c.Owner AND c.Color = 'black' Owners' name ORDER BY o.Name; -----------HDM AG IKEA Ms O Ms T SAP AG Señora R
©
2013 SAP AG. All rights reserved.
235
Explicit INNER JOIN
You can build the JOIN condition on multiple columns for the explicit JOIN. To whom within the EU is a black car registered (at least one)?
SELECT DISTINCT o.Name AS "Owners' name" FROM Owner_EU o JOIN Car_EU c ON o.Country = c.Country AND o.OwnerID = c.Owner WHERE c.Color = 'black' Owners' name ORDER BY o.Name; -----------HDM AG IKEA Ms O Ms T SAP AG Señora R
©
2013 SAP AG. All rights reserved.
236
Implicit INNER JOIN
You can join a table to itself. Who is the manager of which employee?
SELECT e.Name AS Employee, m.Name AS Manager FROM Official e, Official m WHERE e.Manager = m.PNr; EMPLOYEE -------Mr A Mr B Ms C Ms D Mr E Mr F Ms G Ms H
©
2013 SAP AG. All rights reserved.
MANAGER ------Ms D Ms D Ms D Mr I Ms H Ms H Ms H Mr I
237
Explicit INNER JOIN
You can join a table to itself also for the explicit JOIN. Who is the manager of which employee?
SELECT e.Name AS Employee, m.Name AS Manager FROM Official e JOIN Official m ON e.Manager = m.PNr; EMPLOYEE -------Mr A Mr B Ms C Ms D Mr E Mr F Ms G Ms H
©
2013 SAP AG. All rights reserved.
MANAGER ------Ms D Ms D Ms D Mr I Ms H Ms H Ms H Mr I
238
Implicit INNER JOIN
You can combine more than two tables. To whom is which stolen car registered?
SELECT o.Name, c.Brand, c.Color, c.PlateNumber FROM Owner o, Car c, Stolen s WHERE o.OwnerID = c.Owner AND c.PlateNumber = s.PlateNumber; NAME ---Ms W Mr V Ms Y
©
2013 SAP AG. All rights reserved.
BRAND ----Fiat Audi Audi
COLOR -----red yellow orange
PLATENUMBER ----------HD-V 106 HD-VW 1999 HD-Y 333
239
Explicit INNER JOIN
You can combine more than two tables also for the explicit JOIN. To whom is which stolen car registered?
SELECT o.Name, c.Brand, c.Color, c.PlateNumber FROM Owner o JOIN Car c ON o.OwnerID = c.Owner JOIN Stolen s ON c.PlateNumber = s.PlateNumber;
NAME ---Ms W Mr V Ms Y
©
2013 SAP AG. All rights reserved.
BRAND ----Fiat Audi Audi
COLOR -----red yellow orange
PLATENUMBER ----------HD-V 106 HD-VW 1999 HD-Y 333
240
Explicit INNER JOIN
You can use different comparison operators other than EQUAL in the JOIN condition. Which owner is older than which other owner?
SELECT o.Name AS "older", y.Name AS "younger" FROM Owner o JOIN Owner y ON o.Birthday < y.Birthday;
©
2013 SAP AG. All rights reserved.
older ----Ms T Ms T Ms T Ms T Ms T Ms T Ms U Ms U Ms U Mr V Mr V Mr V Mr V Mr V Ms W Ms W Ms W Ms W Ms Y Mr Z Mr Z
younger ------Ms U Mr V Ms W Mr X Ms Y Mr Z Mr X Ms Y Mr Z Ms U Ms W Mr X Ms Y Mr Z Ms U Mr X Ms Y Mr Z Mr X Mr X Ms Y
241
Explicit INNER JOIN
You can use calculations in the JOIN condition. Which car has at least three times the power than which other car?
SELECT m.*, l.* FROM Car m JOIN Car l ON m.HP >= l.HP * 3; CARID ----F06
©
PLATENUMBER ----------HD-VW 1999
2013 SAP AG. All rights reserved.
BRAND ----Audi
COLOR -----yellow
HP --260
OWNER ----H05
CARID ----F01
PLATENUMBER ----------HD-V 106
BRAND ----Fiat
COLOR ----red
HP -75
OWNER ----H06
242
Explicit INNER JOIN
You can use functions in the JOIN condition. Which owner was born in the same year as which other owner?
SELECT o1.Name, o1.Birthday, o2.Name, o2.Birthday FROM Owner o1 JOIN Owner o2 ON YEAR(o1.Birthday) = YEAR(o2.Birthday) AND o1.OwnerID < o2.OwnerID;
NAME ---Mr X Mr X Ms Y
©
2013 SAP AG. All rights reserved.
BIRTHDAY ---------1986-08-30 1986-08-30 1986-02-10
NAME ---Ms Y Mr Z Mr Z
BIRTHDAY ---------1986-02-10 1986-02-03 1986-02-03
243
Joining Tables
OUTER JOIN
©
2013 SAP AG. All rights reserved.
244
OUTER JOIN
The OUTER JOIN has three sub types: LEFT OUTER JOIN RIGHT OUTER JOIN FULL OUTER JOIN
For all three sub types of the OUTER JOIN SAP HANA only provides the explicit syntax variant.
©
2013 SAP AG. All rights reserved.
245
LEFT OUTER JOIN
One row of a table and one row of another table are always connected to a common result row - provided the JOIN condition is fulfilled. In addition, rows of the left table without matching row in the right table are copied to the query result. The missing values (from the right table) are filled with NULL values.
L
©
… … … … … …
X 1 2 3 4 5
2013 SAP AG. All rights reserved.
… … … … … …
… … … … … …
NULL NULL NULL NULL
R
… … … … … …
Y 3 4 5 6 7
… … … … … …
… … … … … … 246
RIGHT OUTER JOIN
One row of a table and one row of another table are always connected to a common result row - provided that the JOIN condition is fulfilled. In addition, rows of the right table without matching row in the left table are copied to the query result. The missing values (from the left table) are filled with NULL values.
L
… … … … … …
X 1 2 3 4 5
… … … … … …
… … … … … …
NULL NULL NULL NULL
©
2013 SAP AG. All rights reserved.
R
… … … … … …
Y 3 4 5 6 7
… … … … … …
… … … … … … 247
FULL OUTER JOIN
One row of a table and one row of another table are always connected to a common result row - provided that the JOIN condition is fulfilled. In addition, rows of both tables without matching records are copied to the query result. The missing values (from the other table) are filled with NULL values.
L
… … … … … …
X 1 2 3 4 5
… … … … … …
… … … … … …
NULL NULL NULL NULL
©
2013 SAP AG. All rights reserved.
NULL NULL NULL NULL
R
… … … … … …
Y 3 4 5 6 7
… … … … … …
… … … … … … 248
OUTER JOIN
SELECT Column, Column, Column FROM Table LEFT OUTER JOIN Table ON JOIN_Condition WHERE Additional_Condition; SELECT Column, Column, Column FROM Table RIGHT OUTER JOIN Table ON JOIN_Condition WHERE Additional_Condition; SELECT Column, Column, Column FROM Table FULL OUTER JOIN Table ON JOIN_Condition WHERE Additional_Condition;
©
2013 SAP AG. All rights reserved.
249
LEFT OUTER JOIN
Which car is registered to which individual or company? Individuals and companies that currently do not have a car registered should be included. Cars without a registration should not be included in the result.
SELECT Name, CarID, Brand FROM Owner LEFT OUTER JOIN Car ON OwnerID = Owner;
©
2013 SAP AG. All rights reserved.
NAME -----Ms T Ms U Ms U SAP AG SAP AG SAP AG SAP AG SAP AG SAP AG SAP AG HDM AG HDM AG Mr V Ms W IKEA IKEA IKEA Mr X Ms Y Mr Z
CARID ----F19 F09 F11 F02 F03 F18 F05 F15 F07 F14 F12 F10 F06 F01 F13 F08 F04 ? F17 ?
BRAND -------VW Skoda BMW VW BMW Renault Mercedes Skoda Audi Mercedes Skoda BMW Audi Fiat Renault VW Mercedes ? Audi ?
250
RIGHT OUTER JOIN
Which car is registered to which individual or company? Cars without a registration should be included. Individuals and companies that currently do not have a car registered should not be included in the result.
SELECT Name, CarID, Brand FROM Owner RIGHT OUTER JOIN Car ON OwnerID = Owner;
©
2013 SAP AG. All rights reserved.
NAME -----Ms W SAP AG SAP AG IKEA SAP AG Mr V SAP AG IKEA Ms U HDM AG Ms U HDM AG IKEA SAP AG SAP AG ? Ms Y SAP AG Ms T ?
CARID ----F01 F02 F03 F04 F05 F06 F07 F08 F09 F10 F11 F12 F13 F14 F15 F16 F17 F18 F19 F20
BRAND -------Fiat VW BMW Mercedes Mercedes Audi Audi VW Skoda BMW BMW Skoda Renault Mercedes Skoda Opel Audi Renault VW Audi
251
FULL OUTER JOIN
Which car is registered to which individual or company? Individuals and companies that currently do not have a car registered should be included. Cars without a registration should be included in the result .
SELECT Name, CarID, Brand FROM Owner FULL OUTER JOIN Car ON OwnerID = Owner;
©
2013 SAP AG. All rights reserved.
NAME -----Ms W SAP AG SAP AG IKEA SAP AG Mr V SAP AG IKEA Ms U HDM AG Ms U HDM AG IKEA SAP AG SAP AG ? Ms Y SAP AG Ms T ? Mr X Mr Z
CARID ----F01 F02 F03 F04 F05 F06 F07 F08 F09 F10 F11 F12 F13 F14 F15 F16 F17 F18 F19 F20 ? ?
BRAND -------Fiat VW BMW Mercedes Mercedes Audi Audi VW Skoda BMW BMW Skoda Renault Mercedes Skoda Opel Audi Renault VW Audi ? ?
252
LEFT OUTER JOIN
For the LEFT OUTER JOIN you can also add a WHERE clause in addition to the JOIN condition. Which car is registered to which individual? Assume that companies don’t have a birthday. Individuals that currently do not have a car registered should be included. Cars without a registration should not be included in the result.
SELECT Name, CarID, Brand FROM Owner LEFT OUTER JOIN Car ON OwnerID = Owner WHERE Birthday IS NOT NULL;
©
2013 SAP AG. All rights reserved.
NAME -----Ms T Ms U Ms U SAP AG SAP AG SAP AG SAP AG SAP AG SAP AG SAP AG HDM AG HDM AG Mr V Ms W IKEA IKEA IKEA Mr X Ms Y Mr Z
CARID ----F19 F09 F11 F02 F03 F18 F05 F15 F07 F14 F12 F10 F06 F01 F13 F08 F04 ? F17 ?
BRAND -------VW Skoda BMW VW BMW Renault Mercedes Skoda Audi Mercedes Skoda BMW Audi Fiat Renault VW Mercedes ? Audi ?
253
RIGHT OUTER JOIN
For the RIGHT OUTER JOIN you can also add a WHERE clause in addition to the JOIN condition. Which car is registered to which individual? Individuals who currently do not have a car registered should be excluded. (Realized by RIGHT OUTER JOIN) Cars without a registration should not be included in the result. (Realized by the WHERE clause)
SELECT Name, CarID, Brand FROM Owner RIGHT OUTER JOIN Car ON OwnerID = Owner WHERE Birthday IS NOT NULL;
©
2013 SAP AG. All rights reserved.
NAME -----Ms W SAP AG SAP AG IKEA SAP AG Mr V SAP AG IKEA Ms U HDM AG Ms U HDM AG IKEA SAP AG SAP AG ? Ms Y SAP AG Ms T ?
CARID ----F01 F02 F03 F04 F05 F06 F07 F08 F09 F10 F11 F12 F13 F14 F15 F16 F17 F18 F19 F20
BRAND -------Fiat VW BMW Mercedes Mercedes Audi Audi VW Skoda BMW BMW Skoda Renault Mercedes Skoda Opel Audi Renault VW Audi
254
FULL OUTER JOIN
For the FULL OUTER JOIN you can also add a WHERE clause in addition to the JOIN condition. Which car is registered to which individual? Individuals that currently do not have a car registered should be included. Cars without a registration should not be included in the result. (Realized by the WHERE clause)
SELECT Name, CarID, Brand FROM Owner FULL OUTER JOIN Car ON OwnerID = Owner WHERE Birthday IS NOT NULL;
©
2013 SAP AG. All rights reserved.
NAME -----Ms W SAP AG SAP AG IKEA SAP AG Mr V SAP AG IKEA Ms U HDM AG Ms U HDM AG IKEA SAP AG SAP AG ? Ms Y SAP AG Ms T ? Mr X Mr Z
CARID ----F01 F02 F03 F04 F05 F06 F07 F08 F09 F10 F11 F12 F13 F14 F15 F16 F17 F18 F19 F20 ? ?
BRAND -------Fiat VW BMW Mercedes Mercedes Audi Audi VW Skoda BMW BMW Skoda Renault Mercedes Skoda Opel Audi Renault VW Audi ? ?
255
LEFT OUTER JOIN
You can LEFT OUTER JOIN a table with itself Who is the manager of which employee? Employees without a manager should be included. Only Managers with employees should be included.
EMPLOYEE -------Mr A Mr B Ms C Ms D Mr E Mr F Ms G Ms H Mr I
MANAGER ------Ms D Ms D Ms D Mr I Ms H Ms H Ms H Mr I ?
SELECT e.Name AS Employee, m.Name AS Manager FROM Official e LEFT OUTER JOIN Official m ON e.Manager = m.PNr;
©
2013 SAP AG. All rights reserved.
256
RIGHT OUTER JOIN
You can RIGHT OUTER JOIN a table with itself Who is the manager of which employee? The MANAGER column should also include officials that are not managing employees. The EMPLOYEE column should only include officials with a manager assigned.
SELECT e.Name AS Employee, m.Name AS Manager FROM Official e RIGHT OUTER JOIN Official m ON e.Manager = m.PNr;
©
2013 SAP AG. All rights reserved.
EMPLOYEE -------? ? ? Mr A Mr B Ms C ? ? ? Mr E Mr F Ms G Ms D Ms H
MANAGER ------Mr A Mr B Ms C Ms D Ms D Ms D Mr E Mr F Ms G Ms H Ms H Ms H Mr I Mr I
257
FULL OUTER JOIN
You can FULL OUTER JOIN a table with itself Who is the manager of which employee? The EMPLOYEE column should include officials without a manager. The MANAGER column should also include officials that are not managing employees.
SELECT e.Name AS Employee, m.Name AS Manager FROM Official e FULL OUTER JOIN Official m ON e.Manager = m.PNr;
©
2013 SAP AG. All rights reserved.
EMPLOYEE -------Mr A Mr B Ms C Ms D Mr E Mr F Ms G Ms H Mr I ? ? ? ? ? ?
MANAGER ------Ms D Ms D Ms D Mr I Ms H Ms H Ms H Mr I ? Mr A Mr B Ms C Mr E Mr F Ms G
258
LEFT/RIGHT/FULL OUTER JOIN You can also … Project to certain columns Reference table aliases or tuple variables in the projection list. Use table aliases or tuple variables in the JOIN condition. Explicitly rename result columns. Sort the query result. Add a GROUP BY clause. Use aggregate expressions. eliminate duplicates by using DISTINCT. Specify a JOIN condition on multiple columns. Use any comparison operator in the JOIN condition. JOIN a table with itself. JOIN more than two tables. ©
2013 SAP AG. All rights reserved.
and: You have to qualify column names with the table alias or with a tuple variable if the column names are not unique. You do not need to include a column from every table involved in the JOIN into the projection list.
259
Unit 4: Reading Data From Multiple Tables Part I Summary
You should now be able to: •
Merge the result of several select statements using the UNION statement • Combine data from several tables when querying data using JOIN constructs • •
©
List the various types of Joins Explain the differences between the various types of Joins, and apply the right type of Join depending on the problem
2013 SAP AG. All rights reserved.
260
Unit 5 Reading Data From Multiple Tables Part II
261
Unit 5: Reading Data From Multiple Tables Part II Learning Objectives After completing this unit, you will be able to: • Use sub-queries to query data from multiple tables in a single select statement
• Explain the difference between uncorrelated and correlated subqueries
©
2013 SAP AG. All rights reserved.
262
Nested Queries
Nested Queries
©
2013 SAP AG. All rights reserved.
263
Nested Queries
In addition to UNION and JOIN, Nested Queries also provide the option of reading from multiple tables and views. Nested Queries contain a so called “Sub Query”.
©
2013 SAP AG. All rights reserved.
264
Sub Query
What is a sub query?
©
2013 SAP AG. All rights reserved.
265
Sub Query
A sub query is a query (SELECT-FROM-WHERE statement) that is used in another query (SELECT-FROM-WHERE statement). The "other Query" – containing the sub query - is called the "outer query". In this context the Sub Query is also referred to as "inner query".
Outer Query
©
2013 SAP AG. All rights reserved.
SELECT PlateNumber, Brand, Color Sub Query FROM Car WHERE Owner IN ( SELECT OwnerID FROM Owner WHERE City = 'Wiesloch');
266
Sub Query
When is a sub query useful?
©
2013 SAP AG. All rights reserved.
267
Sub Query
A sub query is useful if ... It makes the SELECT statement more "readable" - for example, because the use of nested queries is the "obvious way" The SELECT statement has a better performance – however, with a "perfect" optimizer, this should not be the case. The formulation of the SELECT statement is not possible (or only extremely cumbersome) without a sub query - which can be particularly the case for aggregate expressions.
©
2013 SAP AG. All rights reserved.
268
Sub Query
What is the difference between an uncorrelated and a correlated sub query?
©
2013 SAP AG. All rights reserved.
269
Sub Query
An uncorrelated sub query makes no reference to the outer query.
SELECT * FROM Car WHERE Owner IN (SELECT OwnerID FROM Owner WHERE City = 'Wiesloch'); A correlated sub query refers to the outer query. SELECT * FROM Car c WHERE EXISTS (SELECT * FROM Owner o WHERE o.OwnerID = c.Owner AND o.City = 'Wiesloch');
©
2013 SAP AG. All rights reserved.
270
Sub Query
Uncorrelated sub queries
©
2013 SAP AG. All rights reserved.
271
Uncorrelated Sub Query
SELECT Column, Column, Column FROM Table WHERE Column IN (SELECT Column FROM Table WHERE Condition);
©
2013 SAP AG. All rights reserved.
272
Uncorrelated Sub Query
You can use IN, if the outer value should be included in the result set of the sub query. Which cars are registered to an owner from Wiesloch?
SELECT * FROM Car WHERE Owner IN (SELECT OwnerID FROM Owner WHERE City = 'Wiesloch');
CARID ----F01 F19
©
2013 SAP AG. All rights reserved.
PLATENUMBER ----------HD-V 106 HD-VW 2012
BRAND ----Fiat VW
COLOR ----red black
HP --75 125
OWNER ----H06 H01
273
Uncorrelated Sub Query
You can use =
ANY, if the outer value should match any result value of the sub query.
Which cars are registered to an owner from Wiesloch?
SELECT * FROM Car WHERE Owner = ANY (SELECT OwnerID FROM Owner WHERE City = 'Wiesloch');
CARID ----F01 F19
©
2013 SAP AG. All rights reserved.
PLATENUMBER ----------HD-V 106 HD-VW 2012
BRAND ----Fiat VW
COLOR ----red black
HP --75 125
OWNER ----H06 H01
274
Uncorrelated Sub Query
“= ANY” is equivalent to “IN”. So, where is the value of “= ANY” ?
©
2013 SAP AG. All rights reserved.
275
Uncorrelated Sub Query
= ANY is equivalent to IN, but you can use ANY with other comparison operators: = ANY
The external value matches any value of the sub query.
< ANY
The external value is less than any value of the sub query.
<= ANY > ANY
©
The external value is less or equal than any value of the sub query. The external value is greater than any value of the sub query.
>= ANY
The external value is greater or equal than any value of the sub query.
<> ANY
The external value is different to any value of the sub query.
2013 SAP AG. All rights reserved.
276
Uncorrelated Sub Query
You can use > ANY, if the external value should be greater than any value of the sub query.
Which officials have more than the minimum overtime hours?
SELECT Name, Overtime FROM Official WHERE Overtime > ANY (SELECT Overtime FROM Official); NAME ---Ms C Mr F Ms G
©
2013 SAP AG. All rights reserved.
OVERTIME -------20 18 22
277
Uncorrelated Sub Query
You can use > instead of > ANY, if the sub query results in only a single value.
Which officials have more than the minimum overtime hours?
SELECT Name, Overtime FROM Official WHERE Overtime > (SELECT MIN(Overtime) FROM Official); NAME ---Ms C Mr F Ms G
©
2013 SAP AG. All rights reserved.
OVERTIME -------20 18 22
278
Uncorrelated Sub Query
You can use < ALL, if the outer value should be less than all result values of the sub query (other comparison operators are possible) = ALL
The external value matches each result value of the sub query. (This option is quite useful, as the sub query result can contain duplicates.)
< ALL
The external value is less than all values of the sub query.
<= ALL > ALL
©
The external value is less or equal than all values of the sub query. The external value is greater than all values of the sub query.
>= ALL
The external value is greater or equal than all values of the sub query.
<> ALL
The external value is different to all values of the sub query.
2013 SAP AG. All rights reserved.
279
Uncorrelated Sub Query
You can use <= ALL, if the outer value should be less or equal than all result values of the sub query (other comparison operators are possible) Which officials have the fewest overtime hours?
SELECT Name, Overtime FROM Official WHERE Overtime <= ALL (SELECT Overtime FROM Official WHERE Overtime IS NOT NULL); NAME ---Mr A Mr B Mr E ©
OVERTIME -------10 10 10
2013 SAP AG. All rights reserved.
280
Uncorrelated Sub Query
You can use
<=
instead of
<= ALL, if the sub query results in only a single value.
Which officials have the fewest overtime hours? For the example below = would be possible instead of <= as well.
SELECT Name, Overtime FROM Official WHERE Overtime <= (SELECT MIN(Overtime) FROM Official); NAME ---Mr A Mr B Mr E
©
OVERTIME -------10 10 10
2013 SAP AG. All rights reserved.
281
Uncorrelated Sub Query
You can use functions in a sub query.
Which owners were born in the same year as the youngest owner? SELECT * FROM Owner WHERE YEAR(Birthday) = (SELECT MAX(YEAR(Birthday)) FROM Owner);
OWNERID ------H08 H09 H10 ©
2013 SAP AG. All rights reserved.
NAME ---Mr X Ms Y Mr Z
BIRTHDAY ---------1986-08-30 1986-02-10 1986-02-03
CITY --------Walldorf Sinsheim Ladenburg 282
Uncorrelated Sub Query
To whom is (at least) one blue car registered?
SELECT Name FROM Owner WHERE OwnerID IN (SELECT Owner FROM Car WHERE Color = 'blue');
NAME -----SAP AG
©
2013 SAP AG. All rights reserved.
283
Uncorrelated Sub Query
You can use multiple columns in the sub query. To whom within the EU is (at least) one blue car registered?
SELECT Name FROM Owner_EU WHERE (Country, OwnerID) IN (SELECT Country, Owner FROM Car_EU WHERE Color = 'blue');
NAME ------SAP AG Señor Q
©
2013 SAP AG. All rights reserved.
284
Uncorrelated Sub Query
You can use the same table in both the FROM clause of the outer query and in the FROM clause of the sub query. What is the name of Mr A’s manager?
SELECT Name FROM Official WHERE PNr IN (SELECT Manager FROM Official WHERE Name = 'Mr A');
NAME ---Ms D
©
2013 SAP AG. All rights reserved.
285
Uncorrelated Sub Query
The WHERE clause of a sub query can contain another sub query. Who are the owners of stolen cars?
SELECT Name FROM Owner WHERE OwnerID IN ( SELECT Owner FROM Car WHERE PlateNumber IN ( SELECT PlateNumber FROM Stolen ) ); NAME ---Mr V Ms W Ms Y ©
2013 SAP AG. All rights reserved.
286
Uncorrelated Sub Query
You can combine JOIN and Sub Query. To whom is the most powerful car per brand registered?
SELECT DISTINCT Brand, Name FROM Owner RIGHT OUTER JOIN Car ON OwnerID = Owner WHERE (Brand, HP) IN ( SELECT Brand, MAX(HP) FROM Car GROUP BY Brand ) ORDER BY Brand ASC, Name ASC;
©
2013 SAP AG. All rights reserved.
BRAND -------Audi BMW BMW Fiat Mercedes Opel Renault Skoda VW
NAME -----Mr V Ms U SAP AG Ms W SAP AG ? IKEA SAP AG IKEA
287
Uncorrelated Sub Query
A sub query is usually part of the WHERE clause. But you can also use a sub query in the FROM clause.
Which blue cars have less than 120 HP?
SELECT * FROM (SELECT FROM WHERE WHERE Power <
PlateNumber, HP AS Power Car Color = 'blue') 120; PLATENUMBER ----------HD-ML 3206
©
2013 SAP AG. All rights reserved.
POWER ----116
288
Uncorrelated Sub Query
A sub query is usually part of the WHERE clause. But you can also use a sub query in the SELECT clause.
What is the HP deviation of each car when compared to the most powerful yellow car?
SELECT CarID, Brand, (SELECT MAX(HP) FROM Car WHERE Color = 'yellow') - HP AS Deviation FROM Car ORDER BY 3 DESC; CARID ----F01 F18 F09 … F06
©
2013 SAP AG. All rights reserved.
BRAND ------Fiat Renault Skoda … Audi
DEVIATION --------185 170 155 … 0
289
Sub Query
Correlated sub queries
©
2013 SAP AG. All rights reserved.
290
Correlated Sub Query
SELECT Column, Column, Column FROM Table Tuple-Variable WHERE EXISTS (SELECT * FROM Table WHERE Condition);
©
2013 SAP AG. All rights reserved.
291
Correlated Sub Query
A correlated sub query refers to the outer query. Using EXISTS you can check that the query result of the sub query is not empty.
Which vehicles are registered to an owner from Wiesloch? SELECT * FROM Car c WHERE EXISTS (SELECT * FROM Owner o WHERE o.City = 'Wiesloch' AND o.OwnerID = c.Owner);
CARID ----F01 F19
©
2013 SAP AG. All rights reserved.
PLATENUMBER ----------HD-V 106 HD-VW 2012
BRAND ----Fiat VW
COLOR ----red black
HP --75 125
OWNER ----H06 H01
292
Correlated Sub Query
Even with a correlated sub query, you can omit tuple variables if the column names are unique. What vehicles are registered to an owner from Wiesloch? SELECT * FROM Car WHERE EXISTS (SELECT * FROM Owner WHERE City = 'Wiesloch' AND OwnerID = Owner);
CARID ----F01 F19
©
2013 SAP AG. All rights reserved.
PLATENUMBER ----------HD-V 106 HD-VW 2012
BRAND ----Fiat VW
COLOR ----red black
HP --75 125
OWNER ----H06 H01
293
Correlated Sub Query
Using NOT EXISTS you can check if the result of the sub query is empty. Which cars are not reported as stolen?
SELECT CarID FROM Car c WHERE NOT EXISTS (SELECT * FROM Stolen s WHERE s.PlateNumber = c.PlateNumber);
©
2013 SAP AG. All rights reserved.
CARID ----F02 F03 F04 F05 F07 F08 F09 F10 F11 F12 F13 F14 F15 F16 F18 F19 F20
294
Correlated Sub Query
You can use the same table in both the FROM clause of the outer query and the FROM clause of the correlated sub query. Which vehicle has the most HP?
SELECT * FROM Car c1 WHERE NOT EXISTS (SELECT * FROM Car c2 WHERE c2.HP > c1.HP);
CARID ----F06
©
2013 SAP AG. All rights reserved.
PLATENUMBER ----------HD-VW 1999
BRAND ----Audi
COLOR -----yellow
HP --260
OWNER ----H05 295
Correlated Sub Query
Even when using a correlated sub query, you can for example use ">= ALL" How much horsepower has the most powerful car of each brand?
SELECT DISTINCT Brand, HP FROM Car c1 WHERE c1.HP >= ALL (SELECT c2.HP FROM Car c2 WHERE c2.Brand = c1.Brand);
©
2013 SAP AG. All rights reserved.
BRAND -------Fiat BMW Mercedes Audi VW Renault Skoda Opel
HP --75 184 170 260 160 136 136 120
296
Correlated Sub Query
The WHERE clause of a correlated sub query can contain an additional correlated sub query. To whom (at least) one stolen car is registered? SELECT o.Name FROM Owner o WHERE EXISTS ( SELECT FROM WHERE AND
NAME ---Mr V Ms W Ms Y
* Car c c.Owner = o.OwnerID EXISTS ( SELECT * FROM Stolen s WHERE s.PlateNumber = c.PlateNumber )
);
©
2013 SAP AG. All rights reserved.
297
Correlated Sub Query
You can also use a correlated sub query in the SELECT clause. How much horsepower each car is lacking compared to the most powerful car of the same brand?
SELECT CarID, Brand, (SELECT MAX(HP) FROM Car c2 WHERE c2.Brand = c1.Brand) - HP AS Difference FROM Car c1; CARID BRAND DIFFERENCE ----F01 F02 F03 F04 F05 F06 F07 … F20 ©
2013 SAP AG. All rights reserved.
-------Fiat VW BMW Mercedes Mercedes Audi Audi … Audi
---------0 40 0 34 0 0 144 … 76 298
Sub Query
Using Nested Queries you also can …
©
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
2013 SAP AG. All rights reserved.
299
Unit 5: Reading Data From Multiple Tables Part II Summary
You should now be able to: •
Merge the result of several select statements using the UNION statement • Combine data from several tables when querying data using JOIN constructs • •
©
List the various types of Joins Explain the differences between the various types of Joins, and apply the right type of Join depending on the problem
2013 SAP AG. All rights reserved.
300
Unit 6 Understanding NULL values
301
Unit 6: Understanding NULL Values Learning Objectives After completing this unit, you will be able to: • Explain how to interpret NULL values in databases • Understand why the presences of NULL values can lead to unexpected query results • Analyze if two seemingly equivalent SQL statements return different results because of NULL values
©
2013 SAP AG. All rights reserved.
302
NULL Values
What is a NULL value?
©
2013 SAP AG. All rights reserved.
303
NULL Values
A NULL value signals that the corresponding value is unknown or does not exist. The NULL value is a special value reserved by the DBMS. – For numeric data types the NULL value is not equal to 0. – For string based data types the NULL value is not equal to an empty string or to the space character.
A NULL value is used, … – If the corresponding value exists in principle, but is (temporarily or permanently) unknown (such as a birthday of a person) – If the corresponding value does not exist (for example the owner of an unregistered car).
©
2013 SAP AG. All rights reserved.
304
NULL Values
How are NULL values created?
©
2013 SAP AG. All rights reserved.
305
NULL Values
NULL values are “created” when reading rows (SELECT statement) If the source table already contains NULL values When using outer joins NULL values are created in a table when inserting rows (INSERT statement) if No value for a column is provided, NULL values are allowed and no default value is defined A NULL value is explicitly specified for a column and NULL values are allowed NULL values are created in a table when changing rows (UPDATE statement) if A NULL value is explicitly specified for a column and NULL values are allowed NULL values are created in a table when when adding a column (ALTER TABLE statement), 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.
©
2013 SAP AG. All rights reserved.
306
NULL Values
NULL values enforce a trivalent logic
©
2013 SAP AG. All rights reserved.
307
NULL Values
By allowing NULL values a third truth value is necessary. In addition to "true" and “false" there is also the truth value "unknown“ in SQL . Any comparison with a NULL value results in the truth value "unknown“. – This applies to all comparison operators: =, >, >=, <, <=, <>
©
A
A=7
A <> 7
A >= 6
5
false
true
false
7
true
false
true
NULL
unknown
unknown
unknown
2013 SAP AG. All rights reserved.
308
Trivalent Logic: NOT
©
2013 SAP AG. All rights reserved.
X
NOT X
true
false
false
true
unknown
unknown
309
Trivalent Logic: AND
©
X
Y
X AND Y
true
true
true
true
false
false
true
unknown
unknown
false
true
false
false
false
false
false
unknown
false
unknown
true
unknown
unknown
false
false
unknown
unknown
unknown
2013 SAP AG. All rights reserved.
310
Trivalent Logic: OR
©
X
Y
X OR Y
true
true
true
true
false
true
true
unknown
true
false
true
true
false
false
false
false
unknown
unknown
unknown
true
true
unknown
false
unknown
unknown
unknown
unknown
2013 SAP AG. All rights reserved.
311
Trivalent Logic
The trivalent logic has only one more truth value, so where is the problem?
©
2013 SAP AG. All rights reserved.
312
Trivalent Logic 39 = 19,683 binary logical operators are conceivable
©
X
Y
X op Y
true
true
true / false / unknown
true
false
true / false / unknown
true
unknown
true / false / unknown
false
true
true / false / unknown
false
false
true / false / unknown
false
unknown
true / false / unknown
unknown
true
true / false / unknown
unknown
false
true / false / unknown
unknown
unknown
true / false / unknown
2013 SAP AG. All rights reserved.
313
NULLs & Trivalent Logic
Many "obviously true" statements are no longer necessarily true!!!
A=A (A < 5) OR ( A >= 5) (A = ‘Walldorf’) OR (A <> ‘Walldorf’)
A+1>A A * A >= 0 0*A=0 2*A=A+A MAXIMUM >= ALL ( set of all values) MINIMUM <= ALL ( set of all values) A OR (NOT A)
©
2013 SAP AG. All rights reserved.
314
NULL Values
Do the following two SQL queries have the same result? SELECT Name, Overtime FROM Official WHERE Overtime <= 10 OR Overtime > 10; SELECT Name, Overtime FROM Official;
©
2013 SAP AG. All rights reserved.
315
NULL Values
The two SQL queries have different results!
Only officials where Overtime IS NOT NULL applies: SELECT Name, Overtime FROM Official WHERE Overtime <= 10 OR Overtime > 10; All officials (including those with: Overtime IS NULL ) SELECT Name, Overtime FROM Official;
©
2013 SAP AG. All rights reserved.
NAME ---Mr A Mr B Ms C Mr E Mr F Ms G
OVERTIME -------10 10 20 10 18 22
NAME ---Mr A Mr B Ms C Ms D Mr E Mr F Ms G Ms H Mr I
OVERTIME -------10 10 20 ? 10 18 22 ? ?
316
NULL Values
Do the following two SQL queries have the same result? SELECT * FROM Official WHERE PNr <> ALL (SELECT PNr FROM Official WHERE Overtime = Overtime); SELECT * FROM Official WHERE Overtime IS NULL;
©
2013 SAP AG. All rights reserved.
317
NULL Values
The two SQL queries have the same result! SELECT * FROM Official WHERE PNr <> ALL (SELECT PNr FROM Official WHERE Overtime = Overtime);
SELECT * FROM Official WHERE Overtime IS NULL;
©
2013 SAP AG. All rights reserved.
PNR --P04 P08 P09
NAME ---Ms D Ms H Mr I
OVERTIME -------? ? ?
SALARY -----A12 A13 A14
MANAGER ------P09 P09 ?
318
NULL Values
The sub query returns all personnel numbers of the officials, where the number of overtime hours is known. SELECT * FROM Official WHERE PNr <> ALL (SELECT PNr FROM Official WHERE Overtime = Overtime);
©
2013 SAP AG. All rights reserved.
PNR --P01 P02 P03 P05 P06 P07
319
NULL Values
The outer SELECT query returns all officials, whose PersNumber differs from all PersNumbers of the sub query. So the outer SELECT query returns all officials, whose PersNumber differs from the PersNumbers of those officials whose overtime is known. So the outer SELECT query returns all officials whose overtime is unknown.
SELECT * FROM Official WHERE PNr <> ALL (SELECT PNr FROM Official WHERE Overtime = Overtime); PNR --P04 P08 P09
©
2013 SAP AG. All rights reserved.
NAME ---Ms D Ms H Mr I
OVERTIME -------? ? ?
SALARY -----A12 A13 A14
MANAGER ------P09 P09 ? 320
NULL Values
Do the following two SQL queries have the same result? SELECT Name, Overtime FROM Official WHERE Overtime >= ALL (SELECT Overtime FROM Official); SELECT Name, Overtime FROM Official WHERE Overtime = (SELECT MAX(Overtime) FROM Official);
©
2013 SAP AG. All rights reserved.
321
NULL Values
The two SQL queries have different results! SELECT Name, Overtime The FROM Official WHERE Overtime >= ALL (SELECT Overtime FROM Official); SELECT Name, Overtime FROM Official WHERE Overtime = (SELECT MAX(Overtime) FROM Official);
©
2013 SAP AG. All rights reserved.
query result is empty.
NAME ---Ms G
OVERTIME -------22
322
NULL Values
The sub query also returns NULL values.
SELECT Name, Overtime FROM Official WHERE Overtime >= ALL (SELECT Overtime FROM Official);
OVERTIME -------10 10 20 ? 10 18 22 ? ?
Hence there is no value which is greater or equal to all sub query values! SELECT Name, Overtime The FROM Official WHERE Overtime >= ALL (SELECT Overtime FROM Official);
©
2013 SAP AG. All rights reserved.
query result is empty.
323
NULL Values
Do the following two SQL queries have the same result?
SELECT Name, Overtime FROM Official WHERE Overtime >= ALL (SELECT Overtime FROM Official WHERE Overtime IS NOT NULL); SELECT Name, Overtime FROM Official WHERE Overtime = (SELECT MAX(Overtime) FROM Official);
©
2013 SAP AG. All rights reserved.
324
NULL Values
For the existing data, both queries return the same result.
SELECT Name, Overtime FROM Official WHERE Overtime >= ALL (SELECT Overtime FROM Official WHERE Overtime IS NOT
NAME ---Ms G
SELECT Name, Overtime FROM Official WHERE Overtime = (SELECT MAX(Overtime) FROM Official);
NAME ---Ms G
©
2013 SAP AG. All rights reserved.
OVERTIME -------22
NULL); OVERTIME -------22
325
NULL Values
But: If the overtime values were unknown for all officials , the two SQL queries would return different results!
NAME ---Mr A Mr B Ms C Ms D Mr E Mr F Ms G Ms H Mr I
SELECT Name, Overtime FROM Official WHERE Overtime >= ALL (SELECT Overtime FROM Official WHERE Overtime IS NOT NULL); SELECT Name, Overtime FROM Official WHERE Overtime = (SELECT MAX(Overtime) FROM Official);
©
2013 SAP AG. All rights reserved.
OVERTIME -------? ? ? ? ? ? ? ? ?
The query result is empty.
326
NULL Values
The result of the sub query is empty. Hence the WHERE clause of the outer query is fulfilled for all rows. SELECT Name, Overtime FROM Official WHERE Overtime >= ALL (SELECT Overtime FROM Official WHERE Overtime IS NOT NULL);
NAME ---Mr A Mr B Ms C Ms D Mr E Mr F Ms G Ms H Mr I
OVERTIME -------? ? ? ? ? ? ? ? ?
The sub query returns a NULL value. So the WHERE clause of the outer query is not fulfilled for any row. SELECT Name, Overtime FROM Official WHERE Overtime = (SELECT MAX(Overtime) FROM Official);
©
2013 SAP AG. All rights reserved.
The query result is empty.
327
Unit 6: Understanding NULL Values Summary
You should now be able to: •
Explain how to interpret NULL values in databases • Understand why the presences of NULL values can lead to unexpected query results • Analyze if two seemingly equivalent SQL statements return different results because of NULL values
©
2013 SAP AG. All rights reserved.
328
Unit 7 Changing Data Stored In Tables
329
Unit 7: Changing Data Stored In Tables Learning Objectives After completing this unit, 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
©
2013 SAP AG. All rights reserved.
330
Changing Database Access
Which SQL statements can be used to insert, change or delete data?
©
2013 SAP AG. All rights reserved.
331
Changing Database Access
INSERT statement New rows are inserted into a table
UPDATE statement Existing table rows are changed
DELETE statement Existing table rows are deleted
©
2013 SAP AG. All rights reserved.
332
Changing Database Access
INSERT statement
©
2013 SAP AG. All rights reserved.
333
INSERT
INSERT INTO Table VALUES (Value, Value, Value);
INSERT INTO Table(Column, Column) VALUES (Value, Value);
INSERT INTO Table SELECT … FROM … WHERE …;
©
2013 SAP AG. All rights reserved.
334
INSERT
Using INSERT you can specify a value for each column.
If the sequence of the values corresponds to the sequence of columns, you do not need to list the column names. The order of the columns above mentioned is set implicitly when you create the table. Insert a new row into the Official table:
INSERT INTO Official VALUES ('P10', 'Ms J', 20, 'A10',
PNR --… P10 ©
NAME ---… Ms J
OVERTIME -------… 20
2013 SAP AG. All rights reserved.
SALARY -----… A10
'P04');
MANAGER ------… P04 335
INSERT
Using INSERT you can specify a value for each column.
If the sequence of the values does not match the sequence of the corresponding columns, you must list the column names in the correct sequence of the values.
Insert a new row into the Official table:
INSERT INTO Official( Salary, Overtime, Name, PNr, Manager) VALUES ('A12', 50, 'Mr K', 'P11', 'P09');
PNR --… P11 ©
NAME ---… Mr K
OVERTIME -------… 50
2013 SAP AG. All rights reserved.
SALARY -----… A12
MANAGER ------… P09 336
INSERT
You do not have to specify a value for each column of the INSERT.
You must list the column names in the correct sequence for each column, where you specify a value. Missing values will be replaced by NULL or the default value of the column
Insert a new row into the Official table:
INSERT INTO Official(PNr, Name) VALUES ('P12', 'Ms L');
PNR --… P12 ©
NAME ---… Ms L
OVERTIME -------… ?
2013 SAP AG. All rights reserved.
SALARY -----… A06
MANAGER ------… ? 337
INSERT
You can explicitly specify NULL values (for certain columns) for the INSERT. Insert a new row into the Official table:
INSERT INTO Official VALUES ('P13', 'Mr M', NULL, NULL, NULL);
PNR --… P13 ©
NAME ---… Mr M
OVERTIME -------… ?
2013 SAP AG. All rights reserved.
SALARY -----… ?
MANAGER ------… ? 338
INSERT
You can INSERT data read from another table. INSERT all owner IDs and names from city Wiesloch into the Official table:
INSERT INTO Official(PNr, Name) SELECT OwnerID, Name FROM Owner WHERE City = 'Wiesloch'; PNR --… H01 H06
©
NAME ---… Ms T Ms W
OVERTIME -------… ? ?
2013 SAP AG. All rights reserved.
SALARY -----… A06 A06
MANAGER ------… ? ? 339
Changing Database Access
The UPDATE statement
©
2013 SAP AG. All rights reserved.
340
UPDATE
UPDATE Table SET Column = Value, Column = Value, Column = Value WHERE Condition;
©
2013 SAP AG. All rights reserved.
341
UPDATE
You can use the UPDATE statement to change all rows in a table. The overtime value of all officials should be increased by 10:
UPDATE Official SET Overtime = Overtime + 10;
PNR --P01 P02 P03 P04 P05 … ©
2013 SAP AG. All rights reserved.
NAME ---Mr A Mr B Ms C Ms D Mr E …
OVERTIME -------20 20 30 ? 20 …
SALARY -----A09 A10 A09 A12 A08 …
MANAGER ------P04 P04 P04 P09 P08 … 342
UPDATE
You can use the UPDATE statement to change only certain rows in a table by adding a WHERE clause. The overtime value of officials in salary group “A09” should be doubled:
UPDATE Official SET Overtime = Overtime * 2 WHERE Salary = 'A09'; PNR --P01 P02 P03 P04 … ©
2013 SAP AG. All rights reserved.
NAME ---Mr A Mr B Ms C Ms D …
OVERTIME -------20 10 40 ? …
SALARY -----A09 A10 A09 A12 …
MANAGER ------P04 P04 P04 P09 … 343
UPDATE
You can use the UPDATE statement to change values of multiple columns. The overtime value of officials in salary group “A09” should be tripled. In addition their salary group should be changed to “A10”.
UPDATE Official SET Overtime = Overtime * 3, Salary = 'A10' WHERE Salary = 'A09'; PNR --P01 P02 P03 … ©
2013 SAP AG. All rights reserved.
NAME ---Mr A Mr B Ms C …
OVERTIME -------30 10 60 …
SALARY -----A10 A10 A10 …
MANAGER ------P04 P04 P04 … 344
UPDATE
You can use an uncorrelated sub query on the WHERE clause of the UPDATE statement. Officials who are contacts for the car owners are moved to salary group “A15”.
UPDATE Official SET Salary = 'A15' WHERE PNr IN (SELECT PersNumber FROM Contact); PNR --… P07 P08 P09 ©
2013 SAP AG. All rights reserved.
NAME ---… Ms G Ms H Mr I
OVERTIME -------… 22 ? ?
SALARY -----… A11 A15 A15
MANAGER ------… P08 P09 ? 345
UPDATE
You can use a correlated sub query on the WHERE clause of the UPDATE statement. Officials who are contacts for the car owners are moved to salary group “A15”.
UPDATE Official o SET Salary = 'A15' WHERE EXISTS (SELECT * FROM Contact c WHERE c.PersNumber = o.PNr); PNR --… P07 P08 P09 ©
2013 SAP AG. All rights reserved.
NAME ---… Ms G Ms H Mr I
OVERTIME -------… 22 ? ?
SALARY -----… A11 A15 A15
MANAGER ------… P08 P09 ? 346
UPDATE
Usually a sub query is part of the WHERE clause. You can also use a (correlated or uncorrelated) sub query in the SET clause. For the following SQL statement we assume, that the table Car has an additional column Ownername.
UPDATE Car c SET Ownername = (SELECT o.Name FROM Owner o WHERE o.OwnerID = c.Owner); CARID ----F01 F02 F03 …
©
2013 SAP AG. All rights reserved.
PLATENUMBER ----------HD-V 106 HD-VW 4711 HD-JA 1972 …
BRAND ----Fiat VW BMW …
COLOR ----red black blue …
HP --75 120 184 …
OWNER ----H06 H03 H03 …
OWNERNAME --------Ms W SAP AG SAP AG …
347
Changing Database Access
The DELETE statement
©
2013 SAP AG. All rights reserved.
348
DELETE
DELETE FROM Table WHERE Condition;
©
2013 SAP AG. All rights reserved.
349
DELETE
You can DELETE all rows in a table: Delete all entries from table Car:
DELETE FROM Car;
CARID
PLATENUMBER
BRAND
COLOR
HP
OWNER
-----
-----------
-----
-----
--
-----
©
2013 SAP AG. All rights reserved.
350
DELETE
You can DELETE selected rows from a table: Delete all cars with horsepower less than 180 :
DELETE FROM Car WHERE HP < 180; CARID ----F03 F06 F11 F17 F20 ©
PLATENUMBER ----------HD-JA 1972 HD-VW 1999 HD-MM 208 HD-Y 333 ?
2013 SAP AG. All rights reserved.
BRAND ----BMW Audi BMW Audi Audi
COLOR -----blue yellow green orange green
HP --184 260 184 184 84
OWNER ----H03 H05 H02 H09 ? 351
DELETE
You can use an uncorrelated sub query in the DELETE statement. Delete all cars that are reported as stolen.
DELETE FROM Car WHERE PlateNumber IN (SELECT PlateNumber FROM Stolen);
©
2013 SAP AG. All rights reserved.
CARID ----F02 F03 F04 F05 F07 F08 F09 F10 F11 F12 F13 F14 F15 F16 F18 F19 F20
PLATENUMBER ----------HD-VW 4711 HD-JA 1972 HD-AL 1002 HD-MM 3206 HD-ML 3206 HD-IK 1002 HD-UP 13 HD-MT 507 HD-MM 208 HD-XY 4711 HD-IK 1001 HD-MM 1977 HD-MB 3030 ? HD-MQ 2006 HD-VW 2012 ?
352
DELETE
CARID ----F02 Delete all cars that are reported as stolen. F03 F04 F05 DELETE F07 FROM Car c F08 WHERE EXISTS (SELECT * F09 FROM Stolen s F10 WHERE s.PlateNumber = c.PlateNumber); F11 F12 F13 F14 F15 F16 F18 F19 F20
You can use a correlated sub query in the DELETE statement.
©
2013 SAP AG. All rights reserved.
PLATENUMBER ----------HD-VW 4711 HD-JA 1972 HD-AL 1002 HD-MM 3206 HD-ML 3206 HD-IK 1002 HD-UP 13 HD-MT 507 HD-MM 208 HD-XY 4711 HD-IK 1001 HD-MM 1977 HD-MB 3030 ? HD-MQ 2006 HD-VW 2012 ?
353
Unit 7: Changing Data Stored In Tables 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
©
2013 SAP AG. All rights reserved.
354
Unit 8 Defining How Data Is Stored
355
Unit 8: Defining How Data Is Stored Learning Objectives After completing this unit, you will be able to: • List the most important data types SAP HANA supports • Create new database tables in HANA
• Change such tables, e.g. by adding, removing or renaming columns
©
2013 SAP AG. All rights reserved.
356
Data Definition
Which data types are available in SAP HANA?
©
2013 SAP AG. All rights reserved.
357
Data Types
SAP HANA provides the following Data Types … Numeric types – TINYINT, SMALLINT, INTEGER, BIGINT, SMALLDECIMAL, DECIMAL, REAL, DOUBLE
Character string types – VARCHAR, NVARCHAR, ALPHANUM, SHORTTEXT
Date time types – DATE, TIME, SECONDDATE, TIMESTAMP
Binary types – VARBINARY
Large object types – BLOB, CLOB, NCLOB, TEXT
©
2013 SAP AG. All rights reserved.
358
Data Types
SAP HANA data types for integer values:
TINYINT
©
0 to 255
SMALLINT
-32,768 to 32,767
INTEGER
-2,147,483,648 to 2,147,483,647
BIGINT
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
2013 SAP AG. All rights reserved.
359
Data Types
SAP HANA data types for fixed-point values:
DECIMAL(p,s) The DECIMAL(p, s) data type specifies a fixed-point decimal number with precision p and scale s. The precision is the total number of significant digits. The scale is the number of digits from the decimal point to the least significant digit. Precision: p (1 ≤ p ≤ 34) Scale: s decimals (-6,111 ≤ s ≤ 6,176) 0.0000259 259 2590000 1.0000259 123.4567
©
2013 SAP AG. All rights reserved.
→ → → → →
DECIMAL(3,7) DECIMAL(3,0) DECIMAL(3,-4) DECIMAL(8,7) DECIMAL(7,4)
360
Data Types
SAP HANA data types for floating-point values:
©
SMALLDECIMAL
Floating-point decimal number Precision: variable (1 to 16) Scale: variable (-369 to 368)
DECIMAL
Floating-point decimal number Precision: variable (1 to 34) Scale: variable (-6,111 to 6,176 )
REAL
Floating-point binary number (32 Bit)
DOUBLE
Floating-point binary number (64 Bit)
2013 SAP AG. All rights reserved.
361
Data Types
When using numeric data types please note that ...
DECIMAL(3,7) is suitable for storing fixed-point decimals numbers with a precision of 3 and a scale of 7, while DECIMAL is used to store floating point decimal numbers with variable precision and variable scale.
Certain decimal values (for example, 0.2 and 0.4) using binary-based floating-point numbers (REAL or DOUBLE) cannot be represented in an exact way.
Therefore columns of type REAL or DOUBLE should not be used in WHERE or JOIN conditions.
• • •
©
0.4 is stored in a REAL column as 0.4000000059604645 10*(0.04/0.2) results in REAL value 1.9999998807907104 (instead of 2) 10*(0.04/0.2) results in DOUBLE value 1.9999999999999998 (instead of 2)
2013 SAP AG. All rights reserved.
362
Data Types
SAP HANA data types for character strings:
VARCHAR(n)
©
ASCII character string with maximum length n (n ≤ 5,000)
NVARCHAR(n)
Unicode character string with maximum length n (n ≤ 5,000)
ALPHANUM(n)
Alphanumeric character string with maximum length n (n ≤ 127)
SHORTTEXT(n)
Unicode character string with maximum length n (n ≤ 5,000) Special data type supporting text- and string-search features (e. g. Fuzzy Search) based on NVARCHAR(n)
2013 SAP AG. All rights reserved.
363
Data Types
SAP HANA data types for date and time:
©
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'
2013 SAP AG. All rights reserved.
364
Data Types
SAP HANA data types for binary data:
VARBINARY(n)
Binary data, maximum length n Bytes (n ≤ 5,000)
CREATE COLUMN TABLE MyTable ("VARBINARY-Column" VARBINARY(8)); INSERT INTO MyTable VALUES (TO_BINARY('Walldorf')); INSERT INTO MyTable VALUES (TO_BINARY('SAP')); INSERT INTO MyTable VALUES (x'00075341500700FF'); SELECT * FROM MyTable;
©
2013 SAP AG. All rights reserved.
VARBINARY-Column ---------------57616C6C646F7266 534150 00075341500700FF 365
Data Types
SAP HANA data types for large objects:
©
CLOB
Long ASCII character string (maximum 2 GB)
NCLOB
Long Unicode character string (maximum 2 GB)
BLOB
Large binary data (maximum 2 GB)
TEXT
Long Unicode character string (maximum 2 GB) Special data type supporting text- and string-search features (e. g. Fuzzy Search) based on NCLOB
2013 SAP AG. All rights reserved.
366
Data Types
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 that ...
• • • • • • • •
©
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
2013 SAP AG. All rights reserved.
367
Data Definition
Which SQL statements can be used to create, change, rename and delete tables?
©
2013 SAP AG. All rights reserved.
368
Data Definition
CREATE COLUMN TABLE A new (empty) table is created.
ALTER TABLE The definition of an existing table is changed .
RENAME TABLE An existing table is renamed.
DROP TABLE An existing table is deleted.
©
2013 SAP AG. All rights reserved.
369
Data Definition
CREATE COLUMN TABLE
©
2013 SAP AG. All rights reserved.
370
CREATE COLUMN TABLE
CREATE COLUMN TABLE Table
(Column Data_Type, Column Data_Type, Column Data_Type NOT NULL,
Column Data_Type NOT NULL, Column Data_Type DEFAULT Default_Value, Column Data_Type NOT NULL DEFAULT Default_Value,
PRIMARY KEY(Column, Column));
©
2013 SAP AG. All rights reserved.
371
CREATE COLUMN TABLE
It is not mandatory to define a primary key. If you do not define a key, the table con contain duplicates.
CREATE COLUMN TABLE Official (PNr VARCHAR(3), Name VARCHAR(20), Overtime INTEGER, Salary VARCHAR(3), Manager VARCHAR(3));
©
2013 SAP AG. All rights reserved.
372
CREATE COLUMN TABLE
Table names and column names usually do not distinguish between uppercase and lowercase letters. If you want to distinguish between uppercase and lowercase letters, you must use double quotes. We recommend not to use the option of lowercase letters.
CREATE COLUMN ("PNr" "Name" "Overtime" "Salary" "Manager"
©
2013 SAP AG. All rights reserved.
TABLE "Official" VARCHAR(3), VARCHAR(20), INTEGER, VARCHAR(3), VARCHAR(3));
373
CREATE COLUMN TABLE
Normally, table and column names may not contain spaces. If table or column names should contain spaces, you must use double quotes. We recommend not to use the option of spaces in table or column names.
CREATE COLUMN TABLE "Our Officials" ("Official PersNumber" VARCHAR(3), "Official Name" VARCHAR(20), "Overtime Hours" INTEGER, "Salary Group" VARCHAR(3), "Manager PersNumber" VARCHAR(3));
©
2013 SAP AG. All rights reserved.
374
CREATE COLUMN TABLE
If double quotes are used, table and column names may be different, but only by the use of upper and lower case. We do not recommend using this option.
CREATE COLUMN TABLE "Don’t do it" ("sap" INTEGER, "saP" INTEGER, "sAp" INTEGER, "sAP" INTEGER, "Sap" INTEGER, "SaP" INTEGER, "SAp" INTEGER, "SAP" INTEGER);
©
2013 SAP AG. All rights reserved.
375
CREATE COLUMN TABLE
If double quotes are used, table and column names can solely consist of spaces. We do not recommend using this option.
CREATE COLUMN TABLE " " (" " INTEGER, " " INTEGER, " " INTEGER, " " INTEGER); SELECT " ", " FROM " " WHERE " " = "
©
2013 SAP AG. All rights reserved.
", "
"
";
376
CREATE COLUMN TABLE
Condition
"1" = "2"
The value of the column named "1" must match the value of the column named "2" (based on the same row).
"2" = '3'
The value of the column named "2" must match the character string '3' (based on a certain row).
"3" = 4
©
Explanation
2013 SAP AG. All rights reserved.
The numeric value of column named "3" must be equal to 4 (based on a certain row).
377
CREATE COLUMN TABLE
You can define a primary key. If a primary key is defined, the table does not contain duplicates. You can use the primary key definition as part of the column definition, if the primary key consists of only one column.
CREATE COLUMN TABLE Official (PNr VARCHAR(3) PRIMARY KEY, Name VARCHAR(20), Overtime INTEGER, Salary VARCHAR(3), Manager VARCHAR(3));
©
2013 SAP AG. All rights reserved.
378
CREATE COLUMN TABLE
You can use a separate PRIMARY KEY clause. You can define a maximum of one primary key per table.
CREATE COLUMN TABLE Official (PNr VARCHAR(3), Name VARCHAR(20), Overtime INTEGER, Salary VARCHAR(3), Manager VARCHAR(3), PRIMARY KEY(PNr));
©
2013 SAP AG. All rights reserved.
379
CREATE COLUMN TABLE
You must use a separate PRIMARY KEY clause to define a multi-column primary key. For all columns of the PRIMARY KEY NULL values are (implicitly) prohibited.
CREATE COLUMN TABLE Owner_EU (Country VARCHAR(3), OwnerID VARCHAR(3), Name VARCHAR(20), Birthday DATE, City VARCHAR(20), PRIMARY KEY(Country, OwnerID));
©
2013 SAP AG. All rights reserved.
380
CREATE COLUMN TABLE
When defining a column you can specify that no NULL values are allowed by adding the words NOT NULL
For primary key columns, NULL values are implicitly prohibited. For these columns, adding NOT NULL is permitted but not required.
CREATE COLUMN TABLE Official (PNr VARCHAR(3), Name VARCHAR(20) NOT NULL, Overtime INTEGER, Salary VARCHAR(3) NOT NULL, Manager VARCHAR(3), PRIMARY KEY(PNr));
©
2013 SAP AG. All rights reserved.
381
CREATE COLUMN TABLE
You can define a default value for a column by adding DEFAULT .
The default value is used during INSERT, if no value is specified for the corresponding column. The default value will not be used during INSERT, if a NULL value is explicitly specified.
CREATE COLUMN TABLE Official (PNr VARCHAR(3), Name VARCHAR(20), Overtime INTEGER DEFAULT 0, Salary VARCHAR(3) DEFAULT 'A06', Manager VARCHAR(3), PRIMARY KEY(PNr));
©
2013 SAP AG. All rights reserved.
382
CREATE COLUMN TABLE
NULL values are not excluded when setting a default value. Hence the words NOT NULL can also be useful for columns that do have a default value set. The order of the key words "DEFAULT" and "NOT NULL" is irrelevant.
CREATE COLUMN TABLE Official (PNr VARCHAR(3), Name VARCHAR(20), Overtime INTEGER NOT NULL DEFAULT 0, Salary VARCHAR(3) DEFAULT 'A06' NOT NULL, Manager VARCHAR(3), PRIMARY KEY(PNr));
©
2013 SAP AG. All rights reserved.
383
CREATE COLUMN TABLE
When defining a column, you can specify that this column must not contain double values by adding UNIQUE .
In contrast to the PRIMARY
KEY, UNIQUE allows NULL values.
CREATE COLUMN TABLE Car (CarID VARCHAR(3) PRIMARY KEY, PlateNumber VARCHAR(10) UNIQUE, Brand VARCHAR(20), Color VARCHAR(10), HP INTEGER, Owner VARCHAR(3));
©
2013 SAP AG. All rights reserved.
384
CREATE COLUMN TABLE
NULL values are not excluded when setting UNIQUE . Hence the words
NOT NULL can also be useful for columns defined as UNIQUE .
The order of the key word “UNIQUE" and "NOT NULL" is irrelevant
CREATE COLUMN TABLE Car (CarID VARCHAR(3) PRIMARY KEY, PlateNumber VARCHAR(10) UNIQUE NOT NULL, Brand VARCHAR(20), Color VARCHAR(10), HP INTEGER, Owner VARCHAR(3));
©
2013 SAP AG. All rights reserved.
385
CREATE COLUMN TABLE
It is possible to prohibit duplicate values for more than one column. Therefore you can define several keys for one and the same table.
Using
UNIQUE
two single-column keys are defined:
CREATE COLUMN TABLE Car (CarID VARCHAR(3) PRIMARY KEY, PlateNumber VARCHAR(10), Brand VARCHAR(20) UNIQUE, Color VARCHAR(10) UNIQUE, HP INTEGER, Owner VARCHAR(3));
©
2013 SAP AG. All rights reserved.
386
CREATE COLUMN TABLE
You can use a separate Using
UNIQUE
UNIQUE
clause for the key definition.
two single-column keys are defined:
CREATE COLUMN TABLE Car (CarID VARCHAR(3) PRIMARY KEY, PlateNumber VARCHAR(10), Brand VARCHAR(20), Color VARCHAR(10), HP INTEGER, Owner VARCHAR(3), UNIQUE(Brand), UNIQUE(Color));
©
2013 SAP AG. All rights reserved.
387
CREATE COLUMN TABLE
You must use a separate
UNIQUE
clause to define a multi-column key.
Using UNIQUE a two-column key is defined:
CREATE COLUMN TABLE Car (CarID VARCHAR(3) PRIMARY KEY, PlateNumber VARCHAR(10), Brand VARCHAR(20), Color VARCHAR(10), HP INTEGER, Owner VARCHAR(3), UNIQUE(Brand, Color));
©
2013 SAP AG. All rights reserved.
388
Data Definition
ALTER TABLE
©
2013 SAP AG. All rights reserved.
389
ALTER TABLE
ALTER TABLE Table ADD (Column Data_Type, Column Data_Type NOT NULL DEFAULT Default_Value);
ALTER TABLE Table DROP (Column, Column, Column);
ALTER TABLE Table ALTER (Column Data_Type DEFAULT Default_Value, Column Data_Type NULL);
©
2013 SAP AG. All rights reserved.
390
ALTER TABLE
You can ADD columns to an existing table.
You can use NOT NULL only if the table is empty or if you define a DEFAULT value in addition. Newly added columns are filled with NULL values, or – if available – with the corresponding default value.
ALTER TABLE Official ADD (RemainderDays INTEGER, AnnualLeave INTEGER NOT NULL DEFAULT 30);
©
2013 SAP AG. All rights reserved.
391
ALTER TABLE
DROP
You can
You cannot drop columns that are part of the primary key. Also for tables without a primary key at least one column must be left.
columns from an existing table.
ALTER TABLE Official DROP (Salary, Manager, Name);
©
2013 SAP AG. All rights reserved.
392
ALTER TABLE
You can change the definition of an existing column. For columns without a default value, you can specify one. For columns with a default value, you can change the existing value. The new (or first time) defined default value only affects newly inserted rows.
For columns that do not allow NULL values so far, NULL values can be allowed.
ALTER TABLE Official ALTER (Overtime Salary Name
©
2013 SAP AG. All rights reserved.
INTEGER DEFAULT 7, VARCHAR(3) DEFAULT 'A01', VARCHAR(20) NULL);
393
ALTER TABLE
For columns with a default value you can delete the existing default value. The deletion of the default value only affects newly inserted rows. Already existing values of a table are not affected by the deletion of the default value.
ALTER TABLE Official ALTER (Salary VARCHAR(3) DEFAULT NULL);
©
2013 SAP AG. All rights reserved.
394
ALTER TABLE
You can change the data type of existing columns. Type compatibility must be ensured. For string-based data types the new data type must have at least the same maximum length (in comparison to the old data type).
VARCHAR(20) → VARCHAR(25) is allowed, but not VARCHAR(25) → VARCHAR(20) ALTER TABLE Official ALTER (Name Salary
©
2013 SAP AG. All rights reserved.
VARCHAR(25), VARCHAR(5));
395
ALTER TABLE
You can delete the definition of the primary key. Only the primary key property is deleted. The corresponding columns of the (previous) primary key are not deleted.
ALTER TABLE Official DROP PRIMARY KEY;
©
2013 SAP AG. All rights reserved.
396
ALTER TABLE
You can subsequently define a primary key for a table without one. The corresponding columns must not contain NULL values. The key property must not be violated by the existing data.
ALTER TABLE Official ADD PRIMARY KEY(PNr);
©
2013 SAP AG. All rights reserved.
397
Data Definition
RENAME TABLE
©
2013 SAP AG. All rights reserved.
398
RENAME TABLE
RENAME TABLE old_name TO new_name;
©
2013 SAP AG. All rights reserved.
399
RENAME TABLE
RENAME
You can
The table you want to rename can be empty or can contain data. The new table name must be different to the old table name.
an existing table.
RENAME TABLE Official TO Employee;
©
2013 SAP AG. All rights reserved.
400
Data Definition
DROP TABLE
©
2013 SAP AG. All rights reserved.
401
DROP TABLE
DROP TABLE Table;
©
2013 SAP AG. All rights reserved.
402
DROP TABLE
DROP
You can
The table you want to drop can be empty or contain data. Both, the table content and the table itself will be deleted.
an existing table.
DROP TABLE Official;
©
2013 SAP AG. All rights reserved.
403
Data Definition
Renaming a table column
©
2013 SAP AG. All rights reserved.
404
Data Definition
RENAME COLUMN An existing table column is renamed. This statement is used to "permanently" rename a column of an existing table (by which the table definition is changed). Hence, we are not simply renaming a column of the query-result (for which the keyword "AS" can be used in the SELECT clause).
©
2013 SAP AG. All rights reserved.
405
Data Definition
RENAME COLUMN
©
2013 SAP AG. All rights reserved.
406
RENAME COLUMN
RENAME COLUMN Table.Column TO new_Column_Name;
©
2013 SAP AG. All rights reserved.
407
RENAME COLUMN
You can (subsequently) rename a table column. The respective table can be empty or can contain data. The new column name must be different to the old column name.
Column “PNr“ of Table “Official“ is renamed into “PersNumber“:
RENAME COLUMN Official.PNr TO PersNumber;
©
2013 SAP AG. All rights reserved.
408
Unit 8: Defining Data And Access Control Summary
You should now be able to: •
List the most important data types SAP HANA supports • Create new database tables in HANA • Change such tables, e.g. by adding, removing or renaming columns
©
2013 SAP AG. All rights reserved.
409
Unit 9 Using Views for Data Access
410
Unit 9: Using Views For Data Access Learning Objectives
After completing this unit, you will be able to: • Describe the use cases for and advantages of using database views
• Define database views using SQL • Query data through views • Delete database views using SQL
©
2013 SAP AG. All rights reserved.
411
Data Definition
Why do we need views? What are the advantages of the view concept?
©
2013 SAP AG. All rights reserved.
412
Advantages of Views
Decoupling applications from lower levels (relative to the 3 level schema architecture) – Changes to the conceptual schema (such as adding new columns of the table) have no effect on application
Tailored views, individually customized for the users and their tasks – The user (or application programmer) does not know the entire database structure in detail - but the excerpt that is relevant to them.
Simplification of queries – Complex queries referencing views are easier to formulate, provided that the view provides an appropriate pre-selection of data.
Possibility of access restriction – Views can be used to realize value-dependent access privileges (for example, managers can access only the data of their employees).
©
2013 SAP AG. All rights reserved.
413
Data Definition
Leveraging Views
©
2013 SAP AG. All rights reserved.
414
VIEWs
Example of a view containing only red cars:
CREATE VIEW SELECT FROM WHERE
redCars AS CarID, Color, PlateNumber, HP AS Power Car Color = 'red';
CARID ----F01 F09 F12 F13 F18
©
2013 SAP AG. All rights reserved.
COLOR ----red red red red red
PLATENUMBER ----------HD-V 106 HD-UP 13 HD-XY 4711 HD-IK 1001 HD-MQ 2006
POWER ----75 105 105 136 90
415
VIEWs
You can select data from a view in the same way you select from a “normal” table. Which red cars have more than 100 HP?
SELECT FROM WHERE ORDER BY
CarID, PlateNumber, Power redCars Power > 100 Power DESC, CarID ASC;
CARID ----F13 F09 F12
©
2013 SAP AG. All rights reserved.
PLATENUMBER ----------HD-IK 1001 HD-UP 13 HD-XY 4711
POWER ----136 105 105
416
VIEWs
You can INSERT new rows into a view.
The new rows are not inserted into the view, but into the underlying base table!
INSERT INTO redCars VALUES ('F77', 'red', 'HD-MT 2509', 170);
SELECT * FROM Car; CARID ----F01 F02 F03 … F18 F19 F20 F77
©
2013 SAP AG. All rights reserved.
PLATENUMBER ----------HD-V 106 HD-VW 4711 HD-JA 1972 … HD-MQ 2006 HD-VW 2012 ? HD-MT 2509
BRAND ------Fiat VW BMW … Renault VW Audi ?
COLOR ----red black blue … red black green red
HP --75 120 184 … 90 125 184 170
OWNER ----H06 H03 H03 … H03 H01 ? ? 417
VIEWs You can UPDATE rows in a view.
The rows are not changed in the view, but in the underlying base table!
UPDATE redCars SET Power = 120 WHERE Power < 120; SELECT * FROM Car;
©
2013 SAP AG. All rights reserved.
CARID ----F01 … F07 F08 F09 F10 F11 F12 F13 … F18 …
PLATENUMBER ----------HD-V 106 … HD-ML 3206 HD-IK 1002 HD-UP 13 HD-MT 507 HD-MM 208 HD-XY 4711 HD-IK 1001 … HD-MQ 2006 …
BRAND ------Fiat … Audi VW Skoda BMW BMW Skoda Renault … Renault …
COLOR ----red … blue black red black green red red … red …
HP --120 … 116 160 120 140 184 120 136 … 120 …
OWNER ----H06 … H03 H07 H02 H04 H02 H04 H07 … H03 … 418
VIEWs You can DELETE rows from a view.
The rows are not deleted from the view, but from the underlying base table!
DELETE FROM redCars WHERE Power < 125; SELECT * FROM Car;
©
2013 SAP AG. All rights reserved.
CARID ----F01 … F07 F08 F09 F10 F11 F12 F13 … F18 …
PLATENUMBER ----------HD-V 106 … HD-ML 3206 HD-IK 1002 HD-UP 13 HD-MT 507 HD-MM 208 HD-XY 4711 HD-IK 1001 … HD-MQ 2006 …
BRAND ------Fiat … Audi VW Skoda BMW BMW Skoda Renault … Renault …
COLOR ----red … blue black red black green red red … red …
HP --120 … 116 160 120 140 184 120 136 … 120 …
OWNER ----H06 … H03 H07 H02 H04 H02 H04 H07 … H03 … 419
Data Definition
Which SQL statements can I use to create or delete views?
©
2013 SAP AG. All rights reserved.
420
Data Definition
CREATE VIEW Creates a new view.
DROP VIEW Deletes an existing view.
©
2013 SAP AG. All rights reserved.
421
Data Definition
CREATE VIEW
©
2013 SAP AG. All rights reserved.
422
CREATE VIEW
CREATE VIEW SELECT FROM WHERE
©
2013 SAP AG. All rights reserved.
View AS Column, Column, Column Table Condition;
423
CREATE VIEW
You can restrict a view to specific columns of the underlying base table.
CREATE VIEW Autos AS SELECT Brand, Color, HP FROM Car;
©
2013 SAP AG. All rights reserved.
BRAND -------Fiat VW BMW Mercedes Mercedes Audi Audi VW Skoda BMW BMW Skoda Renault Mercedes Skoda Opel Audi Renault VW Audi
COLOR -----red black blue white black yellow blue black red black green red red white black green orange red black green
HP --75 120 184 136 170 260 116 160 105 140 184 105 136 170 136 120 184 90 125 184
424
CREATE VIEW
You can restrict a view to specific rows of the underlying base table.
CREATE VIEW SELECT FROM WHERE
Audis AS Brand, Color, HP Car Brand = 'Audi';
BRAND ----Audi Audi Audi Audi
©
2013 SAP AG. All rights reserved.
COLOR -----yellow blue orange green
HP --260 116 184 184
425
CREATE VIEW
You can restrict a view to a certain amount of rows:
CREATE VIEW SELECT FROM WHERE
"Top3 Audis" AS TOP 3 Brand, Color, HP Car Brand = 'Audi';
BRAND ----Audi Audi Audi
©
2013 SAP AG. All rights reserved.
COLOR -----yellow blue orange
HP --260 116 184
426
CREATE VIEW
The columns of a view can have different names than the columns of the underlying base table.
CREATE VIEW SELECT FROM WHERE
Audis (Manufacturer, Color, Power) AS Brand, Color, HP Car Brand = 'Audi';
MANUFACTURER -----------Audi Audi Audi Audi
©
2013 SAP AG. All rights reserved.
COLOR -----yellow blue orange green
POWER ----260 116 184 184
427
CREATE VIEW
You can (and should) rename the columns in the SELECT clause
CREATE VIEW SELECT FROM WHERE
Audis AS Brand AS Manufacturer, Color, HP AS Power Car Brand = 'Audi';
MANUFACTURER -----------Audi Audi Audi Audi
©
2013 SAP AG. All rights reserved.
COLOR -----yellow blue orange green
POWER ----260 116 184 184
428
CREATE VIEW
If you rename columns in both the CREATE VIEW clause and in the SELECT clause, the name of the CREATE VIEW clause "wins“. We recommend renaming columns only in the SELECT clause.
CREATE VIEW SELECT FROM WHERE
Audis (Manufacturer, Color, Power) AS Brand AS Description, Color, HP AS HorsePower Car Brand = 'Audi';
MANUFACTURER -----------Audi Audi Audi Audi
©
2013 SAP AG. All rights reserved.
COLOR -----yellow blue orange green
POWER ----260 116 184 184
429
CREATE VIEW
A view can contain duplicates.
CREATE VIEW Colors AS SELECT Color FROM Car;
©
2013 SAP AG. All rights reserved.
COLOR -----red black blue white black yellow blue black red black green red red white black green orange red black green
430
CREATE VIEW
You can use DISTINCT in the view definition. Thus the view does not contain duplicates.
CREATE VIEW Colors AS SELECT DISTINCT Color FROM Car;
©
2013 SAP AG. All rights reserved.
COLOR -----red black blue white yellow green orange
431
CREATE VIEW
You can use ORDER BY in the view definition. From a theoretical perspective the use of ORDER BY is highly questionable, because the rows of a view are unsorted! There is no guarantee that the specified ORDER BY sorting is taken into account when reading the view. We recommend avoiding ORDER BY in the view definition.
CREATE VIEW SELECT FROM ORDER BY
©
2013 SAP AG. All rights reserved.
Colors AS DISTINCT Color Car Color;
COLOR -----blue yellow green orange red black white 432
CREATE VIEW
You can use calculated columns in a view.
CREATE VIEW SELECT FROM WHERE
Audis AS CarID, Color, ROUND(HP / 1.35962162, 0) AS "kW" Car Brand = 'Audi';
CARID ----F06 F07 F17 F20
©
2013 SAP AG. All rights reserved.
COLOR -----yellow blue orange green
kW --191 85 135 135
433
CREATE VIEW
You can use functions in the view definition.
CREATE VIEW OwnerOverview AS SELECT Name, YEAR(Birthday) AS YearOfBirth FROM Owner; NAME -----Ms T Ms U SAP AG HDM AG Mr V Ms W IKEA Mr X Ms Y Mr Z ©
2013 SAP AG. All rights reserved.
YEAROFBIRTH ----------1934 1966 ? ? 1952 1957 ? 1986 1986 1986 434
CREATE VIEW
You can use GROUP BY and aggregate expressions in the view definition.
CREATE VIEW SELECT FROM GROUP BY HAVING
Cars AS Brand, COUNT(*) AS Quantity Car Brand COUNT(*) > 2; BRAND -------VW BMW Mercedes Audi Skoda
©
2013 SAP AG. All rights reserved.
QUANTITY -------3 3 3 4 3
435
CREATE VIEW
You can use UNION [ALL] in the view definition.
CREATE VIEW Persons AS SELECT OwnerID AS "Pers ID", Name FROM Owner WHERE Birthday >= '1977-05-21' UNION ALL SELECT PNr, Name FROM Official WHERE Salary = 'A09';
©
2013 SAP AG. All rights reserved.
Pers ID ------H08 H09 H10 P01 P03 P06
NAME ---Mr X Ms Y Mr Z Mr A Ms C Mr F
436
CREATE VIEW
You can use (any) JOIN in the view definition.
CREATE VIEW SELECT FROM WHERE
StolenCars AS c.PlateNumber, c.Brand, c.Color Car c, Stolen s c.PlateNumber = s.PlateNumber;
PLATENUMBER ----------HD-V 106 HD-VW 1999 HD-Y 333
©
2013 SAP AG. All rights reserved.
BRAND ----Fiat Audi Audi
COLOR -----red yellow orange
437
CREATE VIEW
You can use an uncorrelated sub query in the view definition.
CREATE VIEW SELECT FROM WHERE
StolenCars AS PlateNumber, Brand, Color Car PlateNumber IN (SELECT PlateNumber FROM Stolen);
PLATENUMBER ----------HD-V 106 HD-VW 1999 HD-Y 333
©
2013 SAP AG. All rights reserved.
BRAND ----Fiat Audi Audi
COLOR -----red yellow orange
438
CREATE VIEW
You can use a correlated sub query in the view definition.
CREATE VIEW SELECT FROM WHERE
StolenCars AS c.PlateNumber, Car c EXISTS (SELECT FROM WHERE
c.Brand, c.Color * Stolen s s.PlateNumber = c.PlateNumber);
PLATENUMBER ----------HD-V 106 HD-VW 1999 HD-Y 333
©
2013 SAP AG. All rights reserved.
BRAND ----Fiat Audi Audi
COLOR -----red yellow orange
439
CREATE VIEW
You can define a view based on another view.
CREATE VIEW SELECT FROM WHERE
Volkswagen AS CarID, PlateNumber, Color, HP Car Brand = 'VW';
CREATE VIEW SELECT FROM WHERE
blackVolkswagen AS CarID, PlateNumber, HP Volkswagen Color = 'black';
©
2013 SAP AG. All rights reserved.
CARID ----F02 F08 F19
PLATENUMBER ----------HD-VW 4711 HD-IK 1002 HD-VW 2012
HP --120 160 125
440
Data Definition
DROP VIEW
©
2013 SAP AG. All rights reserved.
441
DROP VIEW
DROP VIEW View;
©
2013 SAP AG. All rights reserved.
442
DROP VIEW
You can drop an existing view. Only the view definition is deleted. The underlying data of the view is not deleted.
DROP VIEW redCars;
©
2013 SAP AG. All rights reserved.
443
Views and Changeability
Is it always possible to change the data of the underlying table by referencing a view?
©
2013 SAP AG. All rights reserved.
444
Views and Changeability
Referencing a view, the rows of the underlying base table cannot be changed if (at minimum) any of the following statements is true: The view definition contains a calculation in the projection list (such as HP / 1.36). The view definition uses a function in the projection list (such as YEAR). The view definition uses DISTINCT to eliminate duplicates. The view definition contains the TOP n clause to restrict the number of rows to n. The view definition uses an aggregate function in the projection list (such as MAX). The view definition is based on a GROUP BY clause.
The view definition uses UNION. The view definition contains an (implicit or explicit) JOIN. The view definition uses a sub query in the projection list.
©
2013 SAP AG. All rights reserved.
445
Unit 9: Using Views For Data Access Summary
You should now be able to: • Describe the use cases for and advantages of using database views • Define database views using SQL • Query data through views
• Delete database views using SQL
©
2013 SAP AG. All rights reserved.
446
Unit 10 Defining Data Access
447
Unit 10: Defining Data Access Learning Objectives
After completing this unit, you will be able to: • Access tables in other schemas (assuming suitable permissions) • Control how other users can access data in your own schema
• Explain when database indexes make sense even in HANA • Create and delete database indexes using SQL
© 2013
SAP AG. All rights reserved.
448
Data Definition
Why are there no naming collisions when multiple users create the same table or view?
© 2013
SAP AG. All rights reserved.
449
Schemata
Please note: The following information applies to direct (SQL-based) work on SAP HANA - but not to database access using ABAP! User 1 creates table “Car”. User 2 also creates table “Car”. Why is there no naming collision? The name of the database object (table, view etc.) implicitly contains a schema name as prefix: . “USER1.Car” ≠ “USER2.Car”
© 2013
SAP AG. All rights reserved.
450
Schemata
When creating a user an identically named schema is created implicitly. By default a user works in his own schema (and namespace). You can explicitly specify the schema name if necessary. Which brand has (at least) one black car? SELECT FROM WHERE ORDER BY
© 2013
DISTINCT User1.Car.Brand User1.Car User1.Car.Color = 'black' User1.Car.Brand;
SAP AG. All rights reserved.
BRAND -------BMW Mercedes Skoda VW 451
Schemata
Queries across multiple schemata are possible. To whom is (at least) one black car registered? SELECT DISTINCT Schema1.Owner.Name FROM Schema1.Owner, Schema2.Car WHERE Schema1.Owner.OwnerID = Schema2.Car.Owner AND Schema2.Car.Color = 'black';
NAME -----SAP AG IKEA HDM AG Ms T © 2013
SAP AG. All rights reserved.
452
Access Control
How can I define who has access to which data?
© 2013
SAP AG. All rights reserved.
453
Access Control
To specify who can access which data, you can use the following two main (SQL based) options. They can be used in combination: Create views that represent a portion of the data: – 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: – You grant read only permissions to a user, who should be able to read the owner data but not to change it.
© 2013
SAP AG. All rights reserved.
454
Access Control
Which SQL statements can I use for access control?
© 2013
SAP AG. All rights reserved.
455
Access Control
GRANT You can selectively grant access permissions to a user (or role).
REVOKE You can selectively revoke access permissions from a user (or role).
© 2013
SAP AG. All rights reserved.
456
Access Control
The SQL statement GRANT
© 2013
SAP AG. All rights reserved.
457
GRANT
GRANT Privilege, Privilege ON Database_Object TO Database_User WITH GRANT OPTION;
© 2013
SAP AG. All rights reserved.
458
GRANT
You can define which user is allowed to SELECT from which table.
By adding the WITH GRANT OPTION the recipient of the privilege (grantee) is allowed to grant this privilege to other users (without losing it).
User1 is allowed to read (SELECT) from table Official of schema Schema2. User1 is allowed to grant this SELECT privilege to other users.
GRANT ON TO WITH
© 2013
SELECT Schema2.Official User1 GRANT OPTION;
SAP AG. All rights reserved.
459
GRANT
You can exactly specify which user is allowed to INSERT rows into which table.
The INSERT privilege does not necessarily require the SELECT privilege.
The INSERT privilege does not automatically include the SELECT privilege.
User1 is allowed to INSERT rows into table Owner of schema Schema2. As User 1 has no SELECT privilege, he is not allowed to read the rows he has inserted.
GRANT INSERT ON Schema2.Owner TO User1;
© 2013
SAP AG. All rights reserved.
460
GRANT
You can exactly specify which user is allowed to UPDATE rows of which table.
The UPDATE privilege does not necessarily require the SELECT privilege.
The UPDATE privilege does not automatically include the SELECT privilege.
User1 is allowed to UPDATE table Car_EU of schema Schema2. Because User1 has no SELECT privileges for the table Car_EU the WHERE clause of his UPDATE statement cannot contain a reference to the columns of this table.
GRANT UPDATE ON Schema2.Car_EU TO User1;
© 2013
SAP AG. All rights reserved.
461
GRANT
You can exactly specify which user is allowed to DELETE rows from which table.
The DELETE privilege does not necessarily require the SELECT privilege.
The DELETE privilege does not automatically include the SELECT privilege.
User1 is allowed to DELETE rows from table Stolen of schema Schema2. Because User1 has no SELECT privileges for the table Stolen the WHERE clause of his DELETE statement cannot contain a reference to the columns of this table.
GRANT DELETE ON Schema2.Stolen TO User1;
© 2013
SAP AG. All rights reserved.
462
GRANT
You can assign multiple privileges for the same database object by using a single GRANT statement. User1 is allowed to SELECT, INSERT, UPDATE, DELETE on table Contact of schema Schema2. User1 is allowed to grant these privileges to other users (in whole or in part). GRANT ON TO WITH
© 2013
SELECT, INSERT, UPDATE, DELETE Schema2.Contact User1 GRANT OPTION;
SAP AG. All rights reserved.
463
GRANT
You can assign privileges for all tables and views of a schema by using a single GRANT statement. User1 is allowed to SELECT from all tables and views of schema Schema2. User1 is allowed to grant this privilege to other users (for all or selected tables or views).
GRANT ON TO WITH
© 2013
SELECT SCHEMA Schema2 User1 GRANT OPTION;
SAP AG. All rights reserved.
464
GRANT
Depending on the granularity (or the type of database object) you can assign different access privileges to users.
Granularity
Access Privileges
(single) Schema
SELECT, INSERT, UPDATE, DELETE, DROP, ALTER, CREATE ANY
(single) Table
SELECT, INSERT, UPDATE, DELETE, DROP, ALTER
(single) View
SELECT, INSERT, UPDATE, DELETE, DROP
© 2013
SAP AG. All rights reserved.
465
Access Control
The SQL statement REVOKE
© 2013
SAP AG. All rights reserved.
466
REVOKE
REVOKE Privilege, Privilege, Privilege ON Database_Object FROM Database_User;
© 2013
SAP AG. All rights reserved.
467
REVOKE
Using the REVOKE statement you can selectively revoke access privileges from a user.
User1 will no longer be allowed to read from table Official of schema Schema2 (assuming that this privilege is not granted by other privileges)
REVOKE SELECT ON Schema2.Official FROM User1;
© 2013
SAP AG. All rights reserved.
468
REVOKE
You can revoke multiple privileges for the same database object by using a single REVOKE statement
User1 will no longer be allowed to SELECT, INSERT, UPDATE, DELETE from table Contact of schema Schema2 (assuming that these privileges are not granted by other privileges)
REVOKE SELECT, INSERT, UPDATE, DELETE ON Schema2.Contact FROM User1;
© 2013
SAP AG. All rights reserved.
469
REVOKE
Access privileges on different hierarchical levels of database objects are independent.
Despite the REVOKE statement User1 still has read access to the table Official of schema Schema2 , because he still has a (not withdrawn) read permission on the entire schema Schema2 .
GRANT SELECT ON SCHEMA Schema2 TO User1; REVOKE SELECT ON Schema2.Official FROM User1;
© 2013
SAP AG. All rights reserved.
470
REVOKE
The independence of access privileges at various hierarchical levels is also valid in the contrary case.
Despite the REVOKE statement User1 still has read access to the table Official of schema Schema2 , because he still has a (not withdrawn) read permission on the table.
GRANT SELECT ON Schema2.Official TO User1; REVOKE SELECT ON SCHEMA Schema2 FROM User1;
© 2013
SAP AG. All rights reserved.
471
Access Paths
What is a database index?
© 2013
SAP AG. All rights reserved.
472
Access Paths
An index (plural: indexes or indices) is an access path. An index may speed up search and sorting. – Index access instead of "full table scan" or "full column scan" – Index usage with HANA COLUMN TABLES only in exceptional cases
An index usually slows down inserts, update and deletion. An index has no influence on the query result. – Effect "only" on performance
Indexes are not covered by the SQL standard. – CREATE / DROP INDEX are strictly speaking no SQL statements.
© 2013
SAP AG. All rights reserved.
473
HANA: Why Indexes on Column Store?
– Sorted dictionary of values – (Bit-)Vector of value IDs
Are indexes necessary to speed up data access?
© 2013
SAP AG. All rights reserved.
Column „Name“ conceptual
Column “Name” – internal representation Value ID vector
Dictionary
Miller
4
0
Baker
Jones
1
1
Jones
Millman
5
2
John
Zsuwalski
772
3
Johnson
Baker
0
4
Miller
Miller
4
5
Millman
John
2
Miller
4
Johnson
3
Jones
1
…
…
sorted
HANA stores data in columns, not rows Columns are stored as
… 772
Zsuwalski …
474
HANA: Table Scan With Value ID
Value ID lookup is fast
Dictionary
–
WHERE Name = 'Zsuwalski'
Row ID lookup can still involve “full column scan” – No issue for most value ID vectors, even less if sorted – But may be slow for very large value ID vectors
© 2013
SAP AG. All rights reserved.
binary search on dictionary
– binary search on sorted dictionary
Value ID vector
ID
Value
Row ID
Value ID
0
Baker
0
4
1
Jones
1
1
2
John
2
5
3
Johnson
3
772
4
Miller
4
0
5
Millman
772
full column scan with value ID
…
…
254621
772
Zsuwalski
…
…
…
475
HANA: Inverted Index on Column Store
An inverted index can be reasonable in exceptional cases if column scan performance is not sufficient
WHERE Name = 'Zsuwalski'
© 2013
SAP AG. All rights reserved.
binary search on dictionary
Dictionary
Inverted Index
Value ID vector
ID
Value
Value ID
Rows
Row ID
Value ID
0
Baker
0
56756
0
4
1
Jones
1
345, 76876
…
…
3
772
… 772
Zsuwalski …
…
…
772
3, 254621
…
…
… 254621
772
…
…
476
Access Paths
Which SQL statements can I use to create a database index?
© 2013
SAP AG. All rights reserved.
477
Access Paths
CREATE INDEX A new database index is created.
DROP INDEX An existing database index is deleted.
© 2013
SAP AG. All rights reserved.
478
Access Paths
CREATE INDEX
© 2013
SAP AG. All rights reserved.
479
Access Paths
CREATE INDEX Access_Path ON Table (Column, Column);
CREATE UNIQUE INDEX Access_Path ON Table (Column, Column);
© 2013
SAP AG. All rights reserved.
480
CREATE INDEX
To speed up the read access you can create an index on a table column. The respective table can be empty or contain data.
An index on column PlateNumber of table Car will be created:
CREATE INDEX PlateNumberIndex ON Car (PlateNumber);
© 2013
SAP AG. All rights reserved.
481
CREATE INDEX
You can create a UNIQUE INDEX to ensure that the corresponding column cannot contain duplicate values.
Unlike PRIMARY KEY, UNIQUE INDEX does not prohibit NULL values.
A UNIQUE INDEX can only be created when the column contains no duplicate values.
In table Car no duplicated plate numbers are allowed:
CREATE UNIQUE INDEX PlateNumberIndex ON Car (PlateNumber);
© 2013
SAP AG. All rights reserved.
482
CREATE INDEX
You can create multiple indexes on the same table. A single-column index is created on column Brand of table Car. A single-column index is created on column Color of the same table.
CREATE INDEX BrandsIndex ON Car (Brand); CREATE INDEX ColorsIndex ON Car (Color);
© 2013
SAP AG. All rights reserved.
483
CREATE INDEX
You can create a multi-column index. Here a two-column index is created:
CREATE INDEX BrandsColorsCombinationsIndex ON Car (Brand, Color);
© 2013
SAP AG. All rights reserved.
484
CREATE INDEX
By applying a multi-column UNIQUE INDEX, the relevant combination of columns can be defined as key.
In contrast to the PRIMARY KEY, UNIQUE INDEX does not prohibit NULL values in the key columns.
You can only create a multi-column UNIQUE INDEX, if the relevant column combination does not contain duplicates. Here, the value uniqueness of Country and PlateNumber combinations is enforced:
CREATE UNIQUE INDEX CountryPlateNumberCombiIndex ON Car_EU (Country, PlateNumber);
© 2013
SAP AG. All rights reserved.
485
Access Paths
DROP INDEX
© 2013
SAP AG. All rights reserved.
486
Access Paths
DROP INDEX Access_Path;
© 2013
SAP AG. All rights reserved.
487
DROP INDEX
You can drop an existing index. Only the access path (index) is deleted. The underlying data of the index is not deleted.
DROP INDEX does not syntactically distinguish between NON-UNIQUE and UNIQUE indexes.
DROP INDEX PlateNumberIndex;
© 2013
SAP AG. All rights reserved.
488
Unit 10: Defining Data Access Summary
You should now be able to: •
Access tables in other schemas (assuming suitable permissions) • Control how other users can access data in your own schema • Explain when database indexes make sense even in HANA • Create and delete database indexes using SQL
© 2013
SAP AG. All rights reserved.
489
Unit 11 Database Transactions
490
Unit 11: Database Transactions Learning Objectives
After completing this unit, you will be able to: • Explain what a database transaction is and why it is needed • Explain the acronym ACID
• Finish database transactions in HANA using SQL statements • Describe issues that arise if transactions are not mutually isolated • Understand how classical database systems synchronize concurrent transactions • Understand how HANA synchronizes concurrent transactions
© 2013
SAP AG. All rights reserved.
491
Database Transactions
•
Transaction (TA) = Sequence of associated database operations (SQL statements) for which the ACID requirements must be met.
•
All database operations always take place within transactions (in the extreme case, each operation has its own transaction).
• • • •
A C I D
© 2013
= = = =
Atomicity Consistency Isolation Durability
SAP AG. All rights reserved.
492
Database Transactions
• Atomicity (A): A transaction is either executed completely or not at all. For Example: Transfer from Account1 to Account2
• Consistency (C): A transaction will bring the database from one consistent state to an(other) consistent state. During a transaction inconsistent states are possible, for example account transfers or marriage registration.
• Isolation (I): The database changes performed within a transaction shall only be visible to the outside after the completion of the transaction Important only for multi-user operations. Problematic if changes of canceled transactions can be seen.
• Durability (D): If a transaction is successfully completed (COMMIT), all changes from the transaction must permanently stay even in case of failures, or can be restored automatically. For example: Booking a deposit
© 2013
SAP AG. All rights reserved.
493
Database Transactions •
The ACID requirements must be ensured by the DBMS (not trivial).
•
The DBMS must provide mechanisms/automation for: Logging & Recovery for A + D Synchronization for I (such as locks) Integrity control for C
•
ACID requirements „cost“: With respect to the DBMS implementation effort With regard to performance at runtime
© 2013
SAP AG. All rights reserved.
494
Database Transactions
Which SQL statements can I use to start a transaction?
© 2013
SAP AG. All rights reserved.
495
Database Transactions
SAP HANA does not provide a SQL statement to explicitly start a transaction.
A transaction is implicitly started, if the previous transaction was canceled or successfully completed (or there is no previous transaction) AND one of the following SQL statements is executed SELECT INSERT UPDATE DELETE The transaction begins directly before the SQL statement, by which it is implicitly started.
© 2013
SAP AG. All rights reserved.
496
Database Transactions
Which SQL statements can I use to finish a transaction?
© 2013
SAP AG. All rights reserved.
497
Database Transactions
COMMIT
The currently running transaction should be completed successfully. The durability requirement must be ensured.
ROLLBACK
The currently running transaction should be canceled. The atomicity requirement must be ensured.
© 2013
SAP AG. All rights reserved.
498
Database Transactions
DELETE FROM Car; COMMIT; INSERT INTO Car(CarID) INSERT INTO Car(CarID) INSERT INTO Car(CarID) COMMIT; INSERT INTO Car(CarID) INSERT INTO Car(CarID) INSERT INTO Car(CarID) ROLLBACK; INSERT INTO Car(CarID) INSERT INTO Car(CarID) INSERT INTO Car(CarID) COMMIT; SELECT CarID FROM Car;
© 2013
SAP AG. All rights reserved.
VALUES ('F01'); VALUES ('F02'); VALUES ('F03'); VALUES ('F04'); VALUES ('F05'); VALUES ('F06'); VALUES ('F07'); VALUES ('F08'); VALUES ('F09');
CARID ----F01 F02 F03 F07 F08 F09
499
Database Transactions
DELETE FROM Car; COMMIT; INSERT INTO Car(CarID) VALUES INSERT INTO Car(CarID) VALUES INSERT INTO Car(CarID) VALUES CREATE COLUMN TABLE T(S INT); INSERT INTO Car(CarID) VALUES INSERT INTO Car(CarID) VALUES INSERT INTO Car(CarID) VALUES ROLLBACK; INSERT INTO Car(CarID) VALUES INSERT INTO Car(CarID) VALUES INSERT INTO Car(CarID) VALUES COMMIT; SELECT CarID FROM Car;
© 2013
SAP AG. All rights reserved.
Certain SQL statements cause an implicit COMMIT ('F01'); ('F02'); ('F03'); ('F04'); ('F05'); ('F06'); ('F07'); ('F08'); ('F09');
CARID ----F01 F02 F03 F07 F08 F09
500
Multi-user Mode
Multi-user operations are absolutely necessary - but not without problems
© 2013
SAP AG. All rights reserved.
501
Multi-user Mode
Multi-user mode is absolutely necessary! •
If the transactions took place strictly in sequence one after another, this would be a huge waste of resources, since waiting times could not be used. • • •
Waiting for user input Waiting for disk access (for classical DBMS products) Waiting times caused by the application program
Multiple transaction must run time-shared (“interlocked”) •
When this happens in an uncontrolled manner (for example without synchronization), many problems can occur.
•
The problems can be divided into 5 different error classes (see following slides)
© 2013
SAP AG. All rights reserved.
502
Uncontrolled Multi-user Mode 1/5 Lost-update Problem The changes of a transaction are overwritten by another transaction and therefore lost.
Point in time
Transaction 1
t1
Read x
t2 t3
Read x x := x + 5,000; x := x – 100;
t4 t5 t6
© 2013
SAP AG. All rights reserved.
Transaction 2
Write x Write x
503
Uncontrolled Multi-user Mode 2/5 Inconsistent Analyses A transaction sees an apparent inconsistency, since it refers to two different consistent database states.
Point in time
Transaction 1
t1
Read marital status of Mandy (Result: “unmarried”)
t2
Change marital status of Mandy to “married”
t3
Change marital status of Thomas to “married”
t4
© 2013
Transaction 2
SAP AG. All rights reserved.
Read marital status of Thomas (Result: “married”)
504
Uncontrolled Multi-user Mode 3/5 Phantom Problem (“malicious”, as it cannot be prevented by row-locking) A transaction sees an apparent inconsistency, since another transaction inserted a new row in the meantime. Point in time t1
Transaction 2
Read all SAP employees
t2
Insert Mr Z as a new SAP employee
t3
Register Mr Z as course participant
t4
© 2013
Transaction 1
SAP AG. All rights reserved.
Read all course participants (Mr Z is a participant of an internal course, though he apparently seems to be no SAP employee!)
505
Uncontrolled Multi-user Mode 4/5 Non-repeatable read During the same transaction different results are obtained for repeated reading of the same facts. Point in time
Transaction 1
t1
Read salary of Mr A
t2 t3
© 2013
SAP AG. All rights reserved.
Transaction 2
Raise salary of Mr A Read salary of Mr A
506
Uncontrolled Multi-user Mode 5/5 Dependency on uncommitted changes (Dirty reads) A transaction sees and uses a state that officially never existed.
Point in time
Transaction 1
t1
Raise salary to € 10,000,000.-
t2
Read salary
t3
Donate € 5,000,000.-
t4
COMMIT;
t5
© 2013
Transaction 2
SAP AG. All rights reserved.
ROLLBACK; (transaction canceled)
507
Synchronisation
To avoid the problems shown before, multi-user mode must be controlled or synrchonized – Synchronization = time based coordination of processes
A common option for synchronization is the use of locking – Locks are the most important (but not the only) way – “Multi-version Concurrency Control" can also be used – Alternatively, there is the “Optimistic Synchronization Method“
The goal of synchronization is the logical single-user mode with physical multi-user mode.
© 2013
SAP AG. All rights reserved.
508
Logical Single-user Mode
What does logical single-user mode with physical multi-user mode mean?
© 2013
SAP AG. All rights reserved.
509
Logical Single-user Mode
The user should have the impression of being the only active user of the database. This goal cannot be reached to 100%. If access to a (currently locked) object takes longer, the users will suspect that they are not alone. If the transaction is aborted by the DBMS to resolve a deadlock, the users will suspect that they are not alone.
Externally there should be the impression that the transactions are strictly executed in series (that means one after another - Serializability criterion) In reality: time-shared processing of several transactions – But the impression of serial processing should be created
© 2013
SAP AG. All rights reserved.
510
Serializability
What is the Serializability Criterion?
© 2013
SAP AG. All rights reserved.
511
Serializability
Serializability = There is a serial execution order of the transactions that has the same result – Result in terms of database state or database content
Serializability is therefore a correctness criterion for the "correct" execution of the synchronization (that means for the "right" control of the multi-user mode) Ensuring Serializability is the responsibility of the DBMS (and not of the user or of the application program) What Serializability means, you can best understand with reference to a concrete example … (see following slides).
© 2013
SAP AG. All rights reserved.
512
Transaction 1
UPDATE Official SET Overtime = Overtime + 2 WHERE PNr = 'P07'; UPDATE Official SET Overtime = Overtime + 2 WHERE PNr = 'P07'; COMMIT;
© 2013
SAP AG. All rights reserved.
513
Transaction 2
--Rounding to full ten UPDATE Official SET Overtime = ROUND(Overtime, -1) WHERE PNr = 'P07';
SELECT * FROM Contact; COMMIT;
© 2013
SAP AG. All rights reserved.
514
Transaction 3
UPDATE Official SET Overtime = Overtime * 4 WHERE PNr = 'P07'; UPDATE Official SET Overtime = Overtime / 2 WHERE PNr = 'P07'; COMMIT;
© 2013
SAP AG. All rights reserved.
515
Effect of the 3 Transactions
TA1 increases the overtime value by 4
1a) o = o + 2; 1b) o = o + 2; TA2 rounds the overtime value to full ten
2a) o = round(o); 2b) read access; TA3 doubles the overtime value
3a) o = o * 4; 3b) o = o / 2;
© 2013
SAP AG. All rights reserved.
516
Serial Execution
TA1 increases the overtime value by 4 TA2 rounds the overtime value to full ten TA3 doubles the overtime value
TA1 → TA2 → TA3
22 → 26 → 30 → 60
TA1 → TA3 → TA2
22 → 26 → 52 → 50
TA2 → TA1 → TA3
22 → 20 → 24 → 48
TA2 → TA3 → TA1
22 → 20 → 40 → 44
TA3 → TA1 → TA2
22 → 44 → 48 → 50
TA3 → TA2 → TA1
22 → 44 → 40 → 44
Initial value = 22
Correct result (according to Serializability): 44, 48, 50 und 60
© 2013
SAP AG. All rights reserved.
517
Incorrect Interleaving
Initial Value
22
1.
(1a)
o = o + 2;
24
2.
(2a)
o = round(o);
20
3.
(2b)
read access;
20
4.
(3a)
o = o * 4;
80
5.
(1b)
o = o + 2;
82
6.
(3b)
o = o / 2;
41
The interleaving shown above is incorrect, because the result (41) does not correspond to any value that can occur in serial execution (44, 48, 50 and 60)
© 2013
SAP AG. All rights reserved.
518
Correct Interleaving
Initial Value
22
1.
(3a)
o = o * 4;
88
2.
(2a)
o = round(o);
90
3.
(1a)
o = o + 2;
92
4.
(3b)
o = o / 2;
46
5.
(2b)
read access;
46
6.
(1b)
o = o + 2;
48
© 2013
SAP AG. All rights reserved.
The interleaving shown here is correct, because the result (48) corresponds to one of the values that can occur by serial execution (44, 48, 50 and 60).
519
Multi-user Mode
What are Isolation Levels?
© 2013
SAP AG. All rights reserved.
520
Isolation Levels
Isolation Levels define … •
Whether rows that are read, inserted, updated, or deleted by a transaction are available to other transactions running in parallel.
•
Whether read, insert, update, or delete activities of another transaction running in parallel have an impact on the current transaction.
•
Which problems of uncontrolled multi-user mode may occur and which problems must be prevented (by the DBMS)
© 2013
SAP AG. All rights reserved.
521
Isolation Levels
4 isolation levels are defined by the SQL Standard in such a way that each level defines which problems may occur and which problems must be prevented by the DBMS.
Isolation Level
Uncommitted Dependency (Dirty Reads)
Non-repeatable Reads
Phantom Problem
0
READ UNCOMMITTED
possible
possible
possible
1
READ COMMITTED
impossible
possible
possible
2
REPEATABLE READ
impossible
impossible
possible
3
SERIALIZABLE
impossible
impossible
impossible
© 2013
SAP AG. All rights reserved.
522
Multi-user Mode
Which SQL statement can I use to define the isolation level?
© 2013
SAP AG. All rights reserved.
523
Multi-user Mode
SET TRANSACTION ISOLATION LEVEL
A certain isolation level is set. You must set the isolation level before the transaction starts. The isolation level is only valid for a single (i.e. next to run) transaction. If different isolation levels are set before the transaction starts, only the last setting is decisive. The isolation level “SERIALIZABLE” guarantees serializability.
© 2013
SAP AG. All rights reserved.
524
Multi-user Mode
SET TRANSACTION ISOLATION LEVEL
© 2013
SAP AG. All rights reserved.
525
Isolation Levels
SET TRANSACTION ISOLATION LEVEL IsolationLevel;
© 2013
SAP AG. All rights reserved.
526
Isolation Levels
According to the SQL Standard isolation level SERIALIZABLE is the default. In SAP HANA the isolation level READ COMITTED is the default. It is not possible to set isolation level READ UNCOMMITED in SAP HANA. You can set the remaining three isolation levels as follows: SET TRANSACTION ISOLATION LEVEL READ COMMITTED; SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
© 2013
SAP AG. All rights reserved.
527
Synchronization
How do classical DBMS products synchronize the multi-user mode?
© 2013
SAP AG. All rights reserved.
528
Classical Synchronization
Classical DBMS products usually use locking for multi-user mode synchronization. Database objects (schemata, tables, rows) are locked prior to any access (and therefore cannot or only in a limited way be used by other transactions). Locks are set and released by the DBMS. User or applications do not need to care of the locks (automation of the DBMS).
© 2013
SAP AG. All rights reserved.
529
Synchronization
How does SAP HANA synchronize its multi-user mode?
© 2013
SAP AG. All rights reserved.
530
Synchronization in SAP HANA
The consistency of write access is ensured by X-locks The affected rows are locked “as usual”. The consistency of read access is ensured by “Multi Version Concurrency Control“ (MVCC) No S-locks are set when reading rows . By default, all SELECT statements of the same transaction see the same (possibly obsolete) consistent version of the database state (≈ isolation level REPEATABLE READ). This version contains all changes that were committed before the start of the transaction (together with all changes done by the transaction itself). This is called “transaction-level snapshot isolation”. Write access for other transactions running in parallel creates a new version of the database state (which is not visible to the transaction mentioned above.) Isolation level READ COMMITED corresponds to statement-level snapshot isolation
© 2013
SAP AG. All rights reserved.
531
Multi-Version Concurreny Control – An Example
Timeline of 5 transactions T1-T5 accessing same record „item D“
T2 T4
T5 starts after T3 is committed sees V2 When T4 ends (last transaction that still sees V1), V1 becomes obsolete
© 2013
SAP AG. All rights reserved.
V2 open V1 open
committed
read
read
insert
update
T5
item D versions
T3
T1 writes V1 of item D T3 starts and writes V2. V1 still remains visible for transactions that started before T3 ends. With „transaction-level“ snapshot isolation, T2 and T4 keep seeing V1 of item D (≈ repeatable read)
commit
Tx T1
read
commit
committed out-dated
532
Write Locks & Snapshot Isolation – Additional Remarks
Snapshot Isolation can lead to updates a transaction that started earlier doesn’t see Transaction-level snapshot isolation increases probability of conflicts on application level
© 2013
SAP AG. All rights reserved.
Transaction T1
write
write
read
re-read
Transaction T2
data record versions
Primary key & uniqueness constraints are checked considering all existing versions Deadlock situations can not be prevented
commit
V3 V2 V1
Committed Committed
533
Unit 11: Database Transactions Summary
You should now be able to: •
Explain what a database transaction is and why it is needed • Explain the acronym ACID • Finish database transactions in HANA using SQL statements • Describe issues that arise if transactions are not mutually isolated •
Understand how classical database systems synchronize concurrent transactions • Understand how HANA synchronizes concurrent transactions
© 2013
SAP AG. All rights reserved.
534
Summary
535
Summary
Motivation And Basic Concepts Reading Data From A Table Or View Aggregating Data
Reading Data From Multiple Tables Changing Data Stored in Tables Defining How Data Is Stored Using Views For Data Access Defining Data Access
Understanding NULL Values Database Transactions © 2013
SAP AG. All rights reserved.
536
Open Questions
© 2013
SAP AG. All rights reserved.
537
Done!
© 2013
SAP AG. All rights reserved.
538
Meta Data
How can I access meta data?
© 2013
SAP AG. All rights reserved.
539
Accessing Meta Data
The DBMS provides metadata views (catalog views). This is a requirement of the SQL standard. You can use SQL to read the meta data.
Which tables belong to schema Schema1?
SELECT Table_Name FROM Sys.Tables WHERE Schema_Name = 'SCHEMA1';
© 2013
SAP AG. All rights reserved.
TABLE_NAME ---------CONTACT OFFICIAL OWNER STOLEN OWNER_EU CAR CAR_EU
540
Accessing Meta Data
Which columns are in table Car of schema Schema1?
SELECT FROM WHERE ORDER BY
Column_Name, Position, Data_Type_Name Sys.Table_Columns Schema_Name = 'SCHEMA1' AND Table_Name ='CAR' Position;
COLUMN_NAME ----------CARID PLATENUMBER BRAND COLOR HP OWNER
© 2013
SAP AG. All rights reserved.
POSITION -------1 2 3 4 5 6
DATA_TYPE_NAME -------------VARCHAR VARCHAR VARCHAR VARCHAR INTEGER VARCHAR
541
Accessing Meta Data
How many columns do the tables of schema Schema1 have?
SELECT FROM WHERE GROUP BY ORDER BY
Table_Name, COUNT(*) AS NumColumns Sys.Table_Columns Schema_Name = 'SCHEMA1' Table_Name 2 TABLE_NAME ---------CONTACT STOLEN OWNER OWNER OWNER_EU CAR CAR_EU
© 2013
SAP AG. All rights reserved.
NUMCOLUMNS ---------2 2 4 5 5 6 7 542
Accessing Meta Data
Which tables of schema Schema1 have the fewest columns?
SELECT FROM WHERE GROUP BY HAVING
TABLE_NAME ---------CONTACT STOLEN © 2013
Table_Name, COUNT(*) AS NumColumns Sys.Table_Columns Schema_Name = 'SCHEMA1' Table_Name COUNT(*) = (SELECT TOP 1 COUNT(*) FROM Sys.Table_Columns WHERE Schema_Name = 'SCHEMA1' GROUP BY Table_Name ORDER BY 1); NUMCOLUMNS ---------2 2
SAP AG. All rights reserved.
543
Accessing Meta Data
Which tables of schema Schema1 have the most columns?
SELECT FROM WHERE GROUP BY HAVING
TABLE_NAME ---------CAR_EU
© 2013
Table_Name, COUNT(*) AS NumColumns Sys.Table_Columns Schema_Name = 'SCHEMA1' Table_Name COUNT(*) = (SELECT TOP 1 COUNT(*) FROM Sys.Table_Columns WHERE Schema_Name = 'SCHEMA1' GROUP BY Table_Name ORDER BY 1 DESC); NUMCOLUMNS ---------7
SAP AG. All rights reserved.
544
Meta Data
Which meta data views are available?
© 2013
SAP AG. All rights reserved.
545
Accessing Meta Data
There are several meta data views …
Meta Data View
Information contained
Sys.Tables
Which tables are available?
Sys.Table_Columns
Which columns are in the tables?
Sys.Views
Which views are available?
Sys.View_Columns
Which columns are in the views?
Sys.Indexes
Which indexes are available?
Sys.Index_Columns
Which columns are in the indexes?
© 2013
SAP AG. All rights reserved.
546
© 2013 SAP AG. 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 AG. The information contained herein may be changed without prior notice. Some software products marketed by SAP AG and its distributors contain proprietary software components of other software vendors. National product specifications may vary. These materials are provided by SAP AG and its affiliated companies ("SAP Group") for informational purposes only, without representation or warranty of any kind, and SAP Group shall not be liable for errors or omissions with respect to the materials. The only warranties for SAP Group 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.
SAP and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP AG in Germany and other countries. Please see http://www.sap.com/corporate-en/legal/copyright/index.epx#trademark for additional trademark information and notices.
© 2013
SAP AG. All rights reserved.
547