COMP3311 Week 3 Tuesday Lecture

COMP3311 22T3 ♢ Week 3 Tuesday Lecture ♢ [0/14]
❖ Week 03 Tuesday

In today's lecture ...

Things to do ...

COMP3311 22T3 ♢ Week 3 Tuesday Lecture ♢ [1/14]
❖ Assignment 1


Beer Database, with info on

Goal:   write some SQL and PLpgSQL code to extract info

Due:   11:59pm Friday 17 March   (end week 5)

Supplied:   ass1.sql template, ass1.dump

Submit:   ass1.sql

COMP3311 22T3 ♢ Week 3 Tuesday Lecture ♢ [2/14]
❖ Know Your Product ...

Beer cans often have whimsical labels ...

[Diagram:Pics/misc/can1.jpg]

COMP3311 22T3 ♢ Week 3 Tuesday Lecture ♢ [3/14]
❖ Know Your Product ... (cont)

Sometimes they have scary, but relevant, labels ...

[Diagram:Pics/misc/can2.jpg]

COMP3311 22T3 ♢ Week 3 Tuesday Lecture ♢ [4/14]
❖ Know Your Product ... (cont)

Properties of beers

COMP3311 22T3 ♢ Week 3 Tuesday Lecture ♢ [5/14]
❖ Assignment 1

ER design for beer database:

[Diagram:Pics/assignments/beers.png]

COMP3311 22T3 ♢ Week 3 Tuesday Lecture ♢ [6/14]
❖ Assignment 1 (cont)

How to do the assignment

Need to complete views Q1-Q10 and functions Q11 and Q12

Can define as many axuiliary views/functions as you like

Must be included in ass1.sql, and ass1.sql must load in one pass

COMP3311 22T3 ♢ Week 3 Tuesday Lecture ♢ [7/14]
❖ Assignment 1 (cont)


How we test your submission ...

vxdb2$ dropdb ass1
vxdb2$ createdb ass1
vxdb2$ psql ass1 -f ass1.dump > /dev/null
vxdb2$ psql ass1 -f ass1.sql > .errs 2>&1
vxdb2$ grep ERR .errs
vxdb2$ psql ass1 -f check.sql
vxdb2$ psql ass1 -f run_tests.sql


ass1.sql  must load into a newly built DB without error

COMP3311 22T3 ♢ Week 3 Tuesday Lecture ♢ [8/14]
❖ Dump/Restore

Once a database is built, can make a complete copy in a text file

by running the command

$ pg_dump -O -x DatabaseName > DumpFileName

and can make a new copy via

$ createdb newdb
$ psql newdb -f DumpFileName

We often supply databases using pre-built dump files

COMP3311 22T3 ♢ Week 3 Tuesday Lecture ♢ [9/14]
psql Recap

psql is PostgreSQL's shell   (cf. bash)

Allows you to execute ...

SQL commands: terminated by ;, can extend over multiple lines

Prompt changes

COMP3311 22T3 ♢ Week 3 Tuesday Lecture ♢ [10/14]
❖ Exercise: Exploring the Beer Database


Explore the ass1 database using psql

COMP3311 22T3 ♢ Week 3 Tuesday Lecture ♢ [11/14]
❖ Manipulating Tuples

Three basic mechanisms:

Examples of using  insert

insert into Student(id,name,degree)
       values (5654321,'John',3778);
insert into Student(name,degree)
       values ('John',3778) returning id;
insert into Student(id,name,degree)
       values (default,'John',3778);

COMP3311 22T3 ♢ Week 3 Tuesday Lecture ♢ [12/14]
❖ Modifying a database

SQL commands for changing data in a database

insert into Table values ( Val1, Val2, ... )
delete from Table where Condition
update Table set Field = Value where Condition
create table Table ( Attributes/Constraints )
drop table Table
copy Table ...   -- PostgreSQL specific

PostgreSQL commands for manipulating databases

createdb DatabaseName
dropdb DatabaseName
pg_dump DatabaseName > DumpFile
psql DatabaseName -f DumpFile

COMP3311 22T3 ♢ Week 3 Tuesday Lecture ♢ [13/14]
❖ Exercise: Delete/Update Examples


Carry out the following actions on the beer database:

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


Produced: 2 Mar 2023