The UPDATE statement is used to modify data in a database table.
UPDATE can be used to modify one column at a time or multiple columns at a time. The syntax for updating a single column is as follows:
The syntax for updating multiple columns is as follows:
We use the following table for our examples.
Table Store_Information
We notice that the sales for Los Angeles on Jan-08-1999 is actually 500ドル instead of 300,ドル and that particular entry needs to be updated. To do so, we use the following SQL query:
The resulting table would look like
Table Store_Information
In this case, there is only one row that satisfies the condition in the WHERE clause. If there are multiple rows that satisfy the condition, all of them will be modified. If no WHERE clause is specified, all rows will be modified.
We notice that the 'San Diego' entry has the wrong Sales and TXN_Date information. To fix it, we run the following SQL statement:
The table now becomes,
Table Store_Information
IMPORTANT: When using the UPDATE statement, pay special attention to make sure that some type of filtering criteria is specified. Otherwise, the value of all rows can be changed.
1. Using the same Store_Information table right above, what data is in the table after the following SQL statement is executed?
UPDATE Store_Information
SET Sales = 800
WHERE Store_Name = 'Boston';
2. Continuing to use the same table. What is the content of the table after the following SQL statement is executed?
UPDATE Store_Information
SET Sales = 2000
WHERE Store_Name = 'Los Angeles' AND Txn_Date = 'Jan-10-1999';
3. Again using the same table. What is the content of the table after the following SQL statement is executed?
UPDATE Store_Information
SET Sales = 1000;
Data modified by the UPDATE statement is shown in red below.
1. Table Store_Information
2. Table Store_Information
3. Table Store_Information
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.