sql如何查询每个表的记录数
- 编程技术
- 2025-01-30 09:25:53
- 1
在SQL中,要查询每个表的记录数,可以使用系统表或者信息模式(information schema)来获取这些数据。以下是在不同数据库管理系统(如MySQL、Postg...
在SQL中,要查询每个表的记录数,可以使用系统表或者信息模式(information schema)来获取这些数据。以下是在不同数据库管理系统(如MySQL、PostgreSQL、SQL Server等)中查询每个表记录数的方法。
MySQL
在MySQL中,你可以使用`information_schema.tables`来获取每个表的记录数。
```sql
SELECT table_schema, table_name, table_rows
FROM information_schema.tables
WHERE table_type = 'BASE TABLE';
```
PostgreSQL
在PostgreSQL中,你可以使用`pg_catalog.pg_tables`和`pg_catalog.pg_stats`来获取每个表的记录数。
```sql
SELECT
t.tablename,
s.row_count
FROM
pg_catalog.pg_tables t,
pg_catalog.pg_stats s
WHERE
t.schemaname = 'public' AND
t.tablename = s.tablename AND
s.tablename = t.tablename;
```
SQL Server
在SQL Server中,你可以使用`sys.tables`和`sys.indexes`来获取每个表的记录数。
```sql
SELECT
t.name AS TableName,
SUM(p.rows) AS RowCounts
FROM
sys.tables t
INNER JOIN
sys.indexes i ON t.object_id = i.object_id
INNER JOIN
sys.partitions p ON i.object_id = p.object_id AND i.index_id = p.index_id
WHERE
t.type = 'U'
GROUP BY
t.name
ORDER BY
SUM(p.rows) DESC;
```
Oracle
在Oracle中,你可以使用`user_tables`和`user_tab_columns`来获取每个表的记录数。
```sql
SELECT
table_name,
num_rows
FROM
user_tables;
```
请注意,对于一些数据库系统,你可能需要根据你的数据库权限调整查询,以确保你有权限访问`information_schema`或者相应的系统视图。
本文链接:http://xinin56.com/bian/395838.html
上一篇:专业烟花怎么燃放