【问题标题】:PostgreSQL Unique Index ErrorPostgreSQL 唯一索引错误
【发布时间】:2015-04-24 16:18:16
【问题描述】:

我正忙着编写脚本来恢复数据库备份,但遇到了一些奇怪的事情。

我有一个 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 文件并最后加载数据时,我没有收到任何错误,一切正常。

我有什么遗漏吗?为什么加载数据后不让我创建唯一索引?

【问题讨论】:

  • 也许你在创建索引之前不小心加载了两次数据?

标签: database postgresql indexing backup unique


【解决方案1】:

ug_code 列中有两行数据“传输”,这就是它无法创建索引的原因。

为什么先创建索引会成功,我不知道。但我怀疑它第二次尝试将“传输”插入数据库时​​,它只是失败了 insert 那次并且其他数据被成功插入。

【讨论】:

    猜你喜欢
    • 2018-12-02
    • 2020-10-01
    • 2018-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-25
    • 2019-04-11
    • 1970-01-01
    相关资源
    最近更新 更多