Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres add column with initially calculated values

Tags:

postgresql

I'm looking to add a new column to a pre-existing table which is filled with values. The new column is going to be NOT NULL, and so for each pre-existing row it will need a value.

I'm looking for the initial values on this column to be calculated based off other values in the table at the time of column creation, and only at the time of column creation.

I have a very specific use case, so I'm not looking for a workaround. I'll give a very boiled-down example of what I'm looking for:

Say I have this data:

CREATE TABLE numbers (
  value1 INTEGER NOT NULL,
  value2 INTEGER NOT NULL
);

INSERT INTO numbers(value1, value2) VALUES (10, 20), (2, 5);

I wish to create a new column value3 on the numbers table which, at the time of creation, is always equal to the sum of its corresponding value1 and value2 columns.

E.g.:

ALTER TABLE numbers ADD COLUMN value3 INTEGER;

/* ... some more logic which calculates the initial values ... */

ALTER TABLE numbers
ALTER COLUMN value3 SET NOT NULL;

And after this is done, I'd like the following data:

-- The 3rd value will be the sum of the first 2 values
SELECT * FROM numbers;

value1 | value2 | value3
-------+--------+-------
10     | 20     | 30
2      | 5      | 7

I'll later need to update the data, perhaps ruining the relationship value3 === (value1 + value2):

UPDATE numbers SET value3=9823 WHERE value1=10;

How can I implement the step of inserting calculated initial values into the value3 column?

like image 649
Gershom Maes Avatar asked Mar 19 '18 16:03

Gershom Maes


People also ask

How do I add a column to a PostgreSQL table?

Second, specify the name of the new column as well as its data type and constraint after the ADD COLUMN keywords. When you add a new column to the table, PostgreSQL appends it at the end of the table. PostgreSQL has no option to specify the position of the new column in the table.

How to add a column with the NOT NULL constraint in PostgreSQL?

You can also add a column with the NOT NULL constraint to an existing table in PostgreSQL. The Syntax is as follows: ALTER TABLE table_name ADD COLUMN column_name data_type NOT NULL; But the above statement will lead you to an error if the existing table already has data (rows), as the new column added has the NOT NULL constraint.

How do I add z-scores to a column in PostgreSQL?

The first two parameters get the schema and table name for the table where we are going to add the columns. The next parameter is going to be the prefix for the new calculated column name. The next parameter is an array of text values (which is a data type in PostgreSQL). The array contains the name of all the columns for which we want Z-scores.

How to add a column with a name that already exists in PostgreSQL?

In PostgreSQL, you will encounter an error if you try to add a column with a name that already exists. And to avoid this error you can use the IF NOT EXISTS option with your ADD COLUMN clause. This option makes PostgreSQL add the new column only if the column name does not exist in the table. The syntax to do so is as follows:


1 Answers

I discovered a simple way! The following adds the value3 column with the desired initial values:

ALTER TABLE numbers
ADD COLUMN value3 INTEGER; -- Exclude the NOT NULL constraint here

UPDATE numbers SET value3=value1+value2; -- Insert data with a regular UPDATE

ALTER TABLE numbers
ALTER COLUMN value3 SET NOT NULL; -- Now set the NOT NULL constraint

This method is good when postgres has a native function for the calculation you want to apply to the new column. E.g. in this case the calculation I want is "sum", and postgres does that via the + operator. This method will be more complex for operations not natively provided by postgres.

like image 100
Gershom Maes Avatar answered Oct 27 '22 21:10

Gershom Maes