AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 265021
Accepted
Pavanraotk
Pavanraotk
Asked: 2020-04-15 21:36:57 +0800 CST2020-04-15 21:36:57 +0800 CST 2020-04-15 21:36:57 +0800 CST

对现有表的约束失败

  • 772

我想将以下约束添加到现有表中:

ALTER TABLE ONLY daily_summary_202004 
  ADD CONSTRAINT odo_no_overlapping_202004 
  EXCLUDE USING gist (((vehicle_gid)::text) WITH =, numrange(odo_start, odo_end) WITH &&) 
WHERE (start_date >= '2020-04-01' 
   AND start_date < ' 2020-05-01' 
   AND active) ;

但是表中很少有条目在生产中没有达到约束。

是否可以编写一个 SQL 查询来获取所有错误行,以便在添加约束之前删除它们?

表的 DDL

create table if not exists ifta.daily_summary_202004
(
    id                 uuid           default uuid_generate_v4() not null primary key,
    distance           numeric(10, 3) default 0                  not null,
    fuel_code          varchar(128)                              not null,
    state              varchar                                   not null,
    odo_start          numeric(10, 3)                            not null,
    odo_end            numeric(10, 3),
    start_date         timestamp                                 not null,
    end_date           timestamp,
    vehicle_gid        uuid
);

本质上,不应有重叠的 odo_start 和 odo_end 条目,因此存在约束。但是由于一个现有的错误,我们有一些重叠的条目,我想找到它们并摆脱它们。

postgresql-9.6
  • 1 1 个回答
  • 35 Views

1 个回答

  • Voted
  1. Best Answer
    pifor
    2020-04-16T01:19:24+08:002020-04-16T01:19:24+08:00

    我认为先前给出的查询已关闭您需要的内容:

    select *
    from daily_summary_202004 ds1
    WHERE ds1.start_date >= date '2020-04-01' 
      AND ds1.start_date < date '2020-05-01' 
      AND ds1.active
      AND exists (select *
                      from daily_summary_202004 ds2
                      where ds1.vehicle_gid = ds2.vehicle_gid
                        and numrange(ds1.odo_start, ds1.odo_end) &&  
                            numrange(ds2.odo_start, ds2.odo_end)
                        and ds1.id <> ds2.id
                        );
    

    它选择具有给定约束的表中不存在的行。但是它不能给出应该被拒绝的那些:它只给出候选人。你需要选择。

    如果我用一些数据进行模拟。我得到:

    select * from daily_summary_202004 ;
     id | vehicle_gid | odo_start | odo_end |     start_date      | active 
    ----+-------------+-----------+---------+---------------------+--------
      3 |          10 |        10 |      20 | 2020-04-15 00:00:00 | t
      2 |          10 |         3 |       4 | 2020-04-15 00:00:00 | t
      1 |          10 |         2 |       6 | 2020-04-15 00:00:00 | t
    (3 rows)
    
    ---
    select *
    from daily_summary_202004 ds1
    WHERE ds1.start_date >= date '2020-04-01' 
      AND ds1.start_date < date '2020-05-01' 
      AND ds1.active
      AND exists (select *
                      from daily_summary_202004 ds2
                      where ds1.vehicle_gid = ds2.vehicle_gid
                        and numrange(ds1.odo_start, ds1.odo_end) && numrange(ds2.odo_start, ds2.odo_end)
                        and ds1.id <> ds2.id
                        );
     id | vehicle_gid | odo_start | odo_end |     start_date      | active 
    ----+-------------+-----------+---------+---------------------+--------
      2 |          10 |         3 |       4 | 2020-04-15 00:00:00 | t
      1 |          10 |         2 |       6 | 2020-04-15 00:00:00 | t
    (2 rows)
    
    alter table only daily_summary_202004  
    add constraint odo_no_overlapping
    exclude using gist
       (
       ((vehicle_gid)::text) WITH =, 
         numrange(odo_start, odo_end) WITH &&) 
         WHERE (start_date >= '2020-04-01' 
          AND start_date < ' 2020-05-01' 
        AND active
    ) ;
    psql:tc.sql:39: ERROR:  could not create exclusion constraint "odo_no_overlapping"
    DETAIL:  Key ((vehicle_gid::text), numrange(odo_start::numeric, odo_end::numeric))=(10, [3,4)) conflicts with key ((vehicle_gid::text), numrange(odo_start::numeric, odo_end::numeric))=(10, [2,6)).
    

    此处 PostgreSQL 报告涉及的行,但您还必须选择保留哪一个和拒绝哪一个。

    • 1

相关问题

  • 不与 OR 一起使用的索引

  • 架构复制后函数的权限被拒绝

  • 重新创建一个包含所有级联关系的 Postgres 表

  • 无法创建单个表的数据备份

  • 'returning' 子句能否返回未插入的源列?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve