0

I'm new to the SQL and I have a problem with setting default value for a column using this command. The thing is I have to get product's brutto price.

ALTER TABLE produkt
ADD vat DECIMAL(5,2) DEFAULT 0.23;

ALTER TABLE produkt
ADD price_br INTEGER DEFAULT var*PRICE_NET;

"produkt" is my Table PRICE_NET is netto price of a product.

After trying to add price_br column I'm getting "column not allowed here" error. I assume It's quite an easy task, but i can't really figure it out.

2 Answers 2

3

I think you want a computed column, not a default value. That would be:

ALTER TABLE produkt ADD price_br INTEGER GENERATED ALWAYS AS (var*PRICE_NET);
1
  • Great! Thanks a lot for helping me out.
    – Filgum0326
    May 12, 2021 at 16:36
0

In addition to Gordon's answer.

Whenever you are using GENERATED ALWAYS AS, you have the option to specify whether the generated value should be stored physically on disk or computed dynamically when queried.

ALTER TABLE produkt ADD price_br INTEGER GENERATED ALWAYS AS (var*PRICE_NET) STORED;

Storing the value on disk will improve the query performance, especially if you are querying this column frequently, sorting, indexing etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.