Problem Solution Discussion Repeating and Editing Queries

Temporarily Using a Table from Another Database To use a t able from anot her dat abase t em porarily, you can swit ch t o t hat dat abase and t hen swit ch back when youre done using t he t able. However, you can also use t he t able wit hout swit ching dat abases by referring t o t he t able using it s fully qualified nam e. For exam ple, t o use t he t able other_tbl in anot her dat abase other_db , you can refer t o it as other_db.other_tbl .

1.12 Canceling a Partially Entered Query

1.12.1 Problem

You st art t o ent er a query, t hen decide not t o issue it aft er all.

1.12.2 Solution

Cancel t he query using your line kill charact er or t he \c sequence.

1.12.3 Discussion

I f you change your m ind about issuing a query t hat youre ent ering, cancel it . I f t he query is on a single line, use your line kill charact er t o erase t he ent ire line. The part icular charact er t o use depends on your t erm inal set up; for m e, t he charact er is Ct rl-U. I f youve ent ered a st at em ent over m ult iple lines, t he line kill charact er will erase only t he last line. To cancel t he st at em ent com plet ely, ent er \c and t ype Ret urn. This will ret urn you t o t he mysql prom pt : mysql SELECT - FROM limbs - ORDER BY\c mysql Som et im es \c appears t o do not hing t hat is, t he mysql prom pt does not reappear , which leads t o t he sense t hat youre t rapped in a query and cant escape. I f \c is ineffect ive, t he cause usually is t hat you began t yping a quot ed st ring and havent yet ent ered t he m at ching end quot e t hat t erm inat es t he st ring. Let m ysqls prom pt help you figure out what t o do here. I f t he prom pt has changed from mysql t o , That m eans m ysql is looking for a t erm inat ing double quot e. I f t he prom pt is inst ead, m ysql is looking for a t erm inat ing single quot e. Type t he appropriat e m at ching quot e t o end t he st ring, t hen ent er \c followed by Ret urn and you should be okay.

1.13 Repeating and Editing Queries

1.13.1 Problem

The query you j ust ent ered cont ained an error, and you want t o fix it wit hout t yping t he whole t hing again. Or you want t o repeat an earlier st at em ent wit hout ret yping it .

1.13.2 Solution

Use m ysqls built - in query edit or.

1.13.3 Discussion

I f you issue a long query only t o find t hat it cont ains a synt ax error, what should you do? Type in t he ent ire correct ed query from scrat ch? No need. m ysql m aint ains a st at em ent hist ory and support s input - line edit ing. This allows you t o recall queries so t hat you can m odify and reissue t hem easily. There are m any, m any edit ing funct ions, but m ost people t end t o use a sm all set of com m ands for t he m aj orit y of t heir edit ing. [ 3] A basic set of useful com m ands is shown in t he following t able. Typically, you use Up Arrow t o recall t he previous line, Left Arrow and Right Arrow t o m ove around wit hin t he line, and Backspace or Delet e t o erase charact ers. To add new charact ers t o t he line, j ust m ove t he cursor t o t he appropriat e spot and t ype t hem in. When youre done edit ing, press Ret urn t o issue t he query t he cursor need not be at t he end of t he line when you do t his . [ 3] The input - line edit ing capabilit ies in m ysql are based on t he GNU Readline library. You can read t he docum ent at ion for t his library t o find out m ore about t he m any edit ing funct ions t hat are available. For m ore inform at ion, check t he Bash m anual, available online at ht t p: www.gnu.org m anual . Edit in g Ke y Effe ct of Ke y Up Arrow Scroll up t hrough st at em ent hist ory Down Arrow Scroll down t hrough st at em ent hist ory Left Arrow Move left wit hin line Right Arrow Move right wit hin line Ct r l- A Move t o beginning of line Ct r l- E Move t o end of line Backspace Delet e previous charact er Ct r l- D Delet e charact er under cursor I nput - line edit ing is useful for m ore t han j ust fixing m ist akes. You can use it t o t ry out variant form s of a query wit hout ret yping t he ent ire t hing each t im e. I t s also handy for ent ering a series of sim ilar st at em ent s. For exam ple, if you want ed t o use t he query hist ory t o issue t he series of INSERT st at em ent s shown earlier in Recipe 1.3 t o creat e t he limbs t able, first ent er t he init ial INSERT st at em ent . Then, t o issue each successive st at em ent , press t he Up Arrow key t o recall t he previous st at em ent wit h t he cursor at t he end, backspace back t hrough t he colum n values t o erase t hem , ent er t he new values, and press Ret urn. To recall a st at em ent t hat was ent ered on m ult iple lines, t he edit ing procedure is a lit t le t rickier t han for single- line st at em ent s. I n t his case, you m ust recall and reent er each successive line of t he query in order. For exam ple, if youve ent ered a t wo- line query t hat cont ains a m ist ake, press Up Arrow t wice t o recall t he first line. Make any m odificat ions necessary and press Ret urn. Then press Up Arrow t wice m ore t o recall t he second line. Modify it , press Ret urn, and t he query will execut e. Under Windows, m ysql allows st at em ent recall only for NT- based syst em s. For versions such as Windows 98 or Me, you can use t he special m ysqlc client program inst ead. However, m ysqlc requires an addit ional library file, cygwinb19.dll. I f you find a copy of t his library in t he sam e direct ory where m ysqlc is inst alled t he bin dir under t he MySQL inst allat ion direct ory , you should be all set . I f t he library is locat ed in t he MySQL lib direct ory, copy it int o your Windows syst em direct ory. The com m and looks som et hing like t his; you should m odify it t o reflect t he act ual locat ions of t he t wo direct ories on your syst em : C:\ copy C:\mysql\lib\cygwinb19.dll C:\Windows\System Aft er you m ake sure t he library is in a locat ion where m ysqlc can find it , invoke m ysqlc and it should be capable of input - line edit ing. One unfort unat e consequence of using m ysqlc is t hat it s act ually a fairly old program . For exam ple, even in MySQL 4.x dist ribut ions, m ysqlc dat es back t o 3.22.7. This m eans it doesnt underst and newer st at em ent s such as SOURCE .

1.14 Using Auto-Completion for Database and Table Names