Monday 28 May 2012

INFORMATION_SCHEMA.COLUMNS


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.
ExplanationThis 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 nameData typeDescription
TABLE_CATALOGnvarchar(128)Table qualifier.
TABLE_SCHEMAnvarchar(128)Name of schema that contains the table.
TABLE_NAMEnvarchar(128)Table name.
COLUMN_NAMEnvarchar(128)Column name.
ORDINAL_POSITIONintColumn identification number.
Note: In SQL Server 2005, these column IDs are consecutive numbers.
COLUMN_DEFAULTnvarchar(4000)Default value of the column.
IS_NULLABLEvarchar(3)Nullability of the column. If this column allows for NULL, this column returns YES. Otherwise, NO is returned.
DATA_TYPEnvarchar(128)System-supplied data type.
CHARACTER_MAXIMUM_LENGTHintMaximum length, in characters, for binary data, character data, or text and image data.
-1 for xml and large-value type data. Otherwise, NULL is returned. For more information, see Data Types (Transact-SQL).
CHARACTER_OCTET_LENGTHintMaximum 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.
NUMERIC_PRECISIONtinyintPrecision of approximate numeric data, exact numeric data, integer data, or monetary data. Otherwise, NULL is returned.
NUMERIC_PRECISION_RADIXsmallintPrecision radix of approximate numeric data, exact numeric data, integer data, or monetary data. Otherwise, NULL is returned.
NUMERIC_SCALEintScale of approximate numeric data, exact numeric data, integer data, or monetary data. Otherwise, NULL is returned.
DATETIME_PRECISIONsmallintSubtype code for datetime and SQL-92 interval data types. For other data types, NULL is returned.
CHARACTER_SET_CATALOGnvarchar(128)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.
CHARACTER_SET_SCHEMAnvarchar(128)Always returns NULL.
CHARACTER_SET_NAMEnvarchar(128)Returns the unique name for the character set if this column is character data or text data type. Otherwise, NULL is returned.
COLLATION_CATALOGnvarchar(128)Always returns NULL.
COLLATION_SCHEMAnvarchar(128)Always returns NULL.
COLLATION_NAMEnvarchar(128)Returns the unique name for the collation if the column is character data or text data type. Otherwise, NULL is returned.
DOMAIN_CATALOGnvarchar(128)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.
DOMAIN_SCHEMAnvarchar(128)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.
DOMAIN_NAMEnvarchar(128)If the column is a user-defined data type, this column is the name of the user-defined data type. Otherwise, NULL is returned.
(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


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.
ExplanationThis 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 nameData typeDescription
TABLE_CATALOGnvarchar(128)Table qualifier.
TABLE_SCHEMAnvarchar(128)Name of schema that contains the table.
TABLE_NAMEsysnameTable name.
TABLE_TYPEvarchar(10)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'

INFORMATION_SCHEMA VIEW


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.
  • 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
FOR MORE DETAILS  GO...