0% found this document useful (0 votes)
3 views19 pages

1 RelationalDataManagement

The document outlines the structure and content of a course on Relational Data Management and Scalable Data Engineering, including organizational details for lectures and exercises starting from October 21st. It covers foundational concepts of the relational model, primary and foreign keys, SQL components, data types, and database object definitions. Additionally, it provides guidance on installing PostgreSQL and related tools for practical exercises.

Uploaded by

Xuanyan Wang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views19 pages

1 RelationalDataManagement

The document outlines the structure and content of a course on Relational Data Management and Scalable Data Engineering, including organizational details for lectures and exercises starting from October 21st. It covers foundational concepts of the relational model, primary and foreign keys, SQL components, data types, and database object definitions. Additionally, it provides guidance on installing PostgreSQL and related tools for practical exercises.

Uploaded by

Xuanyan Wang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Relational Data Management

Scalable Data Engineering

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

The Relational Model


▪ Developed by Codd (IBM) in 1970
▪ Considered ingenious but impractical in 1970
▪ Conceptually simple
▪ Computers lacked power to implement the
relational model
▪ Today, microcomputers can run sophisticated
relational database software

[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 (ZX), 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

- Definedness:  ti  R1: ( ti[Y] = ‘NULL’   tj  R2: ti[Y] = tj[X] )

- Minimality:  ZY (ZY), so that the first two conditions are satisfied

R1 PNr Salary Department R2 Name Building


1 1700 IT IT A
2 2172 Sales Sales B
4 1700 NULL Development A

6
SQL – Foundations

7
SQL

SQL (Structured Query Language)


▪ Descriptive Query Language

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

Standard Data Types


▪ e.g. INTEGER, FLOAT, DOUBLE PRECISION,
DECIMAL(precision, scale), CHAR(n), VARCHAR(n),
BIT(n), DATE, TIME, …
▪ Atomic/no structure
▪ Can be evaluated by the database system

Large Objects (LOB, large object)


▪ e.g. CLOB for text data, BLOB for binary data
▪ Semantics hidden from database system, e.g.,
movies, images, …
▪ Cannot be analyzed by database system, e.g. no
comparison operators (<, =, >)
▪ Often stored separately

Structured Data Types


▪ XML types, JSON, Object-relation 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

Definition of Database Objects (DDL)


▪ e.g. table
▪ Creating using CREATE command

CREATE TABLE REGION (


R_REGIONKEY INTEGER NOT NULL PRIMARY KEY,
R_NAME CHAR(25) NOT NULL,
R_COMMENT VARCHAR(152)
)

▪ Deleting using DROP

DROP TABLE REGION

▪ Changing using ALTER

ALTER TABLE REGION ADD COLUMN AREA INT

14
SQL-DDL (2)

Integrity Constraints (constraints)


▪ Primary Key, Foreign Key, Distinct values (UNIQUE), value-based (CHECK)
▪ ALTER TABLE <table> ADD CONSTRAINT <constraint-name>
- PRIMARY KEY (<attribute-list>)
- FOREIGN KEY (<attr-list>) REFERENCES <table>(<attr-list>)
- UNIQUE (<attribute-list>)
- CHECK (<predicate>)
▪ Example
ALTER TABLE REGION
ADD CONSTRAINT MAX5
CHECK (R_REGIONKEY
BETWEEN 1 AND 5)

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

Lending ID BID CID Lend_date Due_date Return_date


1 1 1 03.12.2023 24.12.2024 18.12.2024
Fee ID LID CID Amount Pay_date
2 4 2 09.03.2023 30.03.2023 30.03.2023
1 4 3 6,00 06.09.2021
3 5 1 16.06.2020 07.07.2020 06.07.2020
4 1 3 13.08.2021 03.09.2021 06.09.2021

17
Summary

Relational Model
▪ Foundations
▪ Primary Key/Foreign Key

Data Types
▪ Build-in data types
▪ Type casting

Data Definition and Data Manipulation


▪ INSERT, UPDATE, DELETE, MERGE
▪ Tables
▪ Integrity Constraints

18
Exercise Preparation

Install
▪ PostgreSQL https://www.postgresql.org/download/
▪ pgAdmin https://www.pgadmin.org/ OR DBeaver https://dbeaver.io/

Read the documentation of


▪ PostgreSQL COPY command https://www.postgresql.org/docs/current/sql-copy.html
▪ PostgreSQL UUID type https://www.postgresql.org/docs/current/datatype-uuid.html

19

You might also like