2013-03-15 28 views
21

Đây là chương trình của web_book bàn của tôi:PostgreSQL: làm thế nào để nhân đôi liên tiếp

 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     | 

Bảng này chứa một hàng với id=3. Tôi muốn lặp lại hàng nhưng Nếu tôi cố gắng này:

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

tôi có được điều này:

ERROR: duplicate key value violates unique constraint "web_book_pkey" 
DETAIL: Key (id)=(3) already exists 

Trả lời

40

Bạn cần phải tạo một ID mới cho dòng mới chèn vào:

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; 

Như đã đề cập bởi ClodoaldoNeto bạn có thể làm mọi việc dễ dàng hơn một chút bằng cách chỉ cần bỏ qua cột ID và để định nghĩa mặc định thực hiện công việc:

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; 

Trong trường hợp này, bạn không cần phải biết tên trình tự (nhưng ít rõ ràng hơn những gì đang xảy ra).

+4

@clime Kể từ khi 'nextval' là mặc định cho môi trường 'cột id' nó không phải là cần thiết để khai báo nó. 'INSERT INTO web_book ( page_count, ...) SELECT page_count, ...' –

+0

@ClodoaldoNeto: cảm ơn, điểm tốt. Tôi đã thêm vào đó. –

+0

Tôi hiểu. Cảm ơn bạn! – clime

6

Chỉ định id cột chỉ khi bạn chỉ định giá trị của nó (và đó không phải là trường hợp của bạn). Bạn muốn sử dụng giá trị tiếp theo web_book_id_seq, do đó, không chỉ định nó trong truy vấn INSERT của bạn.

INSERT của bạn nên trông như thế này:

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; 
Các vấn đề liên quan