1

I want to create a column in a table in postgres for special needs, this column must consist for example of a concatenation of two another column.

for example i have

LAYER: 'accessory'
TYPE: 'copper'

I want a column (symbology) to be filled automatically during an insertion or an update by

SYMBOLOGY: 'accessory - copper'

do I need a trigger to do this or is there some other ways to avoid the trigger.

Illustration

LAYER TYPE SYMBOLOGY
accessory copper accessory - copper

cordially

1
  • 1
    Note that tables have columns, not fields. Commented Mar 17, 2021 at 9:06

1 Answer 1

1

You can use a generated column.

alter table things
  add column symbology text
  generated always as (layer || ' - ' || type)
  stored;

Or, if its only for some specific circumstances, you can create a view

create view thing_symbols as
select
  *,
  (layer || ' - ' || type) as symbology
from things;
Sign up to request clarification or add additional context in comments.

2 Comments

is there a solution in 9.x versions for the generated column ?
@Kalifornium . . . In earlier versions of Postgres, you would use either a trigger or a view.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.