1 of 147

Presented by�Lucy Martin //

Sandy Luo //

Colton Carner

2 of 147

DISCLAIMER

This workshop is not officially endorsed by the university or the teaching staff and may not fully reflect the assessable subject content. Any tutors that participate in this workshop have verbally chosen to do so voluntarily and are not expected to represent the teaching team.

3 of 147

4 of 147

CISSA Revision Workshops

  • Every semester, we organise revision workshops for FOC, FOA, OOSD, DS, and SMD
  • Our dedicated tutors, alongside the CISSA education subcommittee, volunteer their time to ensure the success of these workshops
  • Keep in mind that exam formats can vary
  • Use this workshop as a supplemental revision tool, not as a precise blueprint for exams

5 of 147

Structure of Today’s Workshop

  1. ER Modelling
  2. Relational Algebra
  3. SQL
  4. Query Optimisation
  5. Normalisation
  6. Transactions
  7. Distributed DBs
  8. No SQL
  9. General Questions & Answers!

6 of 147

Structure of Today’s Workshop

We’ve made a mini-exam for you to work through in groups :)

  1. Say hello to ~2ish people around you!
    1. Possibly come up with a team name for extra motivation?

  1. Go to LMS > Open the ‘Mini practice exam’

7 of 147

Entity-Relational

Modelling

Sandy

8 of 147

ER Modelling: Exam tips

  • Crow’s foot / Chen’s notation
  • Key / Participation Constraints
  • Weak entities (identifying relationships, notation etc.)
  • Data types

  • We’ll spend ~5min on Q1, ~5min on Q2+3, ~5m on Q4+5

9 of 147

ER Modelling: Q1

  1. The expiry date of a product: dd/mm/yy DATE
  2. An employee’s sign-off time: hh:mm dd/mm/yy DATETIME
  3. Did Cathy test positive for COVID? Yes / No BOOLEAN
  4. A person’s mobile phone number in Australia with +61 prefix (followed by 9 numbers): CHAR(12)
  5. A global enterprise’s annual revenue: Big enough + store decimals DOUBLE
  6. Auto-incremented primary key of table ‘Student’: INT
  7. A person’s age: Realistically ~0 - 100 TINYINT (0-255)
  8. A 16-digit long credit card number: CHAR(16)
  9. One out of 4 positions of an academic in a university:
    • Lecturer, Senior Lecturer, Associate Professor, or Professor ENUM
  10. Video demonstration of an electric appliance: Very large binary data BLOB

10 of 147

ER Modelling: Q2

  • A Student (can) have (many) Exams
  • An Exam (must) have (many) Students
  • A Student (must) have (many) Exams
  • An Exam (must) have (one) Student
  • A Student (can) have (many) Exams
  • An Exam (can) have (one) Student
  • A Student (can) have (one) Exam
  • An Exam (must) have (many) Students

0...m

1...m

1...m

1...1

0...m

0...1

0...1

1...m

11 of 147

ER Modelling: Q3

12 of 147

Main difference:

  • Should booking be an identifying relationship?
  • What is the correct constraint between Accomodation and Person?

13 of 147

Should booking be an identifying relationship?

  • NO; It doesn’t identify any weak entity

Constraint between Accomodation and Person:

  • Accommodations should be able to be booked multiple times (A [0…m] P)
  • A Person can make multiple bookings >= 1 (P [1…m] A)

14 of 147

ER Modelling: Q4

CREATE TABLE School (

SchoolName VARCHAR(50),

SchoolCity VARCHAR(50),

StudentCount SMALLINT,

PRIMARY KEY (SchoolName)

);

CREATE TABLE PerfReview (

PRDate DATE,

QualityRating TINYINT,

SkillsRating TINYINT,

Comments VARCHAR(500),

TeacherID INT,

PRIMARY KEY (PRDate, TeacherID),

FOREIGN KEY (TeacherID) REFERENCES Teacher(TeacherID)

);

CREATE TABLE Teacher (

TeacherID INT,

TeacherFirstName VARCHAR(50),

TeacherLastName VARCHAR(50),

SchoolName VARCHAR(50),

PRIMARY KEY (TeacherID),

FOREIGN KEY (SchoolName) REFERENCES School(SchoolName)

);

CREATE TABLE TeacherLanguages (

TeacherID INT,

LanguageName VARCHAR(20),

PRIMARY KEY (TeacherID, LanguageName),

FOREIGN KEY (TeacherID)

REFERENCES Teacher(TeacherID)

);

15 of 147

ER Modelling: Q5

  • {Title}?
  • {ID, Title}?

Do we know for sure?

Super Key:

A set of fields where no two distinct tuples can have same values in all key fields.

Candidate Key:

Minimal subset of super keys

16 of 147

SQL + Relational Algebra

Colton: 30 mins

17 of 147

SQL: Exam structure

The way we test SQL is pretty consistent each semester: A case study that you need to write SQL queries for.

  • The case study / schema is usually (aka always in past) the same as or based on A2 to save time trying to understand a new schema, so revise that before going in. In this mini-practice exam it’s a different, simpler schema to make it faster to understand so we don’t have to re-write it every sem…
  • 3-4 SQL questions, hardest maybe at ~q8 level from A2
  • We don’t care about small syntax/spelling/columnname etc errors, since you’ll be typing into a text box so can’t test your queries. Main thing is high-level understanding of SQL concepts.

18 of 147

SQL: Ready-set-go!

  1. Attempt Q1 with your friends! Take time to review your notes if you need. If you finish, get started on Q2 early (~5 mins)

  1. We’ll double check quickly together with the entire room

  1. Attempt Q2 with friends (10 mins). No stress if don’t finish, just try and get as far as you can!

19 of 147

Q1: Return the names of tournaments, played at courses in the suburb of Heatherton, where Bella Sandbury ranked in the top ten (use the rank already provided in the schema).

SELECT DISTINCT TournamentName

FROM

Player

NATURAL JOIN PlayerCompetes

NATURAL JOIN Tournament

NATURAL JOIN Course

WHERE

CourseSuburb = ‘Heatherton’

AND PlayerFirstName = ‘Bella’

AND PlayerLastName = ‘Sandbury’

AND Rank <= 10;

20 of 147

Q2: List every hole in every course, along with the lowest number of strokes ever recorded on that hole, the player who achieved this, and in which competition.

SELECT CourseName, HoleNumber, minStrokes, playerID, competitionName

FROM

Hole

NATURAL LEFT JOIN PlayerPlaysHole

NATURAL LEFT JOIN Tournament

NATURAL LEFT JOIN (

SELECT CourseID, HoleNumber, min(NumberOfStrokes) AS minStrokes

FROM Hole NATURAL JOIN PlayerPlaysHole

GROUP BY CourseID, HoleNumber

) AS holeMinStrokes

WHERE ISNULL(holeMinStrokes.minStrokes) OR holeMinStrokes.minStrokes = PlayerPlaysHole.NumberOfStrokes

Could also do a non-natural left join above instead (but requires a lot in the ON clause)

Could also do this using RANK() window function

21 of 147

SQL: More practice?

More practice on LMS!

Modules > Active learning (scroll down) > SpotifySQL + UberEatsSQL

Modules > Practice on your own (scroll down) > ‘SQL: Golf Study’

22 of 147

Relational Algebra: Exam structure

  • Hard to draw in browsers, so usually 1-2 questions involving matching RA statements to problem descriptions in some way.

23 of 147

Relational Algebra: Ready-Set-Go!

  1. Attempt RA question with friends (5 mins) Feel free to check notes etc!

  1. Check as a group

24 of 147

List the topics of forums where the general user with user id “1” has posted at least once before the date “01-01-2022”.

25 of 147

Query Optimisation + Processing

Lucy

26 of 147

Exam tips

  • Always write your units
    • whether it’s tuples, pages or IOs, it’s easy to lose track, be explicit.
  • Understand the formula in the cheatsheet
    • Memorising != understanding,
      • What is 2 in 2*NPasses*NPages in the Sort Merge Join formula?
      • What is B in the Block-oriented NLJ?
    • Crucial for knowing how to adapt the formulas for pipelining
  • Be careful for usability of indexes!
    • Hash index: no range queries (there’s a formula, but it’s majorly inefficient)
    • Cluster index: compare search key fields prefix and query predicates.
    • Be wary of composite index search keys
  • Reduction factor decides cost and result size
    • Cost depends on the search key fields of the index and the query conditions, only consider the matched attributes.
    • Result size depends on the query conditions

27 of 147

Single Relation Plan A: Question 9

product: 1000 pages 100 tuples/page 100,000 tuples

product.totalprice: [0,10000] ∴ 10000 distinct values

product.quantity: [1,100] ∴ 100 distinct values

totalprice < 5000 RF = 0.5

quantity = 20 RF = 0.01

∴ ΠRF = 0.5 ✕ 0.01 = 0.005

Result_size = NTuples(R) ✕ ΠRF ∴ 100,000 ✕ 0.005 = 500 Tuples

28 of 147

Single Relation Plan B: Question 10

product: 1000 pages 100 tuples/page 100,000 tuples

product.totalprice: [0,10000] ∴ 10000 distinct values

product.quantity: [1,100] ∴ 100 distinct values

unclustered B+ tree index<quantity> 100 pages

Note: can only use the RF from the index’s search key <quantity> ∴ 0.01

(100 + 100,000) ✕ 0.01 = 1001 IOs

However, heap scan = 1000 IOs

29 of 147

Multi-Relation Plan A: Question 11

patient: 20 pages sort passes: 1

diagnosis: 10,000 pages sort passes: 2

buffer: 12 pages

20 + ((20/(12-2)) ✕ 10000) = 20020

20 + ((20/10) ✕ 10000) = 20020

20 + (2 ✕ 10000) = 20020 IOs

30 of 147

Multi-Relation Plan A: Question 12

patient: 20 pages sort passes: 1

diagnosis: 10,000 pages sort passes: 2

buffer: 12 pages

2 ✕ 20 ✕ 1�+ 2 ✕ 10,000 ✕ 2�+ 20 + 10,000

= 50,060 IOs

31 of 147

Query Optimisation Introduction

employee: 10 pages 100 tuples/page 1000 tuples

order: 6000 pages 100 tuples/page 600,000 tuples

item: 100 pages 100 tuples/page 10,000 tuples

employee.position 20 distinct values�order.totalcost [0,10000] ∴ 10000 distinct values

clustered B+ tree index<order.totalcost> 50 pages

clustered B+ tree index<item.itemId> 10 pages

employee ⋈ order 100 tuples/page�order ⋈ item 100 tuples/page��sort passes: 2 passes NLJ: page-oriented

32 of 147

Query Optimisation Plan: Question 13, part 1 [A]

employee: 10 pages 100 tuples/page 1000 tuples

order: 6000 pages 100 tuples/page 600,000 tuples

item: 100 pages 100 tuples/page 10,000 tuples

employee.position 20 distinct values�order.totalcost 10000 distinct values

clustered B+ tree index<order.totalcost> 50 pages

clustered B+ tree index<item.itemId> 10 pages

employee ⋈ order 100 tuples/page�order ⋈ item 100 tuples/page�sort passes: 2 passes NLJ: page-oriented

totalcost > 5000 RF = 0.5

result_size = NTuples(R) ✕ ΠRF

∴ 600,000 ✕ 0.5 = 300,000 Tuples

∴ 300,000 ÷ 100 = 3,000 Pages

33 of 147

Query Optimisation Plan: Question 13, part 2 [B]

employee: 10 pages 100 tuples/page 1000 tuples

order: 6000 pages 100 tuples/page 600,000 tuples

item: 100 pages 100 tuples/page 10,000 tuples

employee.position 20 distinct values�order.totalcost 10000 distinct values

clustered B+ tree index<order.totalcost> 50 pages

clustered B+ tree index<item.itemId> 10 pages

employee ⋈ order 100 tuples/page�order ⋈ item 100 tuples/page�sort passes: 2 passes NLJ: page-oriented

E.EmpID = O.EmpID RF = 1/1000 = 0.001

result_size = ΠNTuples ✕ ΠRF

300,000 ✕ 1000 ✕ 0.001 = 300,000 Tuples

∴ 300,000 ÷ 100 = 3,000 Pages employee⋈order: 3,000 pages

34 of 147

Query Optimisation Plan: Question 13, part 3 [C]

employee: 10 pages 100 tuples/page 1000 tuples

order: 6000 pages 100 tuples/page 600,000 tuples

item: 100 pages 100 tuples/page 10,000 tuples

employee.position 20 distinct values�order.totalcost 10000 distinct values

clustered B+ tree index<order.totalcost> 50 pages

clustered B+ tree index<item.itemId> 10 pages

employee ⋈ order 100 tuples/page�order ⋈ item 100 tuples/page�sort passes: 2 passes NLJ: page-oriented

cost = (50 + 6000) ✕ 0.5 = 3,025 IOs

35 of 147

Query Optimisation Plan: Question 13, part 4 [D]

employee: 10 pages 100 tuples/page 1000 tuples

order: 6000 pages 100 tuples/page 600,000 tuples

item: 100 pages 100 tuples/page 10,000 tuples

employee.position 20 distinct values�order.totalcost 10000 distinct values

clustered B+ tree index<order.totalcost> 50 pages

clustered B+ tree index<item.itemId> 10 pages

employee ⋈ order 100 tuples/page�order ⋈ item 100 tuples/page�sort passes: 2 passes NLJ: page-oriented

2 ✕ 3000 ✕ 2 + �2 ✕ 10 ✕ 2 +

3000 + 10

= 12,050 IOs

36 of 147

Query Optimisation Plan: Question 13, part 5 [E]

employee: 10 pages 100 tuples/page 1000 tuples

order: 6000 pages 100 tuples/page 600,000 tuples

item: 100 pages 100 tuples/page 10,000 tuples

employee.position 20 distinct values�order.totalcost 10000 distinct values

clustered B+ tree index<order.totalcost> 50 pages

clustered B+ tree index<item.itemId> 10 pages

employee ⋈ order 100 tuples/page�order ⋈ item 100 tuples/page�sort passes: 2 passes NLJ: page-oriented

employee⋈order: 3,000 pages

2 1 ✕ 3000 +

2 ✕ 100 +

3000 + 100

= 6,300 IOs

37 of 147

Normalization

Sandy

38 of 147

Normalisation:

QA+B: 5min, QC: 5min

39 of 147

Normalisation: Q1A

  • Any repeating groups?
  • No -> 1NF

40 of 147

Normalisation: Q1B

1. Insertion Anomaly

2. Deletion Anomaly

3. Update Anomaly

Examples:

1. Can’t add a writer unless there is a consultation

2. Changing the mobile of a writer needs to be repeated

3. Delete a consultation for HBO -> details of the writer and order could disappear completely

41 of 147

Normalisation: Q1C

  • No repeating groups -> Already in 1NF
  • 2NF: partial dependencies?
    • CusNo -> Name, Agency
    • Writer -> Rating, Mobile
    • Order -> Prod_ID, Prod_Desc
  • Key: {CusNo, Order}

42 of 147

Normalisation: Q1C

2NF: Violated, have a partial dependency CusNo and also Order. We need to resolve both.

  • Customer(CusNo(PK), Name Agency)
  • Order(Order(PK), Prod_ID, Prod_Desc)
  • Consultation(CusNo (PFK), Writer, Rating, Mobile, Order(PFK))

43 of 147

Normalisation: Q1C

3NF: Any transitive dependencies? Key: {CusNo, Order}

  • CusNo -> Name, Agency
  • Writer -> Rating, Mobile
  • Order -> Prod_ID, Prod_Desc
  • Customer(CusNo(PK), Name Agency)
  • Order(Order(PK), Prod_ID, Prod_Desc)
  • Consultation(CusNo (PFK), Writer, Rating, Mobile, Order(PFK))

44 of 147

Normalisation: Q1C

3NF: Violated since we have a transitive dependency Writer hence need to resolve. Customer and Order are finished already.

  • Writer(Writer(PK), Rating, Mobile)
  • Consultation (Cusno(PFK), Writer(FK), Order(PFK))

45 of 147

Data Warehousing

Lucy

46 of 147

Transactions: Ready-set-go!

  1. Attempt the Data Warehousing questions with your friends (~5 mins)

  1. We’ll discuss together

47 of 147

‘Database Concepts’ (Transactions + Distributed + Backups + NoSQL)

Colton: ~40 mins?

48 of 147

DB Concepts: Exam structure

Final part of the subject, and the most varied in assessment structures.

  • Transactions:
    • Discussing usefulness/value of transactions
    • Identifying problems if isolation not enforced (Lost Update / Uncommitted Data / Inconsistent Retrieval) and considering outcomes
    • Discussing transactions in SQL
  • Distributed:
    • Discussing implications/value of distributing databases
    • Recommending a distribution strategy for a case study
  • Backups:
    • Recommending backup strategy for case study
  • NoSQL
    • Strengths / weaknesses of Relational model vs NoSQL models we considered
    • Recommending NoSQL model for case studies
    • Strengths / weakness of ‘BASE’ databases

49 of 147

Transactions: Ready-set-go!

  1. Attempt the Transactions questions with your friends (~10 mins)

  1. We’ll discuss together

50 of 147

Distributed + NoSQL: Ready-set-go!

  1. Attempt Distributed + NoSQL qs with your friends (10 mins)

  1. We’ll discuss together

51 of 147

Backups: Ready-set-go!

  1. Attempt Backups qs with your friends (10 mins)

  1. We’ll discuss together

52 of 147

Feedback form + sign ups

Feedback Form!

53 of 147

OLD STUFF BELOW HERE

54 of 147

Key concepts

  1. Constraints: Key/Cardinality constraints (one/many), participation constraints (mandatory or not)
  2. Weak Entities: can be identified uniquely only by considering the primary key of another entity
  3. Special attributes: Multi-valued, composite, derived, relational
  4. Relationships: Binary(most common), unary, ternary relationships
  5. Notation: Chen’s notation, Crow’s foot notation.
  6. Translating design: from conceptual design to logical design to physical design

55 of 147

Question 1: Data types

56 of 147

Tips

  1. Do not include the actual business or company whose business processes you are modeling.
  2. Avoid using vague words like “has” to label your relationships
  3. Do not overuse weak entities (when an entity has a unique identifier, avoid modelling as a weak entity)
  4. Remember to annotate underline for primary key (and dashed underline for partial key of weak entities). Remember use surrogate key if you cannot find one from the case study.
  5. Proper use of relationship attributes is helpful
  6. Ternary v.s. 2 binary relationships: Use ternary only when a relationship must involve all three entities.

57 of 147

Tips

7. Steps to go through a question about ER modelling in the exam

  • First reading: Read quickly and roughly, just to understand the business scenario
  • Second reading: Read sentence by sentence. Decide entities and their attributes (focus on nouns), figure out relationships (normally in a sentence that involves two or more entities). Pay attention to some keywords when determining the relationship constraints (e.g. at least one, at most one, may, etc...). Entities don’t have to be solid objects, can be things like concepts or events too.
  • Double check special attributes

58 of 147

Tips

8. List your assumptions if you made any, but only use assumptions when there are ambiguities instead of simplifying the case study question

  • Exam questions will be clear and straightforward, so only make assumptions when you have to!

9. Do not draw a messy final model

  • Please make sure your model is clear and readable.
  • Don’t draw a circle like a rectangle! Don’t draw a rectangle like diamond!
  • Make sure we can distinguish between bold lines and regular lines!

10. Check assignment 1 feedback

59 of 147

Bridgman Art Library (adapted from Practice data modelling task)

Bridgman Art Library manages copyright of artworks. Customer organisations that wish to use a copyrighted image can pay a fee to Bridgman Art Library, who will then provide a high-quality image for reproduction.

Customers must register a proposal for the use of the artwork, containing information about the intended use, intended market and intended duration of use. The customer also nominates the price they are willing to pay for their proposed use. The date the proposal was made is also stored.

Bridgman Art Library needs to be able to identify who has owned the artwork at any point in time since the artwork’s creation. Each artwork may have been owned by many owners over time but can only have one owner at any particular moment. About each owner we need to know the address, and country, and name.

A maker (creator of artwork) can be an individual, a company, or an atelier. The database needs to be able to store the maker’s first and last name, nationality, birth year and death year, and the atelier or company name (if known). Bridgman records a single maker for each artwork.

For each artwork, we store the artwork number by each maker, the type of artwork, the title, the artwork’s dimensions – height, width, and whether it is available for copyright reproduction.

Customers can be individuals or companies. We need to know contact information for each customer – first and last name, company name (if relevant), postal address, city, country, email address, and telephone numbers. At any given time, multiple customers may have proposal permission for a particular artwork.

60 of 147

Entities

Bridgman Art Library manages copyright of artworks. Customer organisations that wish to use a copyrighted image can pay a fee to Bridgman Art Library, who will then provide a high-quality image for reproduction.

Customers must register a proposal for the use of the artwork, containing information about the intended use, intended market and intended duration of use. The customer also nominates the price they are willing to pay for their proposed use. The date the proposal was made is also stored.

Bridgman Art Library needs to be able to identify who has owned the artwork at any point in time since the artwork’s creation. Each artwork may have been owned by many owners over time but can only have one owner at any particular moment. About each owner we need to know the address, and country, and name.

A maker (creator of artwork) can be an individual, a company, or an atelier. The database needs to be able to store the maker’s first and last name, nationality, birth year and death year, and the atelier or company name (if known). Bridgman records a single maker for each artwork.

For each artwork, we store the artwork number by each maker, the type of artwork, the title, the artwork’s dimensions – height, width, and whether it is available for copyright reproduction.

Customers can be individuals or companies. We need to know contact information for each customer – first and last name, company name (if relevant), postal address, city, country, email address, and telephone numbers. At any given time, multiple customers may have proposal permission for a particular artwork.

61 of 147

Entities

Bridgman Art Library manages copyright of artworks. Customer organisations that wish to use a copyrighted image can pay a fee to Bridgman Art Library, who will then provide a high-quality image for reproduction.

Customers must register a proposal for the use of the artwork, containing information about the intended use, intended market and intended duration of use. The customer also nominates the price they are willing to pay for their proposed use. The date the proposal was made is also stored.

Bridgman Art Library needs to be able to identify who has owned the artwork at any point in time since the artwork’s creation. Each artwork may have been owned by many owners over time but can only have one owner at any particular moment. About each owner we need to know the address, and country, and name.

A maker (creator of artwork) can be an individual, a company, or an atelier. The database needs to be able to store the maker’s first and last name, nationality, birth year and death year, and the atelier or company name (if known). Bridgman records a single maker for each artwork.

For each artwork, we store the artwork number by each maker, the type of artwork, the title, the artwork’s dimensions – height, width, and whether it is available for copyright reproduction.

Customers can be individuals or companies. We need to know contact information for each customer – first and last name, company name (if relevant), postal address, city, country, email address, and telephone numbers. At any given time, multiple customers may have proposal permission for a particular artwork.

62 of 147

Weak Entities

Bridgman Art Library manages copyright of artworks. Customer organisations that wish to use a copyrighted image can pay a fee to Bridgman Art Library, who will then provide a high-quality image for reproduction.

Customers must register a proposal for the use of the artwork, containing information about the intended use, intended market and intended duration of use. The customer also nominates the price they are willing to pay for their proposed use. The date the proposal was made is also stored.

Bridgman Art Library needs to be able to identify who has owned the artwork at any point in time since the artwork’s creation. Each artwork may have been owned by many owners over time but can only have one owner at any particular moment. About each owner we need to know the address, and country, and name.

A maker (creator of artwork) can be an individual, a company, or an atelier. The database needs to be able to store the maker’s first and last name, nationality, birth year and death year, and the atelier or company name (if known). Bridgman records a single maker for each artwork.

For each artwork, we store the artwork number by each maker, the type of artwork, the title, the artwork’s dimensions – height, width, and whether it is available for copyright reproduction.

Customers can be individuals or companies. We need to know contact information for each customer – first and last name, company name (if relevant), postal address, city, country, email address, and telephone numbers. At any given time, multiple customers may have proposal permission for a particular artwork.

63 of 147

Entities

Customer

Proposal

Artwork

Owner

Maker

64 of 147

Relationships & Business rules

Bridgman Art Library manages copyright of artworks. Customer organisations that wish to use a copyrighted image can pay a fee to Bridgman Art Library, who will then provide a high-quality image for reproduction.

Customers must register a proposal for the use of the artwork, containing information about the intended use, intended market and intended duration of use. The customer also nominates the price they are willing to pay for their proposed use. The date the proposal was made is also stored.

Bridgman Art Library needs to be able to identify who has owned the artwork at any point in time since the artwork’s creation. Each artwork may have been owned by many owners over time but can only have one owner at any particular moment. About each owner we need to know the address, and country, and name.

A maker (creator of artwork) can be an individual, a company, or an atelier. The database needs to be able to store the maker’s first and last name, nationality, birth year and death year, and the atelier or company name (if known). Bridgman records a single maker for each artwork.

For each artwork, we store the artwork number by each maker, the type of artwork, the title, the artwork’s dimensions – height, width, and whether it is available for copyright reproduction.

Customers can be individuals or companies. We need to know contact information for each customer – first and last name, company name (if relevant), postal address, city, country, email address, and telephone numbers. At any given time, multiple customers may have proposal permission for a particular artwork.

65 of 147

Relationships & Constraints

Customer

Proposal

Artwork

Owner

Maker

makes

for

owns

by

66 of 147

Attributes

Bridgman Art Library manages copyright of artworks. Customer organisations that wish to use a copyrighted image can pay a fee to Bridgman Art Library, who will then provide a high-quality image for reproduction.

Customers must register a proposal for the use of the artwork, containing information about the intended use, intended market and intended duration of use. The customer also nominates the price they are willing to pay for their proposed use. The date the proposal was made is also stored.

Bridgman Art Library needs to be able to identify who has owned the artwork at any point in time since the artwork’s creation. Each artwork may have been owned by many owners over time but can only have one owner at any particular moment. About each owner we need to know the address, and country, and name.

A maker (creator of artwork) can be an individual, a company, or an atelier. The database needs to be able to store the maker’s first and last name, nationality, birth year and death year, and the atelier or company name (if known). Bridgman records a single maker for each artwork.

For each artwork, we store the artwork number by each maker, the type of artwork, the title, the artwork’s dimensions – height, width, and whether it is available for copyright reproduction.

Customers can be individuals or companies. We need to know contact information for each customer – first and last name, company name (if relevant), postal address, city, country, email address, and telephone numbers. At any given time, multiple customers may have proposal permission for a particular artwork.

67 of 147

Attributes

Customer

Proposal

Artwork

Owner

Maker

makes

for

owns

by

Address

Name

Country

ID

Start date

End date

First name

Last name

email

Address

City

Country

Phone no.

ID

Intended use

Intended market

Use duration

Price

Date

ID

First name

Last name

type

nationality

Atelier / company name

Birth date

Birth year

ID

number

type

title

reproducible?

dimension

height

width

68 of 147

69 of 147

Relational Algebra

70 of 147

Summary

  1. Selection (σ, sigma): Selects a subset of rows from relation (horizontal filtering).
  2. Projection (π, pi): Retains only wanted columns from relation (vertical filtering).
  3. Union (∪): Tuples in one relation and/or in the other.
  4. Set-difference (–): Tuples in one relation, but not in the other.
  5. Cross-product (x): Allows us to combine two relations.

71 of 147

The following tables are part of a database for a role-playing game:

player (playerid, playername, experience)

playeritem ( FK playerid , FK itemid , quantity)

item (itemid, itemname, value, weight, colour

List the names and values of items bought by players with experience level “Adventurer” who are not named “colton”.

72 of 147

The following tables are part of a database for a role-playing game:

player (playerid, playername, experience)

playeritem ( FK playerid , FK itemid , quantity)

item (itemid, itemname, value, weight, colour

List the names and values of items bought by players with experience level “Adventurer” who are not named “colton”.

πitemname, valueexperience = 'Adventurer' ∧ name != 'colton' (player ⋈ playeritem ⋈ item))

73 of 147

The following tables are part of a database for a role-playing game:

player (playerid, playername, experience)

playeritem ( FK playerid , FK itemid , quantity)

item (itemid, itemname, value, weight, colour

Find names which are shared by at least one player and at least one item.

74 of 147

The following tables are part of a database for a role-playing game:

player (playerid, playername, experience)

playeritem ( FK playerid , FK itemid , quantity)

item (itemid, itemname, value, weight, colour

Find names which are shared by at least one player and at least one item.

πplayername(player) ∩ πitemname (item)

75 of 147

SQL

76 of 147

SQL Questions

  • It’s very likely that there will be SQL questions on the exam related to the A2 case study, this is been the case for most past exams

  • Revise the A2 case study!
    • Redoing it is a great idea!

  • Can try making up your own questions as well

77 of 147

Keywords

SELECT colA, …

    • DISTINCT - This removes any duplicates in the result
    • May use aggregation functions here e.g. COUNT(), SUM(), AVG(), MIN(), MAX()

FROM TableA …

    • Can join with other tables here

WHERE … AND … OR

    • Ensures the rows returned satisfy these conditions

GROUP BY

    • Groups rows together
    • e.g. GROUP BY forum.id would group rows so that all rows with the same forum.id get combined in some way to one row. Aggregation functions can be used here.

HAVING

    • Similar to the WHERE clause, but here we are applying a condition on each group formed in the GROUP BY

ORDER BY … ASC / DESC

    • Sorts the results by certain attribute(s)

LIMIT …

    • Limit so that not all rows are returned e.g. LIMIT 1 returns the first row

78 of 147

Types of Joins

  • NATURAL JOIN
    • Attributes being joined on must have the same name
  • INNER JOIN
    • Specify the condition being joined on
    • E.g. TableA INNER JOIN TableB ON TableA.id = TableB.id
  • Outer Joins
    • Includes records which do not have a match with a record in the other table
    • Gets merged with NULLs�
    • LEFT OUTER JOIN - Includes all records from the left table, even if there’s no matches
    • RIGHT OUTER JOIN - Includes all records from the right table, even if there’s no matches
    • FULL OUTER JOIN - Includes all the matches as well all the unmatched records from both tables

79 of 147

Subqueries & Set operations

  • Using subqueries (nested queries) are very useful!

Sub-query operators

  • IN / NOT IN
    • can check if an attribute is in / not in a subquery e.g. WHERE userID IN (SELECT user FROM …)
  • ANY
    • True if any value returned meets the condition (OR) e.g. WHERE userID = ANY(1, 8, 11)
  • ALL
    • True if all values returned meet the condition (AND) e.g. WHERE userID = ALL(1, 8, 11)
  • EXISTS
    • True if the subquery returns one or more rows e.g. WHERE EXISTS(SELECT user FROM …)

�Set operations (which are supported on MySQL)

  • UNION - Returns all records from both inputs
  • UNION ALL - Use ALL if you want duplicate rows to be shown

80 of 147

Unary Joins, Aliases

From Tutorial 5:

Find the employees whose salary is less than half that of their managers.

This requires a unary join. Use the ‘AS’ keyword as an alias for the table

SELECT Emp.EmployeeName

FROM Employee AS Emp

INNER JOIN Employee AS Boss

ON Emp.BossID = Boss.EmployeeID

WHERE Emp.EmployeeSalary < (Boss.EmployeeSalary / 2);

81 of 147

The golf case study

Every year, golf clubs and sporting organisations around Victoria run a series of tournaments for junior (under-18) golfers. Golf Victoria, the peak body for the sport in Victoria, has decided to implement a central database to record participants and scores in all these junior tournaments, to help them award prizes to deserving players at the end of the season, and also to track participation levels in competitive junior golf for strategic planning purposes

If you’re not familiar with golf, here’s what you need to know:

  • A golf course is made up of 9 or 18 holes. To play a round of golf, you need to successfully complete every hole on the course.
  • To complete a hole, you need to hit the ball into the hole using the fewest “strokes” (number of hits) you can. Each hole has a “par”, which is the typical number of strokes most people should take to complete the hole.
  • Your “score” for a hole is worked out as your number of strokes minus that hole’s par. For example, if you complete a par 3 hole using two strokes, your score is –1 (read as “1 under par”). Lower scores are better in golf.

In these questions, we assume that tournaments only have one round, and that no tournaments carry on over the New Year. Write a single SQL statement for each question – subqueries are allowed from question 21 onwards.

82 of 147

Write a query to return the names of tournaments, played at courses in the suburb of Heatherton, where Bella Sandbury ranked in the top ten.

83 of 147

Write a query to return the names of tournaments, played at courses in the suburb of Heatherton, where Bella Sandbury ranked in the top ten.

Hint: Use SELECT DISTINCT to ensure your answer doesn’t contain duplicate rows.

SELECT DISTINCT TournamentName

FROM Player NATURAL JOIN PlayerCompetes NATURAL JOIN Tournament

NATURAL JOIN Course

WHERE CourseSuburb = ‘Heatherton’

AND PlayerFirstName = ‘Bella’

AND PlayerLastName = ‘Sandbury’

AND Rank <= 10;

84 of 147

SQL Question 2

List the first and last names of players who have played in exactly the same set of tournaments as at least one other player.

85 of 147

Write a query to List the first and last names of players who have played in exactly the same set of tournaments as at least one other player.

SELECT DISTINCT P1.FirstName, P1.LastName

FROM Player AS P1, Player AS P2

WHERE P1.PlayerID <> P2.PlayerID

AND 1 NOT IN (SELECT COUNT(*)

FROM (SELECT TournamentID

FROM PlayerCompetes

WHERE PlayerID = P1.PlayerID

UNION ALL

SELECT TournamentID FROM PlayerCompetes

WHERE PlayerID = P2.PlayerID) AS a

GROUP BY TournamentID);

86 of 147

Query Optimisation

87 of 147

Remember this?

88 of 147

Tips

  1. Understand the formula in the cheatsheet. Memorizing != understanding
  2. Understanding = able to answer questions such as:
  3. What is 2 in 2*NPasses*NPages in the Sort Merge Join formula?
    • 2 -> One for reading and one for writing
  4. What is B in the Block-oriented NLJ?
    • B -> Buffer size, how many pages can fit in the memory
  5. What is 3 in the Hash Join formula?
    • 3 -> One for reading (to bring the pages into memory for hashing), one for writing(after hashing), one for reading again to join
  6. And so on

You should be able to answer all those kind of questions!!! They may not be directly examined but they are very important when you analyse a given pipeline!!!

89 of 147

Tips

3. Be careful for usability of indexes!

  • Hash index: No range query
  • Cluster index: Compare search key fields prefix and query predicates.

4. Reduction factor decides cost and result size:

  • Cost depends on the search key fields of the index and the query predicates, only consider the matched attributes.
  • Result Size depends on the query predicates

90 of 147

Consider the relations:

A (Aid, ...) – 2500 tuples, 10 tuples per page

B (Bid, FK Aid , ...) – 300 tuples, 50 tuples per page

C (Cid, FK Bid , ...) – 2000 tuples, 20 tuples per page

A clustered B+ tree index exists on the Aid column in relation A, with 100 index pages.

Assume that two passes are required to sort. For all join results, 10 tuples can be stored per page.

Calculate the cost of the following plan.

91 of 147

Number of resulting pages for B JOIN C

Cost of NLJ

Cost of accessing A

Cost of sorting B JOIN C

Cost of SMJ

Total cost

92 of 147

Query Optimisation solutions

93 of 147

ntuples

tup/pg

pages

Index | #vals | pages

Student

20,000

20

1,000

UC hash degreename | 40 | 50

SS

50,000

50

1,000

C B+ stuId | | 50

Subject

1,000

10

100

UC B+ level | 10 | 50

Stu X SS

???

100

???

SS X Sub

???

100

???

Index: 50 pages

Sorting 2 passes

C = clustered; UC = unclustered

Extract info from text

94 of 147

Steps

  1. Calculate selection cost + size
  2. Calculate 1st join cost + size
  3. Calculate 2nd join cost (don’t need size)

95 of 147

Steps

  1. Calculate selection cost + size
  2. Calculate 1st join cost + size
  3. Calculate 2nd join cost (don’t need size)

96 of 147

97 of 147

Steps

  1. Calculate selection cost + size
  2. Calculate 1st join cost + size
  3. Calculate 2nd join cost (don’t need size)

For step 2, what do we subtract in the formula?

CostSMJ = NPages(Sub selection) + NPages(SS)

+ 2* NPages(Sub selection)* num_passes(Sub selection)

+ 2* NPages(SS)* num_passes(SS)

98 of 147

99 of 147

100 of 147

Steps

  1. Calculate selection cost + size
  2. Calculate 1st join cost + size
  3. Calculate 2nd join cost (don’t need size)

101 of 147

Don’t forget to add up all the costs to get the total!

102 of 147

103 of 147

What’s the difference?

Index | #vals | pages

Student

UC hash degreename | 40 | 50

SS

C B+ stuId | | 50

Subject

UC B+ level | 10 | 50

104 of 147

Normalisation

105 of 147

Why do we normalise relations?

  • We want to reduce data duplication / redundancy (storing the same piece of data in many places)

  • Allow users to insert, delete and update rows without anomalies.
    • Insertion Anomaly
      • Cannot insert certain attributes without other attributes
    • Deletion Anomaly
      • Accidental loss of attributes due to the deletion of other attributes
    • Update Anomaly
      • Happens when one or more instances of duplicated data are updated but not all

  • Normalisation is the process of taking larger, denormalised tables and breaking them into smaller tables

106 of 147

How to normalise:

107 of 147

How to normalise:

To achieve 1NF

  • Remove cells which have multiple values inside of them
  • Remove repeating groups of attributes
    • Order-Item(Order#, Customer#, ((Item#, Desc, Qty))
    • These brackets denote there is a repeating group

To achieve 2NF

  • Remove partial dependencies
  • This is where a non-key attribute depends on a part of the PK (but not the whole PK)

To achieve 3NF

  • Remove transitive dependencies
  • This is where a non-key attributes depends on another non-key attribute

108 of 147

Normalise to 3NF

Consider the following relation:

MealsOrdered (OrderID, CustomerID, CustomerName, DishID, DishName, DishPrice)

It has the following functional dependencies:

OrderID → CustomerID, CustomerName

CustomerID → CustomerName

DishID → DishName, DishPrice

109 of 147

1NF

MealsOrdered (OrderID, CustomerID, CustomerName, DishID, DishName, DishPrice)

OrderID → CustomerID, CustomerName

CustomerID → CustomerName

DishID → DishName, DishPrice

As the relation is already in 1NF, because there are no repeating groups, we can directly check if it is in 2NF or not.

110 of 147

2NF

MealsOrdered (OrderID, CustomerID, CustomerName, DishID, DishName, DishPrice)

OrderID → CustomerID, CustomerName

CustomerID → CustomerName

DishID → DishName, DishPrice

The functional dependency OrderID → CustomerID, CustomerName shows that CustomerID and CustomerName is determined by part of the key. Similarly for DishID → DishName, DishPrice. Hence, the relation is not in 2NF. It can be decomposed as:

OrderDish (FK OrderID, FK DishID)

Order (OrderID, CustomerID, CustomerName)

Dish (DishID, DishName, DishPrice)

111 of 147

3NF

OrderDish (FK OrderID, FK DishID)

Order (OrderID, CustomerID, CustomerName)

Dish (DishID, DishName, DishPrice)

OrderID → CustomerID, CustomerName

CustomerID → CustomerName

DishID → DishName, DishPrice

We can observe that the FD CustomerID → CustomerName violates 3NF, as it is a transitive dependency. We further decompose this relation to:

OrderDish (FK OrderID, FK DishID)

Order (OrderID, FK CustomerID)

Dish (DishID, DishName, DishPrice)

Customer (CustomerID, CustomerName)

112 of 147

Normalise this table into 3NF

Normalise this StaffBranch table into 3NF

The dependencies are:

    • staffNo → name, position
    • position → salary
    • branchNo → branchAddress, telNos

A candidate key is (StaffNo, BranchNo)

113 of 147

Normalisation Solution

114 of 147

Normalisation Solution:

Normalise this StaffBranch table into 3NF

The dependencies are:

    • staffNo → name, position
    • position → salary
    • branchNo → branchAddress, telNos

A candidate key is (StaffNo, BranchNo)

Remember: We must check if ALL previous normal forms are satisfied before trying to put it into 3NF

Which NF is not satisfied?

What do we do to fix this?

115 of 147

Achieving 1NF

Original Table is StaffBranch(staffNo PK, name, position, salary, branchNo PK, branchAddress, telNos)

Need to make telNo part of the PK to be able to store multiple telNos for each branch!

So we get:

StaffBranch(staffNo PK, name, position, salary, branchNo ?, branchAddress)

?(branchNo ?, telNo ?)

After making the table, think about what might be a good name for this table?

Which attribute should we make the PK and/or which one the FK?

116 of 147

Achieving 1NF

Original Table is StaffBranch(staffNo PK, name, position, salary, branchNo PK, branchAddress, telNos)

Need to make telNo part of the PK to be able to store multiple telNos for each branch!

So we get:

StaffBranch(staffNo PK, name, position, salary, branchNo PK, branchAddress)

?(branchNo PFK, telNo PK)

After making the table, think about what might be a good name for this table?

StaffBranch(staffNo PK, name, position, salary, branchNo PK, branchAddress)

BranchTelephone(branchNo PFK, telNo PK)

Now, it is in 1NF

117 of 147

Is this in 3NF yet?

StaffBranch(staffNo PK, name, position, salary, branchNo PK, branchAddress)

BranchTelephone(branchNo PFK, telNo PK)

The dependencies are:

    • staffNo → name, position
    • position → salary
    • branchNo → branchAddress, telNos

A candidate key is (StaffNo, BranchNo)

118 of 147

Achieving 2NF

StaffBranch(staffNo PK, name, position, salary, branchNo PK, branchAddress)

BranchTelephone(branchNo PFK, telNo PK)

There are attributes that depend only on a PART of the PK (partial dependencies)

    • staffNo → name, position, salary
      • (by Armstrong’s Transitivity Axiom: A → B and B → C ⟹ A → C)

    • branchNo → branchAddress

How do we remove these partial dependencies to put it into 2NF?

staffNo → name, position

position → salary

branchNo → branchAddress, telNos

119 of 147

Achieving 2NF

StaffBranch(staffNo PK, name, position, salary, branchNo PK, branchAddress)

BranchTelephone(branchNo PFK, telNo PK)

There are attributes that depend only on a PART of the PK (partial dependencies)

    • staffNo → name, position, salary (by Armstrong’s Transitivity Axiom)
    • branchNo → branchAddress

How do we remove these partial dependencies to put it into 2NF?

staffNo → name, position

position → salary

branchNo → branchAddress, telNos

Need to decompose staff details and branch details into their own relations!

StaffBranch(staffNo PFK, branchNo PFK)

?(staffNo PK, name, position, salary)

? (branchNo PK, branchAddress)

BranchTelephone(branchNo PFK, telNo PK)

Remember: We still need to keep the original table StaffBranch so that we still have information which links staff and branch (we still want to know which staff work at which branch)

What should we name relations?

120 of 147

Are we in 3NF yet?

StaffBranch(staffNo PFK, branchNo PFK)

Staff(staffNo PK, name, position, salary)

Branch (branchNo PK, branchAddress)

BranchTelephone(branchNo PFK, telNo PK)

Now all the relations are in 2NF

Are all the relations in 3NF? Why or why not?

The dependencies are:

    • staffNo → name, position
    • position → salary
    • branchNo → branchAddress, telNos

A candidate key is (StaffNo, BranchNo)

121 of 147

Are we in 3NF yet?

StaffBranch(staffNo PFK, branchNo PFK)

Staff(staffNo PK, name, position, salary)

Branch (branchNo PK, branchAddress)

BranchTelephone(branchNo PFK, telNo PK)

Now all the relations are in 2NF

Are all the relations in 3NF? Why or why not?

The dependencies are:

    • staffNo → name, position
    • position → salary
    • branchNo → branchAddress, telNos

A candidate key is (StaffNo, BranchNo)

Nope! We’ve got a transitive dependency in the Staff table.

A non-key attribute determines another non-key attribute (position → salary)

Need to separate this out into its own relation

Staff(staffNo PK, name, position ?)

? (position ?, salary)

What should we name the table? Where do we put the FK and PKs?

122 of 147

Achieving 3NF

StaffBranch(staffNo PFK, branchNo PFK)

Staff(staffNo PK, name, position FK)

Position(position PK, salary)

Branch (branchNo PK, branchAddress)

BranchTelephone(branchNo PFK, telNo PK)

Are we done yet?

Are all the relations in 3NF?

The dependencies are:

    • staffNo → name, position
    • position → salary
    • branchNo → branchAddress, telNos

A candidate key is (StaffNo, BranchNo)

123 of 147

We’ve now achieved 3NF!

StaffBranch(staffNo PFK, branchNo PFK)

Staff(staffNo PK, name, position FK)

Position(position PK, salary)

Branch (branchNo PK, branchAddress)

BranchTelephone(branchNo PFK, telNo PK)

124 of 147

Transactions

125 of 147

DB Transactions + ACID

  • We want to have certain guarantees regarding the state of the database before/while/after performing a series of SQL CRUD operations + Schema manipulations
    • PARTICULARLY important when multiple users are accessing the database at the same time, trying to read/write the same data all at the same time!
  • Transactions are tools that allow us to consider related groups of commands as being tied together
  • In relational databases, ACID is generally king. We want our transactions to be:
    • Atomic
    • Consistent
    • Isolated (there are many levels, we consider ‘serializable’, the default/strongest/safest level)
    • Durable

126 of 147

(for interest only)

127 of 147

Transactions

128 of 147

Transactions

129 of 147

Transactions

Alice’s Transaction

Bob’s Transaction

Time

Alice’s Transaction

Bob’s Transaction

OR

Correct

(no issue with Inconsistent Retrieval problem)

Incorrect

(Inconsistent Retrieval)

Alice’s Transaction

Bob’s Transaction

130 of 147

Transactions

Question:

A DBMS which strictly implements ACID compliant transactions can never encounter the ‘Inconsistent retrieval problem’.

Which ACID property is primarily responsible for preventing this problem? Explain how it prevents the inconsistent retrieval problem.

131 of 147

Transactions solutions

132 of 147

Transactions

A:

C:

I:

D:

133 of 147

Transactions

Solution:

Isolation

Isolation guarantees that concurrent execution of transactions leaves the database in the same state as if the transactions were executed sequentially (one after another)

A: Atomic

C: Consistent

I: Isolated

D: Durable

134 of 147

Distributed DBs

135 of 147

Distributed DBs PRO/CON

  • Distributed DBs are needed when you get to a certain scale, or you need global availability
  • PRO:
    • Scalability
    • Availability + Reliability
    • Better response time for localised data
  • CON:
    • More complex (cost, security issues, etc)
    • CAP tradeoffs

136 of 147

Distributed DBs types

  • Types of distributed DBs that we discussed:
    • Data replication (ie full replication)
    • Horizontal partitioning
    • Vertical partitioning
    • Combinations (most common)

137 of 147

Question:

As business grew over time for hiTech Computer Repairs Inc, the company has opened multiple new stores in Australia, and also recently spread to Europe. The management at hiTech Headquarters is interested in upgrading the database associated with the `PC repair services` booking system from centralised to a distributed structure. HiTech repair centers in Australia are unlikely to regularly need access to repair records for European customers, and vice versa.

Which of the following type of distributed database would be best suited for this case: a) Vertically partitioned b) Horizontally Partitioned c) Fully Replicated DB

138 of 147

Distributed Solutions

139 of 147

Answer:

Horizontally Partitioned.

  • Same type of data being accessed (ie from same table) for the ‘service repairs’, BUT:
  • Rows that are for the ‘Australian’ stores are not likely to be used by the ‘European’ stores.
  • Split the European rows into a European server, and keep Australian rows on the Australian server

140 of 147

No SQL

141 of 147

NoSQL conceptually

  • Safe to think about NoSQL as being aggregate-esque
  • Instead of decomposing everything into tables in a way that requires joins to get meaningful data, we group the related data together ahead of time so it’s easier to get to

142 of 147

NoSQL Pros/cons

  • Often easier to work with data which is ‘BIG’ in some/many ways:
    • Velocity
    • Variety
    • Volume

  • In general are designed with BASE in mind: scalable horizontally and favour availability
    • Better for certain use cases… not as good for flexible data analysis

  • We spoke about a few types of NoSQL database:
    • Aggregate: Key/value, Document-store, Column-family
    • Graph

143 of 147

Question:

FaceTube™ is a new online worldwide social video sharing platform that you and your friend have started.

Your friend asks if you think a No-SQL based database for storing the metadata + comments for video uploads is a good idea.

Discuss some pros/cons for using a No-SQL database for this scenario. Your answer should make reference to the properties of the data being stored and BASE.

144 of 147

NoSQL solutions

145 of 147

Answer:

Yes, NoSQL is likely a good choice here

Pros:

  • High availability when partitioned (BA)
  • OK if not completely consistent (BA), as long as other users will eventually (e.g. in a few minutes) see new comments etc (S + E)
  • Can deal with large number of comments in rapid succession

Cons:

  • Harder to perform data analysis
  • Potentially takes up marginally more storage space

146 of 147

Questions & Answers

147 of 147

Feedback form + sign ups

Feedback form