查询标识中的唯一列而不是空列

时间:2018-05-01 03:58:37

标签: postgresql postgresql-9.3

我们是否能够获取用于标识架构中的唯一列而非空列的查询。

请在oracle中参考以下查询。

SELECT Table_name, index_name, num_rows, distinct_keys FROM dba_indexes WHERE table_owner = 'ownername' and uniqueness = 'NONUNIQUE' AND num_rows > 0 AND 100* ( num_rows - ( num_rows - distinct_keys ) ) / num_rows > 99 ;

SELECT t.table_name, c.column_name, t.num_rows, t.null_values FROM dba_tab_columns c, tab_tables t WHERE t.owner = 'ownername'  and t.table_name=c.table_name and t.owner = c.owner and c.nullable='Y' and c.num_nulls=0;

我们可以在postgres中获得同样的查询吗?

谢谢

2 个答案:

答案 0 :(得分:1)

朋友我以前从未需要你需要的东西,但我找到了这个,我希望你可以使用参考

Equivalent of "describe table" in PgAdmin3

 psql -d "$db_name" -c '
    SELECT 
    ordinal_position , table_name , column_name , data_type , is_nullable
    FROM information_schema.columns
    WHERE 1=1
    AND table_name = '\''my_table'\''
    ;'

# or just the col names
psql -d "$my_db" -t -c \
"SELECT column_name FROM information_schema.columns 
WHERE 1=1 AND table_name='my_table'"

PostgreSQL "DESCRIBE TABLE"

https://www.postgresql.org/docs/9.3/static/information-schema.html

请原谅我没有进行查询

答案 1 :(得分:0)

Below the queries will give result:

1) finding columns which is having unique data but no unique key index on those columns.

select distinct a.schemaname,a.tablename,attname ,indexdef
from pg_stats a,pg_indexes b
WHERE a.tablename = b.tablename
AND  a.schemaname ilike 'pegadata'
and n_distinct = -1
AND indexdef NOT ILIKE '%UNIQUE%' ;

2)Finding columns which is having not null but no constraint.


 select distinct  table_schema,table_name,column_name
 from information_schema.columns a , pg_stats b
 where a.table_name = b.tablename
 AND a.TABLE_SCHEMA = 'pegadata'
 and a.IS_NULLABLE = 'YES'
  AND b.null_frac = 0;