mysql to postgres query convertion - COLUMNS.COLUMN_TYPE and information_schema.SEQ_IN_INDEX

29 Views Asked by At

I have two databases(replicas one is mysql, another is postgres).I have a mysql query which gets column names, and other column related info for the table, if a table misses any column, we use this info to add them.

SELECT cols.COLUMN_NAME, cols.ORDINAL_POSITION, cols.DATA_TYPE, cols.COLUMN_TYPE, 
            sta.SEQ_IN_INDEX
            ,COALESCE(cols.NUMERIC_PRECISION, cols.DATETIME_PRECISION) as DATA_WIDTH
            FROM information_schema.`COLUMNS` as cols
            LEFT JOIN information_schema.statistics as sta
            ON  sta.TABLE_SCHEMA = cols.TABLE_SCHEMA
            AND sta.TABLE_NAME = cols.TABLE_NAME
             AND sta.COLUMN_NAME = cols.COLUMN_NAME
             AND sta.INDEX_NAME = 'primary'
            WHERE cols.TABLE_SCHEMA = 'schema1'
            AND cols.TABLE_NAME  = 'TAB_TABLE1'
            ORDER BY cols.ORDINAL_POSITION 

the above one is mysql, I need similar one in postgres, I tried like below, but I am missing COLUMN_TYPE, and SEQ_IN_INDEX.

SELECT cols.column_name, cols.ordinal_position, cols.data_type, 
COALESCE(cols.NUMERIC_PRECISION, cols.DATETIME_PRECISION) as DATA_WIDTH
            FROM INFORMATION_SCHEMA.COLUMNS cols
            WHERE cols.TABLE_SCHEMA = 'schema1'  
            AND cols.TABLE_NAME = LOWER('TAB_TABLE1') 
            ORDER BY cols.ORDINAL_POSITION asc 

I need to do all this thru Java program.

0

There are 0 best solutions below