Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count the Number of Tables in a SQL Server Database [duplicate]

I have a SQL Server 2012 database called MyDatabase. How can I find how many tables are in the database?

I'm assuming the format of the query would be something like the following, but I don't know what to replace database_tables with:

USE MyDatabase SELECT COUNT(*) FROM [database_tables] 
like image 547
Tot Zam Avatar asked Aug 02 '17 15:08

Tot Zam


People also ask

How do I count the number of tables in a SQL database?

You can use INFORMATION_SCHEMA. TABLES to retrieve information about your database tables. As mentioned in the Microsoft Tables Documentation: INFORMATION_SCHEMA.

How many tables are there in SQL Server database?

The number of tables in a database is limited only by the number of objects allowed in a database (2,147,483,647). A standard user-defined table can have up to 1,024 columns.


2 Answers

You can use INFORMATION_SCHEMA.TABLES to retrieve information about your database tables.

As mentioned in the Microsoft Tables Documentation:

INFORMATION_SCHEMA.TABLES returns one row for each table in the current database for which the current user has permissions.

The following query, therefore, will return the number of tables in the specified database:

USE MyDatabase SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' 

As of SQL Server 2008, you can also use sys.tables to count the the number of tables.

From the Microsoft sys.tables Documentation:

sys.tables returns a row for each user table in SQL Server.

The following query will also return the number of table in your database:

SELECT COUNT(*) FROM sys.tables 
like image 60
Tot Zam Avatar answered Nov 03 '22 17:11

Tot Zam


USE MyDatabase SELECT Count(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'; 

to get table counts

SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'dbName'; 

this also works

USE databasename; SHOW TABLES; SELECT FOUND_ROWS(); 
like image 29
Shafeer khan Avatar answered Nov 03 '22 16:11

Shafeer khan