DEV Community

Cover image for Designing a database with MySQL using the Workbench — Part 3
Jimmy Gitonga
Jimmy Gitonga

Posted on

Designing a database with MySQL using the Workbench — Part 3

If you have just stepped in, please have a look at the first two parts in this series, Designing a MySQL database using the Command Line Interface, Part 1 and Part 2. We continue to work with the database we developed in these two articles. As in the previous articles, pick any name for your tables in the database if you find these a bit too abstract.

Now that we are comfortable with the building of a database in MySQL through the Command Line Interface, let us look at designing the databases through the MySQL Workbench 6.3.x. For information on installation procedures for different OSs, look at the ‘workbench-en.pdf’ manual available from the MySQL website. It also has a tutorial on how to build a database in the Workbench.

In this part, we will work with the database “Waks_Noma” that we built previously. We will import the database into the MySQL Workbench(Workbench), understand the visual representation of our _waks_noma_database, add a table into the EER(Entity-Entity Relationship), export it into the existing live database and make queries from Workbench.

Workbench

When one opens Workbench, the initial screen looks like so:

Workbench splash screen

To work on the waks_noma database, we will open our MySQL instance on our local machine.

Click on the Local instance 3306 button and you may be asked enter the name and password.

NOTE: When working in a multi-user environment and online to a live remote server, the name and password security issues should be taken very seriously.

We get:

Workbench Dashboard

The schemas of the databases present on the local computer appear Schemastab at the bottom left. We choose our waks_noma and collapse the Management tab.

Workbench Query Window

a) Adding the Designers’ Table: waseenoma

With waks_mona selected, click on Add Table:

Workbench Add Table

Add the new name add the following columns:

Workbench Table Add Columns

NOTE: Set the PK (Primary Key) to AI( auto-increment), and make sure all tthe colums are NN (NOT_NULL). We will click on Apply and we see something familiar:

Workbench Review Script

Click on Apply to run the script, and get our new table, waseenoma:

Inspect Table

Let us go to our command line and confirm that it works:

mysql> show tables;
+---------------------+
| Tables_in_waks_noma |
+---------------------+
| majobo              |
| majobskils          |
| majobtek            |
| maklayo             |
| maklayojobo         |
| maskils             |
| teknoma             |
| waseenoma           |
+---------------------+
Enter fullscreen mode Exit fullscreen mode

Success.

b) Adding Designers’ names into ‘waseenoma’

Let us go back to our Schemas tab, select waseenoma table. Click on the icon on the right :

Insert Data

We insert our data in the editing tab that is named waseenoma 1 and click on the Apply changes to data :

Apply to save data

Click on Apply:

Review script to apply data

Click on the Apply again.

Now to check that the data has really been applied to the waseenoma, at the command line we get:

mysql> select * from waseenoma;
+----------+--------------+-------------------+
| mnoma_id | mnoma_name   | mnoma_desc        |
+----------+--------------+-------------------+
|        1 | Mchora       | Graphics Chief    |
|        2 | Blossom      | Layout Sweetener  |
|        3 | Helium Ninja | Content Developer |
|        4 | Msafara      | Video Converger   |
+----------+--------------+-------------------+
4 rows in set (0.00 sec)
Enter fullscreen mode Exit fullscreen mode

We are certain that the Workbench works and we can go onto work with EER diagrams.

In the Home tab and click on the “ > ” icon*:*

Workbench create EER Dashboard

Click on the “Create EER Model from Database”.

Workbench Set EER parameters

Unless there is a good reason, like you have many users on the machine, leave these settings as they are. Continue to connect to the DBMS:

Workbench Connect to database

Click on continue:

Workbench Connect to database 2

We choose our database waks_noma:

Workbench Connect to database 3

We execute:

Workbench Connect to database 4

Once we have completed the steps, we open the EER Diagram and arrange our ERDs:

Workbench View EER

c) Joining majobo and waseenoma: majobnoma Table

Workbench join tables

Above the waseenoma table, go an pick “Place a New Table” and insert a table majobnoma. Edit majobnoma and in the Foreign Keys tab, create the FKs to majobo.job_id and wanoma.mnoma_id.

Workbench join tables 2

We notice the “Crow Feet” connection that automatically appear between the majobo table and the wanoma table. You will notice that the existing “joining tables” that we made directly in the command line don’t automatically relate to their respective table FKs.

We will correct this by changing them from the columns that are in the tables and using the Foreign Keys tab update them. Make sure that you connect the column in majobnoma and the referenced column both majobo and waseenoma.

Workbench join tables 3

Once we finish the connecting the joining tables; majobskils, makalyojobo and majobtek, the new EER looks like so:

Workbench View join tables

Exporting the EER to waks_noma

Finally we need to implement this EER onto the actual database. When we query the database, we get:

mysql> show tables;
+---------------------+
| Tables_in_waks_noma |
+---------------------+
| majobo              |
| majobskils          |
| majobtek            |
| maklayo             |
| maklayojobo         |
| maskils             |
| teknoma             |
| waseenoma           |
+---------------------+
8 rows in set (0.07 sec)
Enter fullscreen mode Exit fullscreen mode

As we can see the database does not know about the majobnoma table. In the Workbench, go to the Home tab and get the to the local instance. When you look at the tables, it is clear the majobnoma table is missing.

So we will set up Workbench to create the SQL statements we need to sync the EER diagram with the live database schema. Go to the File > Export > Forward Engineer SQL CREATE Script.

Workbench Link EER with database

Add a name for the SQL script and press continue. The instructions for the options in this tab can be found here — Forward Engineering Using an SQL Script.

Workbench Link EER with database 2

The “Export …” will be highlighted:

Workbench Link EER with database 3

We export the table objects and save. Make sure you are on the local instancetab. We then open the script to run it:

Workbench Link EER with database 4

The following window pops up:

Workbench Link EER run SQL script

And if you have done everything right:

Workbench Link EER success

Opening the Workbench local instance, we can see the majobnoma is in the database now:

Workbench check table majobnoma

And checking through the CLI:

mysql> show tables;
+---------------------+
| Tables_in_waks_noma |
+---------------------+
| majobnoma           |
| majobo              |
| majobskils          |
| majobtek            |
| maklayo             |
| maklayojobo         |
| maskils             |
| teknoma             |
| waseenoma           |
+---------------------+
9 rows in set (0.00 sec)
Enter fullscreen mode Exit fullscreen mode

If you have reached this far, be rest assured you are on your way to being a on your way to being a MySQL ninja!

That concludes our series on designing relational databases using MySQL using the CLI and the Workbench desktop application. If you have enjoyed the series, please give us some feedback to encourage us to extend development of databases with MySQL or PostgreSQL.

Thanks for reading and we hope you at least enjoyed yourself.

Top comments (0)