Workbench
Workbench
1 CONTENTS
2 MySQL Workbench. Introduction .................................................................................. 2
2.1.1 Considering that you have just finished the installation, now the GUI will open
(MySQL Workbench) with the configuration already set. (Figure 1) ............................... 2
2.1.2 Now you are connected to the MySQL database server and you can start
creating a database. Select the “Schemas” tab to view a list of databases running on the
local server. (Figure 4) .................................................................................................. 4
2.1.3 There will be some example schemas installed. You can expand the schema and
the “Tables” section to view the tables in the database (Figure 5) ................................. 4
2.1.4 Right click on a table name (e.g. city) and click on “Select rows – limit 1000”. This
will show you a list of the first 1000 rows in the table, using a simple SELECT SQL query
that you can view in the top window. (Figure 6) ............................................................ 6
2.1.5 Right click on a table name (e.g. city) and click on “Alter Table”. This allows you
to view and modify the table definition (table name, columns, constraints, indexes,
foreign keys, etc.). (Figure 7) ......................................................................................... 6
1
2 MYSQL WORKBENCH. INTRODUCTION
This section presents a tutorial for working with MySQL Workbench: connecting to the
database server, working with schemas, tables and data with a visual interface/GUI.
2.1.1 Considering that you have just finished the installation, now the GUI will open
(MySQL Workbench) with the configuration already set. (Figure 1)
This will be your visual tool to connect to the database server, used for working with
databases and tables, viewing and adding data and writing SQL queries. Click on “Local
Instance MySQL80” in the “MySQL Connections” list to open a connection to the database
server.
Note: if you don’t see the connection in the list, then you might have skipped a step in the
installation guide. It’s no problem, you can create a new connection by clicking on the “+”
2
icon next to “MySQL Connections” as in (Figure 2). Then click “Ok” and move on to the next
step.
Connect to the MySQL server using the root password provided during the setup (e.g.
ewis2020). (Figure 3)
3
2.1.2 Now you are connected to the MySQL database server and you can start creating
a database. Select the “Schemas” tab to view a list of databases running on the
local server. (Figure 4)
2.1.3 There will be some example schemas installed. You can expand the schema and
the “Tables” section to view the tables in the database (Figure 5)
Note: The example schemas will be shown if you followed all steps in the setup tutorial. If not,
then you may skip this part and keep it as reference for when you will create your own
schemas and tables.
4
Figure 5 Expanding database schema and tables
5
2.1.4 Right click on a table name (e.g. city) and click on “Select rows – limit 1000”. This
will show you a list of the first 1000 rows in the table, using a simple SELECT SQL
query that you can view in the top window. (Figure 6)
2.1.5 Right click on a table name (e.g. city) and click on “Alter Table”. This allows you
to view and modify the table definition (table name, columns, constraints,
indexes, foreign keys, etc.). (Figure 7)
Note: On small screens (e.g. laptop) some tabs will not show properly. If this is the case, you
may drag the bottom tab (Action Output) to fill less space, so that you can view the (more
important) column definitions.
6
Figure 7 Alter Table
Click on the arrow next to the company schema, then right click on “Tables”, and click on
“Create Table”. A new table is about to be created. Change the “Table Name” field to
department.
8
3.1.2.1 Adding columns and constraints
To add a new column to a table, click on the first blank row under “Column Name”. Then,
change the name to id to define the first attribute for the department table 1. The id attribute
will be of type INT (leave INT in the “Datatype” field) and will be the PRIMARY KEY for the
department table. Select AI (Auto Increment) constraint to make the id auto generated.
Then, continue to add the other column definitions as shown in Figure 10. After adding all
columns, click “Apply”. A popup window will open, where you can view the DDL command
generated by MySQL Workbench:
Click on “Apply” to execute the DDL command and create the table into the database, then
click on “Finish”.
1
The id column makes it easier to uniquely identify records in the created tables, otherwise the PRIMARY KEY
can also be a combination of attributes (in this case name and location can uniquely identify a department)
9
Figure 10 department table definition
3.1.2.2 Then, proceed to add the other tables in the same way: employee, position.
10
Figure 12 position table definition
After creating the tables, they will show in the schemas navigator as shown in Figure 13.
Department – Employee
Relationship description
• A department can have one or more employees
• An employee can belong to a single department
• Implementation: one-to-many relationship
Implementation
We need to define a FOREIGN KEY in the employee table to reference the department table.
Right click on employee table, then click on “Alter Table”. Switch to “Foreign Keys” tab and
create the FOREIGN KEY constraint as shown in Figure 14. You have to define a name, the
referenced table, the column that will be set as foreign key, the referenced column and the
ON UPDATE and ON DELETE rules (we will use CASCADE for all foreign keys in this example).
12
Employee – Position
Relationship description
• An employee can have a single position in the company
• There can be more employees with the same position in the company (A position may
be assigned to one or more employees)
• Implementation: one-to-many relationship
Implementation
We need to define a FOREIGN KEY in the employee table to reference the position table. In
the “Foreign Keys” tab in employee table, add the FOREIGN KEY constraint as shown in Figure
15.
Employee – Employee
Relationship description
• An employee can have a single manager (which is also an employee of the company)
• There can be more employees that have the same manager (A manager may be
assigned to one or more employees)
13
• Implementation: one-to-many/self-referencing relationship
Implementation
We need to define a FOREIGN KEY in the employee table to reference the employee table. In
the “Foreign Keys” tab in employee table, add the FOREIGN KEY constraint as shown in Figure
16.
14
To remove an existing row, Right click on the button to the left side and click on “Delete
Row(s)”. As before, you have to click on “Apply” to remove the rows from the database.
Alternatively, you can click on “Revert” and the operation will be cancelled (the rows will be
restored to the table view).
15
5 EXPORTING DATA FROM A TABLE
To save the result of a query, you can export as csv file as shown in Figure 19. Give the file a
name and then open the file with a text editor. You will see the content in csv format that you
can use later to process the data.
Q1
16
Q2
-- List the employees name and salary. Show only those having the salary
between 5000 and 7000 EUR and order them descending by salary --
SELECT name, salary FROM employee
WHERE salary BETWEEN 5000 AND 7000
ORDER BY salary DESC;
Q3
Q4
Q5
Q6
Q7
17
Q8
Q9
Q10
Q11
Q12
-- List the employees name, salary, the name of their department and position
--
SELECT emp.name AS employee, dept.name AS department, pos.name AS POSITION,
emp.salary FROM employee AS emp
INNER JOIN department AS dept
ON dept.id = emp.department_id
INNER JOIN POSITION AS pos
ON pos.id = emp.position_id;
18