SQL UPDATE: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=Internal= * SQL * SQL <tt>WHERE</tt>")
 
 
(8 intermediate revisions by the same user not shown)
Line 2: Line 2:
* [[SQL#UPDATE|SQL]]
* [[SQL#UPDATE|SQL]]
* [[SQL_WHERE#Overview|SQL <tt>WHERE</tt>]]
* [[SQL_WHERE#Overview|SQL <tt>WHERE</tt>]]
=Overview=
<syntaxhighlight lang='sql'>
UPDATE person SET name = 'Binh Ngo Jr.', city = 'San Francisco' WHERE id = 1;
</syntaxhighlight>
:{| class="wikitable" style="text-align: left;"
|-
| [[PostgreSQL_DML_Operations#UPDATE|PostgreSQL]] || MySQL
|-
|}
The <code>UPDATE</code> statement may contain a <code>WHERE</code> clause, that identifies the rows to be modified: {{Internal|SQL_WHERE#Overview|The <tt>WHERE</tt> Clause}}
=Conditional <tt>UPDATE</tt>=
<syntaxhighlight lang='sql'>
UPDATE customer
SET active =
  CASE
    WHEN 90 <= (SELECT datediff(now(), max(rental_date))
                FROM rental r
                WHERE r.customer_id = customer.customer_id)
      THEN 0
    ELSE 1
  END
WHERE active = 1;
</syntaxhighlight>
Also see: {{Internal|SQL_Conditional_Logic#Overview|SQL Conditional Logic}}

Latest revision as of 21:14, 30 May 2024

Internal

Overview

UPDATE person SET name = 'Binh Ngo Jr.', city = 'San Francisco' WHERE id = 1;
PostgreSQL MySQL

The UPDATE statement may contain a WHERE clause, that identifies the rows to be modified:

The WHERE Clause

Conditional UPDATE

UPDATE customer
SET active =
  CASE
    WHEN 90 <= (SELECT datediff(now(), max(rental_date))
                FROM rental r
                WHERE r.customer_id = customer.customer_id)
      THEN 0
    ELSE 1
  END
WHERE active = 1;

Also see:

SQL Conditional Logic