COMP3311 Week 2 Monday Lecture
COMP3311 23T1 ♢ Week 2 Monday Lecture ♢ [0/17]
In today's lecture ...
- More SQL Data Definition Language (DDL)
- More Mapping ER → Relational/SQL
Things to do ...
- Quiz before Friday midnight
- SES survey before October 9
- Set up your PostgreSQL server
(170 students have logged in to db2 and have /localstorage)
COMP3311 23T1 ♢ Week 2 Monday Lecture ♢ [1/17]
COMP3311 23T1 ♢ Week 2 Monday Lecture ♢ [2/17]
Entity-relationship data model
- entities, attributes, relationships, subclasses
- relationship variations: total/partial, n:m, 1:n, 1:1
SQL as an implementation of relational data model
- relations → tables, tuples → tuples, attributes → columns/fields
Mapping ER to SQL
- entity sets → tables, entities → tuples, attributes → fields
- relationships → tables or foreign keys
- multi-valued-attributes/weak-entities/subclasses → ?
COMP3311 23T1 ♢ Week 2 Monday Lecture ♢ [3/17]
❖ ER to Relational Mapping | |
Reminder: a useful strategy for database design:
- perform initial data modelling using ER
(conceptual-level modelling)
- transform conceptual design into SQL relational model
(implementation-level modelling)
A formal mapping exists for ER model
→ SQL/Relational model.
This maps "structures"; but additional info is needed, e.g.
- concrete domains for attributes and other constraints
But can't map some things (e.g. n:m total participation)
COMP3311 23T1 ♢ Week 2 Monday Lecture ♢ [4/17]
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]
Built-in types
- numeric:
integer
, numeric(n)
, real
- strings:
char(n)
, varchar(n)
, text
- time:
date
, time
, timestamp
, interval
-
boolean
, monetary, geometric, enumerated, ...
Make your own
create domain Dom as Type Constraint;
create type Name as enum (val1, val2,...);
COMP3311 23T1 ♢ Week 2 Monday Lecture ♢ [6/17]
Constraints in SQL DDL
- on attributes e.g.
integer
, check (x > 0)
, not null
- on table e.g.
unique
, primary key (a,b,c)
- between tables e.g.
foreign key (x) references T(y)
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]
Constraint = SQL expression limiting possible values
Define type + constraints for
- positive integers
- marks (range 0..100)
- unsw course codes (COMP3311)
- person's name (alpha + space + - + ')
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:
Note: the key is preserved in the mapping.
COMP3311 23T1 ♢ Week 2 Monday Lecture ♢ [9/17]
COMP3311 23T1 ♢ Week 2 Monday Lecture ♢ [10/17]
❖ Mapping N:M Relationships | |
Example:
COMP3311 23T1 ♢ Week 2 Monday Lecture ♢ [11/17]
❖ Mapping 1:N Relationships | |
Example:
COMP3311 23T1 ♢ Week 2 Monday Lecture ♢ [12/17]
❖ Mapping 1:1 Relationships | |
Example:
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 ...
COMP3311 23T1 ♢ Week 2 Monday Lecture ♢ [14/17]
❖ Mapping n-way Relationships | |
Relationship mappings above assume binary relationship.
If multiple entities are involved:
- n:m generalises naturally to n:m:p:q
- include foreign key for each participating entity
- include any other attributes of the relationship
- other multiplicities (e.g. 1:n:m) ...
- need to be mapped the same as n:m:p:q
- so not quite an accurate mapping of the ER
Some people advocate converting n-way relationships into:
- a new entity, and a set of n binary relationships
COMP3311 23T1 ♢ Week 2 Monday Lecture ♢ [15/17]
❖ Mapping Composite Attributes | |
Composite attributes are mapped by concatenation or flattening.
Example:
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:
COMP3311 23T1 ♢ Week 2 Monday Lecture ♢ [17/17]
Produced: 21 Feb 2023