Exploring Generated Columns in MySQL with examples.

Perhaps you have columns in your table that work together to provide some meaningful results. During table creation, you have the option to CREATE a GENERATED COLUMN in MySQL. What is that? And, what can you use it for? Read on to learn with me and find out…

Note: All data, names or naming found within the database presented in this post, are strictly used for practice, learning, instruction, and testing purposes. It by no means depicts actual data belonging to or being used by any party or organization.

OS and DB used:


  • Xubuntu Linux 16.04.5 LTS (Xenial Xerus)
  • MySQL 8.0.15

Let’s look at a simple example and this CREATE TABLE statement:

1
2
3
4
5
mysql> CREATE TABLE company_emails(
    -> f_name VARCHAR(20),
    -> l_name VARCHAR(20),
    -> email TEXT GENERATED ALWAYS AS (CONCAT(f_name,'.',l_name,'@some_company.com')));
Query OK, 0 rows affected (0.42 sec)

Next, I will populate the table with some mock data:

1
2
3
4
mysql> INSERT INTO company_emails(f_name, l_name)
    -> VALUES('Josh', 'Otwell'), ('Marko', 'Summers');
Query OK, 2 rows affected (0.09 sec)
Records: 2  Duplicates: 0  Warnings: 0

Notice in the above INSERT, the email column was not part of the listed columns, nor was any data supplied in the VALUES clause for it.

Let’s check the table data now:

1
2
3
4
5
6
7
8
mysql> SELECT * FROM company_emails;
+--------+---------+--------------------------------+
| f_name | l_name  | email                          |
+--------+---------+--------------------------------+
| Josh   | Otwell  | Josh.Otwell@some_company.com   |
| Marko  | Summers | Marko.Summers@some_company.com |
+--------+---------+--------------------------------+
2 rows in set (0.00 sec)

You can see that column email returns the definition we set during table creation, concatenating the f_name and l_name with the company email address. Hence, this is the GENERATED COLUMN.

Let’s see the underlying details for table company_emails:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
mysql> DESC company_emails;
+--------+-------------+------+-----+---------+-------------------+
| Field  | Type        | Null | Key | Default | Extra             |
+--------+-------------+------+-----+---------+-------------------+
| f_name | varchar(20) | YES  |     | NULL    |                   |
| l_name | varchar(20) | YES  |     | NULL    |                   |
| email  | text        | YES  |     | NULL    | VIRTUAL GENERATED |
+--------+-------------+------+-----+---------+-------------------+
3 rows in set (0.01 sec)

mysql> SHOW CREATE TABLE company_emails\G
*************************** 1. row ***************************
       Table: company_emails
Create Table: CREATE TABLE `company_emails` (
  `f_name` varchar(20) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `l_name` varchar(20) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `email` text COLLATE utf8mb4_unicode_ci GENERATED ALWAYS AS (concat(`f_name`,_utf8mb4'.',`l_name`,_utf8mb4'@some_company.com')) VIRTUAL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
1 row in set (0.00 sec)

The Extra column provides an important specifier or definition, VIRTUAL, which is the default should one be omitted in the definition (as was the case in the example here). However, there is also a STORED option for GENERATED COLUMN‘s.

The naming helps (me at least!) with clearing up any distinction between the two but, in order to solidify the concept, we will have an example.

Since I am not changing the column name, but only the definition, I use the MODIFY clause in this ALTER TABLE statement:

1
2
mysql> ALTER TABLE company_emails MODIFY COLUMN email TEXT GENERATED ALWAYS AS (CONCAT(f_name,'.',l_name,'@some_company.com')) STORED;
ERROR 3106 (HY000): 'Changing the STORED status' is not supported for generated columns.

(Told you I was learning!!!)

Hmm… Can’t do that it seems. Why?

See this passage from the ALTER TABLE and Generated Columns official documentation for better understanding:

“Virtual generated columns cannot be altered to stored generated columns, or vice versa. To work around this, drop the column, then add it with the new definition.”

Probably a good idea to keep this in mind during the design phase. Dropping a column and reimplementing it has its drawbacks (in my mind at least) due to table locking and the fact you are essentially discarding data.

Yet, for our arbitrary example, we will proceed forward:

1
2
3
mysql> ALTER TABLE company_emails DROP COLUMN email;
Query OK, 0 rows affected (0.76 sec)
Records: 0  Duplicates: 0  Warnings: 0

1
2
3
4
5
6
7
mysql> ALTER TABLE company_emails
    -> ADD COLUMN email TEXT
    -> GENERATED ALWAYS AS (CONCAT(f_name,'.',l_name,'@some_company.com'))
    -> STORED
    -> AFTER l_name;
Query OK, 2 rows affected (1.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

Let’s run another INSERT then check the data:

1
2
3
4
5
6
7
8
9
10
11
12
13
mysql> INSERT INTO company_emails(f_name, l_name)
    -> VALUES ('Jim', 'Dandy');
Query OK, 1 row affected (0.06 sec)

mysql> SELECT * FROM company_emails;
+--------+---------+--------------------------------+
| f_name | l_name  | email                          |
+--------+---------+--------------------------------+
| Josh   | Otwell  | Josh.Otwell@some_company.com   |
| Marko  | Summers | Marko.Summers@some_company.com |
| Jim    | Dandy   | Jim.Dandy@some_company.com     |
+--------+---------+--------------------------------+
3 rows in set (0.00 sec)

So what’s the difference between them? The crux of it is:

  • STORED columns take up storage space and can have indexes. (I think of them as real).
  • VIRTUAL columns are just essentially generated on the fly as rows are returned in query results.

While the example presented here is mostly arbitrary, I am sure you can find a use for GENERATED COLUMN‘s in your tables… Let me know all about them in the comments below!!!

Like what you have read? See anything incorrect? Please comment below and thanks for reading!!!

Explore the official MySQL 8.0 Online Manual for more information.

A Call To Action!

Thank you for taking the time to read this post. I truly hope you discovered something interesting and enlightening. Please share your findings here, with someone else you know who would get the same value out of it as well.

Visit the Portfolio-Projects page to see blog post/technical writing I have completed for clients.

Have I mentioned how much I love a cup of coffee?!?!

To receive email notifications (Never Spam) from this blog (“Digital Owl’s Prose”) for the latest blog posts as they are published, please subscribe (of your own volition) by clicking the ‘Click To Subscribe!’ button in the sidebar on the homepage! (Feel free at any time to review the Digital Owl’s Prose Privacy Policy Page for any questions you may have about: email updates, opt-in, opt-out, contact forms, etc…)

Be sure and visit the “Best Of” page for a collection of my best blog posts.


Josh Otwell has a passion to study and grow as a SQL Developer and blogger. Other favorite activities find him with his nose buried in a good book, article, or the Linux command line. Among those, he shares a love of tabletop RPG games, reading fantasy novels, and spending time with his wife and two daughters.

Disclaimer: The examples presented in this post are hypothetical ideas of how to achieve similar types of results. They are not the utmost best solution(s). The majority, if not all, of the examples provided, is performed on a personal development/learning workstation-environment and should not be considered production quality or ready. Your particular goals and needs may vary. Use those practices that best benefit your needs and goals. Opinions are my own.

Advertisements

Hey thanks for commenting! Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.