PostgreSQL - Working with a virtual/generated column

PostgreSQL - Working with a virtual/generated column

To create a table in PostgreSQL with a virtual/generated column derived from JSON data, you can use the jsonb data type along with a generated column. Here is an example:

Sample Json:

{ "firstname":"John", "lastName":"Sri" }

  1. Create the table: Define the table with a jsonb column and a generated column that extracts a value from the JSON data.

  2. Insert data & Query data: Insert JSON data into the table and Query the table to see the generated column in action.

Here is a step-by-step guide:

Step 1: Create the Table

In this example:

  • id: A serial primary key column.
  • data: A JSONB column to store the JSON data.
  • first_name: A generated column that extracts the first_name from the JSON data.
  • last_name: A generated column that extracts the last_name from the JSON data.

Here’s how you might use this table:

Step 2: Insert Data & Query Data



This would result in the following output:



In this way, first_name and last_name columns are automatically populated based on the values extracted from the data JSON column.

Comments

Popular posts from this blog

FastEndpoint vs Minimal APIs vs Controller

Kick start 2021 😃😃

Work with Raspberry PI Zero W