簡體   English   中英

PostgreSQL唯一索引錯誤

[英]PostgreSQL Unique Index Error

我正在忙着編寫一個腳本來恢復數據庫備份,我遇到了一些奇怪的事情。

我有一個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文件並最后加載數據時,我沒有錯誤,一切正常。

有什么我想念的嗎? 為什么它不會讓我在加載數據后創建唯一索引?

你的列ug_code有兩行數據“傳輸”,這就是它無法創建索引的原因。

如果你先創建索引,為什么它會成功,我不知道。 但我懷疑它第二次嘗試將“傳輸”插入數據庫時​​,它只是insert時間和其他數據成功insert失敗。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM