What is the percentage of optional columns (that permit NULLs) in case of each base table? It is better to prohibit the use of NULLs in as many columns as possible. Otherwise the results of queries may be misleading.
Notes
The query does not take into account system (hidden) columns like xmin. If a table has now columns, then the main query does not return any row about the table and thus there will not be division by zero.
Type
Sofware measure (Numeric values (software measures) about the database)
SELECT C.table_schema, C.table_name,
Round((SELECT Count(*)
FROM information_schema.Columns AS C1
WHERE C1.is_nullable='YES' AND C1.table_schema=C.table_schema AND C1.table_name=C.table_name)::decimal*100/Count(C.column_name)::decimal,2) AS percentage_of_optional
FROM information_schema.columns C INNER JOIN information_schema.tables T USING (table_schema, table_name)
INNER JOIN information_schema.schemata S ON T.table_schema = S.schema_name
WHERE T.table_type = 'BASE TABLE'
AND (T.table_schema = 'public' OR S.schema_owner<>'postgres')
GROUP BY C.table_schema, C.table_name
ORDER BY percentage_of_optional DESC, C.table_schema, C.table_name;
Collections
This query belongs to the following collections:
Name
Description
Find problems by overview
Queries that results point to different aspects of database that might have problems. A human reviewer has to decide based on the results as to whether there are problems or not .
Find quick numeric overview of the database
Queries that return numeric values showing mostly the number of different types of database objects in the database
Categories
This query is classified under the following categories:
Name
Description
Missing data
Queries of this category provide information about missing data (NULLs) in a database.