Introduction Obtaining and Using Metadata

9.1 Introduction

Most of t he queries used so far have been writ t en t o work wit h t he dat a st ored in t he dat abase. That is, aft er all, what t he dat abase is designed t o hold. But som et im es you need m ore t han j ust dat a values. You need inform at ion t hat charact erizes or describes t hose values—t hat is, t he query m et adat a. Met adat a inform at ion is used m ost oft en in relat ion t o processing result set s, but also is available for ot her aspect s of your int eract ion wit h MySQL. This chapt er describes how t o obt ain and use t he following t ypes of m et adat a: • I n for m a t ion a bou t t h e r e su lt of qu e r ie s. When you delet e or updat e a set of rows, you can det erm ine t he num ber of rows t hat were changed. For a SELECT query, you can find out t he num ber of colum ns in t he result set , as w ell as inform at ion about each colum n in t he result set , such as t he colum n nam e and it s display widt h. Such inform at ion oft en is essent ial for processing t he result s. For exam ple, if youre form at t ing a t abular display, you can det erm ine how wide t o m ake each colum n and whet her t o j ust ify values t o t he left or right . • I n for m a t ion a bou t t a ble s a n d da t a ba se s. I nform at ion pert aining t o t he st ruct ure of t ables and dat abases is useful for applicat ions t hat need t o enum erat e a list of t ables in a dat abase or dat abases host ed on a server for exam ple, t o present a display allowing t he user t o select one of t he available choices . You can also use t his inform at ion t o det erm ine whet her t ables or dat abases exist . Anot her use for t able m et adat a is t o det erm ine t he legal values for ENUM or SET colum ns. • I n for m a t ion a bou t t h e M ySQL se r ve r . Som e API s provide inform at ion about t he dat abase server or about t he st at us of your current connect ion wit h t he server. Knowing t he server version can be useful for det erm ining whet her it support s a given feat ure, which helps you build adapt ive applicat ions. I nform at ion about t he connect ion includes such it em s as t he current user and t he current dat abase. Som e API s t ry t o provide a dat abase- independent int erface for t ypes of m et adat a t hat t end t o be available across a variet y of dat abase engines such as t he nam es of t he colum ns in a result set . But in general, m et adat a inform at ion is closely t ied t o t he st ruct ure of t he dat abase syst em , so it t ends t o be som ewhat dat abase- dependent . This m eans t hat if you port an applicat ion t hat uses recipes in t his chapt er t o ot her dat abases, it m ay need som e m odificat ion. For exam ple, list s of t ables and dat abases in MySQL are available by issuing SHOW st at em ent s. However, SHOW is a MySQL-specific ext ension t o SQL, so even if youre using an API like DBI , DB-API , or JDBC t hat gives you a dat abase- independent way of issuing queries, t he SQL it self is dat abase- specific and will need t o be changed t o work wit h ot her engines. The script s cont aining t he code for t he exam ples shown here are in t he m et adat a direct ory of t he recipes dist ribut ion. Som e of t hem use ut ilit y funct ions locat ed in t he lib direct ory. To creat e any t ables t hat you need for t rying t he exam ples, look in t he t ables direct ory. I n several cases, recipes developed here const ruct queries using a dat abase, t able, or colum n nam e t hat is st ored in a variable. For sim plicit y, generally such nam es are insert ed as is int o t he query st ring. For exam ple: query = SHOW COLUMNS FROM tbl_name; This works properly in t he m aj orit y of cases, but t here are som e possible com plicat ions you should know about , and m ay wish t o t ake int o account when adapt ing t hese recipes for your own use. As of MySQL 3.23.6, nam es are allowed t o cont ain alm ost any charact er, such as spaces. I f you ant icipat e a need t o deal wit h such nam es, surround t he nam e wit h backt icks: query = SHOW COLUMNS FROM `tbl_name`; I f t he server is running in ANSI m ode, nam e quot ing should be done wit h double quot es inst ead: query = SHOW COLUMNS FROM \tbl_name\; To deal wit h t hese issues on a general basis, you can query t he server t o see if it is Version 3.23.6 or lat er see Recipe 9.14 , and you can also use SHOW VARIABLES t o see if it is running in ANSI m ode. The recipes here do not perform all t hese checks, because doing so would obscure t heir m ain point .

9.2 Obtaining the Number of Rows Affected by a Query