System Tables

Transact-SQL Reference

Introduction to System Tables

System tables in SQL Server store the metadata for the database. This metadata describes the database itself, including its objects (tables, views, stored procedures, etc.), users, permissions, and other configuration settings. These tables are essential for the functioning of SQL Server and should be managed with care.

System tables are typically prefixed with sys.. They are located in the master database for server-level information and in each user database for database-specific information.

Categories of System Tables

System tables can be broadly categorized based on the information they contain:

Commonly Used System Tables

Here are some of the most frequently referenced system tables:

Table Name Description
sys.tables Contains a row for each user-defined table and system table in the database.
sys.columns Contains a row for each column in a database.
sys.objects Contains a row for each user-defined schema-scoped object in the database.
sys.schemas Contains a row for each schema in the database.
sys.indexes Contains a row for each index and heap in the database.
sys.database_principals Contains a row for each security principal (user, role, etc.) in the database.
sys.sql_modules Contains one row per Transact-SQL module (stored procedure, function, trigger, view).
sys.dm_exec_sessions Returns information about all active user sessions.

Example: Querying Table Information

To retrieve the names and object IDs of all user tables in the current database, you can use the following T-SQL query:


SELECT 
    name AS TableName,
    object_id AS TableObjectID
FROM 
    sys.tables
WHERE 
    type = 'U'; -- 'U' denotes a user table
            

Important Considerations