12
Teacher Workshop (2017) Database Systems MYSQL WORKBENCH BASICS MySQL Workbench is a powerful visual tool developed by MySQL with three primary areas of functionality: SQL Development: It allows the user to connect to a database and execute SQL queries. Data Modeling: It facilitates visual database design and modeling. Database Administration: It provides a graphical interface to start/stop servers, create user accounts, edit configuration files, among others. After completing this practical class, students are expected to learn, using MySQL Workbench, how to: i) Interact with query interface, ii) Create and save a new data model, and iii) Forward Engineer a data model to the database Details on Workbench can be found here: https://dev.mysql.com/doc/workbench/en/ Source: Assoc. Prof. Abhaya Nayak, Assoc. Prof. Yan Wang, Dr. Shermin Akter, Ms. Hedieh Ranjbartabar, Mr. Rezwan Huq and Mr. Charles Liu

MySQL Workbench Basics - Macquarie University | Tertiary ... · Web viewSQL Development: It allows the user to connect to a database and execute SQL queries. Data Modeling: It facilitates

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: MySQL Workbench Basics - Macquarie University | Tertiary ... · Web viewSQL Development: It allows the user to connect to a database and execute SQL queries. Data Modeling: It facilitates

Teacher Workshop (2017)Database Systems

MYSQL WORKBENCH BASICS

MySQL Workbench is a powerful visual tool developed by MySQL with three primary areas of functionality:

SQL Development: It allows the user to connect to a database and execute SQL queries. Data Modeling: It facilitates visual database design and modeling. Database Administration: It provides a graphical interface to start/stop servers, create

user accounts, edit configuration files, among others.

After completing this practical class, students are expected to learn, using MySQL Workbench, how to:

i) Interact with query interface,ii) Create and save a new data model, andiii) Forward Engineer a data model to the database

Details on Workbench can be found here: https://dev.mysql.com/doc/workbench/en/

Source: Assoc. Prof. Abhaya Nayak, Assoc. Prof. Yan Wang, Dr. Shermin Akter, Ms. Hedieh Ranjbartabar, Mr. Rezwan Huq and Mr. Charles Liu

Page 2: MySQL Workbench Basics - Macquarie University | Tertiary ... · Web viewSQL Development: It allows the user to connect to a database and execute SQL queries. Data Modeling: It facilitates

1. INTERACTING WITH QUERY INTERFACE

Figure 1 shows the user interface after executing MySQL Workbench. As we can see from Figure 1, the previously saved connections can be used to connect to the server.

Saved connection. Click here to connect to the server

Figure 1: MySQL Workbench interface

If you click on that saved connection, you will see the query interface as shown in Figure 2. We can use this interface to develop SQL queries. We can see the query output at the bottom part of this interface as indicated in Figure 2. Moreover, at the upper-left side of this interface, we can see a few administrative options that describe on server status, client connections, user accounts and much more. We will explore these administrative functionalities later in this course.

2

Page 3: MySQL Workbench Basics - Macquarie University | Tertiary ... · Web viewSQL Development: It allows the user to connect to a database and execute SQL queries. Data Modeling: It facilitates

Write down the queries in this window

Administrative options

Query output will be displayed here

Figure 2: Query interface

You can write a query – show tables; in the query interface and it will return 0 rows as you have not created any tables yet.

2. CREATING AND SAVING A DATA MODEL

Figure 3 shows the other options that you can explore from the user interface as illustrated in Figure 1. Clicking the second icon on the shaded left-side navigation panel will result in the following interface as shown in Figure 3. Here, the user can create a new data model which we will explore more in this tutorial. The third option in the navigation panel is for migrating data to MySQL from other sources which will be discussed later.

3

Page 4: MySQL Workbench Basics - Macquarie University | Tertiary ... · Web viewSQL Development: It allows the user to connect to a database and execute SQL queries. Data Modeling: It facilitates

Click here to create a new data model

An existing sample data model

Data Migration Wizard (Advanced option)

Figure 3: Other options in MySQL Workbench

Figure 4 shows the data modeling interface after clicking the + icon to create a new model in Figure 3. Here, we can add an EER diagram to the model. Also, we can change the name of the default schema or create a new schema by clicking on the appropriate icon. Further, we can save the model for later use by exploring the File menu as shown in the figure or simply press CTRL+S.

Double click to add an EER diagram

Double click to change the name of the schema

Click to create a new schema

Click here to get the option of saving your model

Figure 4: Data Modeling Interface

4

Page 5: MySQL Workbench Basics - Macquarie University | Tertiary ... · Web viewSQL Development: It allows the user to connect to a database and execute SQL queries. Data Modeling: It facilitates

If you decide to create a new EER diagram, the interface as shown in Figure 5 will be displayed. We can see a drawing canvas to add different components to the diagram. The components are listed in a vertical bar at the left side of the canvas. We are mainly interested to see the use of Tables and Relationships to develop the data model as these two are the main building blocks of the conceptual design of a database. There are different types of relationship as we can see from Figure 5. Identifying relationship defines a relationship between a strong and a weak entity set (existence dependent on strong entity set) where as Non-identifying relationship indicates a relationship between two strong entity sets. In this manual, we will use non-identifying relationship. Details about them will be covered in your theory and tutorial classes.

You can create a table by clicking on the appropriate icon as shown in Figure 5 and then click on the canvas or simply press T. That will result in Figure 6 which shows a table that has been added in the diagram. However, you want to change the name of the table and to add attributes to the table. To do so, you should right click on the table and then choose ‘edit table1’ option as shown in Figure 6. You can modify the table by also double clicking on the table.

Click here to create a table in the diagram

Click here to create a relationship between tables in the diagram

Tables and Relationships will appear here

Figure 5: Adding a new diagram

5

Page 6: MySQL Workbench Basics - Macquarie University | Tertiary ... · Web viewSQL Development: It allows the user to connect to a database and execute SQL queries. Data Modeling: It facilitates

First right-click on the table and then click here to edit the table or double-click on the table

Figure 6: Adding a table in the data model

Figure 7 shows how you can edit a table. Assume that you want to create a table named Person. The Person table has two attributes such as person_id and person_name that have INT and VARCHAR as data-types, respectively. You can edit this table as shown in Figure 7. Figure 7 also shows some integrity constraints such as PK (primary key), NN (not null), UQ (unique index), AI (auto incremental) and much more. Details can be discussed in the class.

Give a name of the table

Add attributes and appropriate datatype

Figure 7: Editing a table

6

Page 7: MySQL Workbench Basics - Macquarie University | Tertiary ... · Web viewSQL Development: It allows the user to connect to a database and execute SQL queries. Data Modeling: It facilitates

In this tutorial, one of the objectives is to make students familiar with the data modeling using MySQL Workbench. Therefore, you will create another table named Country which has two attributes such as country_id and country_name. Attributes must have an appropriate data type. After creating two tables Person and Country, the resulting data model is shown in Figure 8.

i) Click here to create a one-to-many relationship from Country to Person ora many-to-one relationship from Person to Country

ii) Click on the table of ‘many’ side. In this case, it is Person

iii) At last, click on the table of ‘one’ side. In this case, it is Country

Figure 8: Creating a relationship between tables

Figure 8 also explains how you can establish a relationship between these two tables. We can safely assume the following about the table Person and Country.

A Person must come from a Country. A Country may have many Persons.

Based on these, we can relate Person and Country table using a many to one relationship from Person to Country. Choose the appropriate relationship (1 to n, non-identifying relationship) as shown in Figure 8. Then first click the many side (Person Table) and then click the one side (Country Table). Congratulations, you have created your first relationship between tables. You can see the result in Figure 9.

Figure 9 shows the data model that we just built. It has some interesting features. First, you can see the country_id attribute of Country table is added into the Person table which is named as

7

Page 8: MySQL Workbench Basics - Macquarie University | Tertiary ... · Web viewSQL Development: It allows the user to connect to a database and execute SQL queries. Data Modeling: It facilitates

Country_country_id. This is the Foreign key of Person table since this attribute is the Primary key/Candidate key of another table (Country table).

Second, you can see the notations used to represent the many side. It is known as Crow’s foot/feet. Remember, Crow’s foot always indicates the many side of a relationship.

Foreign key added

Crow’s foot indicates ‘many’ side

Figure 9: Our first data model

3. FORWARD ENGINEERING A DATA MODEL TO THE DATABASE

Finally, we will use Forward Engineering, a feature in MySQL Workbench, which will transform the data model into tables in the database. Before using forward engineer, ensure that you have changed the schema name to your database name which is your student id. Figure 10 shows the way to forward engineer a data model.

8

Page 9: MySQL Workbench Basics - Macquarie University | Tertiary ... · Web viewSQL Development: It allows the user to connect to a database and execute SQL queries. Data Modeling: It facilitates

Figure 10: Forward Engineering a data model

Once you click on Forward Engineer as shown in Figure 10, a series of window will appear to confirm the connection parameters and other optional parameters. Keep clicking Next and you will see the window as shown in Figure 11.

Figure 11: Generated SQL Script

9

Page 10: MySQL Workbench Basics - Macquarie University | Tertiary ... · Web viewSQL Development: It allows the user to connect to a database and execute SQL queries. Data Modeling: It facilitates

Once you click on Next as shown in Figure 11, another window will appear as depicted in Figure 12. This is the confirmation window about forward engineering being successful.

Figure 12: Confirmation of forward engineer to Database

Now you may want to open the query interface again as shown in Figure 2 and write the query –show tables;

What did you see?

In this lab tutorial, you have seen the basics of MySQL Workbench especially how to create a simple data model and forward engineer the model. In coming weeks, we shall explore the data modeling feature of MySQL Workbench in detail.

10