Computer >> 컴퓨터 >  >> 프로그램 작성 >> MySQL

CREATE TABLE 문을 사용하여 MySQL 테이블에 여러 개의 GENERATED COLUMNS를 저장하는 방법은 무엇입니까?


MySQL 테이블에 여러 개의 저장된 생성 열을 추가하는 것이 가능합니다. 다음과 같은 예를 들어 설명할 수 있습니다. -

예시

mysql> Create table profit1(cost int, price int, profit int AS (price-cost) STORED, price_revised int AS (price-2) STORED);
Query OK, 0 rows affected (0.36 sec)

mysql> Describe profit1;
+---------------+---------+------+-----+---------+------------------+
| Field         | Type    | Null | Key | Default | Extra            |
+---------------+---------+------+-----+---------+------------------+
| cost          | int(11) | YES  |     | NULL    |                  |
| price         | int(11) | YES  |     | NULL    |                  |
| profit        | int(11) | YES  |     | NULL    | STORED GENERATED |
| price_revised | int(11) | YES  |     | NULL    | STORED GENERATED |
+---------------+---------+------+-----+---------+------------------+
4 rows in set (0.00 sec)

mysql> Insert into profit1(Cost, Price) values(100,110);
Query OK, 1 row affected (0.09 sec)

mysql> Insert into profit1(Cost, Price) values(200,220);
Query OK, 1 row affected (0.09 sec)

mysql> Select * from profit1;
+------+-------+--------+---------------+
| cost | price | profit | price_revised |
+------+-------+--------+---------------+
| 100  | 110   | 10     | 108           |
| 200  | 220   | 20     | 218           |
+------+-------+--------+---------------+
2 rows in set (0.00 sec)