![]() |
PostgreSQL is one of the most advanced general-purpose object-relational database management systems and is open-source. Being an open-source software, its source code is available under the open-sourcePostgreSQL license, a liberal open-source license. Anyone with the right skills is free to use, modify, and distribute PostgreSQL in any form. As it is highly stable, very low effort is required to maintain this DBMS. In this article, we will be looking at how one can update the top 100 records using PostgreSQL. Understanding this will allow developers to easily manipulate data in their existing tables. Introduction to Updating Records in PostgreSQLUpdating records in a PostgreSQL database involves modifying existing data to reflect changes or corrections. This operation is essential for maintaining data integrity, ensuring accuracy, and keeping information up to date. Generally, UPDATE Statement is used to update or change the already existing record to new records. Setting Up EnvironmentLet us start by creating a table and adding some sample data to the table. We will create a test table with an id and title field. The following query creates the table: CREATE TABLE test(id INTEGER, title VARCHAR(100));
Now that we have the table, let’s insert some data into the data. The following query inserts ten records in the test table: INSERT INTO test (id, title) Output: ![]() Initial Data Now that we have the setup in place, lets now go forward to see how we can update the top 100 records of the table. We are going to have a look in this article at two methods to update the top 100 records of the table. Ways to Update Top Records in PostgreSQLMethod 1: Using SubqueryThe LIMIT clause is used to restrict the records returned by the query. We can make use of the LIMIT clause in conjunction with UPDATE clause to restrict the query to only update the top n records. However, to make this work, we need to provide an ordering using the ORDER BY clause for the SQL engine to understand what records to return. The following query updates the top 3 records depending on the ascending order of the id column: Query: UPDATE test SET id=-10 Output: ![]() Updated data Explanation: The following image shows the records in the table after the update command is executed. Method 2: Using CTEWe can make use of Common Table Expression (CTE) to select only the top n records from the table and later update them using the UPDATE clause. However, to make this work, we need to provide an ordering using the the clause for the SQL engine to understand what records to return. The following query first creates a CTE of the top 3 records ordered by the id field. The update statement uses the CTE to update the top 3 records as above. Query: WITH CTE AS Output: ![]() Updated data Explanation: The following image shows the records in the table after the update command is executed. Technical ExampleLet’s now use the concepts we have learned in this article in a technical example. First let’s create the table and insert some data inside it. The following query creates an employee table and inserts nine records in it. Query: -- create Output: ![]() Initial data Explanation: The above query creates an employee table with three fields and inserts the data for nine employees. The following query returns the initial data in the table. Now let’s try to update the department of the top 3 employees ordered by employee id to ‘Analytics‘. For this we are going to make use of the concepts learned in method 2. We will use make use of the CTE to get the desired results. In the following query, we make use of ORDER BY clause to order the employees by their employee id and the LIMIT clause in the end to restrict the update statement to the top 3 records: Query: WITH CTE AS Output: ![]() Updated data Explanation: In the output as we can the department of Dave, Jake and Jill has been updated to Analytics from Accounting, Sales respectively. ConclusionIn this article we have understand that how to update n records in the table. We have seen the two method through which we can update the n records. The methods are Using Subquery and Using Common Text Expression(CTE). We have also saw examples of how do it. Now you can easily update the n number of record with the help of these method easily. |
Reffered: https://www.geeksforgeeks.org
Databases |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |