Skip to content

GENERATED

CREATE TABLE orders (
order_id INT AUTO_INCREMENT,
product_name VARCHAR(100),
quantity INT,
price DECIMAL(10,2),
total_price DECIMAL(10,2) AS (quantity * price),
PRIMARY KEY(order_id)
);
INSERT INTO orders (product_name, quantity, price)
VALUES('Laptop', 5, 1000.00);
SELECT * FROM orders;
+----------+--------------+----------+-------+-------------+
| order_id | product_name | quantity | price | total_price |
+----------+--------------+----------+-------+-------------+
| 1 | Laptop | 5 | 1000.00 | 5000.00 |
+----------+--------------+----------+-------+-------------+

In the example, the total_price column is a generated column that automatically calculates and stores the total price of each order by multiplying the quantity and price. When a new record is inserted into the orders table, the value for the total_price column is automatically calculated and stored in the total_price column.