postgresql - get a list of columns difference between 2 tables

大憨熊 提交于 2019-12-11 11:12:26

问题


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.

Example:

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?


回答1:


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'
    )


来源:https://stackoverflow.com/questions/36733941/postgresql-get-a-list-of-columns-difference-between-2-tables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!