Вы находитесь на странице: 1из 16

SQL Server INFORMATION_SCHEMA views

(Introduction) Overview The INFORMATION_SCHEMA views allow you to retrieve metadata about the objects within a database. These views can be found in the master database under Views / System Views and be called from any database in your SQL Server instance. The reason these were developed was so that they are standard across all database platforms. In SQL 2005 and SQL 2008 these Information Schema views comply with the ISO standard. Following is a list of each of the views that exist. y y y y y y y y y y y y y y y y y y y y

INFORMATION_SCHEMA.CHECK_CONSTRAINTS INFORMATION_SCHEMA.COLUMN_DOMAIN_USAGE INFORMATION_SCHEMA.COLUMN_PRIVILEGES INFORMATION_SCHEMA.COLUMNS INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE INFORMATION_SCHEMA.DOMAIN_CONSTRAINTS INFORMATION_SCHEMA.DOMAINS INFORMATION_SCHEMA.KEY_COLUMN_USAGE INFORMATION_SCHEMA.PARAMETERS INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS INFORMATION_SCHEMA.ROUTINE_COLUMNS INFORMATION_SCHEMA.ROUTINES INFORMATION_SCHEMA.SCHEMATA INFORMATION_SCHEMA.TABLE_CONSTRAINTS INFORMATION_SCHEMA.TABLE_PRIVILEGES INFORMATION_SCHEMA.TABLES INFORMATION_SCHEMA.VIEW_COLUMN_USAGE INFORMATION_SCHEMA.VIEW_TABLE_USAGE INFORMATION_SCHEMA.VIEWS

These views can be used from any of your databases. So if you want to gather data about Tables from the AdventureWorks database you would issue the following in that database. USE AdventureWorks GO SELECT * FROM INFORMATION_SCHEMA.TABLES

INFORMATION_SCHEMA.CHECK_CONSTRAINTS (CHECK_CONSTRAINTS)
Overview The INFORMATION_SCHEMA.CHECK_CONSTRAINTS view allows you to get information about the check constraints that are setup in your database. A check constraint is a

constraint put on a particular column in a table to ensure that specific data rules are followed for a column. Explanation This view can be called from any of the databases in an instance of SQL Server and will return the results for the data within that particular database. The columns that this view returns are as follows: Column name Data type Description Constraint qualifier. Name of the schema to which the constraint belongs. Constraint name. Actual text of the Transact-SQL definition statement.

CONSTRAINT_CATALOG nvarchar(128) CONSTRAINT_SCHEMA CONSTRAINT_NAME CHECK_CLAUSE nvarchar(128) sysname nvarchar(4000)

(Source: SQL Server 2005 Books Online)

Here is an example of data that was pulled from the AdventureWorks database. This data was pulled using this query: SELECT * FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS

To take a look at this a little further here is a closer look at the first two check constraints. These are usually setup when a table is created. -- this says that the rating value has to be between the values of 1 and 5 ([Rating]>=(1) AND [Rating]<=(5)) -- this says that the TransactionType has to be an upper case value of P, S or W (upper([TransactionType])='P' OR upper([TransactionType])='S' OR upper([TransactionType])='W')

The output from this schema view only shows the constraint name and the constraint details. It does not show the table the constraint is used for. To get this information you can use the INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE view.

INFORMATION_SCHEMA.COLUMN_DOMAIN_USAGE
(COLUMN_DOMAIN_USAGE) Overview The INFORMATION_SCHEMA.COLUMN_DOMAIN_USAGE view allows you to get information about alias data types that are used for columns By default it will show you this information for every single table and view that is in the database that uses an alias data type. Explanation This view can be called from any of the databases in an instance of SQL Server and will return the results for the data within that particular database. The columns that this view returns are as follows: Column name Data type Description

DOMAIN_CATALOG nvarchar(128) Database in which the alias data type exists. DOMAIN_SCHEMA DOMAIN_NAME TABLE_CATALOG TABLE_SCHEMA TABLE_NAME COLUMN_NAME nvarchar(128) Name of schema that contains the alias data type. sysname Alias data type.

nvarchar(128) Table qualifier. nvarchar(128) Table owner. sysname sysname Table in which the alias data type is used. Column using the alias data type.

(Source: SQL Server 2005 Books Online)

Here is an example of data that was pulled from the AdventureWorks database. This data was pulled using this query: SELECT * FROM INFORMATION_SCHEMA.COLUMN_DOMAIN_USAGE In the results below we can see that the data type "Name" in the DOMAIN_NAME column is used in several tables for columns ReviewerName (ProductReview) and Name (AddressType, ProductSubcategory, UnitMeasure).

To see the underlying data types for these aliased data types you can use this query. SELECT DISTINCT DOMAIN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH FROM INFORMATION_SCHEMA.COLUMNS WHERE DOMAIN_NAME IS NOT NULL

INFORMATION_SCHEMA.COLUMNS
(COLUMNS) Overview The INFORMATION_SCHEMA.COLUMNS view allows you to get information about all columns for all tables and views within a database. By default it will show you this information for every single table and view that is in the database. Explanation This view can be called from any of the databases in an instance of SQL Server and will return the results for the data within that particular database. The columns that this view returns are as follows: Column name TABLE_CATALOG TABLE_SCHEMA TABLE_NAME COLUMN_NAME ORDINAL_POSITION Data type nvarchar(128) nvarchar(128) nvarchar(128) nvarchar(128) int Description Table qualifier. Name of schema that contains the table. Table name. Column name. Column identification number.

Note: In SQL Server 2005, these column IDs are consecutive numbers. COLUMN_DEFAULT nvarchar(4000) Default value of the column. Nullability of the column. If this column allows for NULL, this column returns YES. Otherwise, NO is returned. System-supplied data type. Maximum length, in characters, for binary data, character data, or text and image data. CHARACTER_MAXIMUM_LENGTH int -1 for xml and large-value type data. Otherwise, NULL is returned. For more information, see Data Types (Transact-SQL). Maximum length, in bytes, for binary data, character data, or text and image data. -1 for xml and large-value type data. Otherwise, NULL is returned. Precision of approximate numeric data, exact numeric data, integer data, or monetary data. Otherwise, NULL is returned. Precision radix of approximate numeric data, exact numeric data, integer data, or monetary data. Otherwise, NULL is returned. Scale of approximate numeric data, exact numeric data, integer data, or monetary data. Otherwise, NULL is returned. Subtype code for datetime and SQL-92 interval data types. For other data types, NULL is returned. Returns master. This indicates the database in which the character set is located, if the column is character data ortext data type. Otherwise, NULL is returned. Always returns NULL.

IS_NULLABLE

varchar(3)

DATA_TYPE

nvarchar(128)

CHARACTER_OCTET_LENGTH

int

NUMERIC_PRECISION

tinyint

NUMERIC_PRECISION_RADIX

smallint

NUMERIC_SCALE

int

DATETIME_PRECISION

smallint

CHARACTER_SET_CATALOG

nvarchar(128)

CHARACTER_SET_SCHEMA

nvarchar(128)

CHARACTER_SET_NAME

nvarchar(128)

Returns the unique name for the character set if this column is character data or text data type. Otherwise, NULL is returned. Always returns NULL. Always returns NULL. Returns the unique name for the collation if the column is character data or text data type. Otherwise, NULL is returned. If the column is an alias data type, this column is the database name in which the user-defined data type was created. Otherwise, NULL is returned. If the column is a user-defined data type, this column returns the name of the schema of the user-defined data type. Otherwise, NULL is returned. If the column is a user-defined data type, this column is the name of the user-defined data type. Otherwise, NULL is returned.

COLLATION_CATALOG COLLATION_SCHEMA

nvarchar(128) nvarchar(128)

COLLATION_NAME

nvarchar(128)

DOMAIN_CATALOG

nvarchar(128)

DOMAIN_SCHEMA

nvarchar(128)

DOMAIN_NAME

nvarchar(128)

(Source: SQL Server 2005 Books Online)

Here is an example of data that was pulled from the AdventureWorks database. This data was pulled using this query: SELECT * FROM INFORMATION_SCHEMA.COLUMNS To be able to show the output the results were broken into multiple pieces.

To query for just one table you can use a query like this: SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Address'

INFORMATION_SCHEMA.TABLES
(TABLES) Overview The INFORMATION_SCHEMA.TABLES view allows you to get information about all tables and views within a database. By default it will show you this information for every single table and view that is in the database.

Explanation This view can be called from any of the databases in an instance of SQL Server and will return the results for the data within that particular database. The columns that this view returns are as follows: Column name TABLE_CATALOG TABLE_SCHEMA TABLE_NAME TABLE_TYPE Data type nvarchar(128) nvarchar(128) sysname varchar(10) Table qualifier. Name of schema that contains the table. Table name. Type of table. Can be VIEW or BASE TABLE. Description

(Source: SQL Server 2005 Books Online)

Here is an example of data that was pulled from the AdventureWorks database. This data was pulled using this query: SELECT * FROM INFORMATION_SCHEMA.TABLES

To only show a list of tables you would use this query: SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'

To only show a list of only the view you would use this query: SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'VIEW'

SQL Server INFORMATION_SCHEMA views


(Introduction)

Overview The INFORMATION_SCHEMA views allow you to retrieve metadata about the objects within a database. These views can be found in the master database under Views / System Views and be called from any database in your SQL Server instance. The reason these were developed was so that they are standard across all database platforms. In SQL 2005 and SQL 2008 these Information Schema views comply with the ISO standard. Following is a list of each of the views that exist. y y y y y y y y y y y y y y y y y y y y

INFORMATION_SCHEMA.CHECK_CONSTRAINTS INFORMATION_SCHEMA.COLUMN_DOMAIN_USAGE INFORMATION_SCHEMA.COLUMN_PRIVILEGES INFORMATION_SCHEMA.COLUMNS INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE INFORMATION_SCHEMA.DOMAIN_CONSTRAINTS INFORMATION_SCHEMA.DOMAINS INFORMATION_SCHEMA.KEY_COLUMN_USAGE INFORMATION_SCHEMA.PARAMETERS INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS INFORMATION_SCHEMA.ROUTINE_COLUMNS INFORMATION_SCHEMA.ROUTINES INFORMATION_SCHEMA.SCHEMATA INFORMATION_SCHEMA.TABLE_CONSTRAINTS INFORMATION_SCHEMA.TABLE_PRIVILEGES INFORMATION_SCHEMA.TABLES INFORMATION_SCHEMA.VIEW_COLUMN_USAGE INFORMATION_SCHEMA.VIEW_TABLE_USAGE INFORMATION_SCHEMA.VIEWS

These views can be used from any of your databases. So if you want to gather data about Tables from the AdventureWorks database you would issue the following in that database. USE AdventureWorks GO SELECT * FROM INFORMATION_SCHEMA.TABLES

INFORMATION_SCHEMA.CHECK_CONSTRAINTS
(CHECK_CONSTRAINTS) Overview The INFORMATION_SCHEMA.CHECK_CONSTRAINTS view allows you to get information about the check constraints that are setup in your database. A check constraint is a constraint put on a particular column in a table to ensure that specific data rules are followed for a column. Explanation This view can be called from any of the databases in an instance of SQL Server and will return the results for the data within that particular database.

The columns that this view returns are as follows: Column name Data type Description Constraint qualifier. Name of the schema to which the constraint belongs. Constraint name. Actual text of the Transact-SQL definition statement.

CONSTRAINT_CATALOG nvarchar(128) CONSTRAINT_SCHEMA CONSTRAINT_NAME CHECK_CLAUSE nvarchar(128) sysname nvarchar(4000)

(Source: SQL Server 2005 Books Online)

Here is an example of data that was pulled from the AdventureWorks database. This data was pulled using this query: SELECT * FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS

To take a look at this a little further here is a closer look at the first two check constraints. These are usually setup when a table is created. -- this says that the rating value has to be between the values of 1 and 5 ([Rating]>=(1) AND [Rating]<=(5)) -- this says that the TransactionType has to be an upper case value of P, S or W (upper([TransactionType])='P' OR upper([TransactionType])='S' OR upper([TransactionType])='W')

The output from this schema view only shows the constraint name and the constraint details. It does not show the table the constraint is used for. To get this information you can use the INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE view.

INFORMATION_SCHEMA.COLUMN_DOMAIN_USAGE
(COLUMN_DOMAIN_USAGE)

Overview The INFORMATION_SCHEMA.COLUMN_DOMAIN_USAGE view allows you to get information about alias data types that are used for columns By default it will show you this information for every single table and view that is in the database that uses an alias data type. Explanation This view can be called from any of the databases in an instance of SQL Server and will return the results for the data within that particular database. The columns that this view returns are as follows: Column name Data type Description

DOMAIN_CATALOG nvarchar(128) Database in which the alias data type exists. DOMAIN_SCHEMA DOMAIN_NAME TABLE_CATALOG TABLE_SCHEMA TABLE_NAME COLUMN_NAME nvarchar(128) Name of schema that contains the alias data type. sysname Alias data type.

nvarchar(128) Table qualifier. nvarchar(128) Table owner. sysname sysname Table in which the alias data type is used. Column using the alias data type.

(Source: SQL Server 2005 Books Online)

Here is an example of data that was pulled from the AdventureWorks database. This data was pulled using this query: SELECT * FROM INFORMATION_SCHEMA.COLUMN_DOMAIN_USAGE In the results below we can see that the data type "Name" in the DOMAIN_NAME column is used in several tables for columns ReviewerName (ProductReview) and Name (AddressType, ProductSubcategory, UnitMeasure).

To see the underlying data types for these aliased data types you can use this query. SELECT DISTINCT DOMAIN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH FROM INFORMATION_SCHEMA.COLUMNS WHERE DOMAIN_NAME IS NOT NULL

INFORMATION_SCHEMA.COLUMNS
(COLUMNS) Overview The INFORMATION_SCHEMA.COLUMNS view allows you to get information about all columns for all tables and views within a database. By default it will show you this information for every single table and view that is in the database. Explanation This view can be called from any of the databases in an instance of SQL Server and will return the results for the data within that particular database. The columns that this view returns are as follows: Column name TABLE_CATALOG TABLE_SCHEMA TABLE_NAME COLUMN_NAME Data type nvarchar(128) nvarchar(128) nvarchar(128) nvarchar(128) Description Table qualifier. Name of schema that contains the table. Table name. Column name. Column identification number. ORDINAL_POSITION int Note: In SQL Server 2005, these column IDs are consecutive numbers.

COLUMN_DEFAULT IS_NULLABLE

nvarchar(4000) Default value of the column. varchar(3) Nullability of the column. If this column allows for NULL, this

column returns YES. Otherwise, NO is returned. DATA_TYPE nvarchar(128) System-supplied data type. Maximum length, in characters, for binary data, character data, or text and image data. CHARACTER_MAXIMUM_LENGTH int -1 for xml and large-value type data. Otherwise, NULL is returned. For more information, see Data Types (Transact-SQL). Maximum length, in bytes, for binary data, character data, or text and image data. -1 for xml and large-value type data. Otherwise, NULL is returned. Precision of approximate numeric data, exact numeric data, integer data, or monetary data. Otherwise, NULL is returned. Precision radix of approximate numeric data, exact numeric data, integer data, or monetary data. Otherwise, NULL is returned. Scale of approximate numeric data, exact numeric data, integer data, or monetary data. Otherwise, NULL is returned. Subtype code for datetime and SQL-92 interval data types. For other data types, NULL is returned. Returns master. This indicates the database in which the character set is located, if the column is character data ortext data type. Otherwise, NULL is returned. Always returns NULL. Returns the unique name for the character set if this column is character data or text data type. Otherwise, NULL is returned. Always returns NULL. Always returns NULL.

CHARACTER_OCTET_LENGTH

int

NUMERIC_PRECISION

tinyint

NUMERIC_PRECISION_RADIX

smallint

NUMERIC_SCALE

int

DATETIME_PRECISION

smallint

CHARACTER_SET_CATALOG

nvarchar(128)

CHARACTER_SET_SCHEMA

nvarchar(128)

CHARACTER_SET_NAME

nvarchar(128)

COLLATION_CATALOG COLLATION_SCHEMA

nvarchar(128) nvarchar(128)

COLLATION_NAME

nvarchar(128)

Returns the unique name for the collation if the column is character data or text data type. Otherwise, NULL is returned. If the column is an alias data type, this column is the database name in which the user-defined data type was created. Otherwise, NULL is returned. If the column is a user-defined data type, this column returns the name of the schema of the user-defined data type. Otherwise, NULL is returned. If the column is a user-defined data type, this column is the name of the user-defined data type. Otherwise, NULL is returned.

DOMAIN_CATALOG

nvarchar(128)

DOMAIN_SCHEMA

nvarchar(128)

DOMAIN_NAME

nvarchar(128)

(Source: SQL Server 2005 Books Online)

Here is an example of data that was pulled from the AdventureWorks database. This data was pulled using this query: SELECT * FROM INFORMATION_SCHEMA.COLUMNS To be able to show the output the results were broken into multiple pieces.

To query for just one table you can use a query like this: SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Address'

INFORMATION_SCHEMA.TABLES
(TABLES) Overview The INFORMATION_SCHEMA.TABLES view allows you to get information about all tables and views within a database. By default it will show you this information for every single table and view that is in the database. Explanation This view can be called from any of the databases in an instance of SQL Server and will return the results for the data within that particular database. The columns that this view returns are as follows: Column name TABLE_CATALOG TABLE_SCHEMA Data type nvarchar(128) nvarchar(128) Table qualifier. Name of schema that contains the table. Description

TABLE_NAME TABLE_TYPE

sysname varchar(10)

Table name. Type of table. Can be VIEW or BASE TABLE.

(Source: SQL Server 2005 Books Online)

Here is an example of data that was pulled from the AdventureWorks database. This data was pulled using this query: SELECT * FROM INFORMATION_SCHEMA.TABLES

To only show a list of tables you would use this query: SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'

To only show a list of only the view you would use this query: SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'VIEW'

Вам также может понравиться