postgreSQL:如何复制一行
发布时间:2020-12-13 16:35:38 所属栏目:百科 来源:网络整理
导读:这是我的表web_book的方案: Column | Type | Modifiers ----------------+------------------------+------------------------------------------------------- id | integer | not null default nextval('web_book_id_seq'::regclass) page_count | intege
这是我的表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 | 该表包含id = 3的一行。我想复制行,但如果我尝试这样: INSERT INTO web_book SELECT * FROM web_book WHERE id=3; 我得到这个: ERROR: duplicate key value violates unique constraint "web_book_pkey" DETAIL: Key (id)=(3) already exists
您需要为新插入的行创建一个新的ID:
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'),description_en FROM web_book WHERE id=3; 如ClodoaldoNeto所提到的,您可以通过简单地省略ID列,让默认定义做到这一点,使事情变得更简单: INSERT INTO web_book( page_count,description_en ) SELECT page_count,description_en FROM web_book WHERE id=3; 在这种情况下,您不需要知道序列名称(但是这不清楚)。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容