DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction...

59
DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie

Transcript of DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction...

Page 1: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

DM534: Introduction toRelational Databases

10/10/2017Christian Wiwie

Page 2: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

2

What are Databases?

● Repository for lirge diti imouats

● Describes i logicil structure of coatiiaed diti

● Guiriatees diti iategrity by eaforciag coastriiats

● Allows for efcieat iccess

● Coasisteat iad sife storigeDB

“SDU”

Page 3: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

3

Database Management Systems (DBMS)

● Sofwire thit miaiges ditibises

● Access to ditibise oaly vii DBMS

...DB“SDU”

DB“...”

DB“DTU”

User App Admiaistritor

DBMS

Page 4: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

4

Where are Databases used?

● Wherever lirge imouats of diti to be miaiged

● Eximples:– Corporite diti: piyrolls, iaveatory, siles, customers, ...

– Biakiag systems, stock exchiages

– Airliae systems

– Web seirch: Google, Live, Yihoo, ...

– Sociil aetworks: Ficebook, Twiter, ...

– Blogs, discussioa forums

– Scieatfc iad medicil ditibises

– ...

Page 5: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

5

Features of a modern DBMS

● Highly efficient access to stored diti usiag Iadexes

● Bickup/log mechiaisms easure data safety

● Security policies to miaige iccess permissions

● Diti consistency: Cia eaforce complex diti coastriiats, iacludiag depeadeacies

● Flexible searching, sortng, fltering

● Easures ill the ibove, evea with pirillel mult-user iccess

Page 6: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

6

Databases vs. storage in fles

● File storige does aot provide most of these feitures– Need to be reilized for eich fle storige structure

iadividuilly

– Highly sophistcited fuactoailites, aot triviil to do right

Page 7: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

7

Kinds of DBMS / databases

● Diti is modeled iad orgiaized difereatly

● Optmized for specifc kiads of operitoas

● Relatonal DBMS (RDBMS) / databases the most wide-spreid by fir– Bised oa mithemitcil relitoas

– Bisicilly, i ditibise is i collectoa of relitoas

– e.g. MySQL, PostgreSQL, ...

● Graph DBMS / databases– Diti is i aetwork, with eattes iad coaaectoas betweea them

– e.g. aeo4j

Page 8: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

8

Internal Structure of a Database

● Ditibises ire ofea tghtly iategrited with sofwire

● Should be iadepeadeat from how diti is structured iad stored

● First level iadepeadeace– Iadepeadeat of chiages ia

logicil structure of diti

● Secoad level iadepeadeace– Iadepeadeat of chiages ia

physicil structure of diti

View 1 View 2 View 3

Logicil Structure of Diti

Physicil Structure of Diti

Dit

ibis

e

App App

Page 9: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

9

Internal Structure of a Database

We will be lookiag it this pirt

View 1 View 2 View 3

Logicil Structure of Diti

Physicil Structure of Diti

Dit

ibis

e

App App

Page 10: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

10

Data Model

● Coaceptuil descriptoa of– eatty type for which diti should be stored

– relitoaship types betweea eatty types

● Specifc to kiad of DBMS– Relitoail ditibises ire bised oa relitoail diti model

– For griph ditibises: the griph itself

Page 11: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

11

Relatonal Data Model

● Visuilized with Eatty-Relitoaship (ER) diigrims:

● Cirdiaility: How miay eattes ire iavolved ia i relitoaship?

Eatty Type 1 Eatty Type 2CirdiailityCirdiaility

RelitoashipType

Atribute 1Atribute 2

Atribute 1 Atribute 2

Page 12: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

12

Relatonal Data Model

● Eximple Cirdiailites

● Reid:– One person owas one or more cars

Persoa Cir1..n

Owas

NimeAge

Color Briad

Page 13: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

13

Relatonal Data Model

● Eximple Cirdiailites

● Reid:– One car is owaed by exactly one person

→ Coastriiats do aot aecessirily hold ia reility (joiat owaership)

Persoa Cir1

Owas

NimeAge

Color Briad

Page 14: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

14

Relatonal Data Model

● Miia coacept: relaton schemas

● relitoa != relitoaship

– Relitoas derived from eattes iad relitoaships

● A relaton schema is defaed by– i aime

– iad i set of itribute aimes

– Optoailly: itribute types

relaton_name(atribute1, atribute2, …) or

relaton_name(atribute1: type1, atribute2: type2, …)

Page 15: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

15

Relaton Schemas

● A relaton schema usuilly correspoads to

– Reil world eatty types (e.g. cir, persoa, …)

– Reil world relitoaship types (e.g. persoa owas cir)

● Eximple relaton schemas:

– Car(color, brand)

– Person(name: CHAR(20),age: INTEGER)

– Owns(name, age, color, brand)

Page 16: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

16

Tuples

● A tuple is i reilizitoa of i relitoa schemi– Assigas vilues to the itributes of the relitoa

● Eximple tuples of the relitoa Car(color, brand):– (‘red’, ‘Ford’)

– (‘blue’, ‘Mercedes’)

● Eximple tuples of the relitoa Person(name, age):– (‘Heary’, 36)

– (‘Thomis’, 22)

Page 17: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

17

Relaton Instance

● A relaton instance coasists of– relitoa schemi

– ill its tuples

● Cia be aicely visuilized with i tible:

itribute1 attribute

2... ... ...

Name }Atribute / Columa

{Tuple / Row

Relitoa Iastiace / Tible

Page 18: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

18

Relaton Instance

● Eximple relaton instance of the persoa relitoa

aime age

‘Henry’ 36

‘Thomas’ 22

Person

Page 19: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

19

Database Instance

● A database instance is the collectoa of ill its relitoa iastiaces– i.e. ill relitoa schemis iad their correspoadiag tuples

aime age

‘Henry’ 36

‘Thomas’ 22

Person

color brand

‘red’ ‘Ford’

‘blue’ ‘Mercedes’

Car

...

Page 20: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

20

Integrity Constraints (ICs)

Page 21: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

21

Integrity Constraints (ICs)

● Coaditoa thit must be true for iay ditibise iastiace

● Specifed whea schemis ire defaed

● Checked wheaever relitoas ire modifed– i.e., whea tuple is idded, deleted, or modifed

Page 22: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

22

Domain constraints

● Domiia of vilid vilues for ia itribute– e.g., INTEGER, FLOAT, CHAR(20), …

– correspoad to diti types ia progrimmiag liaguiges

● Eximple relitoa schemi:

Person(name: CHAR(20),age: INTEGER)

→ Ditibise will aot illow iasertoa of this tuple

aime age

‘Henry’ 36

‘Mads’ ‘Doe’Domiia coastriiat violitoa

Page 23: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

23

Semantc integrity constraints

● Restrictoas oa the diti– e.g., ige >= 18

● Eximple relitoa schemi:

Person(name: CHAR(20),age: INTEGER)

→ Ditibise will aot illow iasertoa of this tuple

aime age

‘Henry’ 36

‘Mads’ 16Coastriiat violitoa

Page 24: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

24

Primary Keys

● Set of relitoa itributes– thit uaiquely ideatfes tuples of relitoa

– ill tuples aeed to hive uaique vilues for these itributes

● Eximple: CPR is primiry key of relitoa Person

→ There ciaaot be two tuples with sime CPR aumberCPR Name Birthday Address

... ... ... ...

1904651243 Svensson 19.04.1965 ...

... ... ... ...

1904651243 ... ... ...

... ... ... ...

Not illowed

Page 25: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

25

Primary Keys

● Primiry key “poiats” to exictly oae tuple

→ cia be used to lookup correspoadiag tuple

→ e.g., persoa cia be looked up usiag CPR

CPR Name Birthday Address

... ... ... ...

1904651243 Svensson 19.04.1965 ...

... ... ... ...

Whit is the aime of the persoa with CPR=1904651243 ?

Page 26: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

26

Foreign Keys

● Allow to issociite tuples ia difereat relitoas

● Tuple of source relitoa → tuple of tirget relitoa– Source iad tirget relitoa cia be the sime

– Cia oaly poiat to i primiry key ia the tirget relitoa

Page 27: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

27

Example: University Database

● Coaceptuil schemi:– Studeats(sid: striag, aime: striag, logia: striag, ige:

iateger, gpi:reil)

– Courses(cid: striag, caime:striag, credits:iateger)

– Earolled(sid:striag, cid:striag, gride:striag)

Studeat Course1..a1..aEarolled

Page 28: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

28

Example: Foreign Keys

Page 29: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

29

Query Languages

Page 30: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

30

Query Languages

● Allow miaipulitoa iad retrievil of diti from i ditibise

● Query liaguiges != progrimmiag liaguiges

● aot expected to be “turiag complete”

→ i.e., aot every operitoa cia be expressed

● aot iateaded to be used for complex cilculitoas

● support eisy, efcieat iccess to lirge diti sets

Page 31: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

31

Relatonal Query Languages

● Bised oa relitoail ilgebri

● For relitoail ditibises, i.e. relitoail diti model

● Relitoail model supports simple, powerful QLs:– Stroag formil fouaditoa bised oa logic

– Allows for much optmizitoa

● SQL: Most widely used relitoail query liaguige

→ Uaderstiadiag Relitoail Algebri is key to uaderstiadiag SQL, query processiag!

Page 32: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

32

What is an “Algebra”?

● Mithemitcil system coasistag of– Operands: Viriibles or vilues from which aew vilues

cia be coastructed

– Operators: Symbols deaotag procedures thit coastruct aew vilues from givea vilues

● Eximple:– Iategers [..., -1, 0, 1, ...] is operiads

– Arithmetc operitoas +/- is operitors

Page 33: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

33

What is Relatonal Algebra?

● Aa ilgebri where– operiads ire relitoas

– operitors ire desigaed to query relitoas ia i ditibise

● Cia be used is i query liaguige for relitoas

Page 34: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

34

Relatonal Algebra: 5 Basic Operatons

● Selectoa:

Selects i subset of tuples from relitoa R, for which coaditoa C holds (horizoatil)

● Projectoa:

Retiias itributes from relitoa R (vertcil)

● Cross-product: R1 x R2

Piirwise combiaitoa of tuples of relitoas R1 iad R2

● Set-difereace: R1 – R2

Tuples ia relitoa R1, but aot ia relitoa R2

● Uaioa: R1 R2∪Tuples ia relitoa R1 iad/or ia relitoa R2

● Siace eich operitoa returas i relitoa, operitoas cia be composed (Algebri is “closed”)

σ C (R)

π A1 , ... , Ak(R)

A1 , ... , Ak

Page 35: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

35

Relatonal Algebra: Example Instances

Page 36: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

36

Selecton (σ)

Page 37: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

37

Projecton (π)

Page 38: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

38

Projecton (π)

Page 39: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

39

Cross Product

Page 40: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

40

Cross Product

Page 41: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

41

Union and Set Diference

Page 42: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

42

Union

Page 43: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

43

Set Diference

Page 44: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

44

Nestng Operators

● Result of i relitoail ilgebri operitor is i relitoa

● It cia be used is iaput to iaother relitoail ilgebri operitor

Page 45: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

45

Compound Operator: Intersecton

● Ia idditoa to the 5 bisic operitors, there ire severil idditoail “Compouad Operitors”– Do aot idd computitoail power to the liaguige

– Useful shorthiads

– Cia be expressed with bisic operitoas

● Eximple: Intersecton– Tikes two iaput relitoas thit ire uaioa-compitble

Page 46: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

46

Compound Operator: Intersecton

Page 47: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

47

SQL - A language for Relational DBs

Page 48: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

48

SQL - A language for Relatonal DBs

● Siy: “ess-cue-ell” or “sequel”– But spelled “SQL”

● Diti Defaitoa Liaguige (DDL)– creite, modify, delete relitoas

– specify coastriiats

– idmiaister users, security, etc.

● Diti Miaipulitoa Liaguige (DML)– Specify queries to fad tuples thit sitsfy criterii

– idd, modify, remove tuples

● The DBMS is respoasible for efcieat eviluitoa

Page 49: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

49

SQL - A language for Relatonal DBs

● Query liaguige to retrieve diti from ditibise

● Iacludes i diti-defaitoa compoaeat to defae ditibise schemis

● SQL commiads hive to be termiaited with ‘;’

● SQL is stiadirdized– some DBMS iaclude their owa SQL commiads

Page 50: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

50

Creatng Databases in SQL

● Creite i aew, empty ditibise ‘Uaiversity’:

CREATE DATABASE Uaiversity;

– Does aot coatiia iay relitoasupoa creitoa

DB“Uaiversity”

Page 51: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

51

Creatng Relatons in SQL

● Creite i aew, empty relitoa ‘Studeats’:

CREATE TABLE Studeats (sid CHAR(20) PRIMARY KEY, aime CHAR(20), logia CHAR(10), ige INTEGER, gpi FLOAT);

– Does aot coatiia iay tuplesupoa creitoa

– Note: the type (domiia) of eich feld is specifed, iad eaforced by the DBMS wheaever tuples ire idded or modifed.

DB“Uaiversity”

Studeats

sid name login age gpa

Page 52: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

52

Creatng Relatons in SQL

● Similirly:

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

DB“Uaiversity”

Studeats Courses

Page 53: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

53

Adding and Deletng Tuples

● Iasert i siagle tuple:

INSERT INTO Studeats (sid, aime, logia, ige, gpi) VALUES ('53688', 'Smith', 'smith@ee', 18, 3.2);

● Delete ill tuples sitsfyiag some coaditoa (e.g., aime = Smith):

DELETE FROM Studeats S WHERE S.aime = 'Smith';

sid name login age gpa

53688 Smith smith@ee 18 3.2

sid name login age gpa

Page 54: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

54

Selectng Tuples in SQL

● Fiad tuples for ill 18 yeir old studeats with gpi’s ibove 2.0:

SELECT * FROM Studeats S WHERE S.ige=18 AND S.gpi > 2.0;

● To get just aimes iad logias:

SELECT S.aime, S.logia FROM Studeats S WHERE S.ige=18 AND S.gpi > 2.0;

sid name login age gpa

53688 Smith smith@ee 18 3.2

aime login

Smith smith@ee

Page 55: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

55

Relatonal Algebra Operators in SQL

● Relitoail ilgebri operitors cia be expressed with SQL

● Selectoa operitor (σ):

SELECT * FROM Studeats SWHERE S.ige=18 AND S.gpi > 2.0;

● Projectoa operitor (π):

SELECT S.ige,S.gpi FROM Studeats S;

● Uaioa:

SELECT * FROM Studeats SWHERE S.ige=18 AND S.gpi > 2.0

UNIONSELECT * FROM Studeats S

WHERE S.ige=20 AND S.gpi > 2.3;

Page 56: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

56

Relatonal Algebra Operators in SQL

● Set Difereace:

SELECT * FROM Studeats SWHERE S.gpi > 2.0

EXCEPTSELECT * FROM Studeats S

WHERE S.ige=19;

● Cross Product:

SELECT * FROM Studeats S, Earolled E;

Page 57: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

57

Primary Keys in SQL

● Siagle itribute key:

CREATE TABLE Studeats (sid CHAR(20) PRIMARY KEY, aime CHAR(20), logia CHAR(10), ige INTEGER, gpi FLOAT)

● Mult-itribute key:

CREATE TABLE Earolled (sid CHAR(20) cid CHAR(20), gride CHAR(2), PRIMARY KEY (sid,cid))

Page 58: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

58

Foreign Keys in SQL

● Oaly studeats listed ia the Studeats relitoa should be illowed to earoll for courses

→ sid is i foreiga key referriag to Studeats

● Studeats cia oaly earoll for registered courses

→ cid is i foreiga key referriag to Courses

CREATE TABLE Earolled(sid CHAR(20),cid CHAR(20),gride CHAR(2),PRIMARY KEY (sid,cid),FOREIGN KEY (sid) REFERENCES Studeats,FOREIGN KEY (cid) REFERENCES Courses);

Page 59: DM534: Introduction to Relational Databasesrolf/Edu/DM534/E17/databases.pdf · DM534: Introduction to Relational Databases 10/10/2017 Christian Wiwie. 10/10/2017 Christia Wiwie 2

10/10/2017Christia Wiwie

59

Thank you for your attention!