COMP3311 Week 7 Tuesday Lecture

COMP3311 22T3 ♢ Week 7 Tuesday Lecture ♢ [0/14]
❖ Week 07 Tuesday


In today's lecture ...

Things to do ...

COMP3311 22T3 ♢ Week 7 Tuesday Lecture ♢ [1/14]
❖ Rest of Term


Plan for remainder of term:

Week 7  Python, Psycopg2, PostgreSQL
Week 7/8  Relational Design, Normalization
Week 8/9  Relational Algebra, Query Execution
Week 9/10  Transactions, Concurrency Control
COMP3311 22T3 ♢ Week 7 Tuesday Lecture ♢ [2/14]
❖ Assignment 2 Database


Database about Pokemon ("pocket monsters")

The database holds general Pokemon properties Database schema info: assignments/ass2/schema.php
COMP3311 22T3 ♢ Week 7 Tuesday Lecture ♢ [3/14]
❖ Assignment 2 Database (cont)


How Dylan built the database

Database dump is ~23MB

Database under pgsql/data/base directory is ~180MB

Your quota on /localstorage is not infinite;  manage space carefully

COMP3311 22T3 ♢ Week 7 Tuesday Lecture ♢ [4/14]
❖ Assignment 2 Database (cont)


What you get (as templates) and what you submit:

Put these under your VLab directory, not under /localstorage

Submit via give or Webcms3

COMP3311 22T3 ♢ Week 7 Tuesday Lecture ♢ [5/14]
❖ Keep in mind for Assignment 2


If 100's of people are finishing the assignment last minute ...



Beware:  /localstorage  is not backed up ... put only your DB there
COMP3311 22T3 ♢ Week 7 Tuesday Lecture ♢ [6/14]
❖ Psycopg2 recap

Psycopg2 is a library for Python ↔ PostgreSQL interaction.

Where psycopg2 fits in the PL/DB architecture


[Diagram:Pics/pldb/pypsy-arch.png]

COMP3311 22T3 ♢ Week 7 Tuesday Lecture ♢ [7/14]
❖ Psycopg2 recap (cont)

Common psycopg2 operations:

COMP3311 22T3 ♢ Week 7 Tuesday Lecture ♢ [8/14]
❖ Psycopg2 recap (cont)

cur.execute(SQL_statement, Values)

Examples:

# run a fixed query
cur.execute("select * from R where x = 1");

# run a query with values inserted
cur.execute("select * from R where x = %s", (1,))
cur.execute("select * from R where x = %s", [1])

# run a query stored in a variable
query = "select * from Students where name ilike %s"
pattern = "%mith%"
cur.execute(query, [pattern])

COMP3311 22T3 ♢ Week 7 Tuesday Lecture ♢ [9/14]
❖ Psycopg2 recap (cont)

Typical database access pattern in Psycopg2

qry = 'select a,b,c from R where d = %s'
try:
   db = psycopg2.connect('dbname=mydb')
   cur = db.cursor()
   cur.execute(qry, [6])
   for tup in cur.fetchall():
      x,y,z = tup  # extract a,b,c values into x,y,z
      ... do something with x, y, z ...

except Exception as err:
  print("DB error: ", err)

finally:
  if db:
    db.close()

COMP3311 22T3 ♢ Week 7 Tuesday Lecture ♢ [10/14]
❖ Example: University Database

Consider the  uni  database ...

People(id, family, given, fullname, birthday, origin)

Students(id)

Subjects(id, code, name, uoc, offeredby, ...)
Courses(id, subject, term, homepage)
Streams(id, code, name, offeredby, stype, ...)
Programs(id, code, name, uoc, offeredby, ...)

Terms(id, year, ttype, code, name, starting, ending)

Course_enrolments(student, course, mark, grade)
Stream_enrolments(part_of_prog_enr, stream)
Program_enrolments(id, student, term, program, ...))

COMP3311 22T3 ♢ Week 7 Tuesday Lecture ♢ [11/14]
❖ Exercise: Course and Student Info


Write a Python script that, given a partial student name

Usage examples:  ./stu 'smith',     ./stu 'nobody'


Write a Python script that, given a term code

Usage examples:  ./enrs 17s2,     ./enrs 19T1
COMP3311 22T3 ♢ Week 7 Tuesday Lecture ♢ [12/14]
❖ Exercise: Transcripts on uni database

Goal: print a transcript for a given student

Method:

collect and display info about student
query to collect (subj,term,title,mark,grade,uoc)
foreach (subj,term,title,mark,grade,uoc) tuple
  display basic enrolment info (subj,term,title)
  depending on mark/grade,
    display mark and grade and UOC
    increment two UOCs, accumulate weighted sum
 print UOC_awarded, WAM

COMP3311 22T3 ♢ Week 7 Tuesday Lecture ♢ [13/14]
❖ Relational Design Theory


We know how to express ER data models and SQL schemas

But how do we know that our models/schemas are "good"?

Properties of "good" models/schemas

Relational design theory addresses the last issue

Relies on the notion of functional dependency

COMP3311 22T3 ♢ Week 7 Tuesday Lecture ♢ [14/14]


Produced: 28 Mar 2023