(winter 2016) instructor: craig duckett lecture 09: thursday, february 4 th mysql workbench, team...

33
(Winter 2016) Instructor: Craig Duckett Lecture 09 : Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

Upload: emory-tate

Post on 19-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: (Winter 2016) Instructor: Craig Duckett Lecture 09: Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

1

(Winter 2016)

Instructor: Craig Duckett

Lecture 09: Thursday, February 4th

MySQL Workbench,TEAM WORK TIME

Page 2: (Winter 2016) Instructor: Craig Duckett Lecture 09: Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

2

PHASE 2: DESIGN DUE Tuesday, February 16th, uploaded to Team Web Site and ZIPPED and uploaded to StudentTracker by Phase 2 Project Manager

Phase 3: Develop due Tuesday, March 1st

Page 3: (Winter 2016) Instructor: Craig Duckett Lecture 09: Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

3

The Team Project

Five Phase Due Dates

One (1) Team Project for a Client (3-to-4 Members on Team) 1000 points Total

• Phase 1: Discovery (200 Points) GRADED!• Phase 2: Design (200 Points) DUE TUESDAY, FEBRUARY 16th • Phase 3: Develop (200 Points) DUE TUESDAY, MARCH 1st • Phase 4: Distribute (200 Points) DUE THURSDAY, MARCH 10th • Phase 5: Documentation (200 Points) DUE THURSDAY, MARCH 17th (Last Day of Class)

Page 4: (Winter 2016) Instructor: Craig Duckett Lecture 09: Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

4

• MySQL Workbench• Team Work Time

Page 5: (Winter 2016) Instructor: Craig Duckett Lecture 09: Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

MySQL Workbench

Page 6: (Winter 2016) Instructor: Craig Duckett Lecture 09: Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

MySQL Workbench

While most MySQL tasks can be performed using command-line tools, many tasks can actually be made easier using the MySQL Workbench tool. This is a graphical user interface (GUI) tool that allows many tasks to be performed visually.

The workbench also includes features for creating and modifying database models, and a SQL Editor environment for interactively executing SQL statements and creating SQL scripts.

Getting and Installing MySQL Workbench

The MySQL Workbench tool is not installed by default along with the standard MySQL server and client software, so it must be downloaded independently from the MySQL web site at:

http://www.mysql.com/products/workbench/

The tool is provided pre-built for a number of platforms including Microsoft Windows, Mac OS X and popular Linux distributions including Red Hat, Fedora, Ubuntu and SuSE. The workbench is also available in source code form allowing the tool to be compiled on system for which pre-built binaries are not available.

Page 7: (Winter 2016) Instructor: Craig Duckett Lecture 09: Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

MySQL WorkbenchCreating a Server Instance Profile

Once MySQL Workbench has been installed it is ready to be launched. On Windows system, look for MySQL Workbench within the Start menu structure. One Linux systems using the GNOME desktop, the application can usually be found in the Applications -> Programming menu. Once invoked, the initial screen will appear as illustrated in the following figure:

Page 8: (Winter 2016) Instructor: Craig Duckett Lecture 09: Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

MySQL WorkbenchThe workbench uses a “tabbed panel” approach to presenting information. On the initial startup, only the Home tab is displayed providing a range of options that may be performed such as creating and executing SQL queries and scripts and performing data modeling tasks. For the purposes of this chapter, however, we are interested solely in server administration.

MySQL Workbench is able to administer both local and remote MySQL server instances. Before any administrative tasks can be performed, however, a connection to the target MySQL server must first be established. This is achieved by clicking on the New Server Instance option located in the Server Administration column of the home screen. Once selected, the Create New Server Instance Profile wizard will appear. The first screen provides the option to access either a local (i.e. a MySQL database server running on the same computer as the workbench tool) or remote database server.

Page 9: (Winter 2016) Instructor: Craig Duckett Lecture 09: Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

MySQL WorkbenchOn the next screen, enter a descriptive name for the connection, indicate the connection type (TCP/IP, local socket or the more secure TCP/IP with SSH) and specify the name of the user and the port via which the database is to be accessed. Additional options are available by selecting the advanced tab:

After clicking the Next button, the wizard will request the password required to access the database using the user name specified and then attempt to test the connection. If a connection is successfully established a message indicating this will be displayed. If the test fails, return to the previous screen and verify that the connections settings are correct.

Page 10: (Winter 2016) Instructor: Craig Duckett Lecture 09: Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

MySQL WorkbenchOn the next screen, select the operating system and database type that are installed on the target system. If the wizard successfully detected these settings during the connection test, these values will most likely already be accurately configured. On the next screen, the wizard will check the connection to the host machine on which the workbench is running. Once again, the Next button may be pressed to proceed if the test passes successfully.

Finally, review the server instance name and click the Finish button to complete the instance creation process. The new server instance will now be listed in the Server Administration column of the home screen as illustrated in the following figure:

Page 11: (Winter 2016) Instructor: Craig Duckett Lecture 09: Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

MySQL WorkbenchConnecting to a MySQL Database Server

To connect to a database server to perform administrative tasks, simply double click on the server instance created in the previous section of this chapter. MySQL Workbench will prompt for the password associated with the user name contained in the profile and then display the Admin panel for the requested server instance:

Note that the Home panel is accessible at any time simply by clicking on the Home tab located beneath the toolbar. In addition, multiple administrator sessions, connected to different servers, may be establish concurrently from the home panel. Each administration session will appear with its own tab in the main window.

Page 12: (Winter 2016) Instructor: Craig Duckett Lecture 09: Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

MySQL WorkbenchA Tour of the MySQL Workbench Admin Panel

The MySQL Workbench tool provides a wide range of monitoring and configuration options. For the remainder of this chapter we will provide an overview of the various options available.

Server Status

The topmost panel of the admin screen contains information about the server to which the workbench is currently connected:

The Server Status panel is further divided into three subsections:

• INFO - Displays information about the server (hostname, MySQL server version and current operational status).

• SYSTEM – The current load and memory usage of the system that is hosting the MySQL server to which the workbench is currently connected.

• SERVER HEALTH – Four gauges indicating the number of connections to the database, volume of traffic, rate of hits to the query cache and the database key efficiency percentage.

Page 13: (Winter 2016) Instructor: Craig Duckett Lecture 09: Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

MySQL WorkbenchConfiguration

The Configuration panel, illustrated in the following figure, occupies the remainder of the Admin panel and provides a range of categories for accessing information and making configuration changes:

Page 14: (Winter 2016) Instructor: Craig Duckett Lecture 09: Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

MySQL WorkbenchConfiguration categories provided by the workbench are as follows:

• Startup – Allows the target database server to be stopped and started from within the administration panel. The startup message log also displays information about the success or otherwise of the most recent server startup.

• Configuration – Allows the settings contained in the my.cnf configuration file to be modified. Settings are categorized in this panel into related groups.

• Accounts – Allows user accounts to be added and deleted, and the privileges of existing accounts to be modified.

• Connections – Lists the current connections to the database server. To kill a connection or terminate a query, select the corresponding connection and click on the appropriate button at the bottom of the list.

• Variables – Provides access to a number of internal MySQL Server variables, such as the number of active connections, the number of failed connections and the number of SQL statements of a particular type that have been executed.

• Data Dump – Allows the data from specific databases and tables to be dump to or restored from file.

• Logs – Provides access to the log files of the MySQL server to which the workbench is connected. Note that for logs to be visible, the log-output configuration option must be set to TABLE. To make this change, select Configuration followed by the Log Files tab. Scroll down the list of options to log-output. If the check box next to this item is not selected, select it and change the value from File to Table and click the Apply button. Enter appropriate passwords when prompted to do so. After making the change, select the Startup panel and stop and restart the MySQL database server. Once restarted, the log files should be visible on the Logs tab.

Page 15: (Winter 2016) Instructor: Craig Duckett Lecture 09: Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

MySQL Workbench

Using the MySQL Workbench SQL Editor

As outlined in the previous section, the MySQL Workbench tool fulfills a number of different roles in terms of presenting a user friendly graphical interface to the MySQL database management system. One of those roles involves the creation and execution of SQL statements and scripts. The goal of this sectionis to cover this aspect of the workbench tool.

Page 16: (Winter 2016) Instructor: Craig Duckett Lecture 09: Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

MySQL WorkbenchConnecting to a MySQL Database Server

Before any SQL statements can be executed on a database, the MySQL Workbench tool must first establish a connection to the target database server. This may take the form of a local server that is running on the same host as the workbench, or a server running on a remote system. To establish such a connection, begin by launching the workbench tool. Once running, the home screen should appear as shown in the following figure:

Page 17: (Winter 2016) Instructor: Craig Duckett Lecture 09: Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

MySQL WorkbenchIf the required connection is already listed in the SQL Development column of the workbench home page, simply double click on it to establish the connection and enter the corresponding password. Alternatively, click on New Connection to display the new connection dialog:

On the this screen, enter a descriptive name for the connection, indicate the connection type (TCP/IP, local socket or the more secure TCP/IP with SSH) and specify the name of the user and the port via which the database is to be accessed. Additional options are available by selecting the Advanced tab. Once the appropriate information has been provided, click on the Test Connection button to verify the information is correct and the database server is accessible. Assuming a successful test, click on OK to establish the connection profile.

Page 18: (Winter 2016) Instructor: Craig Duckett Lecture 09: Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

MySQL WorkbenchTo connect to the server, simply double click on the connection and enter the password for the specified user.

Page 19: (Winter 2016) Instructor: Craig Duckett Lecture 09: Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

MySQL WorkbenchThe MySQL Workbench SQL Editor

Once a connection has been established to a database server, the SQL Editor panel will appear:

It is possible to return to the workbench home screen at any time simply by selecting the Home tab. Multiple SQL Editor sessions to multiple servers may be established from the home screen, each with its own tab located beneath the toolbar.

The SQL Query panel allows SQL commands to be entered. These may then be executed using the buttons in the toolbar displaying lightning bolts. The leftmost “lightning” button is provided to execute the entire script contained within the SQL Query panel, whilst the second button executes only the currently selected statement.

Page 20: (Winter 2016) Instructor: Craig Duckett Lecture 09: Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

MySQL WorkbenchOutput from SQL statement execution appears in a Results panel located beneath the SQL Query panel. In the following figure, a SQL SHOW DATABASES; statement has just been executed:

The sequence of SQL statements entered into the SQL Query panel may be saved in the form of a script by selecting the File -> Save Script menu option or clicking on the corresponding toolbar button. Saved scripts may similarly be loaded into the SQL Editor using the File -> Open SQL Script… menu option or the Open a SQL Script toolbar button.

Page 21: (Winter 2016) Instructor: Craig Duckett Lecture 09: Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

MySQL WorkbenchUsing MySQL Workbench to Create a Database Model

Perhaps the most powerful feature of the MySQL Workbench tool is the ability to design and manage models. In this section we will work through the steps necessary to design a simple schema using MySQL Workbench and then generate a SQL script and feed it through to a database server to create the corresponding physical database.

In addition to allowing new models to be designed, the workbench tool also includes reverse engineering capabilities, enabling a model to be generated from an existing database.

Page 22: (Winter 2016) Instructor: Craig Duckett Lecture 09: Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

MySQL WorkbenchCreating a New Model

To create a new model, start the MySQL Workbench tool and click on the Create New EER Model option located in the Data Modeling column of the home screen. A new panel will be added to the workbench labeled Model:

Page 23: (Winter 2016) Instructor: Craig Duckett Lecture 09: Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

MySQL WorkbenchBegin the modeling process by double clicking on the tab labeled mydb MySQL Schema. In the resulting properties panel change the name of the schema to mySample:

Page 24: (Winter 2016) Instructor: Craig Duckett Lecture 09: Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

MySQL WorkbenchClose the schema property panel by clicking on the small “x” next in the Schema tab. Having given our schema a name, we can now add a table to the model. This is achieved by double clicking on the Add Table button in the Tables panel:

Page 25: (Winter 2016) Instructor: Craig Duckett Lecture 09: Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

MySQL WorkbenchChange the table name to inventory and then select the Columns tab to begin the process of adding columns to the table:

Page 26: (Winter 2016) Instructor: Craig Duckett Lecture 09: Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

MySQL WorkbenchBy default, the workbench will create a non-null primary key named idinventory. For the purposes of this exercise we will keep this column, though in practice this can be changed. Select the second line, double click in the Column Name field and name the column product. Accept the default value of VARCHAR for the datatype. Repeat these steps to add a price column of type FLOAT and a quantity column of type INTEGER. Each of these should be declared as non-null by checking the boxes in the NN column:

Before proceeding, save the model using the File -> Save Model menu item, or using the appropriate toolbar button.

Page 27: (Winter 2016) Instructor: Craig Duckett Lecture 09: Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

MySQL WorkbenchGenerating a Model Diagram

To generate an EER diagram of our model, select the Model -> Create Diagram from Catalog Objects menu option. The diagram illustrated in the following figure will then be generated:

Page 28: (Winter 2016) Instructor: Craig Duckett Lecture 09: Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

MySQL WorkbenchGenerating a Script and Creating a Database

Having created a model, the next step is to generate a SQL script from the model and feed it through to a database server where it will be executed to create the physical database and table. To achieve this, select the Database -> Forward Engineer… menu option. In the resulting dialog, a range of selections are available to filter and modify the script that will ultimately be generated. Unless you have specific requirements, leave the default selections unchanged. In addition, the objects that are to be generated may also be selected. Since this is a very simple example, and all we have to generate is table information, select only the Export MySQL Table Objects option and click Next to review the generated SQL script:

Page 29: (Winter 2016) Instructor: Craig Duckett Lecture 09: Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

MySQL WorkbenchAssuming the script appears as expected given the initial model from which it has been generated, click Next to specify the server to which the script is to be sent (or save the script if it is to be used later, perhaps within the SQL Editor or mysql client):

Assuming the script appears as expected given the initial model from which it has been generated, click Next to specify the server to which the script is to be sent (or save the script if it is to be used later, perhaps within the SQL Editor or mysql client):

Page 30: (Winter 2016) Instructor: Craig Duckett Lecture 09: Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

MySQL WorkbenchReverse Engineering an Existing Database

In addition to aiding in the design of new models, a model may also be generated from an existing database (a concept referred to as reverse engineering). To reverse engineer an existing database, select the Create EER Model from Existing Database link from the Database Modeling column of the MySQL Workbench home screen and configure the connection to the desired database server in the resulting dialog. Having connected successfully and fetched the database information proceed to the next screen to select the schemata to be reverse engineered. A good example here if you do not have any existing databases is to reverse engineer the mysql database (the database used internally by MySQL). After the wizard has finished fetching additional information, select the Import MySQL table objects option on the Select objects to reverse engineer screen and click Execute.

Upon completion of execution, two new tabs labeled Model and EER diagram respectively will appear in the workbench. The model provides access to the schema for the database and tables:

Page 31: (Winter 2016) Instructor: Craig Duckett Lecture 09: Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

MySQL Workbench

Page 32: (Winter 2016) Instructor: Craig Duckett Lecture 09: Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

MySQL WorkbenchSimilarly, the EER Diagram tab may be used to view the model of the reverse engineered model:

Page 33: (Winter 2016) Instructor: Craig Duckett Lecture 09: Thursday, February 4 th MySQL Workbench, TEAM WORK TIME 1

33

Team Work Time