COMP3311 Week 2 Monday Lecture

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

In today's lecture ...

Things to do ...

COMP3311 23T3 ♢ Week 2 Monday Lecture ♢ [1/23]
❖ Email and Context

When sending email to us (cs3311), please include your zID

Before typing commands, think about the context:

PromptContext
$in Linux shell
run Unix commands, e.g. ls, cd
db=#in psql
run SQL commands, e.g. select, update
db>in sqlite3
run SQL commands, e.g. select, update
nonein vim,   you are doomed
but try :q or ZZ

COMP3311 23T3 ♢ Week 2 Monday Lecture ♢ [2/23]
❖ n:m:p Relationships

Two possible ER models for "prescribes" in the medical scenario


[Diagram:Pics/er-rel/n-m-p.png]


Could be done as a 3-way relationship, or using a new entity

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

Entity-relationship data model

Relational data model

E.g.   Student(zID:integer, name:string, WAM:float, ...)
COMP3311 23T3 ♢ Week 2 Monday Lecture ♢ [4/23]
❖ Relational Data Model

A relational schema consists of

Different kinds of constraints
COMP3311 23T3 ♢ Week 2 Monday Lecture ♢ [5/23]
❖ Relational Data Model (cont)

Mapping an ER model to a relational schema

Example:

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

COMP3311 23T3 ♢ Week 2 Monday Lecture ♢ [6/23]
❖ Exercise: ER-to-relational Mapping


Describe this ER model as an (informal) relational schema


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

COMP3311 23T3 ♢ Week 2 Monday Lecture ♢ [7/23]
❖ Mapping ER to SQL

Above example gives informal description of relational schema

Need a more formal way of describing relational schemas

SQL data definition language (DDL) provides this

SQL is an implementation of relational data model

Mapping ER to SQL
COMP3311 23T3 ♢ Week 2 Monday Lecture ♢ [8/23]
❖ Mapping ER to SQL (cont)

Useful strategy for database design:

A formal mapping exists for ER model SQL/Relational model.

This maps "structures"; but additional info is needed, e.g.

Note: cannot map some things (e.g. n:m total participation)
COMP3311 23T3 ♢ Week 2 Monday Lecture ♢ [9/23]
❖ SQL Schemas


Primary SQL DDL construct is table creation:

create table TableName (
   attr1Name  type [constraints],
   attr2Name  type [constraints],
   attr3Name  type [constraints],
   ...
   primary key (attrxName ),
   foreign key (attryName)
               references OtherTable (attrzName )
);

SQL schema = collection of table definitions, including constraints

COMP3311 23T3 ♢ Week 2 Monday Lecture ♢ [10/23]
❖ SQL Types


Built-in types

Make your own
create domain Name as Type Constraint;
create type Name as enum (val1, val2,...);
COMP3311 23T3 ♢ Week 2 Monday Lecture ♢ [11/23]
❖ Exercise: ER-to-SQL Mapping


Describe this ER model as an SQL schema


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

COMP3311 23T3 ♢ Week 2 Monday Lecture ♢ [12/23]
❖ SQL Constraints


Constraints in SQL DDL

Tuples which do not satisfy constraints cannot be added to DB

Gives strong guarantee that the data is valid (internally consistent)

But does not guarantee that it reflects reality

COMP3311 23T3 ♢ Week 2 Monday Lecture ♢ [13/23]
❖ Exercise: Constraints


Constraint = SQL expression limiting possible values

Define type + constraints for

COMP3311 23T3 ♢ Week 2 Monday Lecture ♢ [14/23]
❖ Exercise: ER-to-SQL Mappings


For each of the following mappings:

give an SQL schema including relevant constraints
COMP3311 23T3 ♢ Week 2 Monday Lecture ♢ [15/23]
❖ Mapping Strong Entities

An entity set E  with atomic attributes  a1, a2, ... an

maps to

A relation R  with attributes (columns)  a1, a2, ... an

Example:

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


Note: the key is preserved in the mapping.

COMP3311 23T3 ♢ Week 2 Monday Lecture ♢ [16/23]
❖ Mapping Weak Entities

Example:

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

COMP3311 23T3 ♢ Week 2 Monday Lecture ♢ [17/23]
❖ Mapping N:M Relationships

Example:

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

COMP3311 23T3 ♢ Week 2 Monday Lecture ♢ [18/23]
❖ Mapping 1:N Relationships

Example:

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

COMP3311 23T3 ♢ Week 2 Monday Lecture ♢ [19/23]
❖ Mapping 1:1 Relationships

Example:

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

COMP3311 23T3 ♢ Week 2 Monday Lecture ♢ [20/23]
❖ Mapping 1:1 Relationships (cont)

If there is no reason to favour one side of the relationship ...

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

COMP3311 23T3 ♢ Week 2 Monday Lecture ♢ [21/23]
❖ Mapping n-way Relationships

Relationship mappings above assume binary relationship.

If multiple entities are involved:

COMP3311 23T3 ♢ Week 2 Monday Lecture ♢ [22/23]
❖ Exercise: Mapping n-way Relationships


Convert the following ER models into an SQL schema:

[Diagram:Pics/er-rel/n-m-p.png]

COMP3311 23T3 ♢ Week 2 Monday Lecture ♢ [23/23]


Produced: 18 Sep 2023