我为每个表分配了一个可计算可空列的赋值。易:
SELECT table_name, count(*) FROM INFORMATION_SCHEMA.COLUMNS
WHERE is_nullable='NO'
GROUP BY table_name;
现在我必须修改它来计算“具有属性”NOT NULL“”的列。以下代码是否会执行此操作,还是只检查天气列名称是否为空?
CREATE TEMP TABLE A AS
SELECT DISTINCT column_name, table_name AS name FROM INFORMATION_SCHEMA.COLUMNS
WHERE column_name IS NOT NULL
GROUP BY table_name, column_name;
SELECT name, count(*) FROM A
GROUP BY name;
如果没有......有任何建议吗?
答案 0 :(得分:39)
没有
此查询
SELECT DISTINCT column_name, table_name
FROM INFORMATION_SCHEMA.COLUMNS
WHERE column_name IS NOT NULL
将返回“column_name”列中具有值的所有行。
该表中的所有行将始终在“column_name”列中具有值。
您只需要知道有多少列可以为空并且有多少列是不可空的?
SELECT is_nullable, COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
GROUP BY is_nullable;
按表名计算?我想你可以用它。
SELECT table_name, is_nullable, count(*)
FROM INFORMATION_SCHEMA.COLUMNS
GROUP BY table_name, is_nullable
ORDER BY table_name, is_nullable;
答案 1 :(得分:0)
要获取任何表中所有NOT NULL列的计数,请使用:
SELECT count(*)
FROM information_schema.columns
WHERE table_schema = 'table_schema_here'
AND table_name = 'table_name_here'
AND is_nullable = 'YES';
我希望这会对某人有所帮助。
答案 2 :(得分:0)
要获取所有NOT NULL列的计数:
SELECT count(*)
FROM information_schema.columns
WHERE table_name = 'table_name'
AND is_nullable = 'NO';