Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL - set a default cell value according to another cell value

If i have a column say column a of any given values, and i want another column column b to have a default value according to the value of column a

In another words:
if column a = 'peter' then column b default value = 'doctor'.

like image 800
mosid Avatar asked May 24 '13 14:05

mosid


People also ask

How do I change the default value of a column in PostgreSQL?

Changing a Column's Default Value. To set a new default for a column, use a command like: ALTER TABLE products ALTER COLUMN price SET DEFAULT 7.77; Note that this doesn't affect any existing rows in the table, it just changes the default for future INSERT commands.

How do I set the default value in a column in pgAdmin?

Set Column Default using pgAdminIn the popup, go to 'Columns' tab and click on the edit icon againt a column to change the default value, as shown below. Now, go to the 'Constraints' tab and set or change the default value. Click on the Save button to save the changes.

Can you define a default value for a column?

Select the column for which you want to specify a default value. In the Column Properties tab, enter the new default value in the Default Value or Binding property. To enter a numeric default value, enter the number. For an object or function enter its name.

How do I find the default value of a column in PostgreSQL?

you can just type " \d table_name" command , then It will displays some information about the table, such as the default value of a column. \d will show the default values of a column .


1 Answers

This is not possible with a simple DEFAULT value, as the manual clearly states:

The value is any variable-free expression (subqueries and cross-references to other columns in the current table are not allowed).

You could use a trigger instead:

CREATE OR REPLACE FUNCTION trg_foo_b_default()   RETURNS trigger   LANGUAGE plpgsql AS $func$ BEGIN    -- For just a few constant options, CASE does the job:    NEW.b := CASE NEW.a                WHEN 'peter'  THEN 'doctor'                WHEN 'weirdo' THEN 'shrink'                WHEN 'django' THEN 'undertaker'                ELSE NULL             END;     /*    -- For more, or dynamic options, consider a lookup table:    SELECT INTO NEW.b  t.b    FROM   def_tbl t    WHERE  t.a = NEW.a;    */     RETURN NEW; END $func$;   CREATE TRIGGER b_default BEFORE INSERT ON foo FOR EACH ROW WHEN (NEW.b IS NULL AND NEW.a IS NOT NULL) EXECUTE PROCEDURE trg_foo_b_default(); 

To make it more efficient use a WHEN clause in the trigger definition (available since Postgres 9.0): This way the trigger function is only executed, when it's actually useful. (Assuming we can let b IS NULL slide if a IS NULL.)

Works in a similar, but subtly different fashion from a DEFAULT value.
With a default value, you can explicitly insert NULL to overrule the default. That's not possible here, NULL in b is replaced with the value derived from a.

like image 159
Erwin Brandstetter Avatar answered Sep 19 '22 06:09

Erwin Brandstetter