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 / 问题 / 206992
Accepted
tinlyx
tinlyx
Asked: 2018-05-17 21:36:25 +0800 CST2018-05-17 21:36:25 +0800 CST 2018-05-17 21:36:25 +0800 CST

检查两个表是否具有相同的结构(兼容)

  • 772

我想知道是否有一种既定的方法来测试两个 PostgreSQL 表/关系是否具有相同的结构。换句话说,在我可以对它们执行集合操作的意义上,如何测试它们是否相互UNION ALL兼容EXCEPT?

我在 DBA.SE 和其他地方搜索,只能找到关于查找两个表的内容是否不同的问题,(例如检查两个表在 PostgreSQL 中是否具有相同的内容),或者何时知道兼容性(例如比较两个表结构相同,但会员编号不同)。但我有兴趣检查 table structure的兼容性。

我使用的是 PostgreSQL 10.3,但符合标准的方式当然更可取。

postgresql union
  • 5 5 个回答
  • 4199 Views

5 个回答

  • Voted
  1. Best Answer
    bigfoot
    2018-05-18T02:29:31+08:002018-05-18T02:29:31+08:00

    具有示例架构,例如:

    create table table1 (
      id integer,
      txt text,
      col1 integer,
      col2 integer);
    
    create table table2 (
      id integer,
      txt text,
      col1 text,
      colx integer);
    

    并假设您不关心列名,您可以使用查询检查联合列中的潜在冲突:

    (select data_type, ordinal_position 
    from information_schema.columns 
    where table_name = 'table1'
    except
    select data_type, ordinal_position 
    from information_schema.columns 
    where table_name = 'table2')
    union all
    (select data_type, ordinal_position 
    from information_schema.columns 
    where table_name = 'table2'
    except
    select data_type, ordinal_position 
    from information_schema.columns 
    where table_name = 'table1'
    )
    order by 2;
    

    结果将显示违规列在表结构和数据类型中的位置。

    data_type   ordinal_position
    integer     3
    text    3
    

    如果您有这个缩短的列表,您可以在 information_schema.columns 字典中查看详细信息,以查看每列的详细信息。

    • 3
  2. jjanes
    2018-05-18T04:30:05+08:002018-05-18T04:30:05+08:00

    我认为最好的方法就是试试看。

    select * from (
        (select * from pgbench_accounts limit 0) 
        union all
        (select * from pgbench_history limit 0)
    ) as foo
    

    要么它抛出一个错误,要么它没有。

    • 3
  3. David Spillett
    2018-05-18T01:17:56+08:002018-05-18T01:17:56+08:00

    从数据库本身提取有关 SQL 数据库的信息的主要标准方法是INFORMATION_SCHEMA模式,尽管您可能需要自己编写此特定查询。

    在这种情况下information_schema.tables将是关键(有关文档,请参阅https://www.postgresql.org/docs/current/static/infoschema-columns.html)。最简单的版本是表是兼容的,如果表 1 中的每一列都有对应的列与表 2 中的和相同,ordinal_position并且data_type每个中的列数相同(因此表 2 中没有额外内容) .

    为了更高级,您可能必须开始对类型匹配更加模糊:例如,具有不同属性(大小、排序规则)的不同字符串类型(char、varchar、text)可能是兼容的,有些 DB 甚至会让您使用UNION数字和字符串类型通过隐式转换数字[1]等等。

    [1]我不是 postgres 人,所以不能说这里是否是这种情况,但很容易测试。MS SQL Server 试图提供一半的帮助,但会强制使用数字类型,SELECT mixedtype = 1 UNION SELECT mixedtype = '2'但SELECT mixedtype = 'a' UNION SELECT mixedtype = 2不能a强制转换为整数 - 所以为了安全起见,我假设字符串和数字类型永远不兼容。

    • 2
  4. Mike
    2018-05-18T00:19:29+08:002018-05-18T00:19:29+08:00

    如果期望字段名称相同,则一种选择是将每个表编写成一个文件,然后使用文件比较工具(例如 WinMerge)并排比较两个脚本。结构上的任何差异都将立即显而易见。

    • 1
  5. Grey Vugrin
    2022-06-30T11:51:56+08:002022-06-30T11:51:56+08:00

    按照接受的答案 - 如果您想知道哪些列名是一个问题,请使用以下内容:

    SELECT column_name, data_type, ordinal_position
      FROM information_schema.columns
     WHERE table_schema = 'some_schema'
       AND table_name   = 'table_a'
        
    except
         
     SELECT column_name, data_type, ordinal_position
      FROM information_schema.columns
     WHERE table_schema = 'some_schema'
       AND table_name   = 'table_b'
    

    如果您只关心名称差异,请SELECT column_name仅使用而不包括其他 2 列。

    • 1

相关问题

  • 我可以在使用数据库后激活 PITR 吗?

  • 运行时间偏移延迟复制的最佳实践

  • 存储过程可以防止 SQL 注入吗?

  • PostgreSQL 中 UniProt 的生物序列

  • PostgreSQL 9.0 Replication 和 Slony-I 有什么区别?

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