|
MySQL is a popular relational database management system used in applications ranging from small projects to large enterprises. The This article explores the structure and use cases of the UPDATE StatementThe UPDATE statement in MySQL is used to modify existing records or data in a table. It is commonly used to correct any errors previously made or update the values of a column. It’s important to remember that changes made through the UPDATE statement are permanent and cannot be undone. To write an UPDATE statement, two keywords play a crucial role i.e. UPDATE and SET. With the help of the SET clause, we assign a new value to our columns. Syntax: UPDATE table_name Note: To be careful when updating records, always use the WHERE keyword with an UPDATE statement. The WHERE clause specifies the records where we want to make changes. Without the WHERE clause, all records of the table will be updated! Demo MySQL DatabaseCreate Table: CREATE TABLE horje( Output: Table – horje MySQL UPDATE Statement ExamplesLet’s look at some examples of UPDATE statements in MySQL. These examples will help you understand the workings of the UPDATE statement and the different use cases of the statement. Example 1: UPDATE Statement with Single ColumnIn this example, we will update single-column data for a particular row. We will update contest_rank to 10 for the id =”Neeraj50″. Query: UPDATE horje Output: Result – Single Column Explanation: The above image shows that the rank has been updated to 10 for the row where the id = “Neeraj50“. Previously it was 10 for this particular record. Thus we can conclude that our desired row data has been updated without hampering the other rows. Example 2: Update With Multiple ColumnsIn this example, we will update two column values at once. We will update contest_rank and courses_enrolled for the id = “Sumit65”. Query: UPDATE horje Output: Result – Multiple Columns Explanation: In the above output, we can observe that the contest_rank and courses_enrolled are 55 and 15 respectively for the row with id = “Sumit65”. Previously contest_rank and courses_enrolled are 4 and 5 respectively for this particular record. Example 3: UPDATE With String ValuesIn this example, we will update the name column for a particular field. We will update name = “Vishu Vaishnav” where id = “vish3001”. Query: UPDATE horje Output: Result – String Value Update Explanation: We can notice that the name has been changed to “Vishu Vaishnav” from “Vishu” for the field, where id is “vish3001”. Thus we can conclude that our data has been updated. Example 4: UPDATE Without WHERE ClauseIn this example, we will not use an update statement. We will increment the monthly score by 1 for each record. With the help of the below query, we can easily perform this task. Query: UPDATE horje Output: Result – Increment By 1 Explanation: We can notice the change in the monthly score. We can observe that the monthly score is incremented by 1 for each record. Example 5: UPDATE With Complex QueryIn this example, we are going to execute a complex query. We will increment contest rank by 5 and courses enrolled by 10 where the id is “vish3001” or the name is ‘Harsh’. To perform this task, we can simply execute the query shown below. Query: UPDATE horje Output: Result – Complex Query Explanation: We can observe that the contest rank is incremented by 5 and the courses enrolled are incremented by 10 for both the field where the name is Neeraj and the second one where the id is “vish3001“. IGNORE Clause with UPDATE StatementIGNORE clause is used with the UPDATE statement in some database systems like MySQL. It allows users to control error handling during the update process. Working of IGNORE clause on UPDATE Statement:
IGNORE Clause SyntaxUPDATE IGNORE table_name ConclusionThe MySQL FAQs on MySQL UPDATE StatementHow do I update multiple columns in a MySQL table?
What happens if I omit the WHERE clause in an UPDATE statement?
What is the
|
Reffered: https://www.geeksforgeeks.org
Databases |
Related |
---|
![]() |
|
![]() |
![]() |
|
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |