数据库常用sql总结

版权声明:未经博主同意,谢绝转载!(请尊重原创,感谢。) https://blog.csdn.net/topdeveloperr/article/details/89761971

本篇博客是对一些比较常见的数据库知识的汇总,并会持续更新。

Postgres

如何获取postgres所有的表里的记录条数?

SELECT 
  nspname AS schemaname,relname,reltuples
FROM pg_class C
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
WHERE 
  nspname NOT IN ('pg_catalog', 'information_schema') AND
  relkind='r' 
ORDER BY reltuples DESC;

如何获取postgres里某个表的主键?

    public List<String> getPrimaryKeysByTable(String tableName, JdbcTemplate jdbcTemplate) {
        String retrievePrimaryKeySql =
                "select kc.column_name from information_schema.table_constraints tc join information_schema.key_column_usage kc on kc.table_name = \'"
                        + tableName
                        + "\' and kc.table_schema = \'public\' and kc.constraint_name = tc.constraint_name where tc.constraint_type = \'PRIMARY KEY\'  and kc.ordinal_position is not null order by column_name";
        return jdbcTemplate.queryForList(retrievePrimaryKeySql, String.class);
    }

猜你喜欢

转载自blog.csdn.net/topdeveloperr/article/details/89761971