0

I want to create a MySQL table that has four column For example, id,piece,price,totalprice

When data inserted into id,piece,price column totalprice must be initialized from (piece * price)

Insert data

id   piece   price
101   2       10

Result

id    piece price totalprice
101   2      10     20

I can create a table with four column but i can't automatically initialized into totalprice column using the piece and price column value.

How to do that?

1 Answers1

0

You can do it as follows :

CREATE TABLE orders (
  id int not null AUTO_INCREMENT primary key,
  piece DOUBLE,
  price DOUBLE,
  totalPrice DOUBLE AS (piece * price)
);

INSERT INTO orders (piece, price) VALUES(1,1),(3,4),(6,8);

check it here : https://dbfiddle.uk/7wcEDixV

SelVazi
  • 10,028
  • 2
  • 13
  • 29