Problem Solution Discussion Displaying Column Lists Interactive Record Editing

• MySQL has no concept of schem a, so t he schem a nam e argum ent is irrelevant and can be t he em pt y st ring. • The t able nam e argum ent is a st ring nam ing t he t able. • The colum n nam e pat t ern is analogous t o using t he LIKE clause in a SHOW COLUMNS st at em ent . The exam ple shown above uses , which m at ches all colum n nam es. You can pass a specific colum n nam e t o get inform at ion for a single colum n. Rem em ber t o escape any and _ charact ers wit h a backslash if you want t o m at ch t hem lit erally.

9.9 Applying Table Structure Information

9.9.1 Problem

I t s all well and good t o be able t o obt ain t able st ruct ure inform at ion, but what can you use it for?

9.9.2 Solution

Lot s of t hings: displaying list s of t able colum ns, creat ing web form elem ent s, producing ALTER TABLE st at em ent s for m odifying ENUM or SET colum ns, and m ore.

9.9.3 Discussion

This sect ion describes som e applicat ions for t he t able st ruct ure inform at ion t hat MySQL provides.

9.9.4 Displaying Column Lists

Probably t he sim plest use of t able inform at ion is t o present a list of t he t ables colum ns. This is com m on in web- based or GUI applicat ions t hat allow users t o const ruct queries int eract ively by select ing a t able colum n from a list and ent ering a value against which t o com pare colum n values. The various versions of t he get_column_names_with_show or get_column_names_with_meta funct ions shown earlier in t he chapt er can serve as t he basis for such list displays.

9.9.5 Interactive Record Editing

Knowledge of a t ables st ruct ure can be very useful for int eract ive record-edit ing applicat ions. Suppose you have an applicat ion t hat ret rieves a record from t he dat abase, displays a form cont aining t he records cont ent so a user can edit it , and t hen updat es t he record in t he dat abase aft er t he user m odifies t he form and subm it s it . You can use t able st ruct ure inform at ion for validat ing colum n values. For exam ple, if a colum n is an ENUM , you can find out t he valid enum erat ion values and check t he value subm it t ed by t he user against t hem t o det erm ine whet her or not it s legal. I f t he colum n is an int eger t ype, you check t he subm it t ed value t o m ake sure t hat it consist s ent irely of digit s, possibly preceded by a sign charact er. I f t he colum n cont ains dat es, look for a legal dat e form at . But what if t he user leaves a field em pt y? I f t he field corresponds t o, say, a CHAR colum n in t he t able, do you set t he colum n value t o NULL or t o t he em pt y st ring? This t oo is a quest ion t hat can be answered by checking t he t ables st ruct ure. Det erm ine whet her or not t he colum n can cont ain NULL values. I f it can, set t he colum n t o NULL ; ot herwise, set it t o t he em pt y st ring.

9.9.6 Mapping Column Types onto Web Page Elements