COMP3311 Week 2 Monday Lecture

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

In today's lecture ...

Things to do ...

COMP3311 23T1 ♢ Week 2 Monday Lecture ♢ [1/17]
❖ CSE Environment


[Diagram:Pics/misc/environ.png]

COMP3311 23T1 ♢ Week 2 Monday Lecture ♢ [2/17]
❖ Recap

Entity-relationship data model

SQL as an implementation of relational data model

Mapping ER to SQL

COMP3311 23T1 ♢ Week 2 Monday Lecture ♢ [3/17]
❖ ER to Relational Mapping

Reminder: a 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.

But can't map some things (e.g. n:m total participation)
COMP3311 23T1 ♢ Week 2 Monday Lecture ♢ [4/17]
❖ 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 23T1 ♢ Week 2 Monday Lecture ♢ [5/17]
❖ SQL Types


Built-in types

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


Constraint = SQL expression limiting possible values

Define type + constraints for

COMP3311 23T1 ♢ Week 2 Monday Lecture ♢ [8/17]
❖ 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 23T1 ♢ Week 2 Monday Lecture ♢ [9/17]
❖ Mapping Weak Entities

Example:

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

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

Example:

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

COMP3311 23T1 ♢ Week 2 Monday Lecture ♢ [11/17]
❖ Mapping 1:N Relationships

Example:

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

COMP3311 23T1 ♢ Week 2 Monday Lecture ♢ [12/17]
❖ Mapping 1:1 Relationships

Example:

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

COMP3311 23T1 ♢ Week 2 Monday Lecture ♢ [13/17]
❖ Mapping 1:1 Relationships (cont)

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

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

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

Relationship mappings above assume binary relationship.

If multiple entities are involved:

Some people advocate converting n-way relationships into:
COMP3311 23T1 ♢ Week 2 Monday Lecture ♢ [15/17]
❖ Mapping Composite Attributes

Composite attributes are mapped by concatenation or flattening.

Example:

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

COMP3311 23T1 ♢ Week 2 Monday Lecture ♢ [16/17]
❖ 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 23T1 ♢ Week 2 Monday Lecture ♢ [17/17]


Produced: 21 Feb 2023