2015-02-23 133 views
0

我正忙於編寫一個腳本來恢復數據庫備份,我遇到了一些奇怪的事情。PostgreSQL唯一索引錯誤

我有一個table.sql文件僅包含創建的表結構等

create table ugroups 
    (
    ug_code char(10) not null , 
    ug_desc char(60) not null 
); 

我有一個第二data.csv文件只包含定界符數據如

xyz | dummy data 
abc | more nothing 
fun | is what this is 

然後,我有第三個index.sql文件,它只創建索引

create unique index i_ugroups on ugroups 
    (ug_code); 

我使用命令終端像這樣

/opt/postgresql/bin/psql -d dbname -c "\i /tmp/table.sql" # loads table.sql 

我有一個批處理腳本,加載完美的數據。然後我用命令

/opt/postgresql/bin/psql -d dbname -c "\i /tmp/index.sql" # loads index.sql 

當我嘗試創建唯一索引這是給我的錯誤

ERROR: could not create unique index "i_ugroups" 
DETAIL: Key (ug_code)=(transfers) is duplicated. 

什麼奇怪的是,當我執行table.sql文件和index.sql一起文件並加載數據我沒有得到任何錯誤,它的一切工作。

有什麼我失蹤?爲什麼它不會讓我在數據加載後創建唯一索引?

+1

也許你在創建索引之前意外加載了兩次數據? – alexius 2015-02-23 11:37:34

回答

0

您的列ug_code中有兩行與數據「轉移」,這就是爲什麼它不能創建索引。

爲什麼它會成功,如果你先創建索引,我不知道。但是我懷疑它第二次嘗試將「傳輸」插入到數據庫中時,它只是不及格insert,而其他數據則成功插入。