What is MySQL GENERATED COLUMN and how to use it while creating a table?


Basically generated columns are a feature that can be used in CREATE TABLE or ALTER TABLE statements and is a way of storing the data without actually sending it through the INSERT or UPDATE clause in SQL. This feature has been added in MySQL 5.7. A generated column works within the table domain. Its syntax would be as follows −

Syntax

column_name data_type [GENERATED ALWAYS] AS (expression)
[VIRTUAL | STORED] [UNIQUE [KEY]]

Here, first of all, specify the column name and its data type.

  • Then add the GENERATED ALWAYS clause to indicate that the column is a generated column.
  • Then, indicate whether the type of the generated column by using the corresponding option − VIRTUAL or STORED. By default, MySQL uses VIRTUAL if you don’t specify explicitly the type of the generated column.

After that, specify the expression within the braces after the AS keyword. The expression can contain literals, built-in functions with no parameters, operators, or references to any column within the same table. If you use a function, it must be scalar and deterministic.

Finally, if the generated column is stored, you can define a unique constraint for it.

Example

In this example, we are creating a table named employee_data having the details of employees along with a generated column as follows −

mysql> Create table employee_data(ID INT AUTO_INCREMENT PRIMARY KEY, First_name VARCHAR(50) NOT NULL, Last_name VARCHAR(50) NOT NULL, FULL_NAME VARCHAR(90) GENERATED ALWAYS AS(CONCAT(First_name,' ',Last_name)));
Query OK, 0 rows affected (0.55 sec)

mysql> DESCRIBE employee_data;
+------------+-------------+------+-----+---------+-------------------+
| Field      | Type        | Null | Key | Default | Extra             |
+------------+-------------+------+-----+---------+-------------------+
| ID         | int(11)     | NO   | PRI | NULL    | auto_increment    |
| First_name | varchar(50) | NO   |     | NULL    |                   |
| Last_name  | varchar(50) | NO   |     | NULL    |                   |
| FULL_NAME  | varchar(90) | YES  |     | NULL    | VIRTUAL GENERATED |
+------------+-------------+------+-----+---------+-------------------+
4 rows in set (0.00 sec)

mysql> INSERT INTO employee_data(first_name, Last_name) values('Yashpal','Sharma');
Query OK, 1 row affected (0.09 sec)

mysql> INSERT INTO employee_data(first_name, Last_name) values('Krishan','Kumar');
Query OK, 1 row affected (0.09 sec)

mysql> INSERT INTO employee_data(first_name, Last_name) values('Rakesh','Arora');
Query OK, 1 row affected (0.08 sec)

mysql> Select * from employee_data;
+----+------------+-----------+----------------+
| ID | First_name | Last_name | FULL_NAME      |
+----+------------+-----------+----------------+
| 1  | Yashpal    | Sharma    | Yashpal Sharma |
| 2  | Krishan    | Kumar     | Krishan Kumar  |
| 3  | Rakesh     | Arora     | Rakesh Arora   |
+----+------------+-----------+----------------+
3 rows in set (0.00 sec)

Lakshmi Srinivas
Lakshmi Srinivas

Programmer / Analyst / Technician

Updated on: 22-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements