Friday, October 13, 2017

Update Data in Table using SQL

Beside insert and display data from table, database administrator can edit the information in a table using SQL statement. The syntax to update an information in a table is:
The Figure 1 below show the command updating the information in the "students" table.
Figure 1: Using SQL to update the student email and yea of intake using SQL statements
One thing need to be careful is to set the WHERE clause for the UPDATE statement. Same like SELECT statements, WHERE clause is used to identify the specific data in the table to perform the operation. Usually the condition for UPDATE statements in the WHERE clause is based on PRIMARY KEY. 

However, if WHERE clause is not define, the SQL will change all the data in the table. The result of such mistake is shown in Figure 2 below:
Figure 2: Result of UPDATE statements without WHERE clause
Notice in Figure 2 where all the stdemail changed into "ahmad@gmail.com", this is the result where no particular reference point is provided for the SQL to update the information. Therefore, the the statements update all the data in the column as defined by the SQL statement.

Beside update one particular data in the table using UPDATE statement with WHERE clause, database administrator can also update a set of data that through defining the correct WHERE statement. For example, updating all the intakeyear from 2017 to 2018. The operation is shown in Figure 3 below.
Figure 3: Update a set of data using SELECT and WHERE clause.