Demo 2

Transforming an ER-diagram to a relation

If you haven't finished demo 1 yet then please continue with it before proceeding with this exercise.

Turning an ER-diagram into a relation has the following rules of thumb:

  1. Every ordinary entity is turned into a relation (table)
  2. Every attribute in the diagram will be added to the associated relation
  3. Every Many-to-Many -relationship is turned into an ordinary relation.
  4. M-to-1 -relationships
  5. Multi-valued attributes are turned into new entities and references from them to the entities, that had the multi-valued attributes in the first place, are made.

Let's turn the ER-diagram we made last time to relations
A model solution to the first one of the last week's excercises

  1. Open the Excel document you made on the first round of excercises.
  2. Last time you made tables where you listed every entity and the attributes connected to them. All these definitions function as a base for creating relations. Update those definitions to correspond to the tables in the database.
  3. M-to-1 -relationships

  4. M-to-M -relationships

  5. 1-to-1 -relationships

  6. Multi-valued attributes

Normalization

Let's make sure there is no unnecessary repetition or excess of information in the relations. By normalizating the database one can avoid problems in updating, adding or deleting data.

Check the following:

In our student database there is one problematic thing:

Company database

Next job: We'll transform the ER-diagram of the company database we made last time to relations. If you have no company database of you own, use the model solution. Fear not, if your own version is not similar to the model solution.

  1. Treat every entity as a new relation. Every table of attributes connected to an entity means fields to be created to the relation.
  2. First, find all the M-to-1 -relationships in the ER-diagram. Add the new fields (foreign keys) to the relation that is on the M-side of the relationship. Make sure you have the correct datatypes, that is the datatypes in the new fields are the same as in the field they refer to.
  3. Find all the M-to-M -relationships in your diagram. Create a new relation for every M-to-M -relationship. As the name of the relation you can use the name of the relationship or derive a new name from it. If you have relationships that have attributes connected to them you already have a base for the relation (see entities).
  4. Add new fields for foreign keys to every relation made of M-to-M -relationships (one field per one entity in the relationship). Remember the correct datatypes! Define the composite keys that consists of the foreign keys as the primary keys for the new relations.
  5. If there are multi-valued attributes in your diagram, create new relations for them. Remove the multi-valued attribute from the entity it was connected to and name the new relation accordingly.
  6. Define the rules for cascades of referential integrity for every foreign key you have.

Normalization

Check every relation in you company database and see if they meet the following requirements:

In case you find problematic relations, divide them as in the student database -example.

Soccer database

Transform the soccer tournament diagram you made last time to relations the same way as in previons examples. The model solution may be very different to your version but use your own if you have it.

After that, check the construction of your database by normalizing it. Fix what there is to fix.


http://appro.mit.jyu.fi/2001/kevat/tietokannat/demot/demo2/index.html
© Tommi Lahtonen ()<URL: http://www.iki.fi/hazor/>
16.03.2001 18:26:07