UPDATE changes field values in a table. You can use it to make a simple, global change to that field, or use it with WHERE to make the change to every row in the table that has a matching value.

 

Simple UPDATE

Use UPDATE to set the value of a particular field:

 

1 SQL> update staging.order_item

set customer_name = ‘Acme Inc.’;

Screen Shot 2016-06-06 at 8.57.08 AM

 

Check your change by retrieving that field from the table:

 

1 SQL> select customer_name, count(*) from staging.order_item;

Screen Shot 2016-06-06 at 9.07.43 AM

 

Use UPDATE to Make Global Edits

To change all fields that contain the same value to a new value, use UPDATE with WHERE.

In our first example, we’ll do a simple update by setting the batch_name variable:

 

1 SQL> update staging.order_item

set batch_name = ‘TriggersOrdersBatches’;

Screen Shot 2016-06-06 at 9.07.51 AM

 

Check your update by retrieving that field from the table:

 

1 SQL> select batch_name, count(*) from staging.order_item;

Screen Shot 2016-06-06 at 9.08.06 AM

Now let’s try a more complicated example and make a batch edit. We’ll update all items where product_name is null to ‘UNKNOWN’:

 

1 SQL> update staging.order_item

set product_name = ‘UNKNOWN’

where product_name is null;

Screen Shot 2016-06-06 at 9.08.16 AM