Академический Документы
Профессиональный Документы
Культура Документы
Relational
Database Systems 1
Wolf-Tilo Balke
Joachim Selke
SELECT
INSERT
UPDATE
DELETE
CREATE TABLE
ALTER TABLE
DROP TABLE
Institut fr Informationssysteme
Technische Universitt Braunschweig
www.ifis.cs.tu-bs.de
Exercise 6.1
Exercise 6.1
Give relational algebra expressions for the following
queries in natural language:
a) Create a list of all hotels that includes the total number
of rooms for each hotel.
hotelName, count roomNo(
hotelNo, hotelNamecount(roomNo) (Hotel Room))
A hotel database:
Exercise 6.1
Exercise 6.2
b) { H.hotelName | Hotel(H) R (
Room(R) H.hotelNo = R.hotelNo R.price > 50)}
List the names of all hotels offering a room that
costs more than 50!
Result = count(guestNo)RichGuests
Relational Database Systems 1 Wolf-Tilo Balke Institut fr Informationssysteme TU Braunschweig
Of course, this does not work for hotels without any rooms.
However, lets assume that there is no such hotel ...
Exercise 6.2
Exercise 6.2
d) { H.hotelName, G.guestName, B1.dateFrom, B2.dateFrom |
c) { H.hotelName | Hotel(H) B G (
Exercise 6.3
RA: hotelName(Hotel)
TRC: { h.hotelName | Hotel(h) }
Exercise 6.3
10
Exercise 6.3
c) List the price and type of all rooms at the Balke Inn.
RA: roomNo, type, price(Room hotelName = Balke Inn(Hotel))
TRC: { r.roomNo, r.roomType, r.price | Room(r)
h (Hotel(h) h.hotelName = Balke Inn
h.hotelNo = r.hotelNo) }
DRC: { r1, r3, r4 | h1, h3 (Room(r1, h1, r3, r4)
Hotel(h1, Balke Inn, h3)) }
Exercise 6.3
11
12
Exercise 6.3
Exercise 6.3
DRC: { g1, g2, g3 | Guest(g1, g2, g3) h1, h3, b3, b4, b5 (
Hotel(h1, Balke Inn, h3)
Booking(h1, g1, b3, b4, b5) b3 TODAY
b4 TODAY) }
e) List the details of all rooms at the Balke Inn,
including the name of the guest currently staying
in the room, if the room is occupied.
RA: roomNo, type, price, guestName(Room
(hotelName = Balke Inn(Hotel)
dateFrom TODAY dateTo TODAY(Booking)
Guest))
Relational Database Systems 1 Wolf-Tilo Balke Institut fr Informationssysteme TU Braunschweig
TRC:
BalkeRoom(r) Room(r) h (Hotel(h)
h.hotelName = Balke Inn h.hotelNo = r.hotelNo)
CurrentBooking(g, r) Guest(g) b (Booking(b)
b.hotelNo = r.hotelNo b.roomNo = r.roomNo
b.dateFrom TODAY b.dateTo TODAY
b.guestNo = g.guestNo)
13
Exercise 6.3
DRC:
BalkeRoom(r1, r2, r3, r4) Room(r1, r2, r3, r4)
h3 Hotel(r2, Balke Inn, h3)
CurrentBooking(g2, r1, r2) g1, g3 (Guest(g1, g2, g3)
b3, b4 (Booking(r2, g1, b3, b4, r1)
b3 TODAY b4 TODAY))
16
Theory is good
But developers were willing to sacrifice theoretical beauty
and clarity for the sake of usability and performance
Vocabulary change
Questions
14
17
18
Design decisions:
During the development of System R, two major
and very controversial decisions had been made
Duplicates
In a relation, there cannot be any duplicate tuples
Also, query results cannot contain duplicates
19
22
Possible solution:
For each domain, define a value indicating that
data is not available, not known, not applicable,
20
Practical considerations
23
24
26
Relational algebra
Requires users to define how and in which order data
should be retrieved
The specific choice of a sequence of operations has an
enormous influence on the systems performance
27
28
29
30
TRC:
{ t1.name, t2.result | students(t1) exams(t2)
t1.matNr = t2.matNr t2.result < 2.0 }
SQUARE:
matNr examsresult (<2.0)
name result students matNr
TRC:
{ t.result | exams(t) t.crsNr = 101 t.result < 2.0}
31
32
Guiding principle:
matNr exams
33
34
Fundamental keywords
SQUARE:
2.0)
SEQUEL:
35
36
SQUARE:
name result
SEQUEL:
SELECT name, result
FROM students, exams
WHERE students.matNr = exams.matNr
AND result < 2.0
Relational Database Systems 1 Wolf-Tilo Balke Institut fr Informationssysteme TU Braunschweig
37
38
Asking queries
Often part of the DML
40
Catalog
humanResources
Schema
people
Schema taxes
...
Catalog
production
Schema products
...
Table staff
Table
has Office
Schema
training
...
41
Schema testing
...
42
Simple SELECT
Joins
Set operations
Aggregation and grouping
Subqueries
Writing good SQL code
45
46
44
SQL queries
47
Example:
SELECT DISTINCT name FROM staff
Returns all different names of staff members,
without duplicates
48
Qualified: tablename.attributename
SELECT * FROM
Return all attributes of the tables in the FROM clause
SELECT movies.* FROM movies, persons WHERE ...
Return all attributes of the movies table
49
Example:
Example:
52
7.4 Conditions
,
ALL
expression
AS
DISTINCT
column name
*
table name
attribute names
,
table names
table name
AS
alias name
( query )
WHERE
50
FROM
query block
SELECT
search condition
GROUP BY
column name
,
HAVING
search condition
Relational Database Systems 1 Wolf-Tilo Balke Institut fr Informationssysteme TU Braunschweig
53
54
7.4 Conditions
7.4 Conditions
Why TRUE, FALSE, and NULL?
AND
OR
search condition
predicate
NOT
(
search condition
AND
TRUE
FALSE
NULL
OR
TRUE
FALSE
NULL
NOT
NULL
TRUE
TRUE
FALSE
NULL
TRUE
TRUE
TRUE
TRUE
TRUE
FALSE
FALSE
TRUE
FALSE
NULL
FALSE
TRUE
NULL
TRUE
NULL
NULL
NULL
NULL
Relational Database Systems 1 Wolf-Tilo Balke Institut fr Informationssysteme TU Braunschweig
NULL
55
expression
expression
NOT
expression
IS
NULL
NOT
expression
NOT
,
IN
NOT
EXISTS
LIKE expression
expression
56
7.4 Conditions
comparison
expression
NULL
7.4 Conditions
predicate
FALSE
expression
ESCAPE
expression
Functions
CASE expressions
CAST expressions
Scalar subqueries
( query )
( query )
expression
comparison
SOME
ANY
ALL
( query )
57
7.4 Conditions
58
7.4 Conditions
Simple comparisons:
Arithmetic operators:
+, -, *, and / with the usual semantics
age + 2
price * quantity
powerStrength > 10
name = Magneto
Magneto <= Professor X
Parenthesis:
Used to modify the evaluation order
expression
(price + 10) * 20
Relational Database Systems 1 Wolf-Tilo Balke Institut fr Informationssysteme TU Braunschweig
59
comparison
expression
60
7.4 Conditions
7.4 Conditions
BETWEEN predicate:
IS NULL predicate:
Examples:
year BETWEEN 2000 AND 2008
score BETWEEN targetScore - 10 AND targetScore + 10
expression
expression
IS
NULL
NOT
NOT
61
7.4 Conditions
match
expression
62
7.4 Conditions
pattern
LIKE expression
NOT
ESCAPE
LIKE predicate:
Examples:
escape
expression
1_inPrison TRUE
1inPrison FALSE
%_% TRUE
%%% FALSE
match
expression
pattern
LIKE expression
NOT
63
7.4 Conditions
ESCAPE
escape
expression
64
7.4 Conditions
IN predicate:
EXISTS predicate:
Examples
EXISTS (SELECT * FROM heroes)
IN
NOT
expression
EXISTS
( query )
( query )
65
66
7.4 Conditions
7.5 Joins
Examples:
result <= ALL (SELECT result FROM results)
TRUE if the current result is the smallest one
comparison
SOME
ANY
ALL
( query )
67
68
7.5 Joins
7.5 Joins
Example:
joined table
Inner joins
Left/right/full outer joins
table name
69
table name
ON
condition
70
7.5 Joins
7.5 Joins
Left outer join: List students and their exam results
students
exams
exams
matNr
firstName
lastName
sex
matNr
crsNr
result
matNr
firstName
lastName
sex
matNr
crsNr
result
1005
Clark
Kent
9876
100
3.7
1005
Clark
Kent
9876
100
3.7
2832
Louise
Lane
2832
102
2.0
2832
Louise
Lane
2832
102
2.0
4512
Lex
Luther
1005
101
4.0
4512
Lex
Luther
1005
101
4.0
5119
Charles
Xavier
1005
100
1.3
5119
Charles
Xavier
1005
100
1.3
lastName
crsNr
result
lastName
crsNr
result
Kent
100
1.3
Kent
100
1.3
Kent
101
4.0
Kent
101
4.0
Lane
102
2.0
Lane
102
2.0
Luther
NULL
NULL
Xavier
NULL
NULL
71
72
7.5 Joins
7.5 Joins
Full outer join:
exams
exams
matNr
firstName
lastName
sex
matNr
crsNr
result
matNr
firstName
lastName
sex
matNr
crsNr
result
1005
Clark
Kent
9876
100
3.7
1005
Clark
Kent
9876
100
3.7
2832
Louise
Lane
2832
102
2.0
2832
Louise
Lane
2832
102
2.0
4512
Lex
Luther
1005
101
4.0
4512
Lex
Luther
1005
101
4.0
5119
Charles
Xavier
1005
100
1.3
5119
Charles
Xavier
1005
100
1.3
lastName
crsNr
result
lastName
crsNr
result
Kent
100
1.3
Kent
100
1.3
Kent
101
4.0
Kent
101
4.0
Lane
102
2.0
Lane
102
2.0
Luther
NULL
NULL
NULL
100
3.7
NULL
100
3.7
Xavier
NULLRelationalNULL
Database Systems 1 Wolf-Tilo Balke Institut fr Informationssysteme TU Braunschweig
73
literal table
UNION
INTERSECT
EXCEPT
query-block
ALL
query
literal table
student
course
result
9876
100
3.7
2832
102
2.0
course
result
1005
101
4.0
100
3.7
1005
100
NULL
100
2.3
6676
102
4.3
100
1.7
3412
NULL
NULL
75
76
Examples:
column
function
expression
function
name
COUNT(*)
Relational Database Systems 1 Wolf-Tilo Balke Institut fr Informationssysteme TU Braunschweig
exams
query
query block
Example:
query
74
77
ALL
expression
DISTINCT
78
7.7 Grouping
Examples:
7.7 Grouping
80
7.7 Grouping
Examples:
Examples:
exams
exams
student
course
Result
9876
100
3.7
2832
102
2.0
1005
101
4.0
1005
100
NULL
6676
102
4.3
3412
NULL
NULL
course
100
3,7
101
4.0
102
3.15
NULL
NULL
course
Result
9876
100
3.7
2832
102
2.0
1005
101
4.0
1005
100
NULL
6676
102
4.3
3412
NULL
NULL
81
7.7 Grouping
course
100
3.7
101
4.0
102
3.15
82
7.7 Grouping
Examples:
HAVING <condition>
The condition may involve group properties
Only those groups are created that fulfill the
HAVING condition
A query may have a WHERE and a HAVING clause
Also, it is possible to have HAVING without GROUP BY
Then, the whole table is treated as a single group
student
83
course
Result
9876
100
3.7
2832
102
2.0
1005
101
4.0
1005
100
NULL
6676
102
4.3
3412
NULL
NULL
course
102
3.15
84
7.8 Ordering
7.8 Ordering
Please note:
Ordering completely breaks with set calculus/algebra
Result after ordering is a list, not a (multi-)set!
SELECT statement
query
,
ODER BY
ASC
column name
integer
DESC
85
7.8 Ordering
87
7.9 Subqueries
88
7.9 Subqueries
86
89
90
7.9 Subqueries
7.9 Subqueries
Expressions:
HasAlias hero_id
Hero
HasPower
Power
aliasName
id
realName
hero_id
power_id
id
name
IN-condition:
powerStrength
description
91
7.9 Subqueries
92
EXISTS-condition:
Easy to read
Easy to write
Easy to understand!
Inline view
SELECT h.realName, a.aliasName
FROM hasAlias a,
(SELECT * FROM heroes WHERE realName LIKE A%) h
WHERE h.id < 100 AND a.hero_id = h.id
Get realNamealias pairs for all heroes with a real name staring
with A and an id smaller than 100
Uncorrelated
Relational Database Systems 1 Wolf-Tilo Balke Institut fr Informationssysteme TU Braunschweig
93
SELECT MOVIE_TITLE
FROM MOVIES
WHERE MOVIE_YEAR = 2009
GOOD
SELECT movie_title
FROM movies
WHERE movie_year = 2009
94
BAD
BAD
SELECT imdbraw.movies.movie_title,
imdbraw.movies.movie_year
FROM imdbraw.movies
WHERE imdbraw.movies.movie_year = 2009
GOOD
95
96
BAD
GOOD
BAD
GOOD
97
GOOD
BAD
BAD
GOOD
98
BAD
101
GOOD
100