COMP3311 Week 2 Tuesday Lecture

COMP3311 23T1 ♢ Week 2 Tuesday Lecture ♢ [0/25]
❖ Week 02 Tuesday

In today's lecture ...

Things to do ...

COMP3311 23T1 ♢ Week 2 Tuesday Lecture ♢ [1/25]
❖ Recap


ER → Relational/SQL Mapping

COMP3311 23T1 ♢ Week 2 Tuesday Lecture ♢ [2/25]
❖ Mapping Subclasses

Three different approaches to mapping subclasses to tables:

Which mapping is best depends on how data is to be used.
COMP3311 23T1 ♢ Week 2 Tuesday Lecture ♢ [3/25]
❖ Mapping Subclasses (cont)

Example of ER-style mapping:

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

COMP3311 23T1 ♢ Week 2 Tuesday Lecture ♢ [4/25]
❖ Mapping Subclasses (cont)

Example of object-oriented mapping:

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

COMP3311 23T1 ♢ Week 2 Tuesday Lecture ♢ [5/25]
❖ Mapping Subclasses (cont)

Example of single-table-with-nulls mapping:

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

COMP3311 23T1 ♢ Week 2 Tuesday Lecture ♢ [6/25]
❖ Converting ER to SQL


General strategies:

COMP3311 23T1 ♢ Week 2 Tuesday Lecture ♢ [7/25]
❖ Exercise: ER-to-SQL (1)

Convert the following class hierarchy to SQL using ER mapping:

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

COMP3311 23T1 ♢ Week 2 Tuesday Lecture ♢ [8/25]
❖ Exercise: ER-to-SQL (2)

Convert this ER design to SQL:

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

COMP3311 23T1 ♢ Week 2 Tuesday Lecture ♢ [9/25]
❖ Exercise: ER-to-SQL (3)

Convert the Publishing ER model to SQL

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

COMP3311 23T1 ♢ Week 2 Tuesday Lecture ♢ [10/25]
❖ 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

COMP3311 23T1 ♢ Week 2 Tuesday Lecture ♢ [11/25]
psql

The psql command is a shell that allows you to

Usage:

$ psql  DatabaseName

COMP3311 23T1 ♢ Week 2 Tuesday Lecture ♢ [12/25]
psql (cont)

psql's normal behaviour

$ psql mydb
...
mydb=# command1
result of command1
mydb=# command2
result of command2
mydb=# command3
result of command3
...
mydb=# \q
$

COMP3311 23T1 ♢ Week 2 Tuesday Lecture ♢ [13/25]
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 23T1 ♢ Week 2 Tuesday Lecture ♢ [14/25]
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 23T1 ♢ Week 2 Tuesday Lecture ♢ [15/25]
psql (cont)


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

COMP3311 23T1 ♢ Week 2 Tuesday Lecture ♢ [16/25]
❖ Exercise: Creating a database

On vxdb2, do the following:

COMP3311 23T1 ♢ Week 2 Tuesday Lecture ♢ [17/25]
❖ 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 23T1 ♢ Week 2 Tuesday Lecture ♢ [18/25]
❖ 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 23T1 ♢ Week 2 Tuesday Lecture ♢ [19/25]
❖ 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 23T1 ♢ Week 2 Tuesday Lecture ♢ [20/25]
❖ 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 23T1 ♢ Week 2 Tuesday Lecture ♢ [21/25]
❖ Bulk Insertion (cont)

A common way of building a database

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

where

COMP3311 23T1 ♢ Week 2 Tuesday Lecture ♢ [22/25]
❖ Bulk Insertion (cont)

How I "debug" a database

$ 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 23T1 ♢ Week 2 Tuesday Lecture ♢ [23/25]
❖ 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 23T1 ♢ Week 2 Tuesday Lecture ♢ [24/25]
❖ Exercise: Playing with Beer Database

Load up the database from  schema.sql  and  data.sql

Fix any errors that appear

Once loaded correctly, find out ...

COMP3311 23T1 ♢ Week 2 Tuesday Lecture ♢ [25/25]


Produced: 23 Feb 2023