MySQL is the #2 ranked database system on DB Engines. I’ve never used MySQL, so I wanted to at least get familiar with what it had to offer.
Editions:
There are Standard and Enterprise editions, as well as an Embedded version. There’s also Heatwave, a managed database service.
I’m going to install the Community edition, which is free to use.
Installation:
On the Community Downloads page, I went to ‘MySQL Installer For Windows’ and from there downloaded the MSI. There are two links, one for a Web installer and the standard (Which I used), which is a much larger file.
For the setup type, I chose ‘Developer Default’. This will install the MySQL service, as well as other tools and documentation.
I just took the defaults for any settings. I did have to set a password for the root user, and created an additional DB user as well.
The MySQL version I’m using is 8.0.33.
Connecting:
The Developer Default install includes Workbench, which is a GUI for managing and interacting with a MySQL instance. The Dev install also includes a Command Line tool.
I’m a fan of the DBeaver tool, which I’ve written about previously. I’ll set up a connection with it to run some queries. Since this is the first time I’ve used DBeaver to connect to MySQL, I’ll be prompted to download the MySQL driver. I’ll connect to localhost and the default port 3306, using the root user.
SQL:
Now we can get to some queries. The install comes with a sakila test database, so I’ll use that.
This command will return the MySQL version number:
SELECT VERSION();
This command returns additional version information:
SHOW VARIABLES LIKE "version%";
Here are a few syntax differences between MySQL and T-SQL (I’m most familiar with SQL Server). Most commands are the same between the two systems, but there are always some differences, since no one fully implements the SQL Standard.
TOP X records:
SELECT name, language_id FROM language ORDER BY name LIMIT 1;
Length of a string. CHAR_LENGTH is the number of characters, where LENGTH is the number of bytes. The values are the same here, but of course Unicode characters would return a different number of bytes. There’s no LEN in MySQL.
SELECT name, CHAR_LENGTH(name) as char_length,
LENGTH(name) as name_length
FROM language ORDER BY name;
Current datetimes
SELECT NOW() as local_datetime,
UTC_TIMESTAMP as utc_datetime,
UNIX_TIMESTAMP() as unix_timestamp;
Datepart:
SELECT EXTRACT(month from NOW()) as current_month;
Format Date
SELECT DATE_FORMAT(NOW(), '%M %e, %Y') as today;
Generate GUID
SELECT UUID();
String concatenation. CONCAT also works in T-SQL, but the plus sign (A + B) doesn’t work in MySQL:
SELECT CONCAT('Combined', ' ', 'String')
as combined_string;
CHARINDEX (This example returns 3):
SELECT LOCATE('C', 'ABCDEF');
Data Types:
There’s a lot of overlap in data types between MySQL and SQL Server. There are a few with different names in MySQL, or that are in MySQL with no equivalent in SQL Server.
Text (65,535 bytes), MediumText (Up to 16.7 million characters) and LongText (Up to 14.2 billion characters)
ENUM: A value from a specified list of possible values.
BOOL: Boolean values.
BIT: Integers from 1 to 64.
TIMESTAMP: Dates ranging from 1970 to 2038.
JSON: JSON values.