postgresql - 获取两个表之间的列差异列表 [英] postgresql - get a list of columns difference between 2 tables

查看:109
本文介绍了postgresql - 获取两个表之间的列差异列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用的是使用 Postgres 8 的 Redshift.
我需要比较 (2) 个几乎相同的表,但另一个表会有额外的列,所以我需要找出列差异.

We are using Redshift which is using Postgres 8.
I need to compare (2) tables which will almost be identical, but the other table will have extra columns so, I need to find out the column difference.

示例:

CREATE TABLE table1 (
    v_id character varying(255) NOT NULL,
    v_created timestamp without time zone NOT NULL,
    abc_102 boolean,
    abc_103 boolean,
    abc_104 boolean,
    def_56 boolean DEFAULT false NOT NULL,
    def_57 boolean DEFAULT false NOT NULL
)

CREATE TABLE table2 (
    v_id character varying(255) NOT NULL,
    v_created timestamp without time zone NOT NULL,
    abc_102 boolean,
    abc_103 boolean,
    abc_104 boolean,
    abc_105 boolean,
    def_56 boolean DEFAULT false NOT NULL,
    def_57 boolean DEFAULT false NOT NULL,
    def_58 boolean DEFAULT false NOT NULL,
)

我可以使用什么查询来提供列差异列表?

What query can I use that will give me a list of the column differences?

推荐答案

您可以通过选择 table2 中所有也出现在 中的列名称来实现此目的>table1:

You can achieve this by selecting all column names from table2 which do not also appear in table1:

SELECT column_name
FROM information_schema.columns 
WHERE table_schema = 'your_schema' AND table_name = 'table2'
    AND column_name NOT IN
    (
        SELECT column_name
        FROM information_schema.columns 
        WHERE table_schema = 'your_schema' AND table_name = 'table1'
    )

这篇关于postgresql - 获取两个表之间的列差异列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆