Sql updating multiple tables
Nevertheless, nothing would have happened – the statement would have worked, affecting 0 rows, because the data table doesn’t contain an employee with such a number at the moment of the query’s execution.Stay focused for another interesting feature we will discuss next.Now, we are ready to dive into the UPDATE statement. By using this code structure, SQL will change the record or records satisfying the WHERE condition, updating the old values of the columns listed with the new ones.We can see that employee number 999901 is John Smith.When updating your table, the WHERE clause is crucial, although by default in My SQL it is set to be optional.
So, let’s exit this connection and then reconnect, typing the password once again! In the previous post about SQL INSERT Statement, we inserted an employee under the number of 9-9-9-9-0-1, remember? The syntax to adhere to is UPDATE table name, the keyword SET, column names and the respective values assigned to them, and finally – WHERE, and a certain condition, or set of conditions, that must be satisfied.
This means SQL’s optimizer will, so to speak, access the record with employee number 999901, and substitute the existing values with the new ones indicated in the UPDATE statement.
Note that we did not update the “hire date” column value, right? This is fine, as we do not have to update each value of the record of interest. With a different birthdate and gender, although with the same hire date.
Remember, you should be careful when using this command because once you execute COMMIT, you cannot reverse any change!
So, after running ROLLBACK, where will we go back to? We see the information we had in the “Departments Duplicate” table at the beginning of this post.