How can we alter table to add MySQL virtual GENERATED COLUMNS?


For adding MySQL virtual GENERATED COLUMNS in a table, we can use the same syntax as adding a column just adding “AS(expression)” after the data type. Its syntax would be as follows −

Syntax

ALTER TABLE table_name
ADD COLUMN column_name AS(expression);

Example

mysql> ALTER TABLE employee_data ADD COLUMN FULLName Varchar(200) AS(CONCAT_WS(" ", 'First_name','Last_name'));
Query OK, 0 rows affected (0.49 sec)
Records: 0 Duplicates: 0 Warnings: 0

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 |
| FULLName   | varchar(200) | YES  |     | NULL    | VIRTUAL GENERATED |
+------------+--------------+------+-----+---------+-------------------+
5 rows in set (0.00 sec)

Updated on: 22-Jun-2020

645 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements