Updates

2.8. Updates

You can update existing rows using the UPDATE command. Suppose you discover the temperature readings are all off by 2 degrees after November 28. You can correct the data as follows:

UPDATE weather

    SET temp_hi = temp_hi - 2,  temp_lo = temp_lo - 2

    WHERE date > '1994-11-28';

Look at the new state of the data:

SELECT * FROM weather;

     city      | temp_lo | temp_hi | prcp |    date

---------------+---------+---------+------+------------

San Francisco |      46 |      50 | 0.25 | 1994-11-27

San Francisco |      41 |      55 |    0 | 1994-11-29

Hayward       |      35 |      52 |      | 1994-11-29

(3 rows)

 

 

2.8  更新

可以使用UPDATE命令更新已有行。假设你发现从11月28号开始,所有的温度度数全部高出2度,那么可以使用以下语句纠正数据:

UPDATE weather

    SET temp_hi = temp_hi - 2,  temp_lo = temp_lo - 2

    WHERE date > '1994-11-28';

 

查看纠偏后的数据:

SELECT * FROM weather;

     city      | temp_lo | temp_hi | prcp |    date

---------------+---------+---------+------+------------

San Francisco |      46 |      50 | 0.25 | 1994-11-27

San Francisco |      41 |      55 |    0 | 1994-11-29

Hayward       |      35 |      52 |      | 1994-11-29

(3 rows)

 

你可能感兴趣的:(PostgreSQL,11.2,Documentation,Oracle,PostgreSQL,11.2,Documentation,Learning)