Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate a row record in same table in postgresql

I want to duplicate a row record in same table but with modifications on some column values. I know i can duplicate a row record by using the following script.

INSERT INTO table_name( 
column_name1, column_name2, column_name3 ....
)
SELECT column_name1, column_name2, column_name3 ....
FROM table_name WHERE id=1;

But it will duplicate the whole row. For modification i further need to add update script.

So my question is, is there any simpler way to handle my scenario. Since the table in which i am working have around 40 columns, so i think this way is not feasible.

Any new ideas are most welcome.

Thanks in advance.

like image 477
Suniel Avatar asked Jul 15 '16 05:07

Suniel


People also ask

How do I duplicate a row in a table in SQL?

To select duplicate values, you need to create groups of rows with the same values and then select the groups with counts greater than one. You can achieve that by using GROUP BY and a HAVING clause.

How do you duplicate rows in pgAdmin?

On the pgAdmin 4 GUI Tool page, it seems the only proposed way to copy is to click on this button : Click the Copy icon to copy the currently selected row.

How do I insert duplicate rows in SQL?

Use the context menu on the same table, to get another script: "Script Table as | SELECT To | New Query Window". This will be a totally standard select list, with all your fields listed out. Copy the whole query and paste it in over the VALUES clause in your first query window. This will give you a complete INSERT ...

How to get duplicate rows in PostgreSQL table?

Getting duplicate rows in postgresql table can be accomplished by using multiple methods each is explained with an example. view source print? We have chosen duplicate row by counting the number of rows for each studentid and chosen the rows having count > 1.

How do you identify duplicates in a table in SQL?

The first two rows are duplicates (except for the DogId column, which is the table’s primary key, and contains a unique value across all rows). The last three rows are also duplicates (except for the DogId column).

How many rows are duplicates in the database?

The first two rows are duplicates, and the last three rows are duplicates. That’s because all three columns contain the same values in each duplicate row. We can use the following query to see how many rows are duplicates: SELECT PetId, PetName, PetType, COUNT (*) AS "Count" FROM Pets GROUP BY PetId, PetName, PetType ORDER BY PetId;

How to delete almost identical rows in a table?

For almost identical rows (identical except for one or more properties): Select one of the rows according to some criteria and delete the remaining ones. That is what my article is about. 1) How to find duplicates? Imagine you have a table containing some data on employees of a company.


1 Answers

Change values of columns directly in SELECT, the aditional update is needless, just like in the below example

INSERT INTO table_name( 
column_name1, column_name2, column_name3 ....
)
SELECT column_name1, 
       'New string value of column 2', 
       column_name3,
       ......
       ......
       1234 as new_val_of_col_25,
       column_name26,
       ......
FROM table_name WHERE id=1;
like image 71
krokodilko Avatar answered Oct 05 '22 06:10

krokodilko