{"id":3740,"date":"2023-10-08T12:11:44","date_gmt":"2023-10-08T12:11:44","guid":{"rendered":"https:\/\/www.copahost.com\/blog\/?p=3740"},"modified":"2023-11-03T11:37:00","modified_gmt":"2023-11-03T11:37:00","slug":"update-sql","status":"publish","type":"post","link":"https:\/\/www.copahost.com\/blog\/update-sql\/","title":{"rendered":"Update SQL: how to update data in SQL"},"content":{"rendered":"\n<p>Update in SQL is an operation that&nbsp;<strong>modifies existing information in database tables<\/strong>&nbsp;.&nbsp;This allows you&nbsp;<strong>to update, create or delete records according to usage needs<\/strong>&nbsp;, ensuring the timeliness and reliability of the data.<\/p>\n\n\n\n<p>In this article, we will explore the basic concepts of Update and present its syntax for different uses.&nbsp;In addition, we will discuss the types of Updates, such as single and multiple Updates, and present techniques and best practices to optimize queries and ensure effectiveness and efficiency in your SQL operations with Update.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_69_1 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.copahost.com\/blog\/update-sql\/#Update_syntax_in_SQL\" title=\"Update syntax in SQL\">Update syntax in SQL<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.copahost.com\/blog\/update-sql\/#Update_types_in_SQL\" title=\"Update types in SQL\">Update types in SQL<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.copahost.com\/blog\/update-sql\/#Single_and_multiple_updates\" title=\"Single and multiple updates\">Single and multiple updates<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.copahost.com\/blog\/update-sql\/#Update_affiliates_and_independents\" title=\"Update affiliates and independents\">Update affiliates and independents<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.copahost.com\/blog\/update-sql\/#Block_and_graph_update\" title=\"Block and graph update\">Block and graph update<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.copahost.com\/blog\/update-sql\/#Techniques_and_best_practices_for_Update_SQL\" title=\"Techniques and best practices for Update SQL\">Techniques and best practices for Update SQL<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.copahost.com\/blog\/update-sql\/#Advanced_Update_SQL_features\" title=\"Advanced Update SQL features\">Advanced Update SQL features<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.copahost.com\/blog\/update-sql\/#Update_with_transactions\" title=\"Update with transactions\">Update with transactions<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.copahost.com\/blog\/update-sql\/#Locks\" title=\"Locks\">Locks<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.copahost.com\/blog\/update-sql\/#Sessions_for_Concurrent_Access_Management\" title=\"Sessions for Concurrent Access Management\">Sessions for Concurrent Access Management<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.copahost.com\/blog\/update-sql\/#Update_using_stored_procedures_and_functions\" title=\"Update using stored procedures and functions\">Update using stored procedures and functions<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.copahost.com\/blog\/update-sql\/#Examples_and_practical_use_of_Update_SQL\" title=\"Examples and practical use of Update SQL\">Examples and practical use of Update SQL<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Update_syntax_in_SQL\"><\/span>Update syntax in SQL<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The command syntax&nbsp;&nbsp;<code>UPDATE<\/code>&nbsp;in SQL is used to update records in a table or dataset.&nbsp;Command has following basic structure:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>UPDATE &#091;table_name]\nSET &#091;column1] = &#091;value1], &#091;column2] = &#091;value2], ...\nWHERE &#091;condition];<\/code><\/pre>\n\n\n\n<p>The command elements&nbsp;&nbsp;<code>UPDATE<\/code>&nbsp;are:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code><strong>UPDATE<\/strong><\/code>: It is part of the command that indicates that we want to update information in a table.<\/li>\n\n\n\n<li><strong>[table_name]<\/strong>&nbsp;: This is the name of the table we want to update.&nbsp;This way, if we are updating a table without specifying a name, we can leave this space blank.<\/li>\n\n\n\n<li><code><strong>SET<\/strong><\/code>: We use this command to define the columns that we want to update, along with their respective values.<\/li>\n\n\n\n<li><strong>[column1] = [value1], [column2] = [value2], \u2026<\/strong>&nbsp;: Here we define the columns we want to update, along with the new values \u200b\u200bwe want to assign to those columns.&nbsp;Values \u200b\u200bcan be expressed as a string, a constant, a variable, or the result of a function.<\/li>\n\n\n\n<li><code><strong>WHERE<\/strong><\/code>: We use this command to specify a condition that must be true for the update to occur.&nbsp;This way, if we are going to update all the records in the table, we can leave this space blank.<\/li>\n\n\n\n<li><strong>[condition]<\/strong>&nbsp;: This is the condition that must be true for the update to occur.&nbsp;The condition can be a combination of columns and operators (e.g. &#8216;=&#8217; for equality, &#8216;&lt;&gt;&#8217; for inequality, &#8216;&gt;&#8217;, &#8216;&lt;&#8216;, &#8216;&gt;=&#8217;, &#8216;&lt;=&#8217; for comparisons, etc.).<\/li>\n<\/ol>\n\n\n\n<p>Here is an example command&nbsp;&nbsp;<code>UPDATE<\/code>&nbsp;that updates a student&#8217;s name in a table called \u201cstudents\u201d:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>UPDATE students\nSET name = 'New Name'\nWHERE student_id = 1;<\/code><\/pre>\n\n\n\n<p>In this example, we are updating the \u201cname\u201d column of the student with the ID \u201c1\u201d to the value \u201cNew Name\u201d.&nbsp;The condition&nbsp;&nbsp;<code>WHERE student_id = 1<\/code>&nbsp;guarantees that only the student with ID 1 will be updated.&nbsp;This way, If we are updating all records in the table, we can leave the condition blank.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Update_types_in_SQL\"><\/span>Update types in SQL<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There are different types of Updates in SQL, each with its own purpose and use.&nbsp;Here are some of the common update types:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Single_and_multiple_updates\"><\/span>Single and multiple updates<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Simple update<\/strong>&nbsp;: We use simple update to update one or more values \u200b\u200bin a table.&nbsp;This way, we use the statement&nbsp;<code>UPDATE&nbsp;<\/code>followed by the name of the table and the fields we want to update, and then specifying the updated value for each field.<\/li>\n<\/ul>\n\n\n\n<p>In the example, we are updating the first name and last name of all customers in the \u201cclients\u201d table to \u201cJohn\u201d and \u201cDoe\u201d, respectively.&nbsp;Additionally, we are only updating the record with customer ID 1. See:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>UPDATE clients\nSET first_name = 'John', last_name = 'Doe'\nWHERE client_id = 1;\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Multiple update&nbsp;<\/strong><strong>:<\/strong>&nbsp;We use multiple update to update multiple values \u200b\u200bin a table at the same time.&nbsp;It is done by using the statement&nbsp;<code>UPDATE&nbsp;<\/code>followed by the name of the table and the fields you want to update, and then specifying the updated value for each field.<\/li>\n<\/ul>\n\n\n\n<p>In this example below, we are updating the name, address and city of all customers in the \u201ccustomers\u201d table with customer ID between 1 and 5.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>UPDATE customers\nSET name = 'John Doe', address = '123 Main St', city = 'Anytown'\nWHERE customer_id BETWEEN 1 AND 5;\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Update_affiliates_and_independents\"><\/span>Update affiliates and independents<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>We use the affiliate and independent update to update data for a specific group of affiliates or independents.&nbsp;This way, we use the statement&nbsp;<code>UPDATE&nbsp;<\/code>followed by the name of the table and the fields we want to update, and then specifying the updated value for each field, and then using code&nbsp;<code>WHERE&nbsp;<\/code>to specify which records we want to update.<\/p>\n\n\n\n<p>In the example, we are updating the commission of all affiliates in the \u201caffiliates\u201d table with ID between 1 and 5 to 20%.&nbsp;Look:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>UPDATE affiliates\nSET commission = 20\nWHERE affiliate_id BETWEEN 1 AND 5;\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Block_and_graph_update\"><\/span>Block and graph update<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Block update<\/strong>&nbsp;: We use block update to update a specific block of cells in a table.&nbsp;This way, we use the statement&nbsp;<code>UPDATE&nbsp;<\/code>followed by the name of the table and the fields we want to update, and then specifying the updated value for each field and the code&nbsp;<code>WHERE&nbsp;<\/code>to specify which block we want to update.<\/li>\n<\/ul>\n\n\n\n<p>This example updates the&nbsp;&nbsp;<code>name<\/code>,&nbsp;&nbsp;<code>price<\/code>&nbsp;and&nbsp;&nbsp;<code>inventory<\/code>&nbsp;record columns with&nbsp;&nbsp;<code>id<\/code>&nbsp;1 in the table&nbsp;&nbsp;<code>products<\/code>.&nbsp;Look:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>UPDATE products\nSET name = 'New Product Name', price = 19.99, inventory = 100\nWHERE id = 1;\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Graph update<\/strong>&nbsp;: We use graph update to update a data graph.&nbsp;So we use the statement&nbsp;<code>UPDATE&nbsp;<\/code>followed by the name of the table and the fields we want to update, and then specifying the updated value for each field and the code&nbsp;<code>WHERE&nbsp;<\/code>to specify which records we want to update.<\/li>\n<\/ul>\n\n\n\n<p>In the example we update the columns&nbsp;&nbsp;<code>name<\/code>,&nbsp;&nbsp;<code>price<\/code>&nbsp;and&nbsp;&nbsp;<code>inventory<\/code>&nbsp;for three different records in the table&nbsp;&nbsp;<code>products<\/code>, using code&nbsp;<code>WHERE&nbsp;<\/code>to specify the records that should be updated:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>UPDATE products\nSET name = 'New Product Name', price = 19.99, inventory = 100\nWHERE id = 1;\n\nUPDATE products\nSET name = 'New Product Name', price = 19.99, inventory = 100\nWHERE id = 2;\n\nUPDATE products\nSET name = 'New Product Name', price = 19.99, inventory = 100\nWHERE id = 3;\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Techniques_and_best_practices_for_Update_SQL\"><\/span>Techniques and best practices for Update SQL<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>UPDATE is an SQL statement, we use it to update records in a table.&nbsp;Therefore, there are several techniques and best practices for using&nbsp;<code>UPDATE&nbsp;<\/code>SQL, which aim to ensure efficiency, consistency and data security.&nbsp;Here are some of them:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Use the code<code>WHERE<\/code><\/strong>&nbsp;: We use it&nbsp;<code>WHERE&nbsp;<\/code>to specify which records will be updated.&nbsp;Thus, when using the code&nbsp;<code>WHERE<\/code>, the number of affected records is minimized, which can improve query performance.<\/li>\n\n\n\n<li><strong>Update multiple fields at once:<\/strong>&nbsp;When we need to update multiple fields in a table, do one&nbsp;<code>UPDATE<\/code>with a single statement rather than executing multiple&nbsp;<code>UPDATE<\/code>separate statements.<\/li>\n\n\n\n<li><strong>Use prepared queries:<\/strong>&nbsp;Prepared queries are a way to prevent SQL injection attacks.&nbsp;This way, they can also improve query performance, as the SQL IDE can optimize the query once and reuse the execution sheet.<\/li>\n\n\n\n<li><strong>Update only necessary columns:<\/strong>&nbsp;Avoid updating columns that do not need Update.&nbsp;Thus, helping to reduce query execution time and minimize the amount of data transferred.<\/li>\n\n\n\n<li><strong>Use the ORDER BY and LIMIT code:<\/strong>&nbsp;When we need to update just some records or records in a specific order, use the code&nbsp;<code>ORDER BY<\/code>and&nbsp;<code>LIMIT<\/code>in conjunction with the code&nbsp;<code>WHERE<\/code>.<\/li>\n\n\n\n<li><strong>Test and evaluate query performance:<\/strong>&nbsp;Test different combinations of code and parameters to find the best performance for your update.&nbsp;Consider using indexes and SQL engine optimization to further improve performance.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Advanced_Update_SQL_features\"><\/span>Advanced Update SQL features<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>SQL offers several features for managing concurrent access and updating of data in a database.&nbsp;Some of these features include:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Update_with_transactions\"><\/span><strong>Update with transactions<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>We can do the Update as part of a transaction.&nbsp;Thus, ensuring that all updates are consistent and that the database is in a consistent state after we complete the transaction.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>BEGIN TRANSACTION;\nUPDATE customers SET name = 'John Doe' WHERE id = 1;\nCOMMIT;\n<\/code><\/pre>\n\n\n\n<p>In this example, updates are made as part of a transaction\u00a0<code>BEGIN TRANSACTION<\/code> and\u00a0<code>COMMIT<\/code>.\u00a0Therefore, by performing the command as shown, Update ensures that the database is consistent after the transaction is complete.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Locks\"><\/span><strong>Locks<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>We use locks to protect data integrity and ensure that only one transaction can access and update a given set of data at a time.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM customers WHERE id = 1 FOR UPDATE;\nUPDATE customers SET name = 'John Doe' WHERE id = 1;\n<\/code><\/pre>\n\n\n\n<p>In this sense, in the example we use the&nbsp;<code>SELECT&nbsp;<\/code>with clause&nbsp;<code>FOR UPDATE<\/code>&nbsp;to block access to the record with id = 1, ensuring that only one transaction can access and update this data set at a time.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Sessions_for_Concurrent_Access_Management\"><\/span><strong>Sessions for Concurrent Access Management<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>We apply sessions to the codes to ensure that a transaction has exclusive access to the data during its execution, preventing other transactions from accessing or modifying the data during this time.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>BEGIN TRANSACTION;\nDECLARE @customer_id INT = 1;\nUPDATE customers SET name = 'John Doe' WHERE id = @customer_id;\nCOMMIT;\n<\/code><\/pre>\n\n\n\n<p>Therefore, we do not use a session to ensure that the transaction has exclusive access to the data during its execution, preventing other transactions from accessing or modifying the data during this time.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Update_using_stored_procedures_and_functions\"><\/span><strong>Update using stored procedures and functions<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>We use it to encapsulate the data update logic and ensure that the execution of the Update command is consistent and safe.&nbsp;This ensures data integrity and protects the database against errors.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE PROCEDURE UpdateCustomer\n@customer_id INT,\n@name NVARCHAR(50)\nAS\nBEGIN\nUPDATE customers SET name = @name WHERE id = @customer_id;\nEND;\n\nEXEC UpdateCustomer @customer_id = 1, @name = 'John Doe';\n<\/code><\/pre>\n\n\n\n<p>And finally, in the last example shown we used a stored procedure to encapsulate the data update logic.&nbsp;This way, This ensures consistent and safe execution of the Update command and ensuring data and database integrity against errors.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Examples_and_practical_use_of_Update_SQL\"><\/span>Examples and practical use of Update SQL<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Here are some practical examples of&nbsp;&nbsp;<code>UPDATE<\/code>&nbsp;SQL statements, using different features and techniques:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Example 1: Update all records in a table<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>UPDATE table_name\nSET column1 = value1,\n    column2 = value2,\n    column3 = value3;\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Example 2: Update records with a specific ID<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>UPDATE table_name\nSET column1 = value1,\n    column2 = value2,\n    column3 = value3\nWHERE id = specific_id;\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Example 3: Update multiple records with a single instruction<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>UPDATE table_name\nSET column1 = value1,\n    column2 = value2\nWHERE condition1;\n\nUPDATE table_name\nSET column3 = value3\nWHERE condition2;\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Example 4: Using queries prepared in SQL Server<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>DECLARE @id INT = 1;\nDECLARE @column1 INT = 2;\nDECLARE @column2 INT = 3;\n\nUPDATE table_name\nSET column1 = column1 + @column1,\n    column2 = column2 + @column2\nWHERE id = @id;\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Example 5: Update only some records with&nbsp;<code>LIMIT<\/code><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>UPDATE table_name\nSET column1 = value1\nWHERE condition1\nORDER BY id\nLIMIT 5;\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Example 6: Using the clause\u00a0\u00a0<code><a href=\"https:\/\/www.copahost.com\/blog\/?p=3815&amp;preview=true\">ORDER BY<\/a><\/code>\u00a0with\u00a0\u00a0<code>UPDATE<\/code>\u00a0and\u00a0<code>DELETE<\/code><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>UPDATE table_name\nSET column1 = value1\nWHERE condition1\nORDER BY id\nDELETE FROM table_name\nWHERE condition2;\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Example 7: Update multiple related tables<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>UPDATE table_name1\nSET column1 = value1\nWHERE condition1;\n\nUPDATE table_name2\nSET column2 = value2\nWHERE table_name1.foreign_key = specific_id;\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Example 8: Using triggers to ensure consistency<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TRIGGER trg_after_update_table1\nON table_name1\nAFTER UPDATE\nAS\nBEGIN\n  UPDATE table_name2\n  SET column2 = updated_value\n  WHERE table_name1.foreign_key = specific_id;\nEND;\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Example 9: Update output files as needed<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>UPDATE table_name\nSET column1 = value1,\n    column2 = value2,\n    column3 = value3\nWHERE condition1\nAND NOT EXISTS (SELECT 1 FROM output_table WHERE output_column = value1);\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Example 10: Update only records that go through a procedure<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>UPDATE table_name\nSET column1 = value1\nWHERE condition1\nAND EXISTS (SELECT 1 FROM another_table WHERE another_column = table_name.foreign_key);\n<\/code><\/pre>\n\n\n\n<p>Therefore, these examples demonstrate different situations in which we use&nbsp;&nbsp;<code>UPDATE<\/code> including multiple record updates, condition-based updates, and updates related to other tables.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Update in SQL is an operation that&nbsp;modifies existing information in database tables&nbsp;.&nbsp;This allows you&nbsp;to update, create or delete records according to usage needs&nbsp;, ensuring the timeliness and reliability of the data. In this article, we will explore the basic concepts of Update and present its syntax for different uses.&nbsp;In addition, we will discuss the types [&hellip;]<\/p>\n","protected":false},"author":17,"featured_media":3751,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[91],"tags":[],"class_list":["post-3740","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mysql"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Update SQL: how to update data in SQL - Copahost<\/title>\n<meta name=\"description\" content=\"Learn how to use Update in SQL to modify data in a table in a database and determine which data will be updated!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.copahost.com\/blog\/update-sql\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Update SQL: how to update data in SQL - Copahost\" \/>\n<meta property=\"og:description\" content=\"Learn how to use Update in SQL to modify data in a table in a database and determine which data will be updated!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.copahost.com\/blog\/update-sql\/\" \/>\n<meta property=\"og:site_name\" content=\"Copahost\" \/>\n<meta property=\"article:published_time\" content=\"2023-10-08T12:11:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-03T11:37:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/09\/update-SQL.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1079\" \/>\n\t<meta property=\"og:image:height\" content=\"677\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Schenia T\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Schenia T\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.copahost.com\/blog\/update-sql\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/update-sql\/\"},\"author\":{\"name\":\"Schenia T\",\"@id\":\"https:\/\/www.copahost.com\/blog\/#\/schema\/person\/2efb96f9dfaf6162f347abcd06b1429f\"},\"headline\":\"Update SQL: how to update data in SQL\",\"datePublished\":\"2023-10-08T12:11:44+00:00\",\"dateModified\":\"2023-11-03T11:37:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/update-sql\/\"},\"wordCount\":1521,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/update-sql\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/09\/update-SQL.png\",\"articleSection\":[\"MySQL\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.copahost.com\/blog\/update-sql\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.copahost.com\/blog\/update-sql\/\",\"url\":\"https:\/\/www.copahost.com\/blog\/update-sql\/\",\"name\":\"Update SQL: how to update data in SQL - Copahost\",\"isPartOf\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/update-sql\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/update-sql\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/09\/update-SQL.png\",\"datePublished\":\"2023-10-08T12:11:44+00:00\",\"dateModified\":\"2023-11-03T11:37:00+00:00\",\"description\":\"Learn how to use Update in SQL to modify data in a table in a database and determine which data will be updated!\",\"breadcrumb\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/update-sql\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.copahost.com\/blog\/update-sql\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.copahost.com\/blog\/update-sql\/#primaryimage\",\"url\":\"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/09\/update-SQL.png\",\"contentUrl\":\"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/09\/update-SQL.png\",\"width\":1079,\"height\":677,\"caption\":\"update sql\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.copahost.com\/blog\/update-sql\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.copahost.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Update SQL: how to update data in SQL\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.copahost.com\/blog\/#website\",\"url\":\"https:\/\/www.copahost.com\/blog\/\",\"name\":\"Copahost\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.copahost.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.copahost.com\/blog\/#organization\",\"name\":\"Copahost\",\"url\":\"https:\/\/www.copahost.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.copahost.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2016\/03\/copahostlogo.png\",\"contentUrl\":\"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2016\/03\/copahostlogo.png\",\"width\":223,\"height\":40,\"caption\":\"Copahost\"},\"image\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.copahost.com\/blog\/#\/schema\/person\/2efb96f9dfaf6162f347abcd06b1429f\",\"name\":\"Schenia T\",\"description\":\"Data scientist, passionate about technology tools and games. Undergraduate student in Statistics at UFPB. Her hobby is binge-watching series, enjoying good music working or cooking, going to the movies and learning new things!\",\"url\":\"https:\/\/www.copahost.com\/blog\/author\/schenia\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Update SQL: how to update data in SQL - Copahost","description":"Learn how to use Update in SQL to modify data in a table in a database and determine which data will be updated!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.copahost.com\/blog\/update-sql\/","og_locale":"en_US","og_type":"article","og_title":"Update SQL: how to update data in SQL - Copahost","og_description":"Learn how to use Update in SQL to modify data in a table in a database and determine which data will be updated!","og_url":"https:\/\/www.copahost.com\/blog\/update-sql\/","og_site_name":"Copahost","article_published_time":"2023-10-08T12:11:44+00:00","article_modified_time":"2023-11-03T11:37:00+00:00","og_image":[{"width":1079,"height":677,"url":"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/09\/update-SQL.png","type":"image\/png"}],"author":"Schenia T","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Schenia T","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.copahost.com\/blog\/update-sql\/#article","isPartOf":{"@id":"https:\/\/www.copahost.com\/blog\/update-sql\/"},"author":{"name":"Schenia T","@id":"https:\/\/www.copahost.com\/blog\/#\/schema\/person\/2efb96f9dfaf6162f347abcd06b1429f"},"headline":"Update SQL: how to update data in SQL","datePublished":"2023-10-08T12:11:44+00:00","dateModified":"2023-11-03T11:37:00+00:00","mainEntityOfPage":{"@id":"https:\/\/www.copahost.com\/blog\/update-sql\/"},"wordCount":1521,"commentCount":0,"publisher":{"@id":"https:\/\/www.copahost.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.copahost.com\/blog\/update-sql\/#primaryimage"},"thumbnailUrl":"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/09\/update-SQL.png","articleSection":["MySQL"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.copahost.com\/blog\/update-sql\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.copahost.com\/blog\/update-sql\/","url":"https:\/\/www.copahost.com\/blog\/update-sql\/","name":"Update SQL: how to update data in SQL - Copahost","isPartOf":{"@id":"https:\/\/www.copahost.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.copahost.com\/blog\/update-sql\/#primaryimage"},"image":{"@id":"https:\/\/www.copahost.com\/blog\/update-sql\/#primaryimage"},"thumbnailUrl":"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/09\/update-SQL.png","datePublished":"2023-10-08T12:11:44+00:00","dateModified":"2023-11-03T11:37:00+00:00","description":"Learn how to use Update in SQL to modify data in a table in a database and determine which data will be updated!","breadcrumb":{"@id":"https:\/\/www.copahost.com\/blog\/update-sql\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.copahost.com\/blog\/update-sql\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.copahost.com\/blog\/update-sql\/#primaryimage","url":"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/09\/update-SQL.png","contentUrl":"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/09\/update-SQL.png","width":1079,"height":677,"caption":"update sql"},{"@type":"BreadcrumbList","@id":"https:\/\/www.copahost.com\/blog\/update-sql\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.copahost.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Update SQL: how to update data in SQL"}]},{"@type":"WebSite","@id":"https:\/\/www.copahost.com\/blog\/#website","url":"https:\/\/www.copahost.com\/blog\/","name":"Copahost","description":"","publisher":{"@id":"https:\/\/www.copahost.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.copahost.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.copahost.com\/blog\/#organization","name":"Copahost","url":"https:\/\/www.copahost.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.copahost.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2016\/03\/copahostlogo.png","contentUrl":"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2016\/03\/copahostlogo.png","width":223,"height":40,"caption":"Copahost"},"image":{"@id":"https:\/\/www.copahost.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.copahost.com\/blog\/#\/schema\/person\/2efb96f9dfaf6162f347abcd06b1429f","name":"Schenia T","description":"Data scientist, passionate about technology tools and games. Undergraduate student in Statistics at UFPB. Her hobby is binge-watching series, enjoying good music working or cooking, going to the movies and learning new things!","url":"https:\/\/www.copahost.com\/blog\/author\/schenia\/"}]}},"_links":{"self":[{"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/posts\/3740","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/comments?post=3740"}],"version-history":[{"count":6,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/posts\/3740\/revisions"}],"predecessor-version":[{"id":3850,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/posts\/3740\/revisions\/3850"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/media\/3751"}],"wp:attachment":[{"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/media?parent=3740"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/categories?post=3740"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/tags?post=3740"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}