Sign In
Sign In

How to Install and Use MySQL Workbench

How to Install and Use MySQL Workbench
Hostman Team
Technical writer
MySQL
11.12.2023
Reading time: 7 min

To create a small database, you only need to write a short SQL script. It may be enough for learning or testing purposes. However, "real" databases, even in small projects, consist of dozens of tables and views and can be very difficult to work with using only SQL. It is hard to keep dozens of entities in your head without getting confused. That's when MySQL Workbench becomes a solution.

MySql Workbench is software for creating and designing databases using schemas and other visual tools. Today, we will show you what Workbench is, how to install it and connect to a cluster, how to create tables and models, and how to import and export data.

How to install MySQL WorkBench

To install MySQL Workbench, go to the official website and choose MySQL Enterprise Edition ->Workbench among the products. Or you can follow this link.

Image7 (1)

Click on "Download Now" and go to the page with options:

Image5 (2)

Here, select the operating system. In our case it is Windows 10 64-bit. After downloading and installing, the application is ready to work. 

How to create a database in MySQL Workbench

Before creating databases, let's define the entities and their attributes. Let's think of a database for an abstract hosting service with three tables: clients, servers, services.

The "Clients" will have the following columns: contract number as primary key, client's full name, service ID, and server ID.

The columns of the "Servers" table will display information about the servers themselves:

  • Server ID

  • IP address

  • Size of both RAM and permanent memory

  • Processor and video card parameters

In the "Services" table, we will specify their cost, name, and description.

Of course, our database will cover only a few of the theoretical hosting activities, but for introduction purposes, such a database will be enough.

To create a database, choose "File"->"New Model" in the main menu:

Image9 (1)

Now we can move on to creating the database.

Clients

In managed MySQL, table creation is implemented in several ways. We will create the "Clients" table without using the visual model. 

To do this, click "Add Table" in our database menu, after which a menu with settings will open.

  • Table Name;

  • Column Name is our attribute name;

  • Datatype is attribute type (number, time, letters, etc.);

  • PK, NN, UQ, etc. are column constraints. PK stands for primary key, NN stands for Not Null. The rest of the constraints are not important to us yet.

19d2f68f 6f50 482e 9bb3 2b81225ff88d

Servers

We create the "Servers" table in the same way. Here, we will focus on column attributes and their selection.

  • Server ID is a primary key with the Integer type. It is essentially a regular number; in practice, the server ID would be the server number;

  • IP is a set of up to 15 ASCII characters. We assume storage of an IPv4 address, for example 255.255.255.255.255; 

  • RAM, GB is the size of RAM expressed using the Float data type (floating point numbers);

  • Disk space, GB is similar to RAM;

  • Processor and GPU (video card) are where we'll place equipment names with a maximum length of 45 characters.

8be54a44 B772 405b 9559 56293058e976

Services

For a change, let's create "Services" using the visual functionality of MySQL Workbench. 

To do this, click on the "Add Diagram" button above the name of our database, "mydb."

88736c0c 4976 4ca9 Af80 Bdff7cbc51a9

Now click on the diagram that will appear, and you'll get to a new tab.

Bb1c960c 18c5 4f4e B4de C1b0797b4b9f

In the left menu of the "Catalog Tree" we can see the already created "Clients" and "Servers" tables. We can transfer them to the visual diagram:

F5d68b88 Ecee 49ac A55e Ec74b2686767

To add a new table to the diagram, select "Place a New Table" from the left menu or press "T". 

652d1de0 Ccf5 4bcb A4fa B61888d9aa88

By clicking twice on the window that appears, we go to the settings. Our "Services" table will contain the columns "Service ID", "Name", "Description" and "Price".

D2204ee4 Ecc1 4856 8b93 13eed33e5d39

Creating relations

The database we are creating is called a relational database. Tables do not exist in a vacuum and are connected to each other: a client rents a certain server or buys a certain service. It is necessary to trace this relationship in our database. 

In practice, it is realized with the help of the foreign key (FK) or foreign key column attribute. FK defines the set of possible column values in a row as those from another table. Suppose we have two tables: "Employees" and "Shifts". "Shifts" has columns "Time" and "Employee (FK)". So, in the column "Employee (FK)," we can put only the employee who is in the table "Employees".   

It is easy to create MySQL links. In order to link two tables, you need to define the type of link between them. Our tables will be linked, but they will not identify each other. In practice, this means that an FK is not a PK. Therefore, we will use the Non-Identifying relationship. Let's move directly to creating the relationships.

Clients-Servers

The linking column here will be the "Server ID". Can the same server be in several clients in our database? Yes, it can, but not at the same time. In order to create such a relationship, we select "Place a New 1:n Non-Identifying Relationship" in the left menu and click first on "Clients" (where the relation goes to) and then on "Servers" (where the relation comes from).

02e532e2 Ba36 4809 942b 41cfb10b7ec5

We have a new column in "Clients," but we would like to link the relation to an existing column. To do this, right-click on "Clients" and select "Edit Clients".

203cb732 17f3 4073 Bb3a Ec137bb50291

Then go to the "Foreign Keys" tab below. Here we can select the "Server ID" column as the "Foreign key" and then delete the resulting column. We perform the same procedure with the Clients-Services link and eventually have the following result:

D961d309 0bae 4bf1 8b5b 7324f8033bf4

Views

In the menu on the left, you might have noticed the "Views" section. Views are the display of the required data based on the available tables. For example, you may need information about clients and the cost of services they use. In this case, you will need a View. Let's try to create one. 

First, we need to fill the database with information. To do this, go to the table parameters and the "Inserts" tab. 

Ed46938f C220 40b6 Bd6e C9d2365f583f

Then fill in the "Servers" and "Clients" in the same way and add a View to the diagram using the "Place a New View" button or the "V" key.

5eb59988 Fed2 4689 81ea 6d4e0800ccfe

Double-click on "view1" and in the window that appears, write the SQL query that corresponds to our mapping.

CREATE VIEW `Full Name + Services` AS
SELECT Name, ServiceName, Price From Clients
JOIN Services ON Clients.service_id = Services.service_id;

E467987f D19f 4a4e B272 46728818be91

But to see the result of the query, we need to connect to the cluster and import our database there.

Connecting to a cluster

The first thing you need is a cluster. We already have one, so we will connect to it. To create a connection, you need to go to the main page and click the plus sign.

7d959594 1171 4e34 A440 21caa0f27498

In the window that appears, we will need the following settings:

  • Connection name is useful if there are several connections;

  • Hostname is our cluster IP address;

  • Port is the cluster port. The default is 3306;

  • Username is the name of your account;

  • Password (if available)

51111fb8 D432 4d9e Bb33 89a9af8fa395

Click "OK," and you will connect to the cluster.

Importing and exporting the database

To work with the database and execute SQL queries, you must first import it into the cluster. To do this, click on the "File" button on the diagram tab and select "Export"->"Forward Engineering SQL CREATE Script" in the pop-up menu.

4b31ecc5 Ecbf 466f 9571 5bff5e5d23ff

Remember to click the checkboxes in the corresponding fields to avoid losing the filled tables. The resulting script will allow us to import the database into the cluster.

Go back to the cluster tab, select "Data Import/Restore" in the "Management" menu.

164d216a 9918 4fd9 B57f 46beee6932e0

Here, we need to select the "Import from Self-Contained File" option, select the script, and click "Start Import".

How to dump a MySQL WorkBench database

To create a dump, you need to click "Data Export" in the "Management" section and select the required parameters.

9844bdc1 B56f 46c0 8107 32b96fdde49b

We will save the entire database: its logical structure and stored data. To complete the process, click "Start Export".

Conclusion

In this article, we have learned the program's basic functionality, created a small database and imported it to the server. Of course, we could only cover some of the features. But this should be enough to start creating your own MySQL databases from scratch and study this tool more deeply.

MySQL
11.12.2023
Reading time: 7 min

Similar

MySQL

How to Find and Delete Duplicate Rows in MySQL with GROUP BY and HAVING Clauses

Duplicate entries may inadvertently accumulate in databases, which are crucial for storing vast amounts of structured data. These duplicates could show up for a number of reasons, including system errors, data migration mistakes, or repeated user submissions. A database with duplicate entries may experience irregularities, sluggish performance, and erroneous reporting. Using the GROUP BY and HAVING clauses, as well as a different strategy that makes use of temporary tables, we will discuss two efficient methods for locating and removing duplicate rows in MySQL. With these techniques, you can be sure that your data will always be accurate, clean, and well-organized. Database duplication in MySQL tables can clog your data, resulting in inaccurate analytics and needless storage. Locating and eliminating them is a crucial database upkeep task. This is a detailed guide on how to identify and remove duplicate rows. If two or more columns in a row have identical values, it is called a duplicate row. For instance, rows that have the same values in both the userName and userEmail columns of a userDetails table may be considered duplicates. Benefits of Removing Duplicate Data The advantage of eliminating duplicate data is that duplicate entries can slow down query performance, take up extra storage space, and produce misleading results in reports and analytics. The accuracy and speed of data processing are improved by keeping databases clean, which is particularly crucial for databases that are used for critical applications or are expanding. Requirements Prior to starting, make sure you have access to a MySQL database or have MySQL installed on your computer. The fundamentals of general database concepts and SQL queries. One can execute SQL commands by having access to a MySQL client or command-line interface. To gain practical experience, you can create a sample database and table that contains duplicate records so that you can test and comprehend the techniques for eliminating them. Creating a Test Database Launch the MySQL command-line tool to create a Test Database. mysql -u your_username -p Create a new database called test_dev_db after entering your MySQL credentials. CREATE DATABASE test_dev_db; Then, switch to this newly created database:. USE test_dev_db; Add several rows, including duplicates, to the userDetails table after creating it with the CREATE TABLE query and INSERT query below. CREATE TABLE userDetails ( userId INT AUTO_INCREMENT PRIMARY KEY, userName VARCHAR(100), userEmail VARCHAR(100) ); INSERT INTO userDetails (userName, userEmail) VALUES (‘Alisha’, ‘[email protected]’), (‘Bobita, ‘[email protected]’), (‘Alisha’, ‘[email protected]’), (‘Alisha’, ‘[email protected]’); Using GROUP BY and HAVING to Locate Duplicates Grouping rows according to duplicate-defining columns and using HAVING to filter groups with more than one record is the simplest method for finding duplicates. Now that you have duplicate data, you can use SQL to determine which rows contain duplicate entries. MySQL's GROUP BY and HAVING clauses make this process easier by enabling you to count instances of each distinct value. An example of a table structure is the userDetails table, which contains the columns userId, userName, and userEmail. The GROUP BY clause is useful for counting occurrences and identifying duplicates because it groups records according to specified column values. The HAVING clause  allows duplicate entries in groups formed by GROUP BY to be found by combining groups based on specific criteria. Table userDetails Structure userId userName userEmail 1 Alisha  [email protected] 2 Bobita  [email protected] 3 Alisha  [email protected] 4 Alisha  [email protected] In the above table userDetails, records with identical userName and userEmail values are considered duplicates. Finding Duplicates Query for find the duplicate entries: SELECT userName, userEmail, COUNT(*) as count FROM userDetails GROUP BY userName, userEmail HAVING count > 1; Rows are grouped by userName and userEmail in the aforementioned query, which also counts entries within the group and eliminates groups with a single entry (no duplicates). Explanation: SELECT userName, userEmail, COUNT(*) as count: Retrieves the count of each combination of username and userEmail, as well as their unique values. GROUP BY userName, userEmail: Records are grouped by username and user email using the GROUP BY userName, userEmail function COUNT (*): Tallies the rows in each set. HAVING occurrences > 1: Recurring entries are identified by displaying only groups with more than one record. This query will return groups of duplicate records based on the selected columns. userName userEmail count Alisha [email protected] 3 Eliminating Duplicate Rows After finding duplicates, you may need to eliminate some records while keeping the unique ones. Joining the table to itself and removing rows with higher userId values is one effective method that preserves the lowest userId for every duplicate. Use the SQL query to remove duplicate rows while keeping the lowest userId entry. DELETE u1 FROM userDetails u1 JOIN userDetails u2 ON u1. userName = u2. userName AND u1. userEmail = u2. userEmail AND u1. userId > u2. userId ; Explanation: u1 & u2: Aliases for the userDetails table to ease a self-join. ON u1. userName = u2. userName AND u1. userEmail = u2. userEmail: Matches rows with identical userName, userEmail. AND u1. userId > u2. userId: Removes rows with higher userId values, keeping only the row with the smallest userId. Because this action cannot be undone, it is advised that you backup your data before beginning the deletion procedure. Confirming Duplicate Removal To confirm that all duplicates have been removed, repeat the Step 1 identification query. SELECT userName, userEmail, COUNT(*) as count FROM userDetails GROUP BY userName, userEmail HAVING count > 1; All duplicates have been successfully eliminated if this query yields no rows. Benefits of Employing GROUP BY and HAVING The GROUP BY and HAVING clauses serve as vital instruments for the aggregation of data and the filtration of grouped outcomes. These functionalities are especially useful for detecting and handling duplicate entries or for condensing extensive datasets. Below are the primary benefits of employing these clauses. Efficient Identification of Duplicates Data Aggregation and Summarization Filtering Aggregated Results with Precision Versatility Across Multiple Scenarios Compatibility and Simplicity Enhanced Query Readability Support for Complex Aggregations The GROUP BY and HAVING clauses serve as essential instruments for data aggregation, identifying duplicates, and filtering results. Their effectiveness, ease of use, and adaptability render them crucial for database management and data analysis activities, allowing users to derive insights and handle data proficiently across a variety of applications. Identifying Duplicates Using a Temporary Table When dealing with large datasets, it can be easier and more efficient to separate duplicates using a temporary table before deleting them. Creating the Table Make a temporary table to store duplicate groups according to predetermined standards (e.g. A. username, along with userEmail. CREATE TEMPORARY TABLE temp_view_duplicates AS SELECT username, userEmail, MIN (userId) AS minuid FROM userDetails GROUP BY username, userEmail, HAVING COUNT(*) > 1; Explanation: CREATE TEMPORARY TABLE temp_view_duplicates AS: Creates a temporary table named temp_view_duplicates. SELECT userName, userEmail, MIN(userId) AS minuid: Groups duplicates by userName and userEmail, keeping only the row with the smallest userId. GROUP BY userName, userEmail: Groups rows by userName, userEmail. HAVING COUNT(*) > 1: Filters only groups with more than one row, identifying duplicates. This temporary table will now contain one representative row per duplicate group (the row with the smallest id). Deleting Duplicates from the Main Table Now that we have a list of unique rows with duplicates in the temp_view_duplicates table, we can use the temporary table to remove duplicates while keeping only the rows with the smallest userId. Use the following DELETE command: DELETE FROM userDetails WHERE (username, userEmail) IN ( SELECT username, userEmail FROM temp_view_duplicates ) AND userId NOT IN ( SELECT minuid FROM temp_view_duplicates ); Explanation: WHERE (username, userEmail,) IN: Targets only duplicate groups identified in temp_view_duplicates. AND userId NOT IN (SELECT minuid FROM temp_view_duplicates): Ensures that only duplicate rows (those with higher userId values) are deleted. Verifying Results To confirm that duplicates have been removed, query the userDetails table: SELECT * FROM userDetails; Only unique rows should remain. Temporary tables (CREATE TEMPORARY TABLE) are automatically dropped when the session ends, so they don’t persist beyond the current session. When making extensive deletions, think about utilizing a transaction to safely commit or undo changes as necessary. Key Advantages of Using a Temporary Table Lower Complexity: By isolating duplicates, the removal process is simpler and clearer. Enhanced Efficiency: It's faster for large datasets, as it avoids repeated joins. Improved Readability: Using a temporary table makes the process more modular and easier to understand. Conclusion Eliminating duplicate records is essential for maintaining a well-organized database, improving performance, and ensuring accurate reporting. This guide presented two approaches: Direct Method with GROUP BY and HAVING Clauses: Ideal for small datasets, using self-joins to delete duplicates. Temporary Table Approach: More efficient for larger datasets, leveraging temporary storage to streamline deletion. Choose the method that best fits your data size and complexity to keep your database clean and efficient.
19 November 2024 · 8 min to read
MySQL

Installing MariaDB on Ubuntu 22.04

MariaDB is an open-source relational database management system, which has made it a popular alternative to MySQL. It is often included in LAMP stacks (which consist of Linux, Apache, MySQL, and PHP, sometimes with Python and Perl compilers). This document provides a brief guide to setting up MariaDB. The setup involves three steps: updating the package index, installing the mariadb-server, and activating a security script restricting unauthorized access to the remote host.   The sequence of commands looks like this: sudo apt update sudo apt install mariadb-server sudo mysql_secure_installation For this tutorial, we will use a Hostman cloud server with Ubuntu 22.04 installed. Before diving into this guide, we recommend performing initial setup steps, such as creating a user account with sudo privileges and setting basic UFW firewall rules. Step 1: Installing MariaDB Repositories are regularly updated to include stable versions of utilities. Distributions, on the other hand, include outdated releases that were current at the time of the build, so they need to be updated to avoid compatibility issues. This procedure is executed with the following command: sudo apt update Now we can install the package we need: sudo apt install mariadb-server The installation proceeds without prompting for a password or making any other configuration changes. Using MariaDB in this form on the server is not advisable, as it will operate in an insecure mode. To rectify this situation, we will apply the mysql_secure_installation script that comes with the software. This script will restrict access to the server and eliminate unauthorized accounts. Step 2: Setting Up MariaDB The security script modifies the insecure options that are set by default. For instance, it creates test accounts and allows remote connections using the root account. This potentially poses a risk of hacking and unauthorized access to the information stored in the created database. To run the script, use the following command: sudo mysql_secure_installation This command will initiate a series of prompts that allow you to change the database's security parameters.  The first prompt relates to the root account, and the system will request the password for the active database. Press Enter. This indicates that there is currently no protection. Enter current password for root (enter for none): Switch to unix_socket authentication [Y/n] Enter n and press Enter Change the root password? [Y/n] Enter Y and press Enter. New password: Re-enter new password: Enter and re-enter the new root user password and press Enter. Answer Yes (Y) to all the following prompts. Remove anonymous users? [Y/n] Disallow root login remotely? [Y/n] Remove test database and access to it? [Y/n] Reload privilege tables now? [Y/n] The system will remove the test database and anonymous accounts, disable access through the root account, and load the updated rules.  The installation and configuration of the MariaDB package are complete, and you can now begin using it. Alternatively, you can proceed to an optional step to create an administrator account to enable password access. Step 3: Create an Administrator Account By default, on Ubuntu, MariaDB is installed using the unix_socket plugin, which means that password prompts do not apply. In most cases, this approach provides a high level of security. However, it also complicates administrative tasks, such as those done through phpMyAdmin. When starting or stopping the server or managing logs, the root account is used. That is why we did not change its data. However, during a package update, these settings may change on their own, so it's better to enable password authentication right away. As an example, we will create an account named admin and give it the same privileges as the root account. First, open the MariaDB command line: sudo mariadb Next, create the new user: GRANT ALL ON *.* TO 'admin'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION; Replace admin and password with any preferred combinations.  After creating the account, flush the privileges while keeping the settings in the current session: FLUSH PRIVILEGES; Now you can close the shell: exit; Next, you should test MariaDB to ensure that the settings are correct. Step 4: Diagnostics When the MariaDB is installed from the official repository, it automatically configures the settings to ensure that the MariaDB module starts automatically. However, it's still a good practice to manually check its status: sudo systemctl status mariadb The output on the screen will look something like this: If the utility is not running, you will need to start it manually and also enable the service: sudo systemctl enable mariadb sudo systemctl start mariadb After forcibly starting the service, you can make a test connection to the database using mysqladmin. It allows you to interact with the database with administrative rights, execute commands, and change settings. Here’s an example of connecting and displaying the version number: sudo mysqladmin version The output on the screen will look like this: If access was configured using the administrator password, you can use the command: mysqladmin -u admin -p version The current version output confirms that the database is running and functioning, and that the user has access to its contents. Conclusions We have completed an overview of the installation and configuration for the MariaDB database management system. We discussed methods to protect against unauthorized access to the database and the creation of a new user who will have access to information equal to that of the root user.
07 November 2024 · 5 min to read
MySQL

How to Calculate Age From Date of Birth Using TIMESTAMPDIFF in MySQL

Calculating age from a date of birth (DOB) is a common requirement in many applications. In MySQL, this can be efficiently achieved using the TIMESTAMPDIFF function. This tutorial will guide you through the process of calculating age using TIMESTAMPDIFF, handling edge cases, and integrating the query into applications. Prerequisites Before diving into the tutorial, ensure you have: A MySQL database set up and accessible. Basic knowledge of SQL queries and MySQL functions. A table with a date of birth column to work with. Overview of TIMESTAMPDIFF Function The TIMESTAMPDIFF function calculates the difference between two dates based on the specified unit of time (e.g., years, months, days). For calculating age, you will use TIMESTAMPDIFF to find the difference in years between the current date and the date of birth. TIMESTAMPDIFF(unit, datetime1, datetime2) unit: The unit of time for the result (e.g., YEAR, MONTH, DAY). datetime1: The first date (usually the date of birth). datetime2: The second date (usually the current date). Writing the Basic TIMESTAMPDIFF Query To calculate age from a date of birth, use the following query: SELECT TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE()) AS ageFROM users; In this query: YEAR specifies that the result should be in years. date_of_birth is the column containing the date of birth. CURDATE() returns the current date. Handling Edge Cases When calculating age, consider the following edge cases: Leap Years Leap years do not significantly affect age calculations, as TIMESTAMPDIFF accurately accounts for these in its calculations. Birthdays on February 29 For individuals born on February 29, TIMESTAMPDIFF will handle their age calculation correctly, but be aware of potential issues if you use functions that do not recognize leap years. Different Date Formats Ensure that the date format stored in the database matches MySQL's date format (YYYY-MM-DD). If you encounter format issues, use the STR_TO_DATE function to convert strings to date formats. Practical Examples and Use Cases Here are some practical examples of using TIMESTAMPDIFF: Example 1: Calculate Age for a Specific User SELECT TIMESTAMPDIFF(YEAR, '1990-05-15', CURDATE()) AS age; This query calculates the age of someone born on May 15, 1990. Example 2: Age Calculation for All Users SELECT name, TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE()) AS ageFROM users; This query retrieves names and ages of all users from the users table. Integrating the Query in Applications To integrate this query into an application: In a PHP Application: $query = "SELECT TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE()) AS age FROM users";$result = mysqli_query($conn, $query); In a Python Application:  query = "SELECT TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE()) AS age FROM users"  cursor.execute(query) Ensure that your application handles database connections securely and efficiently. Performance Considerations The TIMESTAMPDIFF function is optimized for performance, but be mindful of the following: Indexes: Ensure that the date_of_birth column is indexed to speed up queries. Query Optimization: For large datasets, consider optimizing queries to improve performance. Troubleshooting Common Issues Here are some common issues and their solutions: Incorrect Results Issue: Age calculation is incorrect. Solution: Ensure that dates are correctly formatted and the date_of_birth column contains valid date values. Query Errors Issue: Syntax or execution errors. Solution: Verify that the SQL syntax is correct and that you are using valid MySQL functions. Conclusion Calculating age from a date of birth using TIMESTAMPDIFF in MySQL is straightforward and efficient. By following the steps outlined in this tutorial, you can accurately determine age and handle various edge cases. Integrate these calculations into your applications and optimize performance for the best results.
10 September 2024 · 4 min to read

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start.
Email us
Hostman's Support