Want to learn how to compute the value for a Generated Column in PostgreSQL 15.

Поділитися
Вставка
  • Опубліковано 6 жов 2024
  • In this video, we will learn how to use a Generated Column. This special column type, value, is computed from one or more other columns, or other function.
    Generated Columns.
    Learn how to create a table, that has a column of type GENERATED COLUMN.
    Learn how to write insert command on a table that has a GENERATED COLUMN.
    Learn how to select data from a table that has a GENERATED COLUMN.
    Lastly, how to perform the UPDATE command on a table that has a GENERATED COLUMN.
    #softwareNuggets, #postgresql

КОМЕНТАРІ • 7

  • @SoftwareNuggets
    @SoftwareNuggets  Рік тому +1

    Here is the source code for this video:
    select version()
    show server_version;
    drop table HR_Employee
    create table HR_Employee
    (
    emp_id int not null,
    annual_salary numeric not null,

    --a generated column is a table column
    --that is "always generated" from some other column or columns
    --the computation must produce the same datatype
    hourly_rate numeric generated always as (annual_salary / 2080) stored
    )
    insert into HR_Employee(emp_id, annual_salary)
    values
    (1, 35000.00),
    (2, 50000.00),
    (3, 100000.00);
    select *
    from HR_Employee
    select *
    from HR_Employee
    where hourly_rate > 20
    update HR_Employee
    set annual_salary = 55000
    where emp_id = 2;

  • @LibyaMicro
    @LibyaMicro 17 годин тому

    Excellent in Firebird databases there are also computed column that can fetch the value of a field from another table. such as bringing the department name from another table with the value of the field ID in the current table

    • @SoftwareNuggets
      @SoftwareNuggets  16 годин тому

      I need to check out firebird. Thanks for the introduction!!

    • @LibyaMicro
      @LibyaMicro 16 годин тому

      @@SoftwareNuggets However, this can only be done in PostgreSQL by calling a function.
      CREATE TABLE product (
      product_id UUID
      price FLOAT
      available_location VARCHAR GENERATED ALWAYS AS fetch_product_location(product_id)
      )

    • @SoftwareNuggets
      @SoftwareNuggets  16 годин тому

      @LibyaMicro that’s a great example. Thanks for the update.

  • @downey2294
    @downey2294 Рік тому

    what if i wanted to compute the value based on other values in other columns
    lets say that i have 2 entities test and semester
    and the entity test has a foreign key semester_id and a regular column credit_score
    and the entity semester also has credit_score which is the sum of all the credit_score of all the tests that are related to the semester.
    how would i compute the credit_score for semester.

    • @SoftwareNuggets
      @SoftwareNuggets  Рік тому

      Plz send me your table design and some sample data, and I’ll help you. Send to softwarenugget65@gmail.com