首頁 >資料庫 >mysql教程 >MySQL 虛擬生成列如何與內建函數一起使用?

MySQL 虛擬生成列如何與內建函數一起使用?

王林
王林轉載
2023-08-28 12:37:10914瀏覽

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

可以透過一個範例進行說明,在該範例中,我們在名為「employee_data」的表中建立一個虛擬產生的欄位。我們知道,可以使用或不使用關鍵字“virtual”來產生虛擬生成列。

範例

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)
#

以上是MySQL 虛擬生成列如何與內建函數一起使用?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除