TSQL
Database Management
SQL Queries
Programming
Database Tables

How do I get list of all tables in a database using TSQL?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Transact-SQL (T-SQL) is the primary means through which both developers and database administrators interact with SQL Server databases. When managing a SQL Server database, a common task is to obtain a list of all tables within the database. This is useful for a variety of purposes including database auditing, migrations, or maintenance tasks. Below, we delve into how to achieve this using T-SQL with practical examples and deeper insights into SQL Server system views.

Understanding SQL Server Catalog Views

SQL Server provides several system catalog views that store information about all database objects, including tables. These catalog views are essential tools for querying meta data about the database structure. Two of the primary views used to retrieve table information are:

  1. sys.tables: Provides a row for each table in the database. This view includes information about the table itself but not its columns or rows.
  2. INFORMATION_SCHEMA.TABLES: This is a part of the ANSI standard and offers a cross-RDBMS way of querying meta-data. It provides information about all tables in the database.

Using sys.tables

The sys.tables view provides more detailed information specific to SQL Server. This is often the preferred method when working within a purely SQL Server environment. Here's how to use it:

sql
-- List all the tables in the current database
SELECT name, schema_id, create_date FROM sys.tables;

In this query:

  • name returns the table's name.
  • schema_id can be joined to sys.schemas to find the schema name.
  • create_date shows when the table was created.

You might often need the schema name along with the table's name to fully qualify the table, which is very important in databases having multiple schemas:

sql
1-- List all tables including schema names
2SELECT s.name AS schema_name, t.name AS table_name
3FROM sys.tables t
4INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
5ORDER BY schema_name, table_name;

Using INFORMATION_SCHEMA.TABLES

For those working in environments that might include multiple types of database systems or for developers looking for ANSI standard methods, INFORMATION_SCHEMA.TABLES is ideal.

sql
1-- List all tables and views in the current database
2SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE
3FROM INFORMATION_SCHEMA.TABLES
4WHERE TABLE_TYPE = 'BASE TABLE';

This query filters out views and returns only the tables:

  • TABLE_SCHEMA - the schema of the table
  • TABLE_NAME - the name of the table
  • TABLE_TYPE - type of the object, which here is filtered to show only 'BASE TABLE'.

Practical Considerations

When you're tasked with managing or auditing large databases with many objects, it is practical to also consider:

  • Filtering and sorting results: As shown, you may want to order the results or filter them, for instance by schema.
  • Considering permissions: Be aware that viewing metadata might be subject to permissions. Ensure the executing account has appropriate rights.

Here is a brief table that summarizes the key SQL views and commands discussed:

SQL View / CommandUse CaseRemarks
sys.tablesSQL Server-specific view of tablesProvides detailed info about tables
INFORMATION_SCHEMA.TABLESANSI standard view of table metadataCompatible across multiple RDBMS
TABLE_SCHEMASchema name in INFORMATION_SCHEMAUseful for fully qualifying table names
TABLE_NAMETable name in both viewsEssential for identifying the table
TABLE_TYPEType of the table in INFORMATION_SCHEMAUseful to distinguish between table types

Conclusion

By using the sys.tables and INFORMATION_SCHEMA.TABLES views, developers and DBAs can effectively manage and interact with the tables in their SQL Server databases. Each method has its own advantages depending on the specific requirements and environment. The examples provided here are fundamental and can be expanded with more complex SQL clauses as needed, based on the scope of the database management tasks at hand.


Course illustration
Course illustration

All Rights Reserved.