The SQL UPDATE syntax

The general syntax is:

  1. UPDATE tablename
  2. SET columnname = value, columnname = value,

To limit the number of records to UPDATE append a WHERE clause:

  1. UPDATE tablename
  2. SET columnname = value, columnname = value,
  3. WHERE condition

 


 

SQL UPDATE Examples

Problem: discontinue all products in the database

  1. UPDATE Product
  2. SET IsDiscontinued = 1

Note: the value 1 denotes true.
Results: 77 records updated.


Problem: Discontinue products over $50.

  1. UPDATE Product
  2. SET IsDiscontinued = 1
  3. WHERE UnitPrice > 50

Note: the value 1 denotes true.
Results: 7 records updated.


Problem: Discontinue product with Id = 46.

  1. UPDATE Product
  2. SET IsDiscontinued = 1
  3. WHERE Id = 46

This is a more common scenario in which a single record is updated.
Note: the value 1 denotes true.
Results: 1 record updated.


Problem: Supplier Norske Meierier (Id = 15) has moved: update their city, phone and fax.

  1. UPDATE Supplier
  2. SET City = ‘Oslo’, Phone = ‘(0)1-953530’, Fax = ‘(0)1-953555’
  3. WHERE Id = 15

This is a common scenario in which a single record is updated.
Results: 1 record updated.

댓글 남기기