0

I'm struggling with creating a totalCost column that is the sum of costA and costB for each row.

CREATE TABLE Cost (
  customerId      INTEGER NOT NULL,
  costA           FLOAT(4) NOT NULL,
  costB           FLOAT(4) NOT NULL,
  totalCost       FLOAT(4) GENERATED ALWAYS AS (costA + costB) STORED,
  PRIMARY KEY (customerId)
);

Can anyone tell me what I'm missing? I would have thought this would be super simple!

Note - trying to build it in the DDL rather than a query.

Thank you! :)

leesh_656
  • 3
  • 2

1 Answers1

0

You have a trailing comma at the end. SQL doesn't allow that.

CREATE TABLE Cost (
  customerId      INTEGER NOT NULL,
  costA           FLOAT(4) NOT NULL,
  costB           FLOAT(4) NOT NULL,
  totalCost       FLOAT(4) GENERATED ALWAYS AS (costA + costB) STORED,
  PRIMARY KEY (customerId),
                          ^ remove this
);

Note: since float(4) is imprecise, consider using the precise numeric type to store money.

Schwern
  • 139,746
  • 23
  • 170
  • 313