In SQL Server there is two schemas for metadata:
I have heard that INFORMATION_SCHEMA
tables are based on ANSI standard. When developing e.g. stored procedures, should it be wise to use INFORMATION_SCHEMA
tables over sys
tables?
INFORMATION_SCHEMA provides access to database metadata, information about the MySQL server such as the name of a database or table, the data type of a column, or access privileges. Other terms that are sometimes used for this information are data dictionary and system catalog.
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.
The INFORMATION_SCHEMA is a deprecated schema that I believe Microsoft keeps around because it's a notion of ANSI SQL (SQL-92) compliance. As the warning you quoted mentions, the INFORMATION_SCHEMA may be missing meta-data and information on some of the objects in your database.
I would always try to use the Information_schema
views over querying the sys
schema directly.
The Views are ISO compliant so in theory you should be able to easily migrate any queries across different RDBMS.
However, there have been some cases where the information that I need is just not available in a view.
I've provided some links with further information on the views and querying a SQL Server Catalog.
http://msdn.microsoft.com/en-us/library/ms186778.aspx
http://msdn.microsoft.com/en-us/library/ms189082.aspx
Unless you are writing an application which you know for a fact will need to be portable or you only want quite basic information I would just default to using the proprietary SQL Server system views to begin with.
The Information_Schema
views only show objects that are compatible with the SQL-92 standard. This means there is no information schema view for even quite basic constructs such as indexes (These are not defined in the standard and are left as implementation details.) Let alone any SQL Server proprietary features.
Additionally it is not quite the panacea for portability that one may assume. Implementations do still differ between systems. Oracle does not implement it "out of the box" at all and the MySql docs say:
Users of SQL Server 2000 (which also follows the standard) may notice a strong similarity. However, MySQL has omitted many columns that are not relevant for our implementation, and added columns that are MySQL-specific. One such column is the ENGINE column in the INFORMATION_SCHEMA.TABLES table.
Even for bread and butter SQL constructs such as foreign key constraints the Information_Schema
views can be dramatically less efficient to work with than the sys.
views as they do not expose object ids that would allow efficient querying.
e.g. See the question SQL query slow-down from 1 second to 11 minutes - why? and execution plans.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With