You are on page 1of 6

13/11/2020 APSV-20-21-TEMA-10-RDBMSDesign-EN

Actualizado automáticamente cada 5


APSV-20-21-TEMA-10-RDBMSDesign-EN minutos

Workshop

Hands-On
Workshop: RDBMS
Design
Goals
To understand and apply relational
database design techniques.

Tools
This hands-on workshop will use a free
online database where all the exercises
will be carried out. Students can gain
access to it through their web browsers
at https://sqliteonline.com/ choosing
PostgreSQL on the left pane and clicking
on ‘Click to connect’. This will open a
new session on a PostgreSQL database
with an empty instance.

Introduction

Nearly every application or service


requires information persistency
nowadays. RDBMS is the most
widespread technology meeting this
need. This hands-on workshop will focus
on designing a relational database by
going through conceptual analysis,
relational design, and database
instantiation. The next hands-on session
will focus on using relational databases
from the application/service developer
perspective.

In this session, we will start from the


following conceptual model for our
Current Research Information System
(CRIS):

Publicado por Google Drive – Noti car uso inadecuado

https://docs.google.com/document/d/e/2PACX-1vSJg0I4U5Ej_zf2ZstpKA4ASuyr89yNw9PFxpmtqh-par6jYzoaQBF-kOB_SrkarZgF8whfJamUAmEo/pub 1/6
13/11/2020 APSV-20-21-TEMA-10-RDBMSDesign-EN

Actualizado automáticamente cada 5


APSV-20-21-TEMA-10-RDBMSDesign-EN minutos

Activities to be carried out


during the session
Database design
Let’s start the database design by
mapping the entities identified in the
conceptual model to tables in our
relational model. To make it easier, we
will focus first on the Group entity and
then we will move on to the
Researcher entity. The same process
applies to the others.
To begin with, the student must follow
the following mapping rules for the
Group entity into a table:
1. Choose the columns of the
table: name and acronym.
2. Set the domain
constraints for each attribute:
a. Name is a text:
VARCHAR(255)
b. Acronym is a
text: VARCHAR(8)
3. Set the entity
constraint (Primary Key). Both
Name and Acronym are key
candidates. We choose Acronym
as PK.
4. Set the semantic
constraints (NULLABLE,
UNIQUENESS).
a. Name must be
UNIQUE and can’t be
NULL.
b. Acronym, as PK, must
be UNIQUE and can’t be
NULL.
5. Once we have all the table
details we can create the code
to instantiate it (see next
section for details):
CREATE TABLE groups(
Publicado por Google Drive – Noti car uso inadecuado

https://docs.google.com/document/d/e/2PACX-1vSJg0I4U5Ej_zf2ZstpKA4ASuyr89yNw9PFxpmtqh-par6jYzoaQBF-kOB_SrkarZgF8whfJamUAmEo/pub 2/6
13/11/2020 APSV-20-21-TEMA-10-RDBMSDesign-EN
Acronym VARCHAR(8) PRIMARY
KEY, Actualizado automáticamente cada 5
APSV-20-21-TEMA-10-RDBMSDesign-EN minutos
Name VARCHAR(255) NOT NULL
UNIQUE
)
Now, let’s check that the DBMS
enforces the constraints we have just
set. Connect to a PostgreSQL database
at the SQL editor
(https://sqliteonline.com/), create the
table using the statement above, and
then try the following commands and
explain why they worked (or not)[1].
Note: Trying to copy text from a PDF
and pasting it into the browser may
cause the single and double quotes to
be misunderstood by the browser (you
may get an 42601 syntax error at or
near "..."). To solve this issue you
should type the quotes or the whole
statement on your own in your SQL
editor.
INSERT INTO GROUPS(acronym,
name)
VALUES(‘ING’, ‘Internet de Nueva
Generación’)
INSERT INTO GROUPS(acronym,
name)
VALUES(“TooLongAcronym”, “Another
group”)
INSERT INTO GROUPS(acronym,
name)
VALUES(‘ING’, ‘Another group’)
INSERT INTO GROUPS(name)
VALUES(‘Yet another group’)

Next, we follow a similar process to


create the Researchers table.
1. Choose the columns of the
table: name, orcid, office, email.
2. Set the domain
constraints for each attribute:
a. Name is a
text: VARCHAR(255)
b. ORCID is a
text: VARCHAR(16)
c. Office is a
text: VARCHAR(5)
d. Email is a
text: VARCHAR(255)

3. Set the entity


constraint (Primary Key). All
Name, email and orcid are key
candidates. However, managing
business values as PK might
turn cumbersome as
a. these values may
change overtime subject
Publicado por Google Drive – Noti car uso inadecuado
to the business rules and
https://docs.google.com/document/d/e/2PACX-1vSJg0I4U5Ej_zf2ZstpKA4ASuyr89yNw9PFxpmtqh-par6jYzoaQBF-kOB_SrkarZgF8whfJamUAmEo/pub 3/6
13/11/2020 APSV-20-21-TEMA-10-RDBMSDesign-EN
then force to update the
PKs (with all the Actualizado automáticamente cada 5
APSV-20-21-TEMA-10-RDBMSDesign-EN associated troubles); minutos
and,
b. these values may be
long and more difficult
to index and search, thus
slowing down the
database performance.
Indeed, many times key
candidates are long texts which
may impact the database
performance. Further, although
it is uncommon for a person’s
name to vary, more often emails
vary according to
organizational changes, and a
researcher may not have ORCID
(although s/he can request
one). Thus, in these cases it is a
common practice to select PKs
with no business meaning (i.e.
surrogate keys) e.g. numeric,
auto incremental serial values.
You must consider both natural
and surrogate options and
choose the best alternative
according to your business
context. In this case, we choose
to include a surrogate key id.
4. Set the referential
constraint (FK). Each one-to-
many relationship can be
mapped to either a new
attribute (i.e. as a FK) or a new
table, depending on whether
the relationship has its own
attributes. For example, the
‘affiliated with’ relationship can
be mapped to:
a. A new column rGroup in
the Researchers table, being
a foreign key.
b. A new Affiliations table
including Researcher (FK
from Researchers) and
rGroup (FK from Groups) as
columns, besides its own
attributes e.g. Date to keep
the affiliation date of the
researcher to the group.
As a result, we create a new
attribute rgroup in the
Researchers table[2].
5. Set the semantic
constraints (NULLABLE,
UNIQUENESS).
a. Id is the PK, and out of
commodity we decide to
have it auto incremented
by the database by using
the SERIAL keyword.
b. Name can’t be NULL.
c. ORCID must be
UNIQUE.
d. Office can’t be NULL.
Publicado por Google Drive – Noti car uso inadecuado

https://docs.google.com/document/d/e/2PACX-1vSJg0I4U5Ej_zf2ZstpKA4ASuyr89yNw9PFxpmtqh-par6jYzoaQBF-kOB_SrkarZgF8whfJamUAmEo/pub 4/6
13/11/2020 APSV-20-21-TEMA-10-RDBMSDesign-EN
e. Email can’t be NULL
and must be UNIQUE. Actualizado automáticamente cada 5
APSV-20-21-TEMA-10-RDBMSDesign-EN minutos
f. The research group
(rgroup) can’t be null,
and it’s a FK.
6. Once we have all the table
details we can create the code
to instantiate it:
CREATE TABLE researchers(
Id SERIAL PRIMARY KEY,
Name VARCHAR(255) NOT
NULL,
ORCID VARCHAR(16) UNIQUE,
Office VARCHAR(5) NOT NULL,
Email VARCHAR(255) NOT
NULL UNIQUE,
rGroup VARCHAR(8) NOT
NULL,
CONSTRAINT fk_groups
FOREIGN KEY(rGroup)
REFERENCES
Groups(Acronym)
)
Now, let’s check that the DBMS
enforces the constraints we have just
set. Try the following commands in
your SQL editor, and explain why they
worked (or not).
INSERT INTO researchers(name, office,
ORCID, email, rgroup)
VALUES(‘Jose’, ‘B204’,
'0001000200030004',
‘jm.delalamo@upm.es’, 'ING')
INSERT INTO researchers(name, office,
ORCID, email)
VALUES(‘Juan’, ‘B201’,
'0002000200030005', ‘juan@upm.es’)
Now, following the same process create
the Projects table, taking into account
that:
a. Each entity always maps to a
table, including all the entity
attributes as table columns.
b. Each many-to-many
relationship maps to a
new table: The ‘participates’
relationship maps to the
Participants table including
Project (Foreign Key from
Projects) and Researcher (FK
from Researchers) as columns.
The student must carry out the
following tasks in pairs:

1. Design the remaining tables,


including their attributes, entity
constraints (PKs) and referential
constraints (FKs).

2. Set the domain and


semantic constraints for each
column.

3. Check that everything works


Publicado por Google Drive – Noti car uso inadecuado
as expected.
https://docs.google.com/document/d/e/2PACX-1vSJg0I4U5Ej_zf2ZstpKA4ASuyr89yNw9PFxpmtqh-par6jYzoaQBF-kOB_SrkarZgF8whfJamUAmEo/pub 5/6
13/11/2020 APSV-20-21-TEMA-10-RDBMSDesign-EN

Actualizado automáticamente cada 5


APSV-20-21-TEMA-10-RDBMSDesign-EN
Submission minutos

Students must design the tables


required to store the information for
the entities Group, Researcher and
Project, their relationships and their
constraints. The SQL statements for the
creation of the tables must be
submitted to the submission area in
Moodle by the assignment deadline.

[1] You can check the contents of the groups


table using the statement: SELECT * FROM
groups
[2] PostgreSQL sets NO ACTION (i.e.
RESTRICT) mode for every FOREIGN
KEY by default. Note the SERIAL
keyword, which has been used to
create a surrogate PK managed by the
RDBMS.

Publicado por Google Drive – Noti car uso inadecuado

https://docs.google.com/document/d/e/2PACX-1vSJg0I4U5Ej_zf2ZstpKA4ASuyr89yNw9PFxpmtqh-par6jYzoaQBF-kOB_SrkarZgF8whfJamUAmEo/pub 6/6

You might also like