Collection Contents Index Adding data using INSERT Deleting data using DELETE pdf/chap8.pdf

User's Guide
   PART 1. Working with Databases
     CHAPTER 8. Adding, Changing, and Deleting Data       

Changing data using UPDATE


You can use the UPDATE statement to change single rows, groups of rows, or all rows in a table. The UPDATE statement is followed by the name of the table or view. As in all data modification statements, you can change the data in only one table or view at a time.

The UPDATE statement specifies the row or rows you want changed and the new data. The new data can be a constant or an expression that you specify or data pulled from other tables.

If an UPDATE statement violates an integrity constraint, the update does not take place and an error message is generated. The update is canceled, for example, if one of the values being added is the wrong data type, or if it violates a constraint that has been defined for one of the columns or data types involved.

UPDATE syntax 

A simplified version of the UPDATE syntax is:

UPDATE table-name

SET column_name = expression

WHERE search-condition

If the company Newton Ent. (in the customer table of the sample database) is taken over by Einstein, Inc., you can update the name of the company using a statement such as the following:

UPDATE customer
SET company_name = 'Einstein, Inc.'
WHERE company_name = 'Newton Ent.'

You can use any expression in the WHERE clause. If you are not sure how the company name was entered, you could try updating any company called Newton, with a statement such as the following:

UPDATE customer
SET company_name = 'Einstein, Inc.'
WHERE company_name LIKE 'Newton%'

The search condition need not refer to the column being updated. The company ID for Newton Entertainments is 109. As the ID value is the primary key for the table, you could be sure of updating the correct row using the following statement:

UPDATE customer
SET company_name = 'Einstein, Inc.'
WHERE id = 109

The SET clause 

The SET clause specifies the columns to be updated, and their new values. The WHERE clause determines the row or rows are to be updated. If you do not have a WHERE clause, the specified columns of all rows are updated with the values given in the SET clause.

You can provide any expression of the correct data type in the SET clause.

The WHERE clause 

The WHERE clause specifies the rows to be updated. For example, the following statement replaces the One Size Fits All Tee Shirt with an Extra Large Tee Shirt

UPDATE product
SET size  = 'Extra Large'
WHERE name = 'Tee Shirt' 
   AND size = 'One Size Fits All'

The FROM clause 

You can use a FROM clause to pull data from one or more tables into the table you are updating.


Collection Contents Index Adding data using INSERT Deleting data using DELETE pdf/chap8.pdf