area_end found for "osa1" without corresponding start. Fix this to get rid of this warning.
area_end found for "osa2" without corresponding start. Fix this to get rid of this warning.
area_end found for "osa3" without corresponding start. Fix this to get rid of this warning.

English version of the materials are work in progress!

Expect bugs, typos, and other issues. The English version is expected to be completed during spring 2026.

Exercise: Transformation of an ER Diagram

In this guided exercise, we will get to know the transformation of ER diagrams into relational database relations (i.e. effectively tables). Transformation is a useful preliminary step for implementing databases, as by applying transformation rules you can easily form a relational database suitable for the conceptual model.

The exercise has several subtasks. You can progress in the exercise by completing each subtask one at a time.

This exercise is a continuation of Exercise 2.1. Do Exercise 2.1 first before starting this exercise.

Before starting the exercise, familiarize yourself with the following chapters:

  • Chapter 3.1, which presents basic concepts of relational databases. In this task, you must especially understand what relation, primary key, and foreign key are.

  • Chapter 3.2, which presents all transformation rules. In this task, we will get to know the general transformation process, and a more detailed examination of rules is left outside this task.

Huomautus

Jos huomaat virheitä tai sinulla on kysyttävää tehtävistä, ota yhteyttä kurssin opettajiin

Assignment

Let's return to the student database modelled in Exercise 2.1 and transform it into a relational database.

Follow the steps in the sections below and complete the answers in the submission boxes below based on them. You will find a check button at the end of each section with which you can test the correctness of your answer.

Transforming Entity Sets

{#openEntity Sets plugin="jsrunner" id="CR3KriY0RDj2" nocache="true"} groups: - Anonymous showInView: true button: Avaa osatehtävä "Entity Sets" fields: - 947075.ex21-er-avainattribuutit.ALL=ex21-er-avainattribuutit - 947075.ex31-step1.ALL=ex31-step1 - 947075.ex31-step2.ALL=ex31-step2 - 947075.ex31-step3.ALL=ex31-step3 program: |!! if ("<span class="error" ng-non-bindable>The `belongs` filter requires `nocache=true` attribute.</span>" !== "True") { let v = tools.getValue("ex21-er-avainattribuutit", undefined); if (!v) { tools.println("Complete Exercise 2.1 first before starting this exercise!"); return; } } gtools.outdata.saveAreaVisibility = false; gtools.outdata.areaVisibility = { "osa1": true, }; !!

Let's look at the student database ER diagram again for revision:

Let's start by transforming entity sets and their basic attributes. Basic attributes here mean all those attributes that are not composite, multivalued, or derived.

Let's apply the first transformation rule here:

There are different notation ways for a relational database: relations can be described as a table or, for example, as classes of UML diagrams. In this course, we use the same notation way presented in Chapter 3.1:

RELATION_NAME(attribute1, attribute2, attribute3)

Primary keys are marked in these tasks exceptionally by marking key attributes inside {| |} brackets. For example in the following schema

RELATION_NAME({|attribute1, attribute2|}, attribute3)

attributes attribute1 and attribute2 belong to the relation's primary key.

So let's add all entity sets and their basic attributes to the answer box. At this stage, it is not yet worth transforming special attributes, such as composite, derived, or multivalued. Define relations first and set all simple attributes in them:

  1. Move the cursor to an empty line
  2. Press the "New relation" button, which adds a template for a new relation.
  3. Set the relation name to STUDENT and add the student's ordinary attributes (i.e., all attributes that are not composite, multivalued, or derived) to the relation:
    • id
    • email
    • address
    • start_year
  4. Mark the key attribute id as a primary key
    • Paint the attribute id
    • Press the "Mark as primary key" button
  5. Add a new empty line and repeat steps 1-4 for all other entity sets
Example: Transformation of entity sets and basic attributes

You can float the diagram editor with the Float button. Then the text moves to the right edge, and space is freed up for the editor on the right.

Harjoitus: ER-kaavion transformointi

Tässä ohjatussa harjoituksessa tutustutaan ER-kaavioiden transformointiin relaatiotietokantojen relaatioiksi (eli käytännössä tauluiksi). Transformointi on hyödyllinen esiaskel tietokantojen toteuttamiselle, sillä soveltamalla transformointisääntöjä saat helposti muodostettua käsitteelliseen malliin sopivan relaatiotietokannan.

Harjoituksessa on useita osatehtäviä. Pääset harjoituksessa eteenpäin suorittamalla jokainen osatehtävä kerrallaan.

Tämä harjoitus on jatkoa Harjoitukselle 2.1. Tee ensin Harjoitus 2.1 ennen kuin aloitat tämän harjoituksen.

Ennen harjoituksen aloittamista tutustu seuraaviin lukuihin:

  • Luku 3.1, jossa esitetään relaatiotietokantojen peruskäsitteitä. Tässä tehtävässä tulee erityisesti ymmärtää, mitä ovat relaatio, perusavain sekä viiteavain.

  • Luku 3.2, jossa esitellään kaikki transformointisäännöt. Tässä tehtävässä tutustutaan yleiseen transformointiprosessiin, ja tarkempi sääntöjen tarkastelu jää tämän tehtävän ulkopuolelle.

Huomautus

Jos huomaat virheitä tai sinulla on kysyttävää tehtävistä, ota yhteyttä kurssin opettajiin

Tehtävänanto

Palataan Harjoituksessa 2.1 mallinnettuun opiskelijatietokantaan ja transformoidaan se relaatiotietokannaksi.

Seuraa alla olevien osioiden vaiheita ja täydennä alla olevien palautuslaatikoiden vastaukset niiden perusteella. Löydät jokaisen osion lopusta tarkistuspainikkeen, jolla voit testata vastauksesi oikeellisuuden.

Kohdetyyppien transformointi

# openKohdetyypit

Katsotaan kertaukseksi vielä opiskelijatietokannan ER-kaavio:

Aloitetaan transformoimalla kohdetyypit ja niiden perusattribuutteja. Perusattribuuteilla tarkoitetaan tässä kaikkia niitä attribuutteja, jotka eivät ole koottuja, moniarvoisia tai johdettuja.

Sovelletaan tähän ensimmäinen transformointisääntö:

Rule 1 (Strong entity sets): a separate relation is made for every strong entity set. Ordinary attributes of the entity set (i.e., attributes that are not multivalued, derived, or composite) are made attributes of the relation. Primary key of the relation is formed from the key attributes of the entity set.

Figure: Transformation of a strong entity set.
Figure: Transformation of a strong entity set.

Relaatiotietokannalle on olemassa erilaisia merkintätapoja: relaatiot voidaan kuvata taulukkona tai vaikkapa UML-kaavioiden luokkina. Tällä kurssilla käytämme samaa merkintätapaa, joka on esitetty Luvussa 3.1:

RELAATION_NIMI(attribuutti1, attribuutti2, attribuutti3)

Perusavaimet merkitään näissä tehtävissä poikkeuksellisesti merkitsemällä avainattribuutit {| |}-sulkujen sisään. Esimerkiksi seuraavassa kaaviossa

RELAATION_NIMI({|attribuutti1, attribuutti2|}, attribuutti3)

attribuutit attribuutti1 sekä attribuutti2 kuuluvat relaation perusavaimeen.

Lisätään siis kaikki kohdetyypit ja niiden perusattribuutit vastauslaatikkoon. Tässä vaiheessa ei vielä kannata transformoida erikoisattribuutteja, kuten koottuja, johdettua tai moniarvoisia. Määritä ensin relaatiot ja aseta niihin kaikki yksinkertaiset attribuutit:

  1. Siirrä kursori tyhjälle riville
  2. Paina "Uusi relaatio" -painiketta, joka lisää pohjan uudelle relaatiolle.
  3. Aseta relaation nimeksi OPISKELIJA ja lisää relaatiolle opiskelijan tavalliset attribuutit (eli kaikki attribuutit, jotka eivät ole koottuja, moniarvoisia tai johdettuja):
    • id
    • sähköpostiosoite
    • osoite
    • aloitusvuosi
  4. Merkitse avainattribuutti id perusavaimeksi
    • Maalaa attribuutti id
    • Paina "Merkitse perusavaimeksi" -painiketta
  5. Lisää uusi tyhjä rivi ja toista vaiheet 1-4 kaikille muille kohdetyypeille
Esimerkki: Kohdetyyppien ja perusattribuuttien transformointi

Voit kelluttaa kaavioeditorin Kelluta-painikkeella. Silloin teksti siirtyy oikeaan laitaan, ja editorille vapautuu tilaa oikealle.

# 780197.ex31-step1
# 780197.reflect-ex31-step1

Transforming Special Attributes

``{#openSpecial Attributes plugin="jsrunner" id="iW3z4mBHWKNg" nocache="true"} groups: - Anonymous showInView: true button: Avaa osatehtävä "Special Attributes" fields: - 947075.ex21-er-avainattribuutit.ALL=ex21-er-avainattribuutit - 947075.ex31-step1.ALL=ex31-step1 - 947075.ex31-step2.ALL=ex31-step2 - 947075.ex31-step3.ALL=ex31-step3 program: |!! if ("<span class="error" ng-non-bindable>Thebelongsfilter requiresnocache=true` attribute." !== "True") { let v = tools.getValue("ex21-er-avainattribuutit", undefined); if (!v) { tools.println("Complete Exercise 2.1 first before starting this exercise!"); return; }

v = tools.getValue("ex31-step1", undefined); if (!v) { tools.println("Fill and save the "Entity Sets" subtask answer before opening this subtask!"); return; } } gtools.outdata.saveAreaVisibility = false; gtools.outdata.areaVisibility = { "osa2": true, }; !! ```

Next, let's transform all special attributes in entity sets. Special attributes are, among others, multivalued, composite, derived, and all others for which their own transformation rules apply.

In the student database, it is noticed that the entity set STUDENT has two special attributes: name and phone_numbers. Transformation rules 4 and 5 apply to these:

Note that in the example above, a relation F2 was created for the multivalued attribute and the key attribute f1 of relation F was added to it. Relation F2 attribute f1 is thus a foreign key to relation F. In other words, a foreign key, as its name suggests, refers to an attribute in another relation (see Chapter 3.1).

Foreign keys can be marked in a relation schema in the following way:

F2.f1 -> F.f1

Meaning: Relation F2 attribute f1 is a foreign key referring to relation F attribute f1.

Let's transform attributes according to the rules into the box below:

  1. Move the cursor to a new line and add a new relation.
  2. Give the relation the name STUDENT_PHONE_NUMBERS. This relation corresponds to the multivalued attribute phone_numbers of the entity STUDENT.
  3. Add the primary key id of relation STUDENT as attributes to relation STUDENT_PHONE_NUMBERS. Set its name to student_id for clarity.
  4. Add a new attribute phone_number to the relation, which contains a single phone number.
  5. Mark student_id and phone_number as the primary key.
  6. Move the cursor to a new line and add a new foreign key by pressing "New foreign key".
    • Set the foreign key to be the attribute student_id of relation STUDENT_PHONE_NUMBERS and define it to refer to attribute id of relation STUDENT.
  7. Apply rule 4 to attribute name.
Example: Transformation of a multivalued attribute

You can float the diagram editor with the Float button. Then the text moves to the right edge, and space is freed up for the editor on the right.

Erikoisattribuuttien transformointi

# openErikoisattribuutit

Transformoidaan seuraavaksi kaikki kohdetyypeissä olevat erikoisattribuutit. Erikoisattribuutit ovat muun muassa moniarvoiset, kootut, johdetut ja kaikki muut sellaiset, joille pätevät omat transformointisäännöt.

Opiskelijatietokannassa huomataan, että kohdetyypillä OPISKELIJA on kaksi erikoisattribuuttia: nimi sekä puhelinnumerot. Näihin sopivat transformointisäännöt 4 ja 5:

Rule 4 (Composite attributes): composing (simple) attributes are chosen to be included in the relation from every composite attribute. The composite attribute is discarded.

Figure: Transformation of a composite attribute.
Figure: Transformation of a composite attribute.

Rule 5 (Multivalued attributes): a separate relation (so-called "attribute relation") is made for every multivalued attribute. The multivalued attribute as well as key attributes of the entity set to which the multivalued attribute being transformed belongs are chosen as the primary key of the attribute relation.

Figure: Transformation of a multivalued attribute.
Figure: Transformation of a multivalued attribute.

Huomaa, että yllä olevassa esimerkissä moniarvoiselle attribuutille luotiin relaatio F2 ja siihen lisättiin relaation F avainattribuutti f1. Relaation F2 attribuutti f1 on siis viiteavain relaatioon F. Toisin sanoin, viiteavain nimensä mukaisesti viittaa johonkin toisessa relaatiossa olevaan attribuuttiin (ks. Luku 3.1).

Relaatiokaavassa viiteavaimet voidaan merkitä seuraavalla tavalla:

F2.f1 -> F.f1

Eli: Relaation F2 attribuutti f1 on viiteavain, joka viittaa relaation F attribuuttiin f1.

Transformoidaan attribuutit sääntöjen mukaan alla olevaan laatikkoon:

  1. Siirrä kursori uudelle riville ja lisää uusi relaatio.
  2. Anna relaation nimeksi OPISKELIJA_PUHELINNUMEROT. Tämä relaatio vastaa kohteen OPISKELIJA moniarvoista attribuuttia puhelinnumerot.
  3. Lisää relaatiolle OPISKELIJA_PUHELINNUMEROT attribuuteiksi relaation OPISKELIJA perusavain id. Aseta sen nimeksi selkeyden vuoksi opiskelija_id.
  4. Lisää relaatiolle vielä uusi attribuutti puhelinnumero, joka sisältää yksittäisen puhelinnumeron.
  5. Merkitse opiskelija_id sekä puhelinnumero perusavaimeksi.
  6. Siirrä kursori uudelle riville ja lisää vielä uusi viiteavain painamalla "Uusi viiteavain".
    • Aseta viiteavaimeksi relaation OPISKELIJA_PUHELINNUMEROT attribuutti opiskelija_id ja määritä se viittaamaan relaation OPISKELIJA attribuuttiin id.
  7. Sovella vielä sääntöä 4 attribuutille nimi.
Esimerkki: Moniarvoisen attribuutin transformointi

Voit kelluttaa kaavioeditorin Kelluta-painikkeella. Silloin teksti siirtyy oikeaan laitaan, ja editorille vapautuu tilaa oikealle.

# 780197.ex31-step2
# 780197.reflect-ex31-step2

Transforming Relationships

# openRelationships

Finally, let's transform relationship sets. In the relational model, transforming relationships depends only on the maximum cardinality of the relationship. Generally, minimum cardinality is not marked in relations in any way, but the handling of minimum cardinalities is usually universally the responsibility of either the application/person using the database or the database management system.

Let's examine from the ER diagram what kind of cardinalities are in the student database:

  • One-to-many (1:N) relationships are those where the maximum cardinality of one entity participating in the relationship is 1 and the other entity's maximum cardinality is N.
    • Student has a 1:N relationship to Department (student Studies in at most one department, and a department can have multiple students)
    • Department has a 1:N relationship to Faculty (department Belongs to at most one faculty, and a faculty can have multiple departments)
    • Course has a 1:N relationship to Department (course is Organized by at most one department, and a department can have multiple courses)
  • Many-to-many (N:M) relationships are those where the maximum cardinality of both entities participating in the relationship is N.
    • Student has an N:M relationship to Course (student Takes multiple exams for courses, a course can have many students taking exams)
  • One-to-one (1:1) relationships are relatively rare, and there are none in this student database

Before you continue, go through the student database ER diagram once more and ensure that you understand the classification above.

Since the student database has only 1:N and N:M relationships, only the following two transformation rules are needed:

Let's transform relationships into the submission box below:

N:M relationships:

Let's first examine the only N:M relationship TakesExam, whose transformation is straightforward:

  1. Move the cursor to an empty line and add a new relation with the "New relation" button.
  2. Set the relation name to TAKES_EXAM. This relation will become a relationship relation.
  3. Add the attributes belonging to the primary key of relations STUDENT and COURSE as attributes of relation TAKES_EXAM:
    • student_id (corresponds to primary key id of relation STUDENT)
    • course_code (corresponds to primary key course_code of relation COURSE)
  4. Add the attributes of relationship TakesExam to the attributes of relation TAKES_EXAM
    • date
    • grade
  5. Mark the combination of primary keys taken from relations STUDENT and COURSE as well as attribute date as the primary key.
    • Paint attributes student_id, course_code and date and press the "Mark as primary key" button.
    • Note: Here date is exceptionally also taken into the primary key, as in the ER diagram it is marked as a key attribute of the relationship. This choice was justified in Exercise 2.1.
  6. Finally, mark student_id and course_code as foreign keys
    • Move cursor to a new line and press "New foreign key"
    • Set the foreign key to attribute TAKES_EXAM.student_id
    • Set the referenced attribute to STUDENT.id
    • Repeat the same for attribute course_code
Example: Transformation of an N:M relationship

1:N relationships:

Let's examine the relationship Studies:

  1. Move the cursor to the STUDENT relation side (since in the ER diagram entity STUDENT has the "fork" i.e. N-side in the Studies relationship).
  2. Add attribute dept_id to relation STUDENT, corresponding to the id key attribute of DEPARTMENT relation.
  3. Mark dept_id also as a foreign key pointing to attribute id of relation DEPARTMENT
    • Move cursor to a new line and press "New foreign key"
    • Set foreign key as STUDENT.dept_id
    • Set referenced attribute as DEPARTMENT.id
Example: Transformation of a 1:N relationship

Transform the rest of the relationship sets yourself using the instructions above.

You can float the diagram editor with the Float button. Then the text moves to the right edge, and space is freed up for the editor on the right.

Suhteiden transformointi

# openSuhteet

Transformoidaan lopuksi vielä suhdetyypit. Relaatiomallissa suhteiden transformointi riippuu vain suhteen maksimikardinaliteettista. Yleisesti minimikardinaliteettia ei merkitä relaatioihin millään tavalla, vaan minimikardinaliteettien käsittelystä vastaa yleensä joko tietokantaa käyttävä sovellus/henkilö tai tietokannanhallintajärjestelmä.

Tarkastellaan ER-kaaviosta, millaisia kardinaliteetteja on opiskelijatietokannassa:

  • Yhdestä moneen (1:N) -suhteet ovat ne, joissa yhden suhteeseen osallistuvan kohteen maksimikardinaalisuus on 1 ja toisen kohteen maksimikardinaalisuus on N.
    • Opiskelijalla on 1:N-suhde Laitokseen (opiskelija Opiskelee korkeintaan yhdessä laitoksessa, ja laitoksessa voi olla useita opiskelijoita)
    • Laitoksella on 1:N-suhde Tiedekuntaan (laitos Kuuluu korkeintaan yhteen tiedekuntaan, ja tiedekunnalla voi olla useita laitoksia)
    • Kurssilla on 1:N-suhde Laitokseen (kurssia Järjestää korkeintaan yksi laitos, ja laitoksella voi olla useita kursseja)
  • Monesta moneen (N:M) -suhteet ovat ne, joissa kummankin suhteeseen osallistuvan kohteen maksimikardinaalisuus on N.
    • Opiskelijalla on N:M-suhde Kurssiin (opiskelija Tenttii useita kursseja, kurssilla voi olla montaa opiskelijaa tentittävänä)
  • Yhdestä yhteen (1:1) -suhteet ovat suhteellisen harvinaisia, eikä niitä ole tässä opiskelijatietokannassa

Ennen kuin jatkat, käy läpi vielä opiskelijatietokannan ER-kaavio läpi ja varmista, että ymmärrät yllä olevan luokittelun.

Koska opiskelijatietokannassa on vain 1:N- ja N:M-suhteita, tarvitaan vain seuraavat kaksi transformointisääntöä:

Rule 7 (1:N relationship): for every binary 1:N relationship set, key attributes of the 1 side entity set are placed as a foreign key in the relation formed from the N side entity set. If the relationship has attributes, they are placed in the relation formed from the N side entity set (i.e., the same relation to which the foreign key was added).

Figure: Transformation of a binary 1:N relationship set.
Figure: Transformation of a binary 1:N relationship set.

Rule 8 (N:M relationship): a separate relation (so-called "relationship relation") is formed for every binary N:M relationship set. Key attributes of the entity sets related to the N:M relationship are chosen as attributes of the relationship relation's primary key. If the relationship has attributes, they are placed in the relationship relation.

Figure: Transformation of a binary N:M relationship set.
Figure: Transformation of a binary N:M relationship set.

Transformoidaan suhteet alla olevaan palautuslaatikkoon:

N:M-suhteet:

Tarkastellaan ensin ainoaa N:M-suhdetta Tenttii, jonka transformointi on suoraviivaista:

  1. Siirrä kursori tyhjälle riville ja lisää uusi relaatio "Uusi relaatio"-painikkeella.
  2. Aseta relaation nimeksi TENTTII. Tästä relaatiosta tulee suhderelaatio.
  3. Lisää relaation TENTTII attribuuteiksi relaatioiden OPISKELIJA ja KURSSI perusavaimeen kuuluvat attribuutit:
    • opiskelija_id (vastaa relaation OPISKELIJA perusavainta id)
    • kurssi_kurssikoodi (vastaa relaation KURSSI perusavainta kurssikoodi)
  4. Lisää relaation TENTTII attribuutteihin vielä suhteen Tenttii attribuutit
    • päivämäärä
    • arvosana
  5. Merkitse relaatioista OPISKELIJA ja KURSSI otettujen perusavainten sekä attribuutin päivämäärä yhdistelmä perusavaimeksi.
    • Maalaa attribuutit opiskelija_id, kurssi_kurssikoodi ja päivämäärä ja paina "Merkitse perusavaimeksi" -painiketta.
    • Huom: Tässä päivämäärä otetaan poikkeuksellisesti myös perusavaimeen, sillä ER-kaaviossa se on merkitty suhteen avainattribuutiksi. Tätä valintaa perusteltiin Harjoituksessa 2.1.
  6. Merkitse lopuksi opiskelija_id ja kurssi_kurssikoodi viiteavaimeksi
    • Siirrä kursori uudelle riville ja paina "Uusi viiteavain"
    • Aseta viiteavaimeksi attribuutti TENTTII.opiskelija_id
    • Aseta viitatuksi attribuutiksi OPISKELIJA.id
    • Toista sama attribuutille kurssi_kurssikoodi
Esimerkki: N:M-suhteen transformointi

1:N-suhteet:

Tarkastellaan suhdetta Opiskellee:

  1. Siirrä kursori relaation OPISKELIJA puolelle (sillä ER-kaaviossa kohteella OPISKELIJA on "haarukka" eli N-puoli Opiskelee-suhteessa).
  2. Lisää relaatioon OPISKELIJA attribuutti laitos_id, joka vastaa LAITOS-relaation id-avainattribuuttia.
  3. Merkitse laitos_id vielä viiteavaimeksi, joka osoittaa relaation LAITOS attribuuttiin id
    • Siirrä kursori uudelle riville ja paina "Uusi viiteavain"
    • Aseta viiteavaimeksi OPISKELIJA.laitos_id
    • Aseta viitatuksi attribuutiksi LAITOS.id
Esimerkki: 1:N-suhteen transformointi

Transformoi loput suhdetyypit itse käyttäen yllä olevia ohjeita.

Voit kelluttaa kaavioeditorin Kelluta-painikkeella. Silloin teksti siirtyy oikeaan laitaan, ja editorille vapautuu tilaa oikealle.

# 780197.ex31-step3
# 780197.reflect-ex31-step3

Finally

When you are ready, you can continue forward to other exercises.

You can give a short self-assessment of your success and general feedback on the exercise below.

# palaute

Lopuksi

Kun olet valmis, voit jatkaa eteenpäin muihin harjoitustehtäviin.

Voit antaa lyhyen itsearvion onnistumisestasi ja yleisesti palautteen harjoituksesta alla.

# palaute

These are the current permissions for this document; please modify if needed. You can always modify these permissions from the manage page.