COMP3311 Week 2 Wednesday Lecture

COMP3311 23T3 ♢ Week 2 Wednesday Lecture ♢ [0/29]
❖ Week 02 Wednesday

In today's lecture ...

Things to do ...

COMP3311 23T3 ♢ Week 2 Wednesday Lecture ♢ [1/29]
❖ Assignment 1 Database

Database about beer and breweries

[Diagram:Pics/assignments/beer-schema.png]

COMP3311 23T3 ♢ Week 2 Wednesday Lecture ♢ [2/29]
❖ Assignment 1 Database (cont)

Details of entities, with example data (does not include all entities):

Beers(id, name, brewed, style, abv, ibu, sold_in, volume, notes, rating)

(123, 'VB', 2020, *Lager, 5.0, 30, can, 375, 'Worst beer in world', 1)

Brewers(id, name, founded, website, located_in)

(321, 'Carlton', 1899, 'www.carlton.com.au', *Melbourne)

Styles(id, name, min_abv, max_abv)

(456, 'Lager', 4.0, 6.0)

Ingredients(id, itype, name)

(654, 'hop', 'Cascade')

Brewed_by(beer, brewery)

(*VB, *Carlton) ... represented as (123, 321)

COMP3311 23T3 ♢ Week 2 Wednesday Lecture ♢ [3/29]
❖ Recap


ER → Relational/SQL Mapping

COMP3311 23T3 ♢ Week 2 Wednesday Lecture ♢ [4/29]
❖ Exercise: ER-to-SQL for Beer Database

Convert the beer ER data model to an SQL schema

[Diagram:Pics/assignments/beer-schema-small.png]

COMP3311 23T3 ♢ Week 2 Wednesday Lecture ♢ [5/29]
❖ Mapping Composite Attributes

Composite attributes are mapped by concatenation or flattening.

Example:

[Diagram:Pics/er-rel/mapstrent.png]

COMP3311 23T3 ♢ Week 2 Wednesday Lecture ♢ [6/29]
❖ Mapping Multi-valued Attributes (MVAs)

MVAs are mapped by a new table linking values to their entity.

Example:

[Diagram:Pics/er-rel/mapmva.png]

COMP3311 23T3 ♢ Week 2 Wednesday Lecture ♢ [7/29]
❖ Mapping Subclasses

Three different approaches to mapping subclasses to tables:

Which mapping is best depends on how data is to be used.
COMP3311 23T3 ♢ Week 2 Wednesday Lecture ♢ [8/29]
❖ Mapping Subclasses (cont)

Example of ER-style mapping:

[Diagram:Pics/er-rel/mapsubclass.png]

COMP3311 23T3 ♢ Week 2 Wednesday Lecture ♢ [9/29]
❖ Mapping Subclasses (cont)

Example of object-oriented mapping:

[Diagram:Pics/er-rel/mapsubclass2.png]

COMP3311 23T3 ♢ Week 2 Wednesday Lecture ♢ [10/29]
❖ Mapping Subclasses (cont)

Example of single-table-with-nulls mapping:

[Diagram:Pics/er-rel/mapsubclass3.png]

COMP3311 23T3 ♢ Week 2 Wednesday Lecture ♢ [11/29]
❖ Exercise: ER-to-SQL (1)

Convert the following class hierarchy to SQL using ER mapping:

[Diagram:Pics/er-sql/map-disjoint.png]

COMP3311 23T3 ♢ Week 2 Wednesday Lecture ♢ [12/29]
❖ Exercise: ER-to-SQL (2)

Convert this ER design to SQL:

[Diagram:Pics/er-rel/emp-dept-proj.png]

COMP3311 23T3 ♢ Week 2 Wednesday Lecture ♢ [13/29]
❖ Exercise: ER-to-SQL (3)

Convert the Publishing ER model to SQL

[Diagram:Pics/er-rel/publishing.png]

COMP3311 23T3 ♢ Week 2 Wednesday Lecture ♢ [14/29]
❖ PostgreSQL Databases

Create a database in PostgreSQL via

$ createdb  DatabaseName

Creates an empty database (no schema, no data)

Remove a database in PostgreSQL via

$ dropdb  DatabaseName

Removes schema and all data permanently

Remove an entire PostgreSQL server (on vxdb2)

$ rm -fr /localstorage/$USER/pgsql

Removes all server files, all databases, all data !

COMP3311 23T3 ♢ Week 2 Wednesday Lecture ♢ [15/29]
psql

The psql command is a shell that allows you to

Usage:

$ psql  mydb
...
mydb=# \d
...
mydb=$ select * from SomeTable;
...
mydb=# \q

COMP3311 23T3 ♢ Week 2 Wednesday Lecture ♢ [16/29]
psql (cont)


A useful way to use   psql:

$ psql -l

Gives a list of all databases under your PostgreSQL server.

The "databases"   postgres,  template1,   template2

COMP3311 23T3 ♢ Week 2 Wednesday Lecture ♢ [17/29]
psql (cont)

The psql command has several prompts

Note that db will be (replaced by) the name of the current database

Note that # means you are super-user; normal users get >

SQL statements can span several lines, terminated by typing ;

psql meta-commands are single-line commands

COMP3311 23T3 ♢ Week 2 Wednesday Lecture ♢ [18/29]
psql (cont)


psql has a range of meta-commands, beginning with \

COMP3311 23T3 ♢ Week 2 Wednesday Lecture ♢ [19/29]
❖ Exercise: Creating a database

On vxdb2, do the following:

COMP3311 23T3 ♢ Week 2 Wednesday Lecture ♢ [20/29]
❖ Populating a Database

Basic way of adding tuples to a database:

db=# insert into Table values (val1,val2,...);

Adds a tuple to table Table assuming

Ways it can fail ...
COMP3311 23T3 ♢ Week 2 Wednesday Lecture ♢ [21/29]
❖ Exercise: Inserting Tuples

Which  insert  statements are successful?
If successful, what tuple value is inserted?

create type Mood as enum ('sad','happy');

create table People (
	name text not null,
	feels Mood
);

insert into People values ('John','happy');
insert into People values ('Andrew','angry');
insert into People values ('Tina',null);
insert into People(name) values ('Anne');
insert into People(feels) values ('happy');

COMP3311 23T3 ♢ Week 2 Wednesday Lecture ♢ [22/29]
❖ Exercise: More Inserting Tuples

Which  insert  statements are successful?
If successful, what tuple value is inserted?

create domain PosInt as integer check (value > 0);

create table Points (
	x PosInt default 1,
	y posint
);

insert into Points values (3,4);
insert into Points values (3,null);
insert into Points values (-3,4);
insert into Points(y) values (5);
insert into Points values (default,5);

COMP3311 23T3 ♢ Week 2 Wednesday Lecture ♢ [23/29]
❖ Bulk Insertion

Entering tuples interactively one-by-one is not feasible

Alternative: put  insert  statements in a file and run

$ psql DatabaseName -f FileName

Attempts to execute each insert statement:


Note that FileName can contain any SQL statements

Might consist only of  create table  statements to build a schema

COMP3311 23T3 ♢ Week 2 Wednesday Lecture ♢ [24/29]
❖ Bulk Insertion (cont)

A common way of building a database

$ createdb mydb
$ psql mydb -f schema.sql
$ psql mydb -f data.sql

where

COMP3311 23T3 ♢ Week 2 Wednesday Lecture ♢ [25/29]
❖ Bulk Insertion (cont)


How I "debug" a database schema

$ dropdb mydb
$ createdb mydb
$ psql mydb -f schema.sql > .errs 2>&1
$ vi .errs
  # fix any errors that appear in .errs
$ psql mydb -f data.sql > .errs 2>&1
$ vi .errs
  # fix any errors that appear in .errs

Repeat until .errs contains no lines with ERROR

COMP3311 23T3 ♢ Week 2 Wednesday Lecture ♢ [26/29]
❖ Bulk Insertion (cont)

Alternative way of inserting tuples

copy TableName ( attribute names ) from stdin;
... lines containing tab-separated values ...
... one value for each of the named attributes ...
\.

Difference between copy and multiple inserts

COMP3311 23T3 ♢ Week 2 Wednesday Lecture ♢ [27/29]
❖ 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 generally supply databases using pre-built dump files

COMP3311 23T3 ♢ Week 2 Wednesday Lecture ♢ [28/29]
❖ Exercise: Playing with Beer Database

Load up the database from  ass1.dump

Guess some SQL to answer the following:

COMP3311 23T3 ♢ Week 2 Wednesday Lecture ♢ [29/29]


Produced: 20 Sep 2023