COMP3311 Week 2 Monday Lecture

COMP3311 25T1 ♢ Week 2 Monday Lecture ♢ [0/26]
❖ Week 02

In today's lecture ...

Things to do ...

COMP3311 25T1 ♢ Week 2 Monday Lecture ♢ [1/26]
❖ 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 25T1 ♢ Week 2 Monday Lecture ♢ [2/26]
❖ 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 25T1 ♢ Week 2 Monday Lecture ♢ [3/26]
❖ Data Models (recap)

Entity-relationship (ER) data model

Relational data model

E.g.   Student(zID:integer, name:string, WAM:float, ...)
COMP3311 25T1 ♢ Week 2 Monday Lecture ♢ [4/26]
❖ 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 25T1 ♢ Week 2 Monday Lecture ♢ [5/26]
❖ SQL Schemas (recap)


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 25T1 ♢ Week 2 Monday Lecture ♢ [6/26]
❖ SQL Types


Built-in types

Make your own
create domain Name as Type Constraint;
create type Name as enum (val1, val2,...);
COMP3311 25T1 ♢ Week 2 Monday Lecture ♢ [7/26]
❖ 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 25T1 ♢ Week 2 Monday Lecture ♢ [8/26]
❖ Exercise: Constraints

Constraint = SQL expression limiting possible values

Define type + constraints for

COMP3311 25T1 ♢ Week 2 Monday Lecture ♢ [9/26]
❖ Exercise: ER-to-SQL Mapping

Describe this ER model as an SQL schema

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

Use domain definitions to make types more precise.

Use integer id attributes for all entity tables.

COMP3311 25T1 ♢ Week 2 Monday Lecture ♢ [10/26]
❖ Exercise: ER-to-SQL Mappings



For each of the following ER-to-relational mappings

COMP3311 25T1 ♢ Week 2 Monday Lecture ♢ [11/26]
❖ 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 25T1 ♢ Week 2 Monday Lecture ♢ [12/26]
❖ Mapping Weak Entities

Example:

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

COMP3311 25T1 ♢ Week 2 Monday Lecture ♢ [13/26]
❖ Mapping N:M Relationships

Example:

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

COMP3311 25T1 ♢ Week 2 Monday Lecture ♢ [14/26]
❖ Mapping 1:N Relationships

Example:

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

COMP3311 25T1 ♢ Week 2 Monday Lecture ♢ [15/26]
❖ Mapping 1:1 Relationships

Example:

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

COMP3311 25T1 ♢ Week 2 Monday Lecture ♢ [16/26]
❖ Mapping 1:1 Relationships (cont)

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

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

COMP3311 25T1 ♢ Week 2 Monday Lecture ♢ [17/26]
❖ Mapping n-way Relationships

Relationship mappings above assume binary relationship.

If multiple entities are involved:

COMP3311 25T1 ♢ Week 2 Monday Lecture ♢ [18/26]
❖ Exercise: Mapping n-way Relationships


Convert the following ER models into SQL schemas:

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

COMP3311 25T1 ♢ Week 2 Monday Lecture ♢ [19/26]
❖ Mapping Composite Attributes

Composite attributes are mapped by concatenation or flattening.

Example:

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

COMP3311 25T1 ♢ Week 2 Monday Lecture ♢ [20/26]
❖ 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 25T1 ♢ Week 2 Monday Lecture ♢ [21/26]
❖ Mapping Subclasses

Three different approaches to mapping subclasses to tables:

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

Example of ER-style mapping:

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

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

Example of object-oriented mapping:

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

COMP3311 25T1 ♢ Week 2 Monday Lecture ♢ [24/26]
❖ Mapping Subclasses (cont)

Example of single-table-with-nulls mapping:

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

COMP3311 25T1 ♢ Week 2 Monday Lecture ♢ [25/26]
❖ Exercise: Class Hierarchy ER-to-SQL

Convert the following class hierarchy to SQL using ER mapping:

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

COMP3311 25T1 ♢ Week 2 Monday Lecture ♢ [26/26]


Produced: 24 Feb 2025