WithMySQL Workbench
WithMySQL Workbench
Jerzy Letkowski
Prerequisites - Downloads
MySQL Server and MySQL Workbench are expected to be installed. The
setup program(s) can be downloaded from the MySQL Website.
This instruction uses the server and workbench installed using the May 14,
2014 installer, downloaded from http://www.mysql.com/:
Tab: Downloads (GA)
MySQL Community Edition (GPL): Download from MySQL Developer Zone
MySQL on Windows (Installer & Tools)
MySQL Installer: DOWNLOAD
Windows (x86, 32-bit), MSI Installer (mysql-installer-community-5.6.17.0.msi):
DOWNLOAD
The 5.6.17 version was available, at the time of writing this instruction.
Other major operating systems are also supported, including Mac OS X
and Linux.
The download zone of the MySQL Website also provides specialized
installers for the server, workbench and other utilities (connectors, Excel
tool, etc.).
Prerequisites - Installation
Using the standard Window installer is pretty straight forward. Just follow
the default developer's options.
It is very important that you do not forget the root password.
The following document:
Installation of MySQL on Windows*
shows detail steps for a typical Windows installation.
When done, follow the remaining slides to learn more about database
design and implementation, using MySQL Workbench and Community
Server.
* If the link does not work, use the following URL in your browser:
https://docs.google.com/presentation/d/1bMpWcT9MvNLqo_wMIndXyH2rj5K6AyQOK-l2OUogBf4/edit?usp=sharing
Start (double-click) MySQL Workbench!
Click the add model icon (+) in order to create a new relational database model.
Double-click the default schema (database) name tab (mydb) in order to create
a new database name.
Type name election,
and double-click the Add Diagram icon.
MySQL Workbench opens the Diagram canvas along with other useful panels,
including the Birds Eye view panel, and Catalog Tree panel.
Basic Tools:
Table
Relationships:
1:1 (One-to-One) Non-identifying
1:n (One-to-Many) Non-identifying
1:1 (One-to-One) Identifying
1:n (One-to-Many) Identifying
n:m (Many-to-Many) Identifying
1:n (One-to-Many) Using Existing Columns1
The Toolbar contains graphical tools that are used to paint Enhanced Entity
Relationship (EER ) diagrams.
Problem Statement
A Database for an Online Voting System
Our mission is to design a database for an on-line election system that will be utilized
to conduct election of new leaders of a non-profit organization. The organization has
members some of which hold leadership positions (president, vice president, treasurer,
newsletter editor, annual meeting coordinator, secretary, etc.). Some of the members
have been nominated to run for the positions. Assume that they have already accepted
their nominations, thus becoming official candidates for the available positions. The
organizations statute states that each candidate may only run for one position and
each member may cast no more than one vote for each of the positions. The database
should facilitate the voting process and record all votes assigned to the candidates but
it should not tell which member has voted for which candidate.
Entities and Relationships
From the problem definition one could identify the following base entities:
Member: a list (set) of members of the organization, some of whom are candidates;
Office: a list of positions ('president', 'treasurer', 'editor', etc. );
Vote: a list of votes cast for the candidates.
Other entities result from associations between the base entities.
Entity Member
To add entity (table) Member to the data model (EER diagram) click the New Table
icon.
Next click anywhere on the Diagram canvas.
In order to change the name of the table and add its attributes,
double-click table table1.
Type name Member.
Notice that the primary key is automatically set as Not NULL (NN). Datatypes INT, VARCHAR and
CHAR stand for an integer, a variable-length string (text) and a fixed-length string, respectively.
Some individual have hard time of visualizing abstract entities. The attached Excel
macro-workbook, election.xlsm*, contains different depictions of the entities.
sqlInsert(table_name_abs_ref,data_type_abs_ref,record_ref),
that transforms rows of the tables into their SQL-Insert statements. In order to
facilitate this function, a row with "high-level" data types is added (right above the
column names), where n stands for numeric and cnonnumeric.
Note that the last part of this instruction shows how to transform the ERD view
into the SQL statements. Typically, when designing a new database, the SQL
representations are not available until the ERD model is completed and
transformed into SQL. The reason for showing these views at this stage of the ERD
model development is more of a pedagogical nature.
* If the link does not work, use the following URL in your browser:
http://quantlabs.com/db/design/election.xlsm
The Member worksheet of the election.xlsm workbook shows three views of the
Member entity: UML, SQL and Excel Table.
The sqlInsert() function transforms the rows of the Excel Table into SQL-Insert
statements.
To add entity (table) Office to the data model (EER diagram) click the New
Table icon.
Change the name of this table to Office.
The sqlInsert() function transforms the rows of the Excel Table into SQL-Insert
statements.
Notice that m:n stands for Many-to-Many. Since a member (instance of entity Member) participates in
election of many offices (positions) and a given office (instance of entity Office) is being elected by many
members, this is a Many-to-Many relationship.
Despite selecting the UML notation, the relationship tools are shown, using the Crow-Foot convention.
MW inserts a new [default] entity, Member-has-Office and sets the relationships of this entity
with Member and Office to Many-to-One. This is a typical resolution of the Many-to-Many
relationships as required by the relational database model.
The sqlInsert() function transforms the rows of the table into SQL-Insert
statements.
Candidate
When
When done,
done, close
close the
the Candidate Table panel.
Candidate Table panel.
To change the cardinality constraint at the Candidate entity, first double-click the
Member - Candidate link.
Select the Foreign Key tab and uncheck
option Mandatory in the Candidate panel.
MW connects entity Office with entity Candidate with a One-To-Many (1 : 1..*) Non-
identifying Relationship. Notice that this non-identifying relationship link is shown as
dashed line.
Double-click the Candidate table and then change column name
Office_oid to oid.
The sqlInsert() function transforms the rows of the table into SQL-Insert
statements.
MW connects entity Candidate with entity Vote with a One-To-Many (1 : 1..*) Non-
identifying Relationship. This relationship needs to be relaxed as there may be candidates
who will not receive any votes.
Double-click the Candidate - Vote relationship link.
Select the Foreign Key tab and uncheck
option Mandatory in the Candidate panel.
The sqlInsert()function transforms the rows of the table into SQL-Insert statements.
Save the script to a file, click button Copy to Clipboard, and then click button Next.
MW connects to the server (DBMS) and executes the SQL script. The logical design has just been
transformed [automatically] into a physical database.
With all the Insert statements in the Query panel, click the Execute button.
The Output panel shows feedback for each of the executes statements.
In order to clear the Query panel, select all the Insert statements (press Ctrl+A) and press
the Delete key.
In a similar way, bring the other Insert statements (for tables
Office, Ballot, Candidate and Vote) from the Excel
workbook, election.xlsm , to the MW's Query panel.
Execute the statements and explore the database by running
a few queries.
After all the sample records have been added, run a few queries. The first example shows all
positions stored in the Office table.
Type statement SELECT * FROM Office; into the Query panel and click the
Execute button.
Query result!
Query status!
The second example shows all the candidates.
Type statement
SELECT * FROM Member WHERE mid IN (SELECT mid FROM Candidate);
into the Query panel
and click the Execute button.
Type statement
SELECT * FROM Member JOIN Candidate USING(mid) JOIN Office USING(oid);
into the Query panel
and click the Execute button.
The fourth example shows all the members who have not yet picked any of their ballots.
Type statement
SELECT mid, lastName FROM Member WHERE mid NOT IN (SELECT mid FROM Ballot);
into the Query panel
and click the Execute button.
Type statement
SELECT mid, Count(Ballot.mid) FROM Ballot GROUP BY mid;
into the Query panel
and click the Execute button.
A purist would also show the table names in the title case. Finally, save the model and close MW.
This is it!