Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listing all tables in a database

Tags:

sql

database

Is there a SQL command that will list all the tables in a database and which is provider independent (works on MSSQLServer, Oracle, MySQL)?

like image 894
kjv Avatar asked May 10 '09 16:05

kjv


People also ask

How do you find the total tables in a database?

To check the count of tables. mysql> SELECT count(*) AS TOTALNUMBEROFTABLES -> FROM INFORMATION_SCHEMA. TABLES -> WHERE TABLE_SCHEMA = 'business'; The following output gives the count of all the tables.


2 Answers

The closest option is to query the INFORMATION_SCHEMA for tables.

SELECT *
FROM INFORMATION_SCHEMA.Tables
WHERE table_schema = 'mydatabase';

The INFORMATION_SCHEMA is part of standard SQL, but not all vendors support it. As far as I know, the only RDBMS vendors that support it are:

  • MySQL
  • PostgreSQL
  • Microsoft SQL Server 2000/2005/2008

Some brands of database, e.g. Oracle, IBM DB2, Firebird, Derby, etc. have similar "catalog" views that give you an interface where you can query metadata on the system. But the names of the views, the columns they contain, and their relationships don't match the ANSI SQL standard for INFORMATION_SCHEMA. In other words, similar information is available, but the query you would use to get that information is different.

(footnote: the catalog views in IBM DB2 UDB for System i are different from the catalog views in IBM DB2 UDB for Windows/*NIX -- so much for the Universal in UDB!)

Some other brands (e.g. SQLite) don't offer any queriable interface for metadata at all.

like image 71
Bill Karwin Avatar answered Oct 05 '22 02:10

Bill Karwin


No. They all love doing it their own little way.

like image 29
Ignacio Vazquez-Abrams Avatar answered Oct 05 '22 02:10

Ignacio Vazquez-Abrams