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
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.
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.
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.
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.
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).
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With