Finding columns that are NOT NULL in PostgreSQL Finding columns that are NOT NULL in PostgreSQL sql sql

Finding columns that are NOT NULL in PostgreSQL


No.

This query

SELECT DISTINCT column_name, table_nameFROM INFORMATION_SCHEMA.COLUMNSWHERE column_name IS NOT NULL

will return all the rows that have a value in the column "column_name".

All rows in that table will always have a value in the column "column_name".

Do you just need to know how many columns are nullable and how many are non-nullable?

SELECT is_nullable, COUNT(*)FROM INFORMATION_SCHEMA.COLUMNSGROUP BY is_nullable;

Count by table name? I think you can use this.

SELECT table_name, is_nullable, count(*)FROM INFORMATION_SCHEMA.COLUMNSGROUP BY table_name, is_nullableORDER BY table_name, is_nullable;


To get a count of all NOT NULL columns in any table, use:

SELECT count(*)  FROM information_schema.columns WHERE table_schema = 'table_schema_here'   AND table_name   = 'table_name_here'   AND is_nullable = 'YES';

I hope this will help someone.


To get a count of all NOT NULL columns:

SELECT count(*)  FROM information_schema.columns  WHERE  table_name  = 'table_name'     AND is_nullable = 'NO';