Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

postgreSQL: how to duplicate a row

Tags:

postgresql

This is the scheme of my table web_book:

     Column     |          Type          |                       Modifiers                        ----------------+------------------------+-------------------------------------------------------  id             | integer                | not null default nextval('web_book_id_seq'::regclass)  page_count     | integer                | not null  year_published | integer                | not null  file           | character varying(100) | not null  image          | character varying(100) | not null  display_on_hp  | boolean                | not null  name           | character varying(128) | not null  description    | text                   | not null  name_cs        | character varying(128) |   name_en        | character varying(128) |   description_cs | text                   |   description_en | text                   | 

The table contains one row with id=3. I want to duplicate the row but If I try this:

INSERT INTO web_book SELECT * FROM web_book WHERE id=3; 

I get this:

ERROR:  duplicate key value violates unique constraint "web_book_pkey" DETAIL:  Key (id)=(3) already exists 
like image 450
clime Avatar asked Mar 15 '13 10:03

clime


People also ask

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 duplicate a row 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 I create a duplicate row?

Duplicate Rows – Copy and Paste The easiest way to duplicate rows is to use Excel's Copy and Paste functionality. Say you have the following data set and want to copy Row 7 to Row 8.

Does Postgres allow duplicate rows?

PostgreSQL will use this mode to insert each row's index entry. The access method must allow duplicate entries into the index, and report any potential duplicates by returning false from aminsert . For each row for which false is returned, a deferred recheck will be scheduled.


2 Answers

You need to create a new ID for the newly inserted row:

INSERT INTO web_book(     id, page_count, year_published, file, image,     display_on_hp, name, description, name_cs,     name_en, description_cs, description_en ) SELECT nextval('web_book_id_seq'),         page_count,         year_published,         file,         image,         display_on_hp,         name,         description,         name_cs,         name_en,         description_cs,         description_en  FROM web_book WHERE id=3; 

As mentioned by ClodoaldoNeto you can make things a bit easier by simply leaving out the ID column and let the default definition do its job:

INSERT INTO web_book(     page_count, year_published, file, image,     display_on_hp, name, description, name_cs,     name_en, description_cs, description_en ) SELECT page_count,         year_published,         file,         image,         display_on_hp,         name,         description,         name_cs,         name_en,         description_cs,         description_en  FROM web_book WHERE id=3; 

In this case you don't need to know the sequence name (but it is a bit less obvious what's going on).

like image 65
a_horse_with_no_name Avatar answered Sep 23 '22 12:09

a_horse_with_no_name


Specify id column only if you specify its value (and it's not your case). You want to use next sequence web_book_id_seq value, so do not specify it in your INSERT query.

Your INSERT should looks like this:

INSERT INTO web_book (page_count, year_published, file, image, display_on_hp, name, description, name_cs, name_en, description_cs, description_en) SELECT page_count, year_published, file, image, display_on_hp, name, description, name_cs, name_en, description_cs, description_en FROM web_book WHERE id = 3; 
like image 40
user2148736 Avatar answered Sep 21 '22 12:09

user2148736