Repetition and Exam Prep

56
Part 1 - EER and Relational Algebra Repetition and Exam Prep Dr. Philipp Leitner [email protected] @xLeitix

Transcript of Repetition and Exam Prep

Part 1 - EER and Relational Algebra

Repetition and Exam Prep

Dr. Philipp Leitner

[email protected]

@xLeitix

Chalmers6/1/16 2

Very little new lecture content (just finishing up RA)

Introduce the RelaX tool (allows you to test RA expressions)

Provide you with detailed learning objectives for the first half of the course “Study guide” for the exam

Repeat core materials and do some more exercises

Goal for today

Chalmers6/1/16 3

Chalmers6/1/16 4

Chalmers6/1/16 5

Aggregate Functional Operation !

General notation:

[<grouping>]ℱ<functions>(R)

whereas <functions> is a list of [MIN|MAX|AVERAGE|SUM|COUNT] <attribute>

Chalmers6/1/16 6

OUTER JOINS

In NATURAL JOIN and EQUIJOIN, tuples without a matching (or related) tuple are eliminated from the join result

Including tuples with null in the join attributes

A set of operations, called OUTER JOINS, can be used when we want to keep all the tuples, regardless of whether or not they have matching tuples in the other relation.

Chalmers6/1/16 7

Regular JOINS can lead to information loss

Example: DEPARTMENT  MGRSSN=SSN EMPLOYEE

If a department currently does not have a manager (for whatever reason) it won’t show up in the result relation.

Basically always an issue when one or both of the join attributes are allowed to be NULL

(typically the foreign key)

Chalmers6/1/16 8

Types of OUTER JOINS

LEFT OUTER JOIN (keeps every tuple in the left relation) A B

RIGHT OUTER JOIN (keeps every tuple in the right relation) A B

FULL OUTER JOIN (keeps every tuple in both relations) A B

Chalmers6/1/16 9

Example OUTER JOIN

πFname, Minit, Lname, Dname( EMPLOYEE EMPLOYEE.DNO=DEPARTMENT.DNUMBER DEPARTMENT)

Chalmers6/1/16 10

Copyright (c) 2011 Pearson Education

Chalmers6/1/16 11

Some more exercises.

Chalmers6/1/16 12

Copyright (c) 2011 Pearson Education

How many projects are there in total?

Chalmers6/1/16 13

Copyright (c) 2011 Pearson Education

How many projects are there in total?

Chalmers6/1/16 14

Copyright (c) 2011 Pearson Education

How many projects are there at each location?

Chalmers6/1/16 15

Copyright (c) 2011 Pearson Education

How many projects are there at each location?

Chalmers6/1/16 16

Copyright (c) 2011 Pearson Education

Get a list of all projects and the employees (all info, not just the employee Id) that work on them. Projects with no employees working on them should still be listed.

Chalmers6/1/16 17

Copyright (c) 2011 Pearson Education

Get a list of all projects and the employees (all info, not just the employee Id) that work on them. Projects with no employees working on them should still be listed.

Chalmers6/1/16 18

Copyright (c) 2011 Pearson Education

For each project, find out how many employees are assigned to them. Projects without employees should not show up in the results (result should be a list of project numbers and employee counts).

Chalmers6/1/16 19

Copyright (c) 2011 Pearson Education

For each project name, find out how many employees are assigned to them. Projects without employees should not show up in the results (result should be a list of project names and employee counts).

Chalmers6/1/16 20

RelaX

Online tool to evaluate (test) RA queries

Research / teaching tool maintained by the University of Innsbruck

- Expect syntax to be slightly different than what we use in the course and the book - It’s not super-easy to use (but better than nothing!)

Chalmers6/1/16 21

LinksLink to tool with the “EMPLOYEE” database preloaded

https://dbis-uibk.github.io/relax/calc/gist/80a49769c15c993a386c3074b7741255

Link to tool with the “MONDIAL” database preloaded

https://dbis-uibk.github.io/relax/calc/gist/7ec0be1be462f65818454ca3411c7b02

(~1/6 of the full database)

Chalmers6/1/16 22

Some Examples in RelaX

Chalmers6/1/16 23

Some Examples in RelaX

πFname,Lname,Dno EMPLOYEE

πFname σBdate ≥ Date('1960-01-01') EMPLOYEE

Chalmers6/1/16 24

Some Examples in RelaX

Chalmers6/1/16 25

Some Examples in RelaX

TMP = πFname,Lname σBdate ≥ Date('1960-01-01') EMPLOYEE NEWBIES = ρNEWBIES ρFname->First ρLname->Last TMP

γ Plocation; count(Pnumber)->projects ( PROJECT )

Chalmers6/1/16 26

Let’s talk about the exam

Chalmers6/1/16 27

4 hours - but you will probably not need the entire time

Combination of theory and practice 1/4 - 1/3 of the points will be theory-based 2/3 - 3/4 of the points will be based on applied tasks

No multiple-choice! (not like the Kahoot quizzes :) )

General Structure of the Exam

Chalmers6/1/16 28

Repetition - Lecture 1

Introduction to Databases

Chalmers6/1/16 29

Understand and explain:

• Data, information, and knowledge • Basic database operations

CRUD - create, read, update, delete • Databases as collections of meta-data and data • Advantages of using a DBMS • The ANSI/SPARC Three-Schema Architecture

Summary of Central Learning Items

Chalmers6/1/16 30

[Given some example] Draw the basic knowledge management pyramid and apply it to the example. What’s the difference between data and information in this model?

[Given some example] Explain the concept of meta-data in your own words. Exemplify meta-data and data based on the provided example.

Example Questions

Chalmers6/1/16 31

Repetition - Lecture 2 and 3

EER Block

Chalmers6/1/16 32

Understand and apply:

• Basic ER concepts • Entities, relationships, attributes, cardinalities, participation • Keys

• Slightly more advanced concepts • Composed attributes, multi-valued attributes • Weak entity types • Association attributes • Regular (“disjoint”) generalizations

Summary of Central Learning Items (1)

Chalmers6/1/16 33

Understand and apply:

• Advanced concepts • Ternary relationships and their cardinalities • Overlapping generalizations • Derived attributes

Summary of Central Learning Items (2)

Chalmers6/1/16 34

Understand and explain:

• Difference between ER and EER • What the different E(E)R constructs mean, what they are

used for, and what they imply • Union types

Summary of Central Learning Items (3)

Chalmers6/1/16 35

Type 1 (this is almost certainly going to be part of the exam): “Here is a textual domain description, draw an EER diagram”

Type 2 (this may be part of the exam): “Here is an EER diagram, answer some questions about it”

Type 3 (this may be part of the exam): Short theory questions

Types of Questions for this Block

Chalmers6/1/16 36

Consider the following outtake of the domain description of a university management system. Model the described domain using an EER diagram, and using the notation we used in the course. Use the 1,N,M notation for describing cardinalities.

If something is not specified, make an assumption and note it down in plain text.

Example for Type-1

Chalmers6/1/16 37

• The database needs to keep track of universities. Universities have a unique name and one or more campus locations. Each campus location consists of a city and country. Further, we need to save the number of employees as well as the number of students for each university.

• There are two types of universities: research- and teaching-centric ones. Every university in the database falls into at least one of these categories. However, there are also universities that are both. For research-centric universities we need to save the number of published papers.

• Teaching-centric universities offer courses. A course is offered at exactly one teaching university, but any teaching university may offer arbitrarily many courses. Courses are identified through a course id, but also have a unique short name. Every research-centric university manages at least one doctoral programme, but some programmes are joint (i.e., they are managed by more than one university). Doctoral programmes have a unique name and a field of research.

Chalmers6/1/16 39

Repetition - Lecture 4 to 6

Relational Algebra Block

Chalmers6/1/16 40

Understand, explain, and apply:

• Relational Modelling • Relations, attributes, tuples, state • How to map an EER diagram to the relational model

Summary of Central Learning Items - RM

Chalmers6/1/16 41

Type 1 (this is likely going to be part of the exam): Map a given ER diagram to relations

Type 2 (this may be part of the exam): Short theory questions

E.g., “Is the provided relational model / state valid? Briefly explain your reasoning.”

Types of Questions for this Block

Chalmers6/1/16 42

Develop a relational model (using the notation we introduced in the course) for the following ER diagram. You do not need to explicitly demark not-null constraints (but you need to identify primary and foreign key constraints).

Example for Type-1

Chalmers6/1/16 43

Chalmers6/1/16 44

UNIVERSITY(Name, NrStudent, NrEmployees,NrPapers)

CAMPUS(Country, City, University) University -> UNIVERSITY.Name

COURSE(CourseId, ShortName, University) University -> UNIVERSITY.Name

DPROGRAM(Name, Field)

PROGRAM_MANAGEMENT( Program, University)

University -> UNIVERSITY.Name Program -> DPROGRAM.Name

Chalmers6/1/16 45

Understand, apply, and analyze: • Unary Relational Algebra Operators

• Select, Project, Assign, Rename • Relational Algebra Set Operators

• Union, Intersection, Difference, Cartesian Product • Join Operators

• Join, [Equijoin], Natural Join, [Left|Right|Full] Outer Join • Aggregation and Grouping Operators

• Most important: be able to combine operators in non-trivial expressions

Summary of Central Learning Items - RA

Chalmers6/1/16 46

Type 1 (this is likely going to be part of the exam): Given one or more relations, formulate a small number of RA queries.

Type 2 (this may be part of the exam): Given one or more relations and valid state(s), write down the results of a small number of RA expressions or explain in your own words what an expression would return if no state is given.

Type 3 (this may be part of the exam): Given a faulty RA expression, explain what is wrong (and, maybe, how it could be fixed)

Types of Questions for this Block

Chalmers6/1/16 47

Example for Type-1Consider the following Relational Model:

EXECUTION(Exec_id, Job_id, Start, End) Job_id -> JOB.Job_id

JOB(Job_id, User) User -> USER.Username

USER(Username, City, Street, ZIP)

Write the following RA expression: • Find the city of the user with the username ‘philipp’.

Chalmers6/1/16 48

Example for Type-1Consider the following Relational Model:

EXECUTION(Exec_id, Job_id, Start, End) Job_id -> JOB.Job_id

JOB(Job_id, User) User -> USER.Username

USER(Username, City, Street, ZIP)

Write the following RA expression: • Find the city of the user with the username ‘philipp’.

Chalmers6/1/16 49

Example for Type-1Consider the following Relational Model:

EXECUTION(Exec_id, Job_id, Start, End) Job_id -> JOB.Job_id

JOB(Job_id, User) User -> USER.Username

USER(Username, City, Street, ZIP)

Write the following RA expression: • Count how many jobs users living in ‘Gothenburg’ have.

Chalmers6/1/16 50

Example for Type-1Consider the following Relational Model:

EXECUTION(Exec_id, Job_id, Start, End) Job_id -> JOB.Job_id

JOB(Job_id, User) User -> USER.Username

USER(Username, City, Street, ZIP)

Write the following RA expression: • Count how many jobs users living in ‘Gothenburg’ have.

Chalmers6/1/16 51

Example for Type-1Consider the following Relational Model:

EXECUTION(Exec_id, Job_id, Start, End) Job_id -> JOB.Job_id

JOB(Job_id, User) User -> USER.Username

USER(Username, City, Street, ZIP)

Write the following RA expression: • Calculate the duration (end time minus begin time) of each execution. Create a

new relation DURATION(Exec_id, Job_id, Duration) of the result. Assume that both, start and end time cannot be NULL.

Chalmers6/1/16 52

Example for Type-1Consider the following Relational Model:

EXECUTION(Exec_id, Job_id, Start, End) Job_id -> JOB.Job_id

JOB(Job_id, User) User -> USER.Username

USER(Username, City, Street, ZIP)

Write the following RA expression: • Calculate the duration (end time minus begin time) of each execution.

Create a new relation DURATION(Exec_id, Job_id, Duration) of the result. Assume that both, start and end time cannot be NULL.

Chalmers6/1/16 53

Example for Type-1Consider the following Relational Model:

EXECUTION(Exec_id, Job_id, Start, End) Job_id -> JOB.Job_id

JOB(Job_id, User) User -> USER.Username

USER(Username, City, Street, ZIP)

Write the following RA expression: • Calculate the duration (end time minus begin time) of each execution. Create a

new relation DURATION(Exec_id, Job_id, Duration) of the result. If the end time is NULL set the duration to NULL as well.

Chalmers6/1/16 54

Example for Type-1Consider the following Relational Model:

EXECUTION(Exec_id, Job_id, Start, End) Job_id -> JOB.Job_id

JOB(Job_id, User) User -> USER.Username

USER(Username, City, Street, ZIP)

Write the following RA expression: • Calculate the duration (end time minus begin time) of each execution. Create a

new relation DURATION(Exec_id, Job_id, Duration) of the result. If the end time is NULL set the duration to NULL as well.

Chalmers6/1/16 55

Examples for Type-2Consider the following Relational Model:

EXECUTION(Exec_id, Job_id, Start, End) JOB(Job_id, User) USER(Username, City, Street, ZIP) SUBSCRIBED_TO(Username, Cluster_id, Discount) CLUSTER(Id, Name, Location)

What’s wrong with the following RA expression for returning the user details of the user with the highest discount?

Chalmers6/1/16 56

You can already finish Assignment 2

Next week we are going to get started with SQL

Next steps