Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all table names of a particular database by SQL query?

Tags:

sql

sql-server

I am working on application which can deal with multiple database servers like "MySQL" and "MS SQL Server".

I want to get tables' names of a particular database using a general query which should suitable for all database types. I have tried following:

SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' 

But it is giving table names of all databases of a particular server but I want to get tables names of selected database only. How can I restrict this query to get tables of a particular database?

like image 816
Awan Avatar asked Oct 12 '10 10:10

Awan


People also ask

How extract all table names in SQL?

In MySQL, there are two ways to find the names of all tables, either by using the "show" keyword or by query INFORMATION_SCHEMA. In the case of SQL Server or MSSQL, You can either use sys. tables or INFORMATION_SCHEMA to get all table names for a database.


2 Answers

Probably due to the way different sql dbms deal with schemas.

Try the following

For SQL Server:

SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_CATALOG='dbName' 

For MySQL:

SELECT TABLE_NAME  FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA='dbName'  

For Oracle I think the equivalent would be to use DBA_TABLES.

like image 177
Michael Baylon Avatar answered Sep 28 '22 13:09

Michael Baylon


Stolen from here:

USE YOURDBNAME GO  SELECT * FROM sys.Tables GO 
like image 24
bla Avatar answered Sep 28 '22 12:09

bla