Home>

Is it possible to use the trigger function of PostgreSQL to say "Do not UPDATE when the data of a certain column of a certain table is 1"?
If possible, please tell us the specific implementation.

[table definition]

CREATE TABLE id_list (
    id
);

[Table contents]

# SELECT id FROM id_list;
  id
-------
     1
     2
     3

[What I want to achieve]
If id is 1, do not update the data

# UPDATE id_list SET id = 4 WHERE id = 1;
# SELECT id FROM id_list;
  id
-------
     1 ← Not updated if id is 1
     2
     3
# UPDATE id_list SET id = 4 WHERE id = 2;
# SELECT id FROM id_list;
  id
-------
     1
     4 ← Updated if id is 4
     3
  • Answer # 1

    There is a function you want. Please use it after confirming the operation properly.
    ... But even with these types of triggers, it is possible to interrupt the entire operation by raising an error.