DM534: Introduction to Relational Databases (Part 2) 2019 Slides - - PowerPoint PPT Presentation

dm534 introduction to relational databases part 2
SMART_READER_LITE
LIVE PREVIEW

DM534: Introduction to Relational Databases (Part 2) 2019 Slides - - PowerPoint PPT Presentation

DM534: Introduction to Relational Databases (Part 2) 2019 Slides by Christian Wiwie (edits by Rolf Fagerberg) Relatjonal Query Languages Based on relatjonal algebra For relatjonal databases, i.e. relatjonal data model Relatjonal


slide-1
SLIDE 1

DM534: Introduction to Relational Databases (Part 2)

2019 Slides by Christian Wiwie (edits by Rolf Fagerberg)

slide-2
SLIDE 2

Nov 19, 2019 2

Relatjonal Query Languages

  • Based on relatjonal algebra
  • For relatjonal databases, i.e. relatjonal data model
  • Relatjonal model supports simple, powerful QLs:

– Strong formal foundatjon based on logic – Allows for much optjmizatjon

  • SQL (Structured Query Language)

– Most widely used relatjonal query language

→ Understanding Relatjonal Algebra is key to understanding SQL, query processing!

slide-3
SLIDE 3

Nov 19, 2019 3

What is an “Algebra”?

  • Mathematjcal system consistjng of

– Operands: Values from which new values can be

constructed by applying operatjons

– Operatjons: Procedures that construct new values from

given values

– Operators: Symbols denotjng operatjons

  • Variables are letuers that can represent values
slide-4
SLIDE 4

Nov 19, 2019 4

Examples for Algebras

  • Integer algebra

– Operands: The set of integers [..., -1, 0, 1, …] – Operatjons: Additjon, subtractjon, multjplicatjon,

division, …

– Operators: +, -, *, ∕, …

  • Example for algebraic expressions:

(3 + 5) * 2 5 – x / 3

slide-5
SLIDE 5

Nov 19, 2019 5

Algebraic expressions

  • Can be visualized as expression trees

(3 + 5) * 2

3 + 5 * 2

slide-6
SLIDE 6

Nov 19, 2019 6

Algebraic expressions

  • Can be visualized as expression trees

(3 + 5) * 2 vs. 3 + 5 * 2

3 + 5 * 2 3 + 5 2 *

slide-7
SLIDE 7

Nov 19, 2019 7

Algebraic expressions

  • Can be visualized as expression trees

5 – x / 3

5

  • /

x 3

slide-8
SLIDE 8

Nov 19, 2019 8

What is Relatjonal Algebra?

  • An algebra where

– operands are relatjons – operatjons compute new relatjons from relatjons

  • Can be used as a query language for relatjons

– “Language” of relatjonal databases

slide-9
SLIDE 9

Nov 19, 2019 9

What is Relatjonal Algebra?

=> Expressions of relatjonal algebra can also be visualized as trees

  • OP1 and OP2 are relatjonal operatjons
  • R1, R2, R3 are variables for relatjons

R1 OP1 OP2 R2 R3

slide-10
SLIDE 10

Nov 19, 2019 10

Relatjonal Algebra: 5 Basic Operatjons

  • Selectjon:

Selects a subset of tuples from relatjon R, for which conditjon C holds (horizontal)

  • Projectjon:

Retains atuributes from relatjon R (vertjcal)

  • Cross-product: R1 x R2

Pairwise combinatjon of tuples of relatjons R1 and R2

  • Set-difgerence: R1 – R2

Tuples in relatjon R1, but not in relatjon R2

  • Union: R1

R2 ∪ R3

Tuples in relatjon R1 and/or in relatjon R2

  • Since each operatjon returns a relatjon, operatjons can be composed

(Algebra is “closed”)

σ C(R) π A1,..., Ak(R) A1,..., Ak

slide-11
SLIDE 11

Nov 19, 2019 11

What is Relatjonal Algebra?

=> An expression tree could like this

  • What does this express?

σBrand=Ford ∪ Car σColor=blue Car

slide-12
SLIDE 12

Nov 19, 2019 12

Relatjonal Algebra: Example Instances

slide-13
SLIDE 13

Nov 19, 2019 13

Selectjon (σ*σ*)

slide-14
SLIDE 14

Nov 19, 2019 14

Projectjon (σ*π*)

slide-15
SLIDE 15

Nov 19, 2019 15

Projectjon (σ*π*)

slide-16
SLIDE 16

Nov 19, 2019 16

Cross Product

slide-17
SLIDE 17

Nov 19, 2019 17

Cross Product

slide-18
SLIDE 18

Nov 19, 2019 18

Union and Set Difgerence

slide-19
SLIDE 19

Nov 19, 2019 19

Union

slide-20
SLIDE 20

Nov 19, 2019 20

Set Difgerence

slide-21
SLIDE 21

Nov 19, 2019 21

Nestjng Operators

  • Result of a relatjonal algebra operator is a relatjon
  • It can be used as input to another relatjonal algebra
  • perator
slide-22
SLIDE 22

Nov 19, 2019 22

Nestjng Operators

  • As expression tree:

σratjng>8 πsname,ratjng S2

slide-23
SLIDE 23

Nov 19, 2019 23

Compound Operator: Intersectjon

  • In additjon to the 5 basic operators, there are

several additjonal “Compound Operators”

– Do not add computatjonal power to the language – Useful shorthands – Can be expressed with basic operatjons

  • Example: Intersectjon

– Takes two input relatjons that are union-compatjble

slide-24
SLIDE 24

Nov 19, 2019 24

Compound Operator: Intersectjon

slide-25
SLIDE 25

Nov 19, 2019 25

SQL - A language for Relational DBs

slide-26
SLIDE 26

Nov 19, 2019 26

SQL - A language for Relatjonal DBs

  • Say: “ess-cue-ell” or “sequel”

– But spelled “SQL”

  • Data Defjnitjon Language (DDL)

– create, modify, delete relatjons – specify constraints – administer users, security, etc.

  • Data Manipulatjon Language (DML)

– Specify queries to fjnd tuples that satjsfy criteria – add, modify, remove tuples

  • The DBMS is responsible for effjcient evaluatjon
slide-27
SLIDE 27

Nov 19, 2019 27

SQL - A language for Relatjonal DBs

  • Query language to retrieve data from database
  • Includes a data-defjnitjon component to defjne

database schemas

  • SQL commands have to be terminated with ‘;’
  • SQL is standardized

– some DBMS include their own SQL commands

slide-28
SLIDE 28

Nov 19, 2019 28

Creatjng Databases in SQL

  • Create a new, empty database ‘University’:

CREATE DATABASE University;

– Does not contain any relatjons

upon creatjon

DB “University”

slide-29
SLIDE 29

Nov 19, 2019 29

Creatjng Relatjons in SQL

  • Create a new, empty relatjon ‘Students’:

CREATE TABLE Students (sid CHAR(20) PRIMARY KEY, name CHAR(20), login CHAR(10), age INTEGER, gpa FLOAT);

– Does not contain any tuples

upon creatjon

– Note: the type (domain) of each

fjeld is specifjed, and enforced by the DBMS whenever tuples are added or modifjed.

DB “University” Students sid name login age gpa

slide-30
SLIDE 30

Nov 19, 2019 30

Creatjng Relatjons in SQL

  • Similarly:

CREATE TABLE Courses ( cid CHAR(20) PRIMARY KEY, cname CHAR(20), credits INTEGER);

DB “University” Students Courses

slide-31
SLIDE 31

Nov 19, 2019 31

Adding and Deletjng Tuples

  • Insert a single tuple:

INSERT INTO Students (sid, name, login, age, gpa) VALUES ('53688', 'Smith', 'smith@ee', 18, 3.2);

  • Delete all tuples satjsfying some conditjon (e.g.,

name = Smith):

DELETE FROM Students S WHERE S.name = 'Smith';

sid name login age gpa 53688 Smith smith@ee 18 3.2 sid name login age gpa

slide-32
SLIDE 32

Nov 19, 2019 32

Selectjng Tuples in SQL

  • Find tuples for all 18 year old students with gpa’s

above 2.0:

SELECT * FROM Students S WHERE S.age=18 AND S.gpa > 2.0;

  • To get just names and logins:

SELECT S.name, S.login FROM Students S WHERE S.age=18 AND S.gpa > 2.0;

sid name login age gpa 53688 Smith smith@ee 18 3.2 name login Smith smith@ee

slide-33
SLIDE 33

Nov 19, 2019 33

Relatjonal Algebra Operators in SQL

  • Relatjonal algebra operators can be expressed with SQL
  • Selectjon operator (σ):

SELECT * FROM Students S WHERE S.age=18 AND S.gpa > 2.0;

  • Projectjon operator (π):

SELECT S.age,S.gpa FROM Students S;

  • Union:

SELECT * FROM Students S WHERE S.age=18 AND S.gpa > 2.0 UNION SELECT * FROM Students S WHERE S.age=20 AND S.gpa > 2.3;

slide-34
SLIDE 34

Nov 19, 2019 34

Relatjonal Algebra Operators in SQL

  • Set Difgerence:

SELECT * FROM Students S WHERE S.gpa > 2.0 EXCEPT SELECT * FROM Students S WHERE S.age=19;

  • Cross Product:

SELECT * FROM Students S, Enrolled E;

slide-35
SLIDE 35

Nov 19, 2019 35

Primary Keys in SQL

  • Single aturibute primary key:

CREATE TABLE Students (sid CHAR(20) PRIMARY KEY, name CHAR(20), login CHAR(10), age INTEGER, gpa FLOAT)

  • Multj-aturibute primary key:

CREATE TABLE Enrolled (sid CHAR(20) cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid,cid))

slide-36
SLIDE 36

Nov 19, 2019 36

Foreign Keys in SQL

  • Only students listed in the Students relatjon should be

allowed to enroll for courses

→ sid is a foreign key referring to Students

  • Students can only enroll for registered courses

→ cid is a foreign key referring to Courses CREATE TABLE Enrolled (sid CHAR(20),cid CHAR(20),grade CHAR(2), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Students, FOREIGN KEY (cid) REFERENCES Courses);

slide-37
SLIDE 37

Nov 19, 2019 37

Thank you for your attention!