436

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?

2
  • 2
    For Mysql you can do simple. SHOW TABLES; Commented Nov 8, 2016 at 0:37
  • This question is similar to: How do I get list of all tables in a database using TSQL?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Jan 10 at 19:40

21 Answers 21

672

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I prefer this solution when I have different table schemata within one database (SQL Server): SELECT CONCAT(TABLE_SCHEMA, '.', TABLE_NAME) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_CATALOG='MyDB'
One must be using that particular DB to get the information. USE dbName GO for SQL-Server. If you're not using the DB, result will not be displayed, even if there are tables in that DB.
For Mysql you can do simple. SHOW TABLES;
The SQL Server entry works fine for me without the USE dbName GO prefix on Server 2014.
1) You have suggested the same thing for an SQL Server and MySQL 2) INFORMATION_SCHEMA.TABLES exists only in MySQL 3) I really wonder how comes you got so many upvotes for this ...
|
108

Stolen from here:

USE YOURDBNAME
GO 
SELECT *
FROM sys.Tables
GO

4 Comments

Most probably SELECT Name FROM sys.Tables where is_ms_shipped = 0
this is database provider specific, and not ANSI SQL compatible.
Alternatively, SELECT * FROM yourdbname.sys.Tables; if you prefer to be more concise. Works in SQL Server, at least.
Careful with using sys.Tables, you can end up with multiple names because you are not taking into account the schema. Two tables exactly the same name with different schemas will show twice
42

The following query will select all of the Tables in the database named DBName:

USE DBName
GO 
SELECT *
FROM sys.Tables
GO

2 Comments

NO it only Part seperator. you can change to other.Its not an T-SQL syntax.
USE DatabaseSample SELECT * FROM sys.Tables
21

Just put the DATABASE NAME in front of INFORMATION_SCHEMA.TABLES:

select table_name from YOUR_DATABASE.INFORMATION_SCHEMA.TABLES where TABLE_TYPE = 'BASE TABLE'

Comments

20
USE DBName;
SELECT * FROM sys.Tables;

We can deal without GO in-place of you can use semicolon ;.

1 Comment

For SQL Server on Azure, this worked for me, but the accepted answer didn't. Thanks.
15

In mysql, use:

SHOW TABLES;

After selecting the DB with:

USE db_name

4 Comments

Osm,,, swt n short
This is MySQL, question is tagged wit MSSQL server
@Melle Look at the description of the question
This is not the best answer, see my comment under the accepted (and correct ) answer.
12

In order if someone would like to list all tables within specific database without using the "use" keyword:

SELECT TABLE_NAME FROM databasename.INFORMATION_SCHEMA.TABLES

1 Comment

You will get all the table by using above query, If you want to just get table which is in public > table . Then add SELECT TABLE_NAME FROM postgres.INFORMATION_SCHEMA.tables where table_schema = public Remember using postgres
12
SELECT * FROM information_schema.tables;

Comments

9
SELECT name FROM databaseName.sys.Tables;

Comments

7

To select the database query below :

use DatabaseName

Now

SELECT * FROM INFORMATION_SCHEMA.TABLES

Now you can see the created tables below in console .

PFA.

Query

Comments

4

For MySQL, you can do this:

SHOW TABLES;

Comments

2
select * from sys.tables
order by schema_id      --comments: order by 'schema_id' to get the 'tables' in 'object explorer order'
go

1 Comment

Any chance you could elaborate a bit on what you mean by this? Maybe explain it?
2

In our Oracle DB (PL/SQL) below code working to get the list of all exists tables in our DB.

select * from tab;

and

select table_name from tabs;

both are working. let's try and find yours.

Comments

2
SELECT TABLE_NAME
FROM your_database_name.INFORMATION_SCHEMA.TABLES
ORDER BY TABLE_NAME;

Comments

1
Exec sp_MSforeachtable 'Select ''?'''

2 Comments

Hi Amirreza, I believe you are talking about sp_MSforeachtable ?
@bummi: yes great,Thank you for attention and sorry for typing mistake. Edited
1
USE dbName;

SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE (TABLE_SCHEMA = 'dbName' OR TABLE_SCHEMA = 'schemaName')
ORDER BY TABLE_NAME

If you are working with multiple schemata on an MS SQL server, then SELECT-ing TABLE_NAME without also simultaneously selecting TABLE_SCHEMA might be of limited benefit, so I have assumed we are interested in the tables belonging to a known schema when using MS SQL Server.

I have tested the query above with SQL Server Management Studio using an SQL Server database of mine and with MySQL Workbench using a MySQL database, and in both cases it gives the table names.

The query bodges Michael Baylon's two different queries into one that can then run on either database type. The first part of the WHERE clause works on MySQL databases and the second part (after the OR) works on MS SQL Server databases. It is ugly and logically a little incorrect as it supposes that there is no undesired schema with the same name as the database. This might help someone who is looking for one single query that can run on either database server.

Comments

1

UPDATE FOR THE LATEST VERSION OF MSSQL SERVER (17.7)

SELECT name FROM sys.Tables WHERE type_desc = 'USER_TABLE'

Or SELECT * for get all columns.

1 Comment

I just realized that this query give wrong result for non-english characters in table name while Michael Baylon's gives correct results. (character was in Turkish language lowercase "ı" )
1
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
ORDER BY TABLE_NAME

1 Comment

While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.
0

Yes oracle is :

select * from user_tables

That is if you only want objects owned by the logged in user/schema otherwise you can use all_tables or dba_tables which includes system tables.

2 Comments

they're looking for cross provider queries not provider specific.
There is no consistent way to do this from sql as all db engines implement the data dictionary differently. It might be possible to abstract this using jdbc/odbc and a 3GL language such as C/Java/C# if this is a programmatic requirement, definitely possible if you have a different implementation for each database. The op should clarify their requirements...
0

Building from Michael Baylon's answer, I needed a list which also included schema information and this is how I modified his query.

SELECT TABLE_SCHEMA + '.' + TABLE_NAME as 'Schema.Table'
  FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_CATALOG = 'dbName'
  ORDER BY TABLE_SCHEMA, TABLE_NAME

Comments

0

Simply get all improtanat information with this below SQL in Mysql

    SELECT t.TABLE_NAME , t.ENGINE , t.TABLE_ROWS ,t.AVG_ROW_LENGTH, 
t.INDEX_LENGTH FROM 
INFORMATION_SCHEMA.TABLES as t where t.TABLE_SCHEMA = 'YOURTABLENAMEHERE' 
order by t.TABLE_NAME ASC limit 10000;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.