1 RelationalDataManagement
1 RelationalDataManagement
1
Organizational Stuff
Lecture videos
▪ We try to upload the videos till Friday, the week before the exercise
Exercises
▪ Will start from Mon, 21st Oct with the actual first exercise on Relational Data Management
▪ Please prepare the exercises, the exercise appointments are meant to ask questions and work at your
solutions
▪ There will be no sample solution provided
▪ There won’t be any grading of the exercises
2
The Relational Model
[Codd, E.F. (1970). "A Relational Model of Data for Large Shared
Data Banks". Communications of the ACM 13 (6): 377–387.]
3
Relational Model – Foundations
Foundations Example
▪ A domain D is a set of atomic values with a ▪ Domain
specific data type - D1 = {a,b,c}, D2 = {0,1}
- Domains: Integer, String[20], Date, … ▪ Cartesian product
▪ Relation R is defined on a relational schema RS - D1 D2 = { (a,0), (a,1), (b,0), (b,1), (c,0), (c,1) }
- Relational Schema RS: Set of attributes ▪ Possible relations
{A1,…,Ak}
- R1= { (a,0), (b,0), (c,0), (c,1) }
- Attribute Aj: Domain Dj = dom(Aj)
- R2 =
- Relation: Subset of the cartesian product of the
domains R D1 D2 ... Dk, k 1 R1 A B
▪ Tuple: Element of a relation
▪ Cardinality of a relation: number of tuples in a a 0
relation
b 0
In general
▪ Database schema = Set of relational schemas c 0
▪ Database = Set of relations c 1
4
Primary Key Concept
Notation
▪ Given a relational schema RS and X RS, then t[X] denotes the tuple restricted to the attributes in X
Primary Key
▪ A primary key is a special column (or combination of columns) designated to uniquely identify each tuple
within a relational schema (RS)
▪ A set of attributes X RS is called a primary key, if following conditions are met:
- Uniqueness: for all relations R of the relational schema RS applies that
ti, tj R: ti[X] = tj[X] i = j
- Definedness: ti R: ti[X] ‘NULL’
R PNr Salary
- Minimality: Z X (ZX), still satisfying the conditions above
1 1700
Please note
▪ Key property also applies to data that will be stored in the relation in the future 2 2172
▪ Key candidates: Several subsets of a relational schema may qualify as key
4 1700
(one has to be chosen)
▪ Attributes of the key are underlined
5
Foreign Key Concept
Foreign Key
▪ A foreign key is a column or group of columns in a relational table that provides a link between data in two
relational schemas.
▪ Y RS1 is foreign key of a relation R1 with reference to a relation R2, if following conditions are met:
- X RS2 is primary key in R2
- Minimality: ZY (ZY), so that the first two conditions are satisfied
6
SQL – Foundations
7
SQL
Consists of
▪ DQL (data query language)
▪ DML (data manipulation language)
▪ DDL (data definition language):
▪ DCL (data control language): access right management
SQL Standard
▪ SQL/86, SQL/89, SQL/92 (=SQL2), SQL/99, SQL:2003 (=SQL3), SQL:2006
▪ Part 1: SQL/Framework (Standard Description)
▪ Part 2: SQL/Foundation
▪ Part 3: SQL/CLI (call level interface)
▪ Part 4: SQL/PSM (persistent storage modules)
8
Data Types
9
Data Types
10
Type Casting
Type casting
▪ Type conversion can be applied explicitly
▪ CAST-Operator
- Syntax: CAST(<value> AS <data-type>)
▪ Also conversion between numerical data types and strings possible
Example
SELECT 1/2, CAST(1/2 AS DOUBLE), CAST(1 AS DOUBLE)/2, 1.0/2
1 2 3 4
---- ---- ---- ----
0 0 0.5 0.5
INTEGER DOUBLE
implicite type conversion
(INTEGER → DOUBLE)
11
Data Definition Language (DDL) and
Data Manipulation Language (DML)
12
Database Objects
Relation (TABLE)
▪ Unordered set of tuples
▪ Permanent or temporary
View (VIEW)
▪ Virtual relation
▪ e.g. simplifiction of queries, user-defined view on
data
Index (INDEX)
▪ Primary or secundary
▪ Data structure to store / find data
▪ Additional: preserving uniqueness and sorting
13
SQL-DDL
14
SQL-DDL (2)
15
SQL-DML
Inserting Tuples
INSERT INTO REGION VALUES (6,'Antarctica','')
INSERT INTO NATION (N_NATIONKEY, N_NAME, N_REGIONKEY)
SELECT NATIONKEY, NAME, 6 FROM ANTARCTIC_NATIONS
Updating Tuples
UPDATE LINEITEM
SET L_DISCOUNT = L_DISCOUNT + 0.01
WHERE L_SUPPKEY = 12
Deleting Tuples
DELETE FROM REGION WHERE R_REGIONKEY > 5
16
Modeling a (simple) Library
Book Customer
ID ISBN Title Author Publischer ID Name Birthday
1 9780261102385 The Lord of the Rings John R. R. Tolkien HarperCollins 1 Alice 01.01.1965
2 9783551557403 Harry Potter 1-7 J. K. Rowling Carlsen 2 Bob 13.03.1997
3 9791035817695 Fahrenheit 451 Ray Bradbury Belin Education 3 Carl 03.02.2005
4 9780571311576 Solaris Stanislaw Lem Faber And Faber 4 Diana 09.09.1911
5 9780575093133 Roadside Picnic B. & A. Strugatsky Gollancz
17
Summary
Relational Model
▪ Foundations
▪ Primary Key/Foreign Key
Data Types
▪ Build-in data types
▪ Type casting
18
Exercise Preparation
Install
▪ PostgreSQL https://www.postgresql.org/download/
▪ pgAdmin https://www.pgadmin.org/ OR DBeaver https://dbeaver.io/
19