codekingpro/portable-devtools
115k
1<?xml version="1.0" encoding="UTF-8" standalone="no"?>2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>6.2. Updating Data</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="dml-insert.html" title="6.1. Inserting Data" /><link rel="next" href="dml-delete.html" title="6.3. Deleting Data" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">6.2. Updating Data</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="dml-insert.html" title="6.1. Inserting Data">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="dml.html" title="Chapter 6. Data Manipulation">Up</a></td><th width="60%" align="center">Chapter 6. Data Manipulation</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="dml-delete.html" title="6.3. Deleting Data">Next</a></td></tr></table><hr /></div><div class="sect1" id="DML-UPDATE"><div class="titlepage"><div><div><h2 class="title" style="clear: both">6.2. Updating Data <a href="#DML-UPDATE" class="id_link">#</a></h2></div></div></div><a id="id-1.5.5.4.2" class="indexterm"></a><a id="id-1.5.5.4.3" class="indexterm"></a><p>3 The modification of data that is already in the database is4 referred to as updating. You can update individual rows, all the5 rows in a table, or a subset of all rows. Each column can be6 updated separately; the other columns are not affected.7 </p><p>8 To update existing rows, use the <a class="xref" href="sql-update.html" title="UPDATE"><span class="refentrytitle">UPDATE</span></a>9 command. This requires10 three pieces of information:11 </p><div class="orderedlist"><ol class="orderedlist compact" type="1"><li class="listitem"><p>The name of the table and column to update</p></li><li class="listitem"><p>The new value of the column</p></li><li class="listitem"><p>Which row(s) to update</p></li></ol></div><p>12 </p><p>13 Recall from <a class="xref" href="ddl.html" title="Chapter 5. Data Definition">Chapter 5</a> that SQL does not, in general,14 provide a unique identifier for rows. Therefore it is not15 always possible to directly specify which row to update.16 Instead, you specify which conditions a row must meet in order to17 be updated. Only if you have a primary key in the table (independent of18 whether you declared it or not) can you reliably address individual rows19 by choosing a condition that matches the primary key.20 Graphical database access tools rely on this fact to allow you to21 update rows individually.22 </p><p>23 For example, this command updates all products that have a price of24 5 to have a price of 10:25</p><pre class="programlisting">26UPDATE products SET price = 10 WHERE price = 5;27</pre><p>28 This might cause zero, one, or many rows to be updated. It is not29 an error to attempt an update that does not match any rows.30 </p><p>31 Let's look at that command in detail. First is the key word32 <code class="literal">UPDATE</code> followed by the table name. As usual,33 the table name can be schema-qualified, otherwise it is looked up34 in the path. Next is the key word <code class="literal">SET</code> followed35 by the column name, an equal sign, and the new column value. The36 new column value can be any scalar expression, not just a constant.37 For example, if you want to raise the price of all products by 10%38 you could use:39</p><pre class="programlisting">40UPDATE products SET price = price * 1.10;41</pre><p>42 As you see, the expression for the new value can refer to the existing43 value(s) in the row. We also left out the <code class="literal">WHERE</code> clause.44 If it is omitted, it means that all rows in the table are updated.45 If it is present, only those rows that match the46 <code class="literal">WHERE</code> condition are updated. Note that the equals47 sign in the <code class="literal">SET</code> clause is an assignment while48 the one in the <code class="literal">WHERE</code> clause is a comparison, but49 this does not create any ambiguity. Of course, the50 <code class="literal">WHERE</code> condition does51 not have to be an equality test. Many other operators are52 available (see <a class="xref" href="functions.html" title="Chapter 9. Functions and Operators">Chapter 9</a>). But the expression53 needs to evaluate to a Boolean result.54 </p><p>55 You can update more than one column in an56 <code class="command">UPDATE</code> command by listing more than one57 assignment in the <code class="literal">SET</code> clause. For example:58</p><pre class="programlisting">59UPDATE mytable SET a = 5, b = 3, c = 1 WHERE a > 0;60</pre><p>61 </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="dml-insert.html" title="6.1. Inserting Data">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="dml.html" title="Chapter 6. Data Manipulation">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="dml-delete.html" title="6.3. Deleting Data">Next</a></td></tr><tr><td width="40%" align="left" valign="top">6.1. Inserting Data </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 6.3. Deleting Data</td></tr></table></div></body></html>