INFORMATION_SCHEMA Support in MySQL, PostgreSQL
By Pete Freitag
I've known about the INFORMATION_SCHEMA
views (or system tables) in SQL Server for a while, but I just leared recently that they are actually part of the SQL-92 standard and supported on other database platforms.
The INFORMATION_SCHEMA
views provide meta data information about the tables, columns, and other parts of your database. Because the structure of these tables are standardized you can write SQL statements that work on various database platforms.
For example suppose you want to return a resultset with a list of all columns in a table called employees
SELECT table_name, column_name, is_nullable, data_type, character_maximum_length FROM INFORMATION_SCHEMA.Columns WHERE table_name = 'employees'
Quite a handy feature, but it's hard to find what versions the of various database platforms started supporting this feature, here's a quick list:
- Microsoft SQL Server - Supported in Version 7 and up
- MySQL - Supported in Version 5 and up
- PostgreSQL - Supported in Version 7.4 and up
- Oracle - Does not appear to be supported
- Apache Derby - NOT Supported As of Version 10.3
I have been using the INFORMATION_SCHEMA
views to build some automatic datatype validation. With the INFORMATION_SCHEMA
you can get the datatype, max character length, and if null values are allowed, and perform validation before it hits the database. And if a column is made wider, you don't have to make any code changes.
Ofcourse if you are using ColdFusion 8, you can use the new cfdbinfo
tag to get the same column information. The cfdbinfo
actually uses the JDBC Driver's getMetaData()
method (this is part of the JDBC Standard that Drivers implement this method). Apache Derby doesn't support the INFORMATION_SCHEMA
views because they prefer to simply implement the JDBC Driver's getMetaData()
method.
Here's a list of the information schema views:
- INFORMATION_SCHEMA.SCHEMATA
- INFORMATION_SCHEMA.TABLES
- INFORMATION_SCHEMA.COLUMNS
- INFORMATION_SCHEMA.STATISTICS
- INFORMATION_SCHEMA.USER_PRIVILEGES
- INFORMATION_SCHEMA.SCHEMA_PRIVILEGES
- INFORMATION_SCHEMA.TABLE_PRIVILEGES
- INFORMATION_SCHEMA.COLUMN_PRIVILEGES
- INFORMATION_SCHEMA.CHARACTER_SETS
- INFORMATION_SCHEMA.COLLATIONS
- INFORMATION_SCHEMA.COLLATION_CHARACTER_SET_APPLICABILITY
- INFORMATION_SCHEMA.TABLE_CONSTRAINTS
- INFORMATION_SCHEMA.KEY_COLUMN_USAGE
- INFORMATION_SCHEMA.ROUTINES
- INFORMATION_SCHEMA.VIEWS
- INFORMATION_SCHEMA.TRIGGERS
- INFORMATION_SCHEMA.PROFILING
INFORMATION_SCHEMA Support in MySQL, PostgreSQL was first published on February 18, 2008.
If you like reading about sql, mysql, postgresql, sqlserver, derby, information_schema, standards, or cfdbinfo then you might also like:
- Order by NULL Values in MySQL, Postgresql and SQL Server
- SELECT a random row with SQL
- SQL Reserved Key Words Checker Tool
- Top 10 Reserved SQL Keywords
Discuss / Follow me on Twitter ↯
Tweet Follow @pfreitagComments
http://www.postgresql.org/docs/7.4/static/release-7-4.html
I am trying to find something on Oracle's site, but it appears to be down? I found several web pages that mention 9i supporting it, but I will confirm and update if the information is incorrect.
Any easy way to find out is if anyone reading this has Oracle 9i installed?
- MySQL's implementation was unusably slow prior to 5.1.23, but they appear to have improved it. http://bugs.mysql.com/19588
- PostgreSQL's implementation of INFORMATION_SCHEMA is buggy. I have read that most people INFORMATION_SCHEMA and instead use the old PostgreSQL-proprietary system tables.
http://www.postgresql.org/docs/8.2/static/catalogs.html
- Oracle has proprietary catalog tables, and does not support INFORMATION_SCHEMA.
http://download-west.oracle.com/docs/cd/B19306_01/server.102/b14237/statviews_1.htm
IBM DB2 also uses proprietary catalog tables, and does not support INFORMATION_SCHEMA.
http://publib.boulder.ibm.com/infocenter/db2luw/v8/index.jsp?topic=/com.ibm.db2.udb.doc/admin/r0001063.htm
- InterBase/Firebird also uses proprietary catalog tables and does not support INFORMATION_SCHEMA. The InterBase manual shows how to create views against its catalog tables to match the spec for INFORMATION_SCHEMA relations.
But I have to disagree with Bill aobut PostgreSQL, it's information schema implementation is not buggy, it's just that people go with "true and tested" which means system tables (the same happens with mssql) and would happen with Oracle which right now doesn't support this standard.
Examples are user_tables for all tables owned by you, all_tables for all tables that you have access to and dba_tables for all tables.
These views are very comprehensive and would allow you to recreate any schema object.
The point is that Oracle has indeed its own set of system views for metadata. That is nice and all, and the information you get from these is very rich, no complaints there. But it is *not* an implementation of the standard SQL feature called "information_schema" and you cannot simply port queries from one platform to the other.
thank you all for this fruitful information. I am very new to DB.
I but i was also trying to figure out how to get the schema name, that table belongs to? Because the same table name can be used under different schema's. It would be good if we retrieve the schema name also.
I tried the below query
SELECT schema_name, table_name, column_name, is_nullable, data_type, character_maximum_length
FROM INFORMATION_SCHEMA.Columns
WHERE table_name = 'Sales'
Error: column "schema_name" does not exist
Thanks
Deepak
Nice post, keep it up.
Can you please provide the links for your list of Information Schema compatibility.
I found:
MySQL 5.0:
http://dev.mysql.com/doc/refman/5.0/en/information-schema.html
MS SQL 2005
http://msdn2.microsoft.com/en-us/library/ms186778.aspx
PostgreSQL 8.1
http://www.postgresql.org/docs/8.1/interactive/information-schema.html
However, I have been unable to locate this information for Oracle, and I was under the impression that Oracle does *not* provide an information_schema (although they do of course offer their own system views - the USER_%, DBA_% and ALL_% views)
kind regards,
Roland Bouman