Home > Database > Mysql Tutorial > body text

How do MySQL virtual generated columns work with built-in functions?

王林
Release: 2023-08-28 12:37:10
forward
899 people have browsed it

MySQL 虚拟生成列如何与内置函数一起使用?

This can be explained with an example where we create a dummy generated column in a table called "employee_data". We know that virtual generated columns can be generated with or without the keyword "virtual".

Example

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)
Copy after login

The above is the detailed content of How do MySQL virtual generated columns work with built-in functions?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template