Database Exercises
Create Model

You can use MySQL Workbench when you are designing a database. You can do it example like this:

  • Plan your database ER-diagram with paper and pencil
  • In Workbench Add New Model and re-name it (the name will be your database-name)
  • Add the tables with columns
  • Add the Foreign keys to child-tables
  • Add a Diagram
  • Drag the tables to the diagram

Database from Model

When you have created the Model, you can syncronize this model to your MySQL-server. Which means that Workbench will generate the tables for you. Let's make an example.

  1. Download this model studentRegister.mwb
  2. Open the Model with Workbench
  3. Start your MySQL-Server and connect to it with Workbench
  4. In Workbench choose the EER Diagram -tab
  5. From Database-menu choose Syncronize Model
  6. Click Next until you get the View where you can check studentRegister and click Next
  7. Check that the arrows are like in Image1 and click Next
  8. In Workbench choose the localhost -tab and refresh the page (Image2), now you should have the database studentregister

student image
Image1
student image
Image2

Model from Database

With MySQL Workbench you can also create a model from your existing database.

  1. In Workbench click->File->New Model
  2. Rename the mydb to library
  3. click Database->Syncronize Model
  4. Choose your connection (=local_mysql)
  5. Click next until, you can choose Model Schema and RDBMS Schema, both should be library
  6. Then you have to be very careful: you should Update Model, otherwise you will destroy your database. The arrows should be like this:
    workbench image
  7. Click Add diagram
  8. Drag the tables to the Diagram. The diagram should look like this:
    workbench image
  9. You can edit the symbols if you click the line between tables and choose Foreign key. So, example if you remove the Mandatory from borrow table, you will get below ER-diagram. But editing those options does not really affect anything in the database. They only affects to the diagram.
    workbench image

Example : CarsDb

In this exercise you will make a database from the backup-file(carsdb.sql) and then create the model based on the database.

Restore the database
  1. Download the file carsdb.sql
  2. In MySQL Workbench: Server->Data Import
  3. Check the option "Import from Self-Contained File
  4. Click the ellipses-button (...) and choose the file carsdb.sql
  5. Click Start Import
  6. Refresh the Schemas View and check that you have the database with tables: car,carownner, owner

Create the Model
  1. Follow the instructions from previous exercise. Note that the model name should be carsdb.
  2. Create the ER-Diagram. The diagram should look like this
    er diagram



Toggle Menu