Click here to monitor SSC
  • Av rating:
  • Total votes: 48
  • Total comments: 5
Robert Sheldon

Using Information Schema Views

01 October 2009

Many seasoned database developers tuck away all the commonly-used INFORMATION_SCHEMA queries as templates. They're an indispensable supplement to sp_help and sp_helpText  to get handy information about your database objects, and, even if you use SQL Prompt, they're  usually the best standard way to access such information programmatically within a routine. They are ISO standard SQL and are here to stay.  Rob Sheldon goes through the basics in a timely refresher course.

Information schema views return information about the metadata in a SQL Server database. You can write code that uses these views to retrieve metadata, without worrying about changes to the system tables. For example, you can retrieve such metadata as the tables created in a database, the privileges granted on those tables, or the constraints defined on the tables.

SQL Server 2005 and 2008 automatically create 20 information schema views in every database. The views comply with the SQL-92 standard and are created in the schema INFORMATION_SCHEMA. Note, however, that in SQL Server an information schema view returns information only about those objects that the current user has permission to access. For example, if a user does not have the privileges necessary to access a particular table in a database, that user will not be able to view the columns from that table in the COLUMNS view.

When you call an information schema view, you must qualify the view name with the schema INFORMATION_SCHEMA. In addition, if you’re using the views to retrieve data from a database other than the current database, you must also qualify the name by including the database name as well as the schema name.

SQL Server uses SQL-92 metadata names for the information schema views and their columns. That means a database is referred to as a catalog, and a user-defined data type as a domain. However, most other metadata names are consistent between SQL Server and SQL-92.

The rest of the article describes each information schema view available in SQL Server 2005 and 2008 and provides examples that demonstrate how to use them. Note that these examples are based on the AdventureWorks2008 sample database in SQL Server 2008, although in many cases the statements are not specific to any one database.

CHECK_CONSTRAINTS

The CHECK_CONSTRAINTS information schema view displays the check constraints that exist in the current or specified database. The data includes the check expression that is part of the Transact-SQL constraint definition. In the following SELECT statement, I retrieve the schema, constraint, and constraint expression for each check constraint in the AdventureWorks2008 database:

SELECT CONSTRAINT_SCHEMA,

   CONSTRAINT_NAME,

   CHECK_CLAUSE

FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS

ORDER BY CONSTRAINT_SCHEMA,

   CONSTRAINT_NAME

The CHECK_CONSTRAINTS view does not include the table name. However, if the table name is part of the constraint name, as is the case in the AdventureWorks2008 database, you can order the query according to constraint name. In the example above, I order the results first by schema name and then by constraint name so that tables are grouped together. (Ed: if you use the CONSTRAINT_SCHEMA, BOL  for the 2008 version states 'The only reliable way to find the schema of a object is to query the sys.objects catalog view'. A bug?)

COLUMN_DOMAIN_USAGE

The COLUMN_DOMAIN_USAGE information schema view displays the columns that are configured with user-defined data types. For example, if your database contains a user-defined data type called IndName, the view will return a row for each column in a table or view defined with the IndName data type.

In the following SELECT statement, I retrieve the schema, table, column, and data type for each column configured with a user-defined data type in the AdventureWorks2008 database:

SELECT TABLE_SCHEMA,

   TABLE_NAME,

   COLUMN_NAME,

   DOMAIN_NAME

FROM INFORMATION_SCHEMA.COLUMN_DOMAIN_USAGE

ORDER BY TABLE_SCHEMA,

   TABLE_NAME,

   COLUMN_NAME,

   DOMAIN_NAME

COLUMN_PRIVILEGES

The COLUMN_PRIVILEGES information schema view displays information about each column-level privilege that has been granted to or granted by the current user. The view returns such details as who granted the privilege, who has been granted the privilege, the column on which the privilege is granted, and the type of privilege.

The following example retrieves the column-level privileges granted in the HumanResources schema of the AdvenureWorks2008 database:

SELECT GRANTOR,

   GRANTEE,

   TABLE_NAME,

   COLUMN_NAME,

   PRIVILEGE_TYPE

FROM INFORMATION_SCHEMA.COLUMN_PRIVILEGES

WHERE TABLE_SCHEMA = 'HumanResources'

As you can see, the statement will return the grantor, grantee, table name, column names, and privilege type for each column-level privilege.

COLUMNS

The COLUMNS information schema view displays a list of columns in the specified database. As I indicated above, this data includes only columns that can be accessed by the current user (which is true for all information schema views). The COLUMNS view returns not only the object names that qualify the column (database, schema, table, and column names), but also information such as the ordinal position, default values, nullability, and data type.

In the following example, I retrieve the columns, their data types, nullability, and default values (if any) for the columns in the vEmployee view in the AdventureWorks2008 database:

SELECT COLUMN_NAME,

   DATA_TYPE,

   IS_NULLABLE,

   COLUMN_DEFAULT

FROM INFORMATION_SCHEMA.COLUMNS

WHERE TABLE_NAME = 'vEmployee'

CONSTRAINT_COLUMN_USAGE

The CONSTRAINT_COLUMN_USAGE information schema view displays a list of columns in the current or specified database on which constraints are defined. The view returns the object names that qualify the columns (database, schema, table, and column names) as well as the object names that qualify the constraints (database, schema, and constraint names).

The following example uses the CONSTRAINT_COLUMN_USAGE view to retrieve the constraints defined on columns in the HumanResources schema of the AdventureWorks2008 database:

SELECT TABLE_NAME,

   COLUMN_NAME,

   CONSTRAINT_NAME

FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE

WHERE TABLE_SCHEMA = 'HumanResources'

ORDER BY TABLE_NAME,

   COLUMN_NAME,

   CONSTRAINT_NAME

CONSTRAINT_TABLE_USAGE

The CONSTRAINT_TABLE_USAGE information schema view displays a list of tables in the current or specified database on which constraints are defined. The view returns the object names that qualify the tables (database, schema, and table names) as well as the object names that qualify the constraints (database, schema, and constraint names).

The following example uses the CONSTRAINT_TABLE_USAGE view to retrieve the constraints defined on tables in the HumanResources schema of the AdventureWorks2008 database:

SELECT TABLE_NAME,

   CONSTRAINT_NAME

FROM INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE

WHERE TABLE_SCHEMA = 'HumanResources'

ORDER BY TABLE_NAME,

   CONSTRAINT_NAME

DOMAIN_CONSTRAINTS

The DOMAIN_CONSTRAINTS information schema view displays a row for each user-defined data type in the current or specified database that has a constraint bound to it. The information includes the object names that qualify the constraints (database, schema, and constraint names) as well as the object names that qualify the user-defined data types (database, schema, and type names). The view also returns information about constraint deferability.

In the following example, I retrieve the constraint names and user-defined data type names for each data type in the HumanResources schema that has a constraint bound to it:

SELECT CONSTRAINT_NAME,

   DOMAIN_NAME

FROM INFORMATION_SCHEMA.DOMAIN_CONSTRAINTS

WHERE CONSTRAINT_SCHEMA = 'HumanResources'

ORDER BY CONSTRAINT_NAME

DOMAINS

The DOMAINS information schema view displays a list of user-defined data types in the current or specified database. In the following example, I retrieve the name of the user-defined data types, the built-in data types on which they’re based, and the character length of the data types:

SELECT DOMAIN_NAME,

   DATA_TYPE,

   CHARACTER_MAXIMUM_LENGTH

FROM INFORMATION_SCHEMA.DOMAINS

KEY_COLUMN_USAGE

The KEY_COLUMN_USAGE information schema view displays each column that is configured as a key constraint. The information includes the object names that qualify the constraints (database, schema, and constraint names) as well as the object names that qualify the columns (database, schema, table, and column names). The view also returns the columns’ ordinal positions.

The following SELECT statement uses the KEY_COLUMN_USAGE view to retrieve the columns in the Employee table that are constrained by a key:

SELECT COLUMN_NAME,

   CONSTRAINT_NAME

FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE

WHERE TABLE_NAME = 'Employee'

PARAMETERS

The PARAMETERS information schema view displays a list of parameters for user-defined functions and stored procedures in the current or specified database. For functions, the view displays additional rows for the return values. The PARAMETERS view is one of the meatier information schema views in terms of the amount of information it returns. There are of course the details you would expect—the name of the parameter, associated routine, schema, and database. But the view also returns such details as the parameter data type, its ordinal position, and collation and character information.

In the following SELECT statement, I retrieve the routine name (stored procedure or function), the parameter name, the data type, and the mode (IN or OUT) for all routine parameters in the HumanResources schema of the AdventureWorks2008 database:

SELECT SPECIFIC_NAME,

   PARAMETER_NAME,

   DATA_TYPE,

   PARAMETER_MODE

FROM INFORMATION_SCHEMA.PARAMETERS

WHERE SPECIFIC_SCHEMA = 'HumanResources'

ORDER BY SPECIFIC_NAME,

   PARAMETER_NAME

REFERENTIAL_CONSTRAINTS

The REFERENTIAL_CONSTRAINTS information schema view displays a row for each FOREIGN KEY constraint in the current or specified database. The information includes the object names that qualify the constraints (database, schema, and constraint names) as well as details about matching conditions, update rules, and delete rules. The view also returns details specific to UNIQUE constraints.

The following example uses the REFERENTIAL_CONSTRAINTS view to retrieve the FOREIGN KEY constraints in the Production schema of the AdventureWorks2008 database:

SELECT CONSTRAINT_NAME,

   MATCH_OPTION,

   UPDATE_RULE,

   DELETE_RULE

FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS

WHERE CONSTRAINT_SCHEMA = 'Production'

ORDER BY CONSTRAINT_NAME

As you can see, I retrieve the constraint name, match option, and details about update and delete rules.

ROUTINE_COLUMNS

The ROUTINE_COLUMNS information schema view displays details about each column returned by the table-valued functions in the current or specified database. The information includes the object names that qualify the columns (database, schema, function, and column names) as well as such details as ordinal position, column default, data type, and character and collation information.

In the following example, I retrieve the columns, their data types, and their nullability for the columns returned by the ufnGetContactInformation function in the AdventureWorks2008 database.

SELECT COLUMN_NAME,

  DATA_TYPE,

  IS_NULLABLE

FROM INFORMATION_SCHEMA..ROUTINE_COLUMNS

WHERE TABLE_NAME = 'ufnGetContactInformation'

ROUTINES

The ROUTINES information schema view displays information about the stored procedures and functions in the current or specified database. The information includes the object names that qualify the routines (database, schema, and routine names), the routine definition (Transact-SQL), and character and collation information. The view also includes several columns that return only null values. These columns are reserved for future use.

The following SELECT statement uses the ROUTINES view to return the routine names and their schemas, as well as the routine definitions:

SELECT ROUTINE_SCHEMA,

   ROUTINE_NAME,

   ROUTINE_DEFINITION

FROM INFORMATION_SCHEMA.ROUTINES

SCHEMATA

The SCHEMATA information schema view displays each schema in the current or specified database. The information includes the database and schema names, the schema owner, and details about the character set. The following example retrieves the name and owner of each schema in the AdventureWorks2008 database:

SELECT SCHEMA_NAME,

   SCHEMA_OWNER

FROM AdventureWorks2008.INFORMATION_SCHEMA.SCHEMATA

TABLE_CONSTRAINTS

The TABLE_CONSTRAINTS information schema view displays a list of table constraints in the current or specified database. The view returns the object names that qualify the table (database, schema, and table names) as well as the object names that qualify the constraints (database, schema, and constraint names). The view also returns the constraint type (CHECK, UNIQUE, PRIMARY KEY, or FOREIGN KEY) and provides information about whether constraint checking is deferrable and whether it is at first deferred.

The following example uses the TABLE_CONSTRAINTS view to retrieve the constraints defined the Production schema of the AdventureWorks2008 database:

SELECT TABLE_NAME,

   CONSTRAINT_NAME,

   CONSTRAINT_TYPE

FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS

WHERE TABLE_SCHEMA = 'Production'

Notice that the SELECT statement retrieves the table names, constraint names, and constraint type for each table constraint.

TABLE_PRIVILEGES

The TABLE_PRIVILEGES information schema view displays information about each table-level privilege that has been granted to or granted by the current user. The view returns such details as who granted the privilege, who has been granted the privilege, the table on which the privilege is granted, and the type of privilege.

The following example retrieves the table-level privileges granted in the Production schema of the AdvenureWorks2008 database:

SELECT GRANTOR,

   GRANTEE,

   TABLE_NAME,

   PRIVILEGE_TYPE

FROM INFORMATION_SCHEMA.TABLE_PRIVILEGES

WHERE TABLE_SCHEMA = 'Production'

As you can see, the SELECT statement returns the names of the grantors and grantees, as well as the table names and privilege types.

TABLES

The TABLES information schema view displays a list of tables in the current or specified database. The information includes the object names that qualify the table (database, schema, and table names) as well as the table type (BASE TABLE or VIEW). In the following SELECT statement, I retrieve the tables, their associated schemas, and the table type for the AdventureWorks2008 database:

SELECT TABLE_SCHEMA,

  TABLE_NAME,

  TABLE_TYPE

FROM INFORMATION_SCHEMA.TABLES

ORDER BY TABLE_TYPE,

   TABLE_SCHEMA,

   TABLE_NAME

VIEW_COLUMN_USAGE

The VIEW_COLUMN_USAGE information schema view displays the columns defined in the views in the current or specified database. The information includes the object names that qualify the views (database, schema, and view names) as well as the objects names that qualify the source columns (database, schema, table, and column names). The following example returns a list of views along with their base tables, the tables’ schemas, and the source columns:

SELECT VIEW_NAME,

  TABLE_SCHEMA,

  TABLE_NAME,

  COLUMN_NAME

FROM INFORMATION_SCHEMA.VIEW_COLUMN_USAGE

WHERE TABLE_SCHEMA = 'Person'

ORDER BY VIEW_NAME,

   TABLE_SCHEMA,

   TABLE_NAME,

   COLUMN_NAME

VIEW_TABLE_USAGE

The VIEW_TABLE_USAGE information schema view displays the tables that are used in the views in the current or specified database. The information includes the object names that qualify the views (database, schema, and view names) as well as the objects names that qualify the base tables (database, schema, and table names). The following example returns a list of views along with their base tables and the tables’ schemas:

SELECT VIEW_NAME,

  TABLE_SCHEMA,

  TABLE_NAME

FROM INFORMATION_SCHEMA.VIEW_TABLE_USAGE

WHERE TABLE_SCHEMA = 'Person'

ORDER BY VIEW_NAME,

   TABLE_SCHEMA,

   TABLE_NAME

VIEWS

The VIEWS information schema view displays a list of views in the current or specified database. The information includes the object names that qualify the views (database, schema, and view names) as well as the view definitions (Transact-SQL). The view also returns the WITH CHECK OPTION setting and specifies whether the view is updateable.

In the following SELECT statement, I retrieve the views, their associated schemas, and the view definitions for the AdventureWorks2008 database:

SELECT TABLE_SCHEMA,

   TABLE_NAME,

   VIEW_DEFINITION

FROM INFORMATION_SCHEMA.VIEWS

ORDER BY TABLE_SCHEMA,

   TABLE_NAME

Information Schema Views

This article should have given you a good overview of information schema views in SQL Server 2005 and 2008. For more information about each view, see the view’s topic in SQL Server Books Online. (e.g. CHECK_CONSTRAINTS). There you will find a description of all the columns returned by each view. You can also find additional information in the topic “Information Schema Views (Transact-SQL).”

Robert Sheldon

Author profile:

After being dropped 35 feet from a helicopter and spending the next year recovering, Robert Sheldon left the Colorado Rockies and emergency rescue work to pursue safer and less painful interests—thus his entry into the world of technology. He is now a technical consultant and the author of numerous books, articles, and training material related to Microsoft Windows, various relational database management systems, and business intelligence design and implementation. He has also written news stories, feature articles, restaurant reviews, legal summaries, and the novel 'Dancing the River Lightly'. You can find more information at http://www.rhsheldon.com.

Search for other articles by Robert Sheldon

Rate this article:   Avg rating: from a total of 48 votes.


Poor

OK

Good

Great

Must read
Have Your Say
Do you have an opinion on this article? Then add your comment below:
You must be logged in to post to this forum

Click here to log in.


Subject: using join
Posted by: okz (view profile)
Posted on: Saturday, October 03, 2009 at 12:26 AM
Message: The CHECK_CONSTRAINTS view does not include the table name but we can get it with a join.
SELECT CK.constraint_schema,
CTU.constraint_name,
table_name,
check_clause
FROM information_schema.check_constraints CK
INNER JOIN information_schema.constraint_table_usage CTU
ON CTU.constraint_name = CK.CONSTRAINT_NAME
ORDER BY CK.CONSTRAINT_SCHEMA,CK.CONSTRAINT_NAME, table_name

Subject: more readable format!
Posted by: okz (view profile)
Posted on: Saturday, October 03, 2009 at 12:36 AM
Message: SELECT ck.constraint_schema,
ctu.constraint_name,
table_name,
check_clause
FROM information_schema.check_constraints ck
INNER JOIN information_schema.constraint_table_usage ctu
ON ctu.constraint_name = ck.constraint_name
ORDER BY ck.constraint_schema,
ck.constraint_name,
table_name

Subject: Good one
Posted by: Anonymous (not signed in)
Posted on: Wednesday, October 07, 2009 at 9:44 PM
Message: This is very easy to understand.
I feel like reading SQLAuthority.com article.

Subject: Bug with INFORMATION_SCHEMA.ROUTINES
Posted by: Phil Factor (view profile)
Posted on: Thursday, October 08, 2009 at 4:00 AM
Message: INFORMATION_SCHEMA.ROUTINES only shows the first 4000 characters of the routine's definition. If you are just looking at one routine, you can use sp_Helptext instead. The system table [syscomments] was used in SQL Server 2000 and 7, but the sp code is split in blocks of 4000 characters in each row, and the sequence is in the column [colid]. This makes searching for words or phrases awkward as the split can occur in any part of a word. From SQL Server 2005 onwards sys.sql_modules catalog view solves the problem because the definition column is nvarchar(max).
This is a nuisance, as it would be better where possible to use the standards-compliant Information_Schemas instead.
e.g.
select object_name([object_ID]), [definition] from sys.sql_modules where [definition] like '%DROP%' --find out the routines where the word 'drop' occurs

Subject: VIEWS
Posted by: mjswart (view profile)
Posted on: Friday, October 30, 2009 at 8:04 AM
Message: I got hung up on this one for a few minutes this week.

INFORMATION_SCHEMA.VIEWS has columns TABLE_SCHEMA and TABLE_NAME. To refer to what would (seemingly) should be: VIEW_SCHEMA and VIEW_NAME.

 

Phil Factor
Searching for Strings in SQL Server Databases

Sometimes, you just want to do a search in a SQL Server database as if you were using a search engine like Google.... Read more...

 View the blog

Top Rated

Searching for Strings in SQL Server Databases
 Sometimes, you just want to do a search in a SQL Server database as if you were using a search engine... Read more...

Continuous Delivery and the Database
 Continuous Delivery is fairly generally understood to be an effective way of tackling the problems of... Read more...

The SQL Server Sqlio Utility
 If, before deployment, you need to push the limits of your disk subsystem in order to determine whether... Read more...

The PoSh DBA - Reading and Filtering Errors
 DBAs regularly need to keep an eye on the error logs of all their SQL Servers, and the event logs of... Read more...

MySQL Compare: The Manual That Time Forgot, Part 1
 Although SQL Compare, for SQL Server, is one of Red Gate's best-known products, there are also 'sister'... Read more...

Most Viewed

Beginning SQL Server 2005 Reporting Services Part 1
 Steve Joubert begins an in-depth tour of SQL Server 2005 Reporting Services with a step-by-step guide... Read more...

Ten Common Database Design Mistakes
 If database design is done right, then the development, deployment and subsequent performance in... Read more...

SQL Server Index Basics
 Given the fundamental importance of indexes in databases, it always comes as a surprise how often the... Read more...

Reading and Writing Files in SQL Server using T-SQL
 SQL Server provides several "standard" techniques by which to read and write to files but, just... Read more...

Concatenating Row Values in Transact-SQL
 It is an interesting problem in Transact SQL, for which there are a number of solutions and... Read more...

Why Join

Over 400,000 Microsoft professionals subscribe to the Simple-Talk technical journal. Join today, it's fast, simple, free and secure.