Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres: update all values in column by one?

Tags:

php

postgresql

Is there a way to do this? I imagine the following will not work.

UPDATE table SET column = column + 1 ...

Other than writing a function or using PHP, is there a way to do this with a query?

like image 316
Evil Elf Avatar asked Nov 03 '10 14:11

Evil Elf


People also ask

How do I update multiple columns in PostgreSQL?

It is very easy to update multiple columns in PostgreSQL. Here is the syntax to update multiple columns in PostgreSQL. UPDATE table_name SET column1 = value1, column2 = value2, ... [WHERE condition];

How update works in Postgres?

PostgreSQL implements multiversioning by keeping the old version of the table row in the table – an UPDATE adds a new row version (“tuple”) of the row and marks the old version as invalid. In many respects, an UPDATE in PostgreSQL is not much different from a DELETE followed by an INSERT .

How do you update value in Pgadmin?

To change a numeric value within the grid, double-click the value to select the field. Modify the content in the square in which it is displayed. To change a non-numeric value within the grid, double-click the content to access the edit bubble.


2 Answers

Did you try it? It should just work.

like image 118
Marco Mariani Avatar answered Sep 22 '22 13:09

Marco Mariani


It'll just work:

# psql -U postgres
psql (9.0.1)
Type "help" for help.

postgres=# create database test;
CREATE DATABASE
postgres=# \c test
You are now connected to database "test".
test=# create table test (test integer);
CREATE TABLE
test=# insert into test values (1);
INSERT 0 1
test=# insert into test values (2);
INSERT 0 1
test=# select * from test;
 test 
------
    1
    2
(2 rows)

test=# update test set test = test + 1;
UPDATE 2
test=# select * from test;
 test 
------
    2
    3
(2 rows)
like image 36
jcollie Avatar answered Sep 18 '22 13:09

jcollie