❖ Week 07 Tuesday |
ass2
❖ 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 |
❖ Assignment 2 Database |
Database about Pokemon ("pocket monsters")
❖ Assignment 2 Database (cont) |
How Dylan built the database
createdb
psql -f
\copy
Database under pgsql/data/base
Your quota on /localstorage
❖ Assignment 2 Database (cont) |
What you get (as templates) and what you submit:
helpers.sql
helpers.py
/localstorage
Submit via give
❖ Keep in mind for Assignment 2 |
If 100's of people are finishing the assignment last minute ...
(it takes much longer to get your question answered)
vxdb2
(it runs s-l-o-w; queries take much longer to run)
/localstorage
❖ Psycopg2 recap |
Psycopg2
Where psycopg2
❖ Psycopg2 recap (cont) |
Common psycopg2
conn = psycopg2.connect(
)
conn.close()
conn.commit()
cur = conn.cursor()
cur.execute(
,
)
cur.mogrify(
,
)
list = cur.fetchall()
tup = cur.fetchone()
list = cur.fetchmany(
)
❖ Psycopg2 recap (cont) |
cur.execute(
,
)
# 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])
❖ 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()
❖ Example: University Database |
Consider the uni
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, ...))
❖ Exercise: Course and Student Info |
Write a Python script that, given a partial student name
./stu 'smith'
./stu 'nobody'
Write a Python script that, given a term code
./enrs 17s2
./enrs 19T1
❖ Exercise: Transcripts on uni |
Goal: print a transcript for a given student
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
❖ 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
Relies on the notion of functional dependency
Produced: 28 Mar 2023