Mapping ER to SQL

COMP3311 20T3 ♢ ER->SQL Mapping ♢ [0/26]
❖ Mapping ER to SQL


We have explored mapping ER designs to relational schemas

SQL schemas are essentially more detailed versions of relational schemas

The mapping is much the same, except that

There are also some ideas from ER than do not map to an SQL schema
COMP3311 20T3 ♢ ER->SQL Mapping ♢ [1/26]
❖ Reminder: SQL/Relational Model vs ER Model

Correspondences between SQL/relational and ER data models:

Differences between SQL and ER models: Note that ...
COMP3311 20T3 ♢ ER->SQL Mapping ♢ [2/26]
❖ Mapping ER to SQL

Some conventions that we use in mapping ER to SQL

COMP3311 20T3 ♢ ER->SQL Mapping ♢ [3/26]
❖ Mapping Strong Entities

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

maps to

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

Example:

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


Note: the key is preserved in the mapping.

COMP3311 20T3 ♢ ER->SQL Mapping ♢ [4/26]
❖ Mapping Weak Entities

Example:

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

COMP3311 20T3 ♢ ER->SQL Mapping ♢ [5/26]
❖ Mapping N:M Relationships

Example:

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

COMP3311 20T3 ♢ ER->SQL Mapping ♢ [6/26]
❖ Mapping N:M Relationships (cont)

create table Customers (
    custNo  serial primary key,
    name    text not null,
    address text  -- don't need to know customer's address
);
create table Accounts (
    acctNo   char(5) check (acctNo ~ '[A-Z]-[0-9]{3}'),
    title    text not null,    -- acctNos are like 'A-123'
    balance  float default 0.0,
    primary key (acctNo)
);
create table Owns (
    customer_id integer references Customers(custNo),
    account_id  char(5) references Accounts(acctNo),
    last_accessed timestamp,
    primary key (customer_id, account_id)
);

COMP3311 20T3 ♢ ER->SQL Mapping ♢ [7/26]
❖ Mapping 1:N Relationships

Example:

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

COMP3311 20T3 ♢ ER->SQL Mapping ♢ [8/26]
❖ Mapping 1:N Relationships (cont)

create table Branches (
    branchNo serial primary key,
    address  text not null,
    assets   currency
);
create table Customers (
    custNo  serial primary key,
    name    text not null,
    address text,
    hasHome integer not null, -- total participation
    joined  date not null,
    foreign key (hasHome) references Branches(branchNo)
);

hasHome implements the 1:n relationship;  not null  implements total participation

COMP3311 20T3 ♢ ER->SQL Mapping ♢ [9/26]
❖ Mapping 1:1 Relationships

Example:

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

COMP3311 20T3 ♢ ER->SQL Mapping ♢ [10/26]
❖ Mapping 1:1 Relationships (cont)

create table Branches (
    branchNo serial primary key,
    address  text not null,
    assets   currency          -- a new branch
);                             --    may have no accounts
create table Managers (
    empNo    serial primary key,
    name     text not null,
    salary   currency not null, -- when first employed, 
                                --    must have a salary
    manages  integer not null,  -- total participation
    foreign key (manages) references Branches(branchNo)
);

If both entities have total participation, cannot express this in SQL
except by putting a (redundant)  not null  foreign key in one table

COMP3311 20T3 ♢ ER->SQL Mapping ♢ [11/26]
❖ Mapping n-way Relationships

Example:

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

A customer accesses one of their accounts at a specific ATM

COMP3311 20T3 ♢ ER->SQL Mapping ♢ [12/26]
❖ Mapping n-way Relationships (cont)

create table Customers (
    custNo   serial primary key, ...
);
create table Accounts (
    acctNo   char(5) ... primary key, ...
);
create table ATMs (
    atmID    serial primary key,
    cash     currency check (cash >= 0),
    location text not null
);
create table Withdrawal (
    customer_id  integer references Customers(custNo),
    account_id   char(5) references Accounts(acctNo),
    atm_id       integer references ATMs(atmID),
    amount       currency not null,
    when         timestamp default now(),
    primary key  (customer_id,account_id,atm_id)
);

COMP3311 20T3 ♢ ER->SQL Mapping ♢ [13/26]
❖ Mapping Composite Attributes

Composite attributes are mapped by concatenation or flattening.

Example:

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

COMP3311 20T3 ♢ ER->SQL Mapping ♢ [14/26]
❖ Mapping Composite Attributes (cont)

-- Version 1: concatenated
create table People (
    ssn     integer primary key,
    name    text not null,
    address text not null
);
-- Version 2: flattened
create table People (
    ssn     integer primary key,
    given   text not null,
    family  text,
    number  integer not null,
    street  text not null,
    suburb  text not null,
    pcode   char(4) not null check (pcode ~ '[0-9]{4}')
);

address  =  (number::text||' '||street||', '||suburb||' '||pcode)

Searching:  suburb = 'Coogee'  vs  address like '%Coogee%' 

Sorting:   order by family  vs  can't be done (easily)

COMP3311 20T3 ♢ ER->SQL Mapping ♢ [15/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 20T3 ♢ ER->SQL Mapping ♢ [16/26]
❖ Mapping Multi-valued Attributes (MVAs) (cont)


create table People (
    ssn      integer primary key,
    name     text not null,
    birthday date
);
create table FavColour (
    person_id integer references People(ssn),
    colour    text,
    primary key (person_id,colour)
);

Note that  colour  is implicitly  not null  because it is part of the primary key

COMP3311 20T3 ♢ ER->SQL Mapping ♢ [17/26]
❖ Mapping Subclasses

Three different approaches to mapping subclasses to tables:

Which mapping is best depends on how data is to be used.
COMP3311 20T3 ♢ ER->SQL Mapping ♢ [18/26]
❖ Mapping Subclasses (cont)

Example of ER-style mapping:

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

COMP3311 20T3 ♢ ER->SQL Mapping ♢ [19/26]
❖ Mapping Subclasses (cont)

create table People (
    ssn     integer primary key,
    name    text not null,
    address text
);
create table Employees (
    person_id integer primary key,
    salary    currency not null,
    position  text not null,
    foreign key (person_id) references People(ssn)
);
create table Managers (
    employee_id integer primary key,
    bonus       currency,
    foreign key (employee_id)
                references Employees(person_id)
);

COMP3311 20T3 ♢ ER->SQL Mapping ♢ [20/26]
❖ Mapping Subclasses (cont)

Example of object-oriented mapping:

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

COMP3311 20T3 ♢ ER->SQL Mapping ♢ [21/26]
❖ Mapping Subclasses (cont)

create table People (
    ssn     integer primary key,
    name    text not null,
    address text
);
create table Employees (
    ssn       integer primary key,
    name      text not null,
    address   text
    salary    currency not null,
    position  text not null,
    foreign key (snn) references People(ssn)
);
create table Managers (
    ssn       integer primary key,
    name      text not null,
    address   text
    salary    currency not null,
    position  text not null,
    bonus     currency,
    foreign key (snn) references People(ssn)
);

COMP3311 20T3 ♢ ER->SQL Mapping ♢ [22/26]
❖ Mapping Subclasses (cont)

Example of single-table-with-nulls mapping:

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

COMP3311 20T3 ♢ ER->SQL Mapping ♢ [23/26]
❖ Mapping Subclasses (cont)

create table People (
    ssn       integer primary key,
    ptype     char(1)  not null 
                       check (ptype in ('P','E','M')),
    name      text not null,
    address   text
    salary    currency,
    position  text,
    bonus     currency,
    constraint subclasses check
               ((ptype = 'P' and salary is null
                and position is null and bonus is null)
               or
                (ptype = 'E' and salary is not null
                 and position is not null and bonus is null)
               or
                (ptype = 'M' and salary is not null
                 and position is not null and bonus is not null))
);

COMP3311 20T3 ♢ ER->SQL Mapping ♢ [24/26]
❖ Mapping Subclasses (cont)

Example:

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


Every employee is either permanent or casual, but not both.

COMP3311 20T3 ♢ ER->SQL Mapping ♢ [25/26]
❖ Mapping Subclasses (cont)

ER-style mapping to SQL schema:

create table Employees (
    empID   serial primary key,
    name    text not null,
    address text not null
);
create table Permanents (
    employee_id integer primary key,
    salary      currency not null,
    foreign key (employee_id) references Employees(empID)
);
create table Casuals (
    employee_id integer primary key,
    pay_rate    currency not null,
    foreign key (employee_id) references Employees(empID)
);

Does not capture either participation or disjoint-ness constraints!

Would need to program a solution to this e.g web-form that requires user to enter both Employee and subclass info

COMP3311 20T3 ♢ ER->SQL Mapping ♢ [26/26]


Produced: 22 Sep 2020